diff --git a/tests/.gitignore b/tests/.gitignore index a9fbd64..c86f4e9 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,2 +1,4 @@ /pylint.log +/sqlite3 +/sqlite3.db /tests.log diff --git a/tests/database.sh b/tests/database.sh new file mode 100755 index 0000000..da94a35 --- /dev/null +++ b/tests/database.sh @@ -0,0 +1,110 @@ +#!/bin/sh +#$Id$ +#Copyright (c) 2016 Pierre Pronchery +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, version 3 of the License. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + + + +#variables +DEBUG="_debug" +DEVNULL="/dev/null" +PROGNAME="database.sh" +#executables +RM="rm -f" +SQLITE2="sqlite" +SQLITE3="sqlite3" + + +#functions +#debug +_debug() +{ + echo "$@" 1>&2 + "$@" + ret=$? + #ignore errors when the command is not available + [ $ret -eq 127 ] && return 0 + return $ret +} + + +#error +_error() +{ + echo "$PROGNAME: $@" 1>&2 + return 2 +} + + +#usage +_usage() +{ + echo "Usage: $PROGNAME [-P prefix] target" 1>&2 + return 1; +} + + +#main +clean=0 +while getopts "cP:" name; do + case "$name" in + c) + clean=1 + ;; + P) + #XXX ignored for compatibility + ;; + ?) + _usage + exit $? + ;; + esac +done +shift $((OPTIND - 1)) +if [ $# -ne 1 ]; then + _usage + exit $? +fi +target="$1" +database="${target#$OBJDIR}" + +[ "$clean" -ne 0 ] && exit 0 + +case "$database" in + sqlite2.db) + $RM -- "$target" || exit 2 + #XXX run twice to ensure there are no false positives + echo .read "sqlite2.sql" | $DEBUG $SQLITE2 "$target" > "$DEVNULL" 2>&1 + echo .read "sqlite2.sql" | $DEBUG $SQLITE2 "$target" + res=$? + if [ $res -ne 0 ]; then + _error "$target: Error $res" + exit $? + fi + ;; + sqlite3.db) + $RM -- "$target" || exit 2 + #XXX run twice to ensure there are no false positives + echo .read "sqlite3.sql" | $DEBUG $SQLITE3 "$target" > "$DEVNULL" 2>&1 + echo .read "sqlite3.sql" | $DEBUG $SQLITE3 "$target" + res=$? + if [ $res -ne 0 ]; then + _error "$target: Error $res" + exit $? + fi + ;; + *) + _error "$database: Unknown database target" + exit 2 + ;; +esac diff --git a/tests/project.conf b/tests/project.conf index 1416117..9fe11d3 100644 --- a/tests/project.conf +++ b/tests/project.conf @@ -1,5 +1,20 @@ -targets=pylint.log,tests.log -dist=Makefile,pylint.sh,python.sh,tests.sh +targets=pylint.log,sqlite3,sqlite3.db,tests.log +cppflags_force=-I ../include +cflags_force=`pkg-config --cflags libSystem` +cflags=-W -Wall -g -O2 -pedantic -fPIE -D_FORTIFY_SOURCE=2 -fstack-protector-all +ldflags_force=`pkg-config --libs libSystem` -L $(OBJDIR)../src -lDatabase +ldflags=-pie +dist=Makefile,database.sh,pylint.sh,python.sh,sqlite3.sql,tests.sh + +[sqlite3] +type=binary +sources=sqlite3.c +depends=$(OBJDIR)sqlite3.db + +[sqlite3.db] +type=script +script=./database.sh +depends=sqlite3.sql [pylint.log] type=script @@ -9,4 +24,4 @@ depends=pylint.sh [tests.log] type=script script=./tests.sh -depends=python.sh,tests.sh +depends=python.sh,$(OBJDIR)sqlite3$(SOEXT),tests.sh diff --git a/tests/sqlite3.c b/tests/sqlite3.c new file mode 100644 index 0000000..e11d048 --- /dev/null +++ b/tests/sqlite3.c @@ -0,0 +1,95 @@ +/* $Id$ */ +/* Copyright (c) 2016 Pierre Pronchery */ +/* This file is part of DeforaOS Database libDatabase */ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + + + +#include +#include +#include +#include +#include "Database.h" + +#ifndef PROGNAME +# define PROGNAME "sqlite3" +#endif + + +/* sqlite3 */ +/* private */ +/* prototypes */ +static int _sqlite3(void); + +static int _usage(void); + + +/* functions */ +/* sqlite3 */ +static int _sqlite3(void) +{ + Config * config; + Database * db; + String * filename; + char const * p; + + if((config = config_new()) == NULL) + { + error_print(PROGNAME); + return -1; + } + if((p = getenv("OBJDIR")) != NULL) + filename = string_new_append(p, "/sqlite3.db", NULL); + else + filename = string_new("sqlite3.db"); + if(filename == NULL + || config_set(config, NULL, "filename", filename) != 0) + { + error_print(PROGNAME); + string_delete(filename); + config_delete(config); + return -1; + } + string_delete(filename); + db = database_new("sqlite3", config, NULL); + config_delete(config); + if(db == NULL) + return 2; + if(database_query(db, "SELECT * FROM \"table\"", NULL, NULL) != 0) + { + error_print(PROGNAME); + database_delete(db); + return 2; + } + database_delete(db); + return 0; +} + + +/* usage */ +static int _usage(void) +{ + fputs("Usage: " PROGNAME "\n", stderr); + return 1; +} + + +/* public */ +/* functions */ +/* main */ +int main(int argc, char * argv[]) +{ + if(argc != 1) + return _usage(); + return (_sqlite3() == 0) ? 0 : 2; +} diff --git a/tests/sqlite3.sql b/tests/sqlite3.sql new file mode 100644 index 0000000..7d96a37 --- /dev/null +++ b/tests/sqlite3.sql @@ -0,0 +1,21 @@ +-- $Id$ +-- Copyright (c) 2016 Pierre Pronchery +-- This file is part of DeforaOS Database libDatabase +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, version 3 of the License. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + + +DROP TABLE "table"; + +CREATE TABLE "table" ( + table_id INTEGER PRIMARY KEY +); diff --git a/tests/tests.sh b/tests/tests.sh index 80869a1..d2bcfb7 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -100,6 +100,7 @@ target="$1" $DATE > "$target" FAILED= echo "Performing tests:" 1>&2 +_test "sqlite3" echo "Expected failures:" 1>&2 _fail "python.sh" if [ -n "$FAILED" ]; then