From 9eb50358277b1ebfb9642706185f8f0ad883aef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Kr=C3=BCger?= Date: Thu, 16 Feb 2017 14:33:19 +0100 Subject: [PATCH] Add SQLCipher test --- .travis.yml | 3 ++ Makefile.in | 2 +- tests/sqlcipher.cc | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/sqlcipher.cc diff --git a/.travis.yml b/.travis.yml index 2f4b3bd6..4b76e670 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,10 @@ addons: - gcc-5 - g++-5 - libsqlite3-dev + - libsqlcipher-dev - libboost-all-dev before_install: - export CXX="g++-5" CC="gcc-5" + +script: ./configure && make test && make clean && make LDFLAGS="-lsqlcipher -DENABLE_SQLCIPHER_TESTS" test diff --git a/Makefile.in b/Makefile.in index ded867ce..3752dc18 100644 --- a/Makefile.in +++ b/Makefile.in @@ -84,7 +84,7 @@ tests/%.result_: tests/%.test a=$$? ;\ if [ $$a != 0 ]; \ then \ - if [ $$a -ge 128 and ] ; \ + if [ $$a -ge 128 ] ; \ then \ echo Crash!! > $@ ; \ elif [ $$a -eq 42 ] ;\ diff --git a/tests/sqlcipher.cc b/tests/sqlcipher.cc new file mode 100644 index 00000000..be95b5eb --- /dev/null +++ b/tests/sqlcipher.cc @@ -0,0 +1,90 @@ +#ifdef ENABLE_SQLCIPHER_TESTS +#include +#include +#include +#include +using namespace sqlite; +using namespace std; + +struct TmpFile +{ + string fname; + + TmpFile() + { + char f[]="/tmp/sqlite_modern_cpp_test_XXXXXX"; + int fid = mkstemp(f); + close(fid); + + fname = f; + } + + ~TmpFile() + { + unlink(fname.c_str()); + } +}; + +int main() +{ + try + { + TmpFile file; + sqlcipher_config config; + { + config.key = "DebugKey"; + sqlcipher_database db(file.fname, config); + + db << "CREATE TABLE foo (a integer, b string);"; + db << "INSERT INTO foo VALUES (?, ?)" << 1 << "hello"; + db << "INSERT INTO foo VALUES (?, ?)" << 2 << "world"; + + string str; + db << "SELECT b from FOO where a=?;" << 2 >> str; + + if(str != "world") + { + cout << "Bad result on line " << __LINE__ << endl; + exit(EXIT_FAILURE); + } + } + try { + config.key = "DebugKey2"; + sqlcipher_database db(file.fname, config); + db << "INSERT INTO foo VALUES (?, ?)" << 3 << "fail"; + + cout << "Can open with wrong key"; + exit(EXIT_FAILURE); + } catch(exceptions::notadb) { + // Expected, wrong key + } + { + config.key = "DebugKey"; + sqlcipher_database db(file.fname, config); + db.rekey("DebugKey2"); + } + { + config.key = "DebugKey2"; + sqlcipher_database db(file.fname, config); + db << "INSERT INTO foo VALUES (?, ?)" << 3 << "fail"; + } + } + catch(sqlite_exception e) + { + cout << "Unexpected error " << e.what() << endl; + exit(EXIT_FAILURE); + } + catch(...) + { + cout << "Unknown error\n"; + exit(EXIT_FAILURE); + } + + cout << "OK\n"; + exit(EXIT_SUCCESS); +} +#else +int main() { + return 42; //Skip test +} +#endif