|
10 | 10 |
|
11 | 11 | int main(int argc, char *argv[]) |
12 | 12 | { |
13 | | - QCoreApplication app(argc, argv); |
| 13 | + QCoreApplication app(argc, argv); |
14 | 14 |
|
15 | | - for (const auto& driver : QSqlDatabase::drivers()) |
16 | | - qDebug() << driver; |
| 15 | + for (const auto& driver : QSqlDatabase::drivers()) { |
| 16 | + qDebug() << "Available QSqlDatabase driver: " << driver; |
| 17 | + } |
17 | 18 |
|
18 | | - Q_ASSERT(QSqlDatabase::isDriverAvailable("QSQLITE")); // from Qt |
19 | | - Q_ASSERT(QSqlDatabase::isDriverAvailable("QSQLCIPHER")); // from our plugin |
| 19 | + Q_ASSERT(QSqlDatabase::isDriverAvailable("QSQLITE")); // from Qt |
| 20 | + Q_ASSERT(QSqlDatabase::isDriverAvailable("QSQLCIPHER")); // from our plugin |
20 | 21 |
|
21 | | - QTemporaryDir tmp; |
22 | | - Q_ASSERT(tmp.isValid()); |
| 22 | + QTemporaryDir tmp; |
| 23 | + Q_ASSERT(tmp.isValid()); |
23 | 24 |
|
24 | | - auto withDB = [&](const char *driver, auto fn) { |
25 | | - QString path = QDir(tmp.path()).absoluteFilePath(QString(driver) + ".db"); |
26 | | - { |
27 | | - QSqlDatabase db = QSqlDatabase::addDatabase(driver, "db"); |
28 | | - db.setDatabaseName(path); |
29 | | - Q_ASSERT(db.open()); |
30 | | - fn(db); |
31 | | - } |
32 | | - QSqlDatabase::removeDatabase("db"); |
33 | | - }; |
| 25 | + auto withDB = [&](const char *driver, auto fn) { |
| 26 | + QString path = QDir(tmp.path()).absoluteFilePath(QString(driver) + ".db"); |
| 27 | + { |
| 28 | + QSqlDatabase db = QSqlDatabase::addDatabase(driver, "db"); |
| 29 | + db.setDatabaseName(path); |
| 30 | + Q_ASSERT(db.open()); |
| 31 | + fn(db); |
| 32 | + } |
| 33 | + QSqlDatabase::removeDatabase("db"); |
| 34 | + }; |
34 | 35 |
|
35 | | - // QSQLITE |
36 | | - { |
| 36 | + // QSQLITE |
| 37 | + { |
37 | 38 | std::cout << "Running Task 1..." << std::endl; |
38 | | - // Create a SQLite db |
39 | | - withDB("QSQLITE", [](auto db){ |
40 | | - db.exec("create table foo (bar integer)"); |
41 | | - db.exec("insert into foo values (42)"); |
42 | | - }); |
| 39 | + // Create a SQLite db |
| 40 | + withDB("QSQLITE", [](auto db){ |
| 41 | + db.exec("create table foo (bar integer)"); |
| 42 | + db.exec("insert into foo values (42)"); |
| 43 | + }); |
43 | 44 |
|
44 | 45 | std::cout << "Running Task 2..." << std::endl; |
45 | | - // Check that we can read from the SQLite db |
46 | | - withDB("QSQLITE", [](auto db){ |
47 | | - QSqlQuery q = db.exec("select bar from foo"); |
48 | | - Q_ASSERT(q.next()); |
49 | | - Q_ASSERT(q.value(0).toInt() == 42); |
50 | | - }); |
| 46 | + // Check that we can read from the SQLite db |
| 47 | + withDB("QSQLITE", [](auto db){ |
| 48 | + QSqlQuery q = db.exec("select bar from foo"); |
| 49 | + Q_ASSERT(q.next()); |
| 50 | + Q_ASSERT(q.value(0).toInt() == 42); |
| 51 | + }); |
51 | 52 |
|
52 | 53 | std::cout << "Running Task 3..." << std::endl; |
53 | | - // Check that SQLite is not SQLCipher |
54 | | - withDB("QSQLITE", [](auto db){ |
55 | | - QSqlQuery q = db.exec("select sqlcipher_export()"); |
56 | | - QString errmsg = q.lastError().databaseText(); |
57 | | - Q_ASSERT(errmsg.startsWith("no such function")); |
58 | | - }); |
59 | | - } |
| 54 | + // Check that SQLite is not SQLCipher |
| 55 | + withDB("QSQLITE", [](auto db){ |
| 56 | + QSqlQuery q = db.exec("select sqlcipher_export()"); |
| 57 | + QString errmsg = q.lastError().databaseText(); |
| 58 | + Q_ASSERT(errmsg.startsWith("no such function")); |
| 59 | + }); |
| 60 | + } |
60 | 61 |
|
61 | | - // QSQLCIPHER |
62 | | - { |
| 62 | + // QSQLCIPHER |
| 63 | + { |
63 | 64 | std::cout << "Running Task 4..." << std::endl; |
64 | | - // Check that SQLCipher is not SQLite |
65 | | - withDB("QSQLCIPHER", [](auto db){ |
66 | | - QSqlQuery q = db.exec("select sqlcipher_export()"); |
67 | | - QString errmsg = q.lastError().databaseText(); |
68 | | - Q_ASSERT(errmsg.startsWith("wrong number of arguments")); |
69 | | - }); |
| 65 | + // Check that SQLCipher is not SQLite |
| 66 | + withDB("QSQLCIPHER", [](auto db){ |
| 67 | + QSqlQuery q = db.exec("select sqlcipher_export()"); |
| 68 | + QString errmsg = q.lastError().databaseText(); |
| 69 | + Q_ASSERT(errmsg.startsWith("wrong number of arguments")); |
| 70 | + }); |
70 | 71 |
|
71 | 72 | std::cout << "Running Task 5..." << std::endl; |
72 | | - // Create a SQLCipher db with a passphrase |
73 | | - withDB("QSQLCIPHER", [](auto db){ |
74 | | - db.exec("pragma key='foobar'"); |
75 | | - db.exec("create table foo (bar integer)"); |
76 | | - db.exec("insert into foo values (42)"); |
77 | | - }); |
| 73 | + // Create a SQLCipher db with a passphrase |
| 74 | + withDB("QSQLCIPHER", [](auto db){ |
| 75 | + std::cout << "Running Task 5.1..." << std::endl; |
| 76 | + db.exec("PRAGMA key = 'foobar';"); |
| 77 | + std::cout << "Running Task 5.2... (this might trigger a segfault)" << std::endl; |
| 78 | + db.exec("CREATE TABLE `foo` (`bar` INTEGER);"); |
| 79 | + std::cout << "Running Task 5.3..." << std::endl; |
| 80 | + db.exec("INSERT INTO `foo` VALUES (42);"); |
| 81 | + }); |
78 | 82 |
|
79 | 83 | std::cout << "Running Task 6..." << std::endl; |
80 | | - // Check that we can't read from the SQLCipher db without the passphrase |
81 | | - withDB("QSQLCIPHER", [](auto db){ |
82 | | - QSqlQuery q = db.exec("select bar from foo"); |
83 | | - Q_ASSERT(!q.next()); |
84 | | - }); |
| 84 | + // Check that we can't read from the SQLCipher db without the passphrase |
| 85 | + withDB("QSQLCIPHER", [](auto db){ |
| 86 | + QSqlQuery q = db.exec("select bar from foo"); |
| 87 | + Q_ASSERT(!q.next()); |
| 88 | + }); |
85 | 89 |
|
86 | 90 | std::cout << "Running Task 7..." << std::endl; |
87 | | - // Check that we can read from the SQLCipher db with the passphrase |
88 | | - withDB("QSQLCIPHER", [](auto db){ |
89 | | - db.exec("PRAGMA key = 'foobar';"); |
90 | | - QSqlQuery q = db.exec("select bar from foo"); |
91 | | - Q_ASSERT(q.next()); |
92 | | - Q_ASSERT(q.value(0).toInt() == 42); |
93 | | - }); |
| 91 | + // Check that we can read from the SQLCipher db with the passphrase |
| 92 | + withDB("QSQLCIPHER", [](auto db){ |
| 93 | + db.exec("PRAGMA key = 'foobar';"); |
| 94 | + QSqlQuery q = db.exec("select bar from foo"); |
| 95 | + Q_ASSERT(q.next()); |
| 96 | + Q_ASSERT(q.value(0).toInt() == 42); |
| 97 | + }); |
94 | 98 | std::cout << "Success! All tests completed." << std::endl; |
95 | | - } |
| 99 | + } |
96 | 100 |
|
97 | | - return 0; |
| 101 | + return 0; |
98 | 102 | } |
0 commit comments