implemented append mode for xcbc, testcase
[strongswan.git] / src / charon / plugins / unit_tester / tests / test_sqlite.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <library.h>
17 #include <daemon.h>
18 #include <utils/enumerator.h>
19
20 #include <unistd.h>
21
22
23 #define DBFILE "/tmp/strongswan-test.db"
24
25 /*******************************************************************************
26 * sqlite simple test
27 ******************************************************************************/
28 bool test_sqlite()
29 {
30 database_t *db;
31 char *txt = "I'm a superduper test";
32 char buf[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
33 chunk_t data = chunk_from_buf(buf);
34 int row;
35 chunk_t qdata;
36 char *qtxt;
37 bool good = FALSE;
38 enumerator_t *enumerator;
39
40 db = lib->db->create(lib->db, "sqlite://" DBFILE);
41 if (!db)
42 {
43 return FALSE;
44 }
45 if (db->execute(db, NULL, "CREATE TABLE test (txt TEXT, data BLOB)") < 0)
46 {
47 return FALSE;
48 }
49 if (db->execute(db, &row, "INSERT INTO test (txt, data) VALUES (?,?)",
50 DB_TEXT, txt, DB_BLOB, data) < 0)
51 {
52 return FALSE;
53 }
54 if (row != 1)
55 {
56 return FALSE;
57 }
58 enumerator = db->query(db, "SELECT txt, data FROM test WHERE oid = ?",
59 DB_INT, row,
60 DB_TEXT, DB_BLOB);
61 if (!enumerator)
62 {
63 return FALSE;
64 }
65 while (enumerator->enumerate(enumerator, &qtxt, &qdata))
66 {
67 if (good)
68 { /* only one row */
69 good = FALSE;
70 break;
71 }
72 if (streq(qtxt, txt) && chunk_equals(data, qdata))
73 {
74 good = TRUE;
75 }
76 }
77 enumerator->destroy(enumerator);
78 if (!good)
79 {
80 return FALSE;
81 }
82 if (db->execute(db, NULL, "DELETE FROM test WHERE oid = ?", DB_INT, row) != 1)
83 {
84 return FALSE;
85 }
86 if (db->execute(db, NULL, "DROP TABLE test") < 0)
87 {
88 return FALSE;
89 }
90 db->destroy(db);
91 unlink(DBFILE);
92 return TRUE;
93 }
94