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