Skip to content

Commit 9eb5035

Browse files
committed
Add SQLCipher test
1 parent 32f8f25 commit 9eb5035

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ addons:
1010
- gcc-5
1111
- g++-5
1212
- libsqlite3-dev
13+
- libsqlcipher-dev
1314
- libboost-all-dev
1415

1516
before_install:
1617
- export CXX="g++-5" CC="gcc-5"
18+
19+
script: ./configure && make test && make clean && make LDFLAGS="-lsqlcipher -DENABLE_SQLCIPHER_TESTS" test

Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ tests/%.result_: tests/%.test
8484
a=$$? ;\
8585
if [ $$a != 0 ]; \
8686
then \
87-
if [ $$a -ge 128 and ] ; \
87+
if [ $$a -ge 128 ] ; \
8888
then \
8989
echo Crash!! > $@ ; \
9090
elif [ $$a -eq 42 ] ;\

tests/sqlcipher.cc

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#ifdef ENABLE_SQLCIPHER_TESTS
2+
#include <iostream>
3+
#include <cstdlib>
4+
#include <unistd.h>
5+
#include <sqlite_modern_cpp/sqlcipher.h>
6+
using namespace sqlite;
7+
using namespace std;
8+
9+
struct TmpFile
10+
{
11+
string fname;
12+
13+
TmpFile()
14+
{
15+
char f[]="/tmp/sqlite_modern_cpp_test_XXXXXX";
16+
int fid = mkstemp(f);
17+
close(fid);
18+
19+
fname = f;
20+
}
21+
22+
~TmpFile()
23+
{
24+
unlink(fname.c_str());
25+
}
26+
};
27+
28+
int main()
29+
{
30+
try
31+
{
32+
TmpFile file;
33+
sqlcipher_config config;
34+
{
35+
config.key = "DebugKey";
36+
sqlcipher_database db(file.fname, config);
37+
38+
db << "CREATE TABLE foo (a integer, b string);";
39+
db << "INSERT INTO foo VALUES (?, ?)" << 1 << "hello";
40+
db << "INSERT INTO foo VALUES (?, ?)" << 2 << "world";
41+
42+
string str;
43+
db << "SELECT b from FOO where a=?;" << 2 >> str;
44+
45+
if(str != "world")
46+
{
47+
cout << "Bad result on line " << __LINE__ << endl;
48+
exit(EXIT_FAILURE);
49+
}
50+
}
51+
try {
52+
config.key = "DebugKey2";
53+
sqlcipher_database db(file.fname, config);
54+
db << "INSERT INTO foo VALUES (?, ?)" << 3 << "fail";
55+
56+
cout << "Can open with wrong key";
57+
exit(EXIT_FAILURE);
58+
} catch(exceptions::notadb) {
59+
// Expected, wrong key
60+
}
61+
{
62+
config.key = "DebugKey";
63+
sqlcipher_database db(file.fname, config);
64+
db.rekey("DebugKey2");
65+
}
66+
{
67+
config.key = "DebugKey2";
68+
sqlcipher_database db(file.fname, config);
69+
db << "INSERT INTO foo VALUES (?, ?)" << 3 << "fail";
70+
}
71+
}
72+
catch(sqlite_exception e)
73+
{
74+
cout << "Unexpected error " << e.what() << endl;
75+
exit(EXIT_FAILURE);
76+
}
77+
catch(...)
78+
{
79+
cout << "Unknown error\n";
80+
exit(EXIT_FAILURE);
81+
}
82+
83+
cout << "OK\n";
84+
exit(EXIT_SUCCESS);
85+
}
86+
#else
87+
int main() {
88+
return 42; //Skip test
89+
}
90+
#endif

0 commit comments

Comments
 (0)