implemented append mode for xcbc, testcase
[strongswan.git] / src / charon / plugins / unit_tester / tests / test_mysql.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 /*******************************************************************************
21 * mysql simple test
22 ******************************************************************************/
23 bool test_mysql()
24 {
25 database_t *db;
26 char *txt = "I'm a superduper test";
27 char buf[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
28 chunk_t data = chunk_from_buf(buf);
29 int row;
30 chunk_t qdata;
31 char *qtxt;
32 bool good = FALSE;
33 enumerator_t *enumerator;
34
35 db = lib->db->create(lib->db, "mysql://testuser:testpass@localhost/test");
36 if (!db)
37 {
38 return FALSE;
39 }
40 if (db->execute(db, NULL, "CREATE TABLE test ("
41 "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
42 "txt TEXT, data BLOB)") < 0)
43 {
44 return FALSE;
45 }
46 if (db->execute(db, &row, "INSERT INTO test (txt, data) VALUES (?,?)",
47 DB_TEXT, txt, DB_BLOB, data) < 0)
48 {
49 return FALSE;
50 }
51 if (row != 1)
52 {
53 return FALSE;
54 }
55 enumerator = db->query(db, "SELECT txt, data FROM test WHERE id = ?",
56 DB_INT, row,
57 DB_TEXT, DB_BLOB);
58 if (!enumerator)
59 {
60 return FALSE;
61 }
62 while (enumerator->enumerate(enumerator, &qtxt, &qdata))
63 {
64 if (good)
65 { /* only one row */
66 good = FALSE;
67 break;
68 }
69 if (streq(qtxt, txt) && chunk_equals(data, qdata))
70 {
71 good = TRUE;
72 }
73 }
74 enumerator->destroy(enumerator);
75 if (!good)
76 {
77 return FALSE;
78 }
79 if (db->execute(db, NULL, "DELETE FROM test WHERE id = ?", DB_INT, row) != 1)
80 {
81 return FALSE;
82 }
83 if (db->execute(db, NULL, "DROP TABLE test") < 0)
84 {
85 return FALSE;
86 }
87 db->destroy(db);
88 return TRUE;
89 }
90