diff options
author | Christophe Genevey Metat <genevey.christophe@gmail.com> | 2016-05-24 16:16:41 +0000 |
---|---|---|
committer | Christophe Genevey Metat <genevey.christophe@gmail.com> | 2016-05-24 16:16:41 +0000 |
commit | b2bbad6a70f3d7e089b14f282dd8e6a4dfe6ce46 (patch) | |
tree | 8ea05556a7415041e5b417f7e9dae02a915c3d6f /src/my | |
parent | 52aa7299f2c3a9f9e80b366ca9068a6edf3b9bab (diff) | |
download | gnunet-b2bbad6a70f3d7e089b14f282dd8e6a4dfe6ce46.tar.gz gnunet-b2bbad6a70f3d7e089b14f282dd8e6a4dfe6ce46.zip |
test case for mysql
Diffstat (limited to 'src/my')
-rw-r--r-- | src/my/test_my.c | 327 |
1 files changed, 327 insertions, 0 deletions
diff --git a/src/my/test_my.c b/src/my/test_my.c new file mode 100644 index 000000000..1ab6ef1b7 --- /dev/null +++ b/src/my/test_my.c | |||
@@ -0,0 +1,327 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file my/test_my.c | ||
22 | * @brief Tests for convenience MySQL database | ||
23 | * @author Christophe Genevey | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include <mysql/mysql.h> | ||
27 | #include "gnunet_my_lib.h" | ||
28 | |||
29 | /** | ||
30 | * Setup prepared statements. | ||
31 | * | ||
32 | * @param mysql connection handle to initialize | ||
33 | * @return | ||
34 | #GNUNET_OK on success | ||
35 | #GNUNET_SYSERR on failure | ||
36 | */ | ||
37 | static int | ||
38 | mysql_prepare (MYSQL * mysql) | ||
39 | { | ||
40 | MYSQL_RES *result; | ||
41 | MYSQL_STMT *stmt; | ||
42 | |||
43 | stmt = mysql_stmt_init (mysql); | ||
44 | #define PREPARE (name, sql, ...) | ||
45 | do | ||
46 | { | ||
47 | result = mysql_stmt_prepare(stmt, name, sql, __VA_ARGS__); | ||
48 | if (result) | ||
49 | { | ||
50 | GNUNET_break (0); | ||
51 | mysql_stmt_free_result (stmt); | ||
52 | result = NULL; | ||
53 | return GNUNET_SYSERR; | ||
54 | } | ||
55 | mysql_stmt_free_result (stmt); | ||
56 | result = NULL; | ||
57 | } while (0); | ||
58 | |||
59 | PREPARE ("test_insert", | ||
60 | "INSERT INTO test_my (" | ||
61 | "pub" | ||
62 | ",sig" | ||
63 | ",abs_time" | ||
64 | ",forever" | ||
65 | ",hash" | ||
66 | ",vsize" | ||
67 | ",u16" | ||
68 | ",u32" | ||
69 | ",u64" | ||
70 | ") VALUES " | ||
71 | "($1, $2, $3, $4, $5, $6, | ||
72 | $7, $8, $9);", | ||
73 | 9, NULL); | ||
74 | |||
75 | PREPARE ("test_select", | ||
76 | "SELECT" | ||
77 | "pub" | ||
78 | ",sig" | ||
79 | ",abs_time" | ||
80 | ",forever" | ||
81 | ",hash" | ||
82 | ",vsize" | ||
83 | ",u16" | ||
84 | ",u32" | ||
85 | ",u64" | ||
86 | " FROM test_my" | ||
87 | " ORDER BY abs_time DESC" | ||
88 | " LIMIT 1;", | ||
89 | 0, NULL); | ||
90 | return GNUNET_OK; | ||
91 | #undef PREPARE | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Run actual test queries. | ||
96 | * | ||
97 | * @param mysql coonection handle to initialize | ||
98 | * @return 0 on succes | ||
99 | */ | ||
100 | static int | ||
101 | run_queries (MYSQL * mysql) | ||
102 | { | ||
103 | struct GNUNET_CRYPTO_RsaPublicKey *pub; | ||
104 | struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL; | ||
105 | struct GNUNET_CRYPTO_RsaSignature *sig; | ||
106 | struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL; | ||
107 | struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get () ; | ||
108 | struct GNUNET_TIME_Absolute abs_time2; | ||
109 | struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
110 | struct GNUNET_TIME_Absolute forever2; | ||
111 | struct GNUNET_HashCode hc; | ||
112 | struct GNUNET_HashCode hc2; | ||
113 | MYSQL_RES * result; | ||
114 | int ret; | ||
115 | struct GNUNET_CRYPTO_RsaPrivateKey *priv; | ||
116 | const char msg[] = "hello"; | ||
117 | void *msg2; | ||
118 | struct GNUNET_HashCode hmsg; | ||
119 | size_t msg2_len; | ||
120 | uint16_t u16; | ||
121 | uint16_t u162; | ||
122 | uint32_t u32; | ||
123 | uint32_t u322; | ||
124 | uint64_t u64; | ||
125 | uint64_t u642; | ||
126 | |||
127 | priv = GNUNET_CRYPTO_rsa_private_key_create (1024); | ||
128 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); | ||
129 | memset (&hmsg, 42, sizeof (hmsg)); | ||
130 | sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, | ||
131 | &hmsg); | ||
132 | u16 = 16; | ||
133 | u32 = 32; | ||
134 | u64 = 64; | ||
135 | |||
136 | { | ||
137 | struct GNUNET_MY_QueryParam params_insert[] = { | ||
138 | GNUNET_MY_query_param_rsa_public_key (pub), | ||
139 | GNUNET_MY_query_param_rsa_signature (sig), | ||
140 | GNUNET_MY_query_param_absolute_time (&abs_time), | ||
141 | GNUNET_MY_query_param_absolute_time (&forever), | ||
142 | GNUNET_MY_query_param_auto_from_type (&hc), | ||
143 | GNUNET_MY_query_param_fixed_size (msg, strlen (msg)), | ||
144 | GNUNET_MY_query_param_uint16 (&u16), | ||
145 | GNUNET_MY_query_param_uint32 (&u32), | ||
146 | GNUNET_MY_query_param_uint64 (&u64), | ||
147 | GNUNET_MY_query_param_end | ||
148 | }; | ||
149 | |||
150 | struct GNUNET_MY_QueryParam params_select[] = { | ||
151 | GNUNET_MY_query_param_end | ||
152 | }; | ||
153 | |||
154 | struct GNUNET_MY_ResultSpec results_select[] = { | ||
155 | GNUNET_MY_result_spec_rsa_public_key ("pub", &pub2), | ||
156 | GNUNET_MY_result_spec_rsa_signature ("sig", &sig2), | ||
157 | GNUNET_MY_result_spec_absolute_time ("abs_time", &abs_time2), | ||
158 | GNUNET_MY_result_spec_absolute_time ("forever", &forever2), | ||
159 | GNUNET_MY_result_spec_auto_from_type ("hash", &hc2), | ||
160 | GNUNET_MY_result_spec_variable_size ("vsize", &msg2, &msg2_len), | ||
161 | GNUNET_MY_result_spec_uint16 ("u16", &u162), | ||
162 | GNUNET_MY_result_spec_uint32 ("u32", &u322), | ||
163 | GNUNET_MY_result_spec_uint64 ("u64", &u642), | ||
164 | GNUNET_MY_result_epc_end | ||
165 | }; | ||
166 | |||
167 | if(GNUNET_MY_exec_prepared ( mysql, | ||
168 | "test_insert", | ||
169 | params_insert)) | ||
170 | { | ||
171 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
172 | "Database failure: \n"); | ||
173 | |||
174 | //free result | ||
175 | |||
176 | GNUNET_CRYPTO_rsa_signature_free (sig); | ||
177 | GNUNET_CRYPTO_rsa_private_key_free (priv); | ||
178 | GNUNET_CYPTO_rsa_public_key_free (pub); | ||
179 | return 1; | ||
180 | } | ||
181 | |||
182 | //free result | ||
183 | |||
184 | result = GNUNET_MY_exec_prepared (mysql, "test_select", params_select); | ||
185 | /* if(1 != mysql_fetch_length (result)) | ||
186 | { | ||
187 | GNUNET_break (0); | ||
188 | GNUNET_CRYPTO_rsa_signature_free (sig); | ||
189 | GNUNET_CRYPTO_rsa_private_key_free (priv); | ||
190 | GNUNET_CRYPTO_rsa_public_key_free (pub); | ||
191 | |||
192 | return 1; | ||
193 | } | ||
194 | */ | ||
195 | if (GNUNET_MY_exec_prepared (mysql | ||
196 | , "test_select" | ||
197 | , params_select)) | ||
198 | { | ||
199 | GNUNET_break (0); | ||
200 | GNUNET_CRYPTO_rsa_signature_free (sig); | ||
201 | GNUNET_CRYPTO_rsa_private_key_free (priv); | ||
202 | GNUNET_CRYPTO_rsa_public_key_free (pub); | ||
203 | |||
204 | return 1; | ||
205 | } | ||
206 | |||
207 | |||
208 | ret = GNUNET_MY_extract_result (result, | ||
209 | results_select, | ||
210 | 0); | ||
211 | GNUNET_break (GNUNET_YES == ret); | ||
212 | GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us); | ||
213 | GNUNET_break (forever.abs_value_us == forever2.abs_value_us); | ||
214 | GNUNET_break (0 == | ||
215 | memcmp (&hc, | ||
216 | &hc2, | ||
217 | sizeof (struct GNUNET_HashCode))); | ||
218 | GNUNET_break (0 == | ||
219 | GNUNET_CRYPTO_rsa_signature_cmp (sig, | ||
220 | sig2)); | ||
221 | GNUNET_break (0 == | ||
222 | GNUNET_CRYPTO_rsa_public_key_cmp (pub, | ||
223 | pub2)); | ||
224 | GNUNET_break (strlen (msg) == msg2_len); | ||
225 | GNUNET_break (0 == | ||
226 | strncmp (msg, | ||
227 | msg2, | ||
228 | msg2_len)); | ||
229 | GNUNET_break (16 == u162); | ||
230 | GNUNET_break (32 == u322); | ||
231 | GNUNET_break (64 == u642); | ||
232 | } | ||
233 | |||
234 | GNUNET_CRYPTO_rsa_signature_free (sig); | ||
235 | GNUNET_CRYPTO_rsa_private_key_free (priv); | ||
236 | GNUNET_CRYPTO_rsa_public_key_free (pub); | ||
237 | |||
238 | if (GNUNET_OK != ret) | ||
239 | return 1; | ||
240 | return 0; | ||
241 | } | ||
242 | |||
243 | |||
244 | int | ||
245 | main (int argc, const char * const argv[]) | ||
246 | { | ||
247 | |||
248 | MYSQL *mysql; | ||
249 | // MYSQL_RES *result; | ||
250 | |||
251 | int ret; | ||
252 | |||
253 | char *hote = ""; | ||
254 | char *pseudo = ""; | ||
255 | char *mdp = ""; | ||
256 | char *database = ""; | ||
257 | |||
258 | mysql_init (mysql); | ||
259 | |||
260 | mysql_options (mysql, | ||
261 | MYSQL_READ_DEFAULT_GROUP, | ||
262 | NULL); | ||
263 | |||
264 | GNUNET_log_setup ( "test-my", | ||
265 | "WARNING", | ||
266 | NULL); | ||
267 | |||
268 | if (! mysql_real_connect (mysql | ||
269 | ,hote | ||
270 | ,pseudo | ||
271 | ,mdp,database | ||
272 | ,0 | ||
273 | ,NULL | ||
274 | ,0 | ||
275 | )) | ||
276 | { | ||
277 | fprintf( stderr, | ||
278 | "Cannot run test, database connection failed : %s\n", | ||
279 | mysql_error (mysql)); | ||
280 | GNUNET_break (0); | ||
281 | |||
282 | return 0; | ||
283 | } | ||
284 | |||
285 | if (mysql_query (mysql, "CREATE TABLE test_my(" | ||
286 | "pub INT" | ||
287 | ", sig INT" | ||
288 | ", abs_time BIGINT" | ||
289 | ", forever BIGINT" | ||
290 | ", hash INT" | ||
291 | ", vsize VARCHAR" | ||
292 | ", u16 SMALLINT" | ||
293 | ", u32 INT" | ||
294 | ", u64 BIGINT" | ||
295 | ")")) | ||
296 | { | ||
297 | fprintf (stderr, | ||
298 | "Failed to create table : %s\n", | ||
299 | mysql_error (mysql)); | ||
300 | |||
301 | mysql_close (mysql); | ||
302 | return 1; | ||
303 | } | ||
304 | |||
305 | if (GNUNET_OK != | ||
306 | mysql_prepare (mysql)) | ||
307 | { | ||
308 | GNUNET_break (0) ; | ||
309 | mysql_close (mysql); | ||
310 | return 1; | ||
311 | } | ||
312 | |||
313 | ret = run_queries (mysql); | ||
314 | |||
315 | if (mysql_query (mysql, | ||
316 | "DROP TABLE test_my;")) | ||
317 | { | ||
318 | fprintf (stderr, "Failed to drop table : %s\n", | ||
319 | mysql_error (mysql)); | ||
320 | mysql_close (mysql); | ||
321 | return 1; | ||
322 | } | ||
323 | |||
324 | mysql_close (mysql); | ||
325 | |||
326 | return ret; | ||
327 | } | ||