2 This file is part of GNUnet
3 Copyright (C) 2016 GNUnet e.V.
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.
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.
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.
22 * @brief Tests for convenience MySQL database
23 * @author Christophe Genevey
26 #include <mysql/mysql.h>
27 #include "gnunet_my_lib.h"
28 #include "gnunet_mysql_lib.h"
29 #include "gnunet_util_lib.h"
33 * Run actual test queries.
35 * @param contexte the current context of mysql
39 run_queries (struct GNUNET_MYSQL_Context *context)
41 struct GNUNET_CRYPTO_RsaPublicKey *pub = NULL;
42 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
43 struct GNUNET_CRYPTO_RsaSignature *sig = NULL;;
44 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
45 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
46 struct GNUNET_TIME_Absolute abs_time2;
47 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
48 struct GNUNET_TIME_Absolute forever2;
49 const struct GNUNET_TIME_AbsoluteNBO abs_time_nbo = GNUNET_TIME_absolute_hton (abs_time);
50 struct GNUNET_HashCode hc;
51 struct GNUNET_HashCode hc2;
52 const char msg[] = "hello";
56 const char msg3[] = "world";
68 struct GNUNET_MYSQL_StatementHandle *statements_handle_insert = NULL;
69 struct GNUNET_MYSQL_StatementHandle *statements_handle_select = NULL;
71 struct GNUNET_CRYPTO_RsaPrivateKey *priv = NULL;
72 struct GNUNET_HashCode hmsg;
74 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
75 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
76 memset (&hmsg, 42, sizeof(hmsg));
77 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
83 memset (&hc, 0, sizeof(hc));
84 memset (&hc2, 0, sizeof(hc2));
86 statements_handle_insert
87 = GNUNET_MYSQL_statement_prepare (context,
88 "INSERT INTO test_my2 ("
101 "( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
103 if (NULL == statements_handle_insert)
105 fprintf (stderr, "Failed to prepared statement INSERT\n");
106 GNUNET_CRYPTO_rsa_signature_free (sig);
107 GNUNET_CRYPTO_rsa_private_key_free (priv);
108 GNUNET_CRYPTO_rsa_public_key_free (pub);
112 struct GNUNET_MY_QueryParam params_insert[] = {
113 GNUNET_MY_query_param_rsa_public_key (pub),
114 GNUNET_MY_query_param_rsa_signature (sig),
115 GNUNET_MY_query_param_absolute_time (&abs_time),
116 GNUNET_MY_query_param_absolute_time (&forever),
117 GNUNET_MY_query_param_absolute_time_nbo (&abs_time_nbo),
118 GNUNET_MY_query_param_auto_from_type (&hc),
119 GNUNET_MY_query_param_fixed_size (msg, strlen (msg)),
120 GNUNET_MY_query_param_string (msg3),
121 GNUNET_MY_query_param_uint16 (&u16),
122 GNUNET_MY_query_param_uint32 (&u32),
123 GNUNET_MY_query_param_uint64 (&u64),
124 GNUNET_MY_query_param_end
127 if (GNUNET_OK != GNUNET_MY_exec_prepared(context,
128 statements_handle_insert,
131 fprintf (stderr, "Failed to execute prepared statement INSERT\n");
132 GNUNET_CRYPTO_rsa_signature_free (sig);
133 GNUNET_CRYPTO_rsa_private_key_free (priv);
134 GNUNET_CRYPTO_rsa_public_key_free (pub);
138 statements_handle_select
139 = GNUNET_MYSQL_statement_prepare (context,
153 if (NULL == statements_handle_select)
155 fprintf(stderr, "Failed to prepared statement SELECT\n");
156 GNUNET_CRYPTO_rsa_signature_free (sig);
157 GNUNET_CRYPTO_rsa_private_key_free (priv);
158 GNUNET_CRYPTO_rsa_public_key_free (pub);
162 struct GNUNET_MY_QueryParam params_select[] = {
163 GNUNET_MY_query_param_end
166 if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
167 statements_handle_select,
170 fprintf (stderr, "Failed to execute prepared statement SELECT\n");
171 GNUNET_CRYPTO_rsa_signature_free (sig);
172 GNUNET_CRYPTO_rsa_private_key_free (priv);
173 GNUNET_CRYPTO_rsa_public_key_free (pub);
177 struct GNUNET_MY_ResultSpec results_select[] = {
178 GNUNET_MY_result_spec_rsa_public_key (&pub2),
179 GNUNET_MY_result_spec_rsa_signature (&sig2),
180 GNUNET_MY_result_spec_absolute_time (&abs_time2),
181 GNUNET_MY_result_spec_absolute_time (&forever2),
182 GNUNET_MY_result_spec_auto_from_type (&hc2),
183 GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len),
184 GNUNET_MY_result_spec_string (&msg4),
185 GNUNET_MY_result_spec_uint16 (&u162),
186 GNUNET_MY_result_spec_uint32 (&u322),
187 GNUNET_MY_result_spec_uint64 (&u642),
188 GNUNET_MY_result_spec_end
191 ret = GNUNET_MY_extract_result (statements_handle_select,
194 GNUNET_assert (GNUNET_YES == ret);
195 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
196 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
200 sizeof (struct GNUNET_HashCode)));
202 GNUNET_assert (NULL != sig2);
203 GNUNET_assert (NULL != pub2);
205 GNUNET_CRYPTO_rsa_signature_cmp (sig,
208 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
211 GNUNET_break (strlen (msg) == msg2_len);
217 GNUNET_break (strlen (msg3) == strlen(msg4));
222 GNUNET_break (16 == u162);
223 GNUNET_break (32 == u322);
224 GNUNET_break (UINT64_MAX == u642);
226 GNUNET_MY_cleanup_result (results_select);
228 GNUNET_CRYPTO_rsa_signature_free (sig);
229 GNUNET_CRYPTO_rsa_private_key_free (priv);
230 GNUNET_CRYPTO_rsa_public_key_free (pub);
232 if (GNUNET_OK != ret)
240 main (int argc, const char *const argv[])
242 struct GNUNET_CONFIGURATION_Handle *config;
243 struct GNUNET_MYSQL_Context *context;
246 GNUNET_log_setup ("test-my",
250 config = GNUNET_CONFIGURATION_create ();
252 GNUNET_CONFIGURATION_parse (config, "test_my.conf"))
254 fprintf (stderr, "Failed to parse configuaration\n");
258 context = GNUNET_MYSQL_context_create (config,
262 fprintf(stderr, "Failed to connect to database\n");
266 (void) GNUNET_MYSQL_statement_run (context,
267 "DROP TABLE test_my2;");
270 GNUNET_MYSQL_statement_run (context,
271 "CREATE TABLE IF NOT EXISTS test_my2("
274 ",abs_time BIGINT NOT NULL"
275 ",forever BIGINT NOT NULL"
276 ",abs_time_nbo BIGINT NOT NULL"
277 ",hash BLOB NOT NULL CHECK(LENGTH(hash)=64)"
278 ",vsize BLOB NOT NULL"
280 ",u16 SMALLINT NOT NULL"
282 ",u64 BIGINT NOT NULL"
286 "Failed to create table. Database likely not setup correctly.\n");
287 GNUNET_MYSQL_statements_invalidate (context);
288 GNUNET_MYSQL_context_destroy (context);
293 ret = run_queries (context);
295 GNUNET_MYSQL_context_destroy (context);
296 GNUNET_free (config);