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"
32 * Run actual test queries.
34 * @param contexte the current context of mysql
38 run_queries (struct GNUNET_MYSQL_Context *context)
40 const struct GNUNET_CRYPTO_RsaPublicKey *pub;
41 struct GNUNET_CRYPTO_RsaSignature *sig;
42 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
43 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
44 struct GNUNET_HashCode hc;
45 const char msg[] = "hello";
50 struct GNUNET_MYSQL_StatementHandle *statements_handle_insert;
51 // struct GNUNET_MYSQL_StatementHandle *statements_handle_select;
53 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
54 struct GNUNET_HashCode hmsg;
56 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
57 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
58 memset (&hmsg, 42, sizeof(hmsg));
59 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
65 statements_handle_insert = GNUNET_MYSQL_statement_prepare (context,
66 "INSERT INTO test_my ("
77 "($1, $2, $3, $4, $5, $6,"
80 if (NULL == statements_handle_insert)
82 fprintf(stderr, "Failed to prepared statement INSERT\n");
86 struct GNUNET_MY_QueryParam params_insert[] = {
87 GNUNET_MY_query_param_rsa_public_key (pub),
88 GNUNET_MY_query_param_rsa_signature (sig),
89 GNUNET_MY_query_param_absolute_time (&abs_time),
90 GNUNET_MY_query_param_absolute_time (&forever),
91 GNUNET_MY_query_param_auto_from_type (&hc),
92 GNUNET_MY_query_param_fixed_size (msg, strlen (msg)),
93 GNUNET_MY_query_param_uint16 (&u16),
94 GNUNET_MY_query_param_uint32 (&u32),
95 GNUNET_MY_query_param_uint64 (&u64),
96 GNUNET_MY_query_param_end
100 if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
101 statements_handle_insert,
104 fprintf (stderr, "Failed to execute prepared statement\n");
107 /* statements_handle_select = GNUNET_MYSQL_statement_prepare (context,
119 " ORDER BY abs_time DESC "
122 if (NULL == statements_handle_select)
124 fprintf(stderr, "Failed to prepared statement SELECT\n");
128 struct GNUNET_MY_QueryParam params_select[] = {
129 GNUNET_MY_query_param_end
132 if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
133 statements_handle_select,
136 fprintf (stderr, "Failed to execute prepared statement\n");
145 main (int argc, const char * const argv[])
147 struct GNUNET_CONFIGURATION_Handle *config;
148 struct GNUNET_MYSQL_Context *context;
152 GNUNET_log_setup ( "test-my",
156 config = GNUNET_CONFIGURATION_create ();
159 fprintf (stderr, "Failed to create a configuration\n");
163 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf"))
165 fprintf (stderr, "Failed to parse configuaration\n");
169 context = GNUNET_MYSQL_context_create (config,
173 fprintf(stderr, "Failed to connect to database\n");
177 if (GNUNET_OK != GNUNET_MYSQL_statement_run (context,
178 "CREATE TABLE test_my("
181 ", abs_time BIGINT NOT NULL"
182 ", forever BIGINT NOT NULL"
183 ", hash INT NOT NULL CHECK(LENGTH(hash)=64)"
184 ", vsize VARCHAR(32) NOT NULL"
185 ", u16 SMALLINT NOT NULL"
187 ", u64 BIGINT NOT NULL"
191 "Failed to create table \n");
192 GNUNET_MYSQL_statements_invalidate (context);
193 GNUNET_MYSQL_context_destroy (context);
198 ret = run_queries (context);
200 if(GNUNET_OK != GNUNET_MYSQL_statement_run (context,
201 "DROP TABLE test_my"))
203 fprintf (stderr, "Failed to drop table test_my\n");
204 GNUNET_MYSQL_statements_invalidate (context);
207 GNUNET_MYSQL_context_destroy (context);