attributes: Move the configuration attributes framework to libcharon
[strongswan.git] / src / libcharon / 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 <collections/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 chunk_t data = chunk_from_chars(0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08);
33 int row;
34 chunk_t qdata;
35 char *qtxt;
36 bool good = FALSE;
37 enumerator_t *enumerator;
38
39 db = lib->db->create(lib->db, "sqlite://" DBFILE);
40 if (!db)
41 {
42 return FALSE;
43 }
44 if (db->execute(db, NULL, "CREATE TABLE test (txt TEXT, data BLOB)") < 0)
45 {
46 return FALSE;
47 }
48 if (db->execute(db, &row, "INSERT INTO test (txt, data) VALUES (?,?)",
49 DB_TEXT, txt, DB_BLOB, data) < 0)
50 {
51 return FALSE;
52 }
53 if (row != 1)
54 {
55 return FALSE;
56 }
57 enumerator = db->query(db, "SELECT txt, data FROM test WHERE oid = ?",
58 DB_INT, row,
59 DB_TEXT, DB_BLOB);
60 if (!enumerator)
61 {
62 return FALSE;
63 }
64 while (enumerator->enumerate(enumerator, &qtxt, &qdata))
65 {
66 if (good)
67 { /* only one row */
68 good = FALSE;
69 break;
70 }
71 if (streq(qtxt, txt) && chunk_equals(data, qdata))
72 {
73 good = TRUE;
74 }
75 }
76 enumerator->destroy(enumerator);
77 if (!good)
78 {
79 return FALSE;
80 }
81 if (db->execute(db, NULL, "DELETE FROM test WHERE oid = ?", DB_INT, row) != 1)
82 {
83 return FALSE;
84 }
85 if (db->execute(db, NULL, "DROP TABLE test") < 0)
86 {
87 return FALSE;
88 }
89 db->destroy(db);
90 unlink(DBFILE);
91 return TRUE;
92 }
93