c9529cbaf623291cfd3afb134ba953c08a22e64f
[oweals/gnunet.git] / src / my / test_my.c
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 #include "gnunet_mysql_lib.h"
29 #include "gnunet_util_lib.h"
30
31 /**
32   * Run actual test queries.
33   *
34   * @param contexte the current context of mysql
35   * @return 0 on succes
36   */
37 static int
38 run_queries (struct GNUNET_MYSQL_Context *context)
39 {
40      struct GNUNET_CRYPTO_RsaPublicKey *pub;
41      struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
42      struct GNUNET_CRYPTO_RsaSignature *sig;
43      struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
44      struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
45      struct GNUNET_TIME_Absolute abs_time2;
46      struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
47      struct GNUNET_TIME_Absolute forever2;
48      struct GNUNET_HashCode hc;
49      struct GNUNET_HashCode hc2;
50      const char msg[] = "hello";
51      void *msg2;
52      size_t msg2_len;
53
54      uint16_t u16;
55      uint16_t u162;
56      uint32_t u32;
57      uint32_t u322;
58      uint64_t u64;
59      uint64_t u642;
60
61      int ret;
62
63      struct GNUNET_MYSQL_StatementHandle *statements_handle_insert;
64      struct GNUNET_MYSQL_StatementHandle *statements_handle_select;
65
66      struct GNUNET_CRYPTO_RsaPrivateKey *priv;
67      struct GNUNET_HashCode hmsg;
68
69      priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
70      pub =  GNUNET_CRYPTO_rsa_private_key_get_public (priv);
71      memset (&hmsg, 42, sizeof(hmsg));
72      sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
73                                         &hmsg);
74      u16 = 16;
75      u32 = 32;
76      u64 = 64;
77
78 /*     statements_handle_insert = GNUNET_MYSQL_statement_prepare (context,
79                                         "INSERT INTO test_my ("
80                                         " pub"
81                                         ",sig"
82                                         ",abs_time"
83                                         ",forever"
84                                         ",hash"
85                                         ",vsize"
86                                         ",u16"
87                                         ",u32"
88                                         ",u64"
89                                         ") VALUES "
90                                         "( ?, ?, ?, ?, ?, ?, ?, ?, ?)");
91
92      if (NULL == statements_handle_insert)
93      {
94           fprintf (stderr, "Failed to prepared statement INSERT\n");
95           return 1;
96      }
97
98      struct GNUNET_MY_QueryParam params_insert[] = {
99           GNUNET_MY_query_param_rsa_public_key (pub),
100           GNUNET_MY_query_param_rsa_signature (sig),
101           GNUNET_MY_query_param_absolute_time (&abs_time),
102           GNUNET_MY_query_param_absolute_time (&forever),
103           GNUNET_MY_query_param_auto_from_type (&hc),
104           GNUNET_MY_query_param_fixed_size (msg, strlen (msg)),
105           GNUNET_MY_query_param_uint16 (&u16),
106           GNUNET_MY_query_param_uint32 (&u32),
107           GNUNET_MY_query_param_uint64 (&u64),
108           GNUNET_MY_query_param_end
109      };
110 */
111      statements_handle_insert = GNUNET_MYSQL_statement_prepare (context,
112                                         "INSERT INTO test_my2 ("
113                                         " abs_time"
114                                         ",forever"
115                                         ",u16"
116                                         ",u32"
117                                         ",u64"
118                                         ") VALUES "
119                                         "( ?, ?, ?, ?, ?)");
120
121       struct GNUNET_MY_QueryParam params_insert[] = {
122           GNUNET_MY_query_param_absolute_time (&abs_time),
123           GNUNET_MY_query_param_absolute_time (&forever),
124           GNUNET_MY_query_param_uint16 (&u16),
125           GNUNET_MY_query_param_uint32 (&u32),
126           GNUNET_MY_query_param_uint64 (&u64),
127           GNUNET_MY_query_param_end
128      };
129
130      if (GNUNET_OK != GNUNET_MY_exec_prepared(context,
131                                              statements_handle_insert,
132                                              params_insert))
133      {
134           fprintf (stderr, "Failed to execute prepared statement INSERT\n");
135           return 1;
136      }
137
138
139
140 /*   statements_handle_select = GNUNET_MYSQL_statement_prepare (context,
141                                                                  "SELECT"
142                                                                  " pub"
143                                                                  ",sig"
144                                                                  ",abs_time"
145                                                                  ",forever"
146                                                                  ",hash"
147                                                                  ",vsize"
148                                                                  ",u16"
149                                                                  ",u32"
150                                                                  ",u64"
151                                                                  " FROM test_my"
152                                                                  " ORDER BY abs_time DESC "
153                                                                  " LIMIT 1;");
154
155 */ 
156      statements_handle_select = GNUNET_MYSQL_statement_prepare (context,
157                                                                  "SELECT"
158                                                                  " abs_time"
159                                                                  ",forever"
160                                                                  ",u16"
161                                                                  ",u32"
162                                                                  ",u64"
163                                                                  " FROM test_my2");
164
165      if (NULL == statements_handle_select)
166      {
167           fprintf(stderr, "Failed to prepared statement SELECT\n");
168           return 1;
169      }
170
171      struct GNUNET_MY_QueryParam params_select[] = {
172           GNUNET_MY_query_param_end
173      };
174
175      if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
176                                              statements_handle_select,
177                                              params_select))
178      {
179           fprintf (stderr, "Failed to execute prepared statement SELECT\n");
180           return 1;
181      }
182
183 /*
184      struct GNUNET_MY_ResultSpec results_select[] = {
185           GNUNET_MY_result_spec_rsa_public_key (&pub2),
186           GNUNET_MY_result_spec_rsa_signature (&sig2),
187           GNUNET_MY_result_spec_absolute_time (&abs_time2),
188           GNUNET_MY_result_spec_absolute_time (&forever2),
189           GNUNET_MY_result_spec_auto_from_type (&hc2),
190           GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len),
191           GNUNET_MY_result_spec_uint16 (&u162),
192           GNUNET_MY_result_spec_uint32 (&u322),
193           GNUNET_MY_result_spec_uint64 (&u642),
194           GNUNET_MY_result_spec_end
195      };
196 */
197      struct GNUNET_MY_ResultSpec results_select[] = {
198           GNUNET_MY_result_spec_absolute_time (&abs_time2),
199           GNUNET_MY_result_spec_absolute_time (&forever2),
200           GNUNET_MY_result_spec_uint16 (&u162),
201           GNUNET_MY_result_spec_uint32 (&u322),
202           GNUNET_MY_result_spec_uint64 (&u642),
203           GNUNET_MY_result_spec_end
204      };
205
206      ret = GNUNET_MY_extract_result (statements_handle_select,
207                                      results_select);
208
209      GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
210      GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
211
212      GNUNET_break (16 == u162);
213      GNUNET_break (32 == u322);
214      GNUNET_break (64 == u642);
215
216      if (GNUNET_OK != ret)
217      {
218           fprintf(stderr, "Failed to extract result\n");
219           return 1;
220      }
221
222      return 0;
223 }
224
225
226 int 
227 main (int argc, const char * const argv[])
228 {
229      struct GNUNET_CONFIGURATION_Handle *config;
230      struct GNUNET_MYSQL_Context *context;
231
232      int ret;
233
234      GNUNET_log_setup (  "test-my",
235                          "WARNING",
236                          NULL);
237
238      config = GNUNET_CONFIGURATION_create ();
239      if (NULL == config)
240      {
241           fprintf (stderr, "Failed to create a configuration\n");
242           return 1;
243      }
244
245      if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf"))
246      {
247           fprintf (stderr, "Failed to parse configuaration\n");
248           return 1;
249      }
250
251      context = GNUNET_MYSQL_context_create (config, 
252                                              "datastore-mysql");
253      if (NULL == context)
254      {
255           fprintf(stderr, "Failed to connect to database\n");
256           return 77;
257      }
258
259 /*     if (GNUNET_OK != GNUNET_MYSQL_statement_run (context,
260                                                   "CREATE TABLE test_my("
261                                                   "pub INT NOT NULL"
262                                                   ", sig INT NOT NULL"
263                                                   ", abs_time BIGINT NOT NULL"
264                                                   ", forever BIGINT NOT NULL"
265                                                   ", hash VARCHAR(32) NOT NULL CHECK(LENGTH(hash)=64)"
266                                                   ", vsize VARCHAR(32) NOT NULL"
267                                                   ", u16 SMALLINT NOT NULL"
268                                                   ", u32 INT NOT NULL"
269                                                   ", u64 BIGINT NOT NULL"
270                                                   ")"))
271      {
272           fprintf (stderr, 
273                     "Failed to create table \n"); 
274           GNUNET_MYSQL_statements_invalidate (context);    
275           GNUNET_MYSQL_context_destroy (context);
276           
277           return 1;
278      }
279 */
280      if (GNUNET_OK != GNUNET_MYSQL_statement_run (context,
281                                                   "CREATE TABLE test_my2("
282                                                   " abs_time BIGINT NOT NULL"
283                                                   ", forever BIGINT NOT NULL"
284                                                   ", u16 SMALLINT NOT NULL"
285                                                   ", u32 INT NOT NULL"
286                                                   ", u64 BIGINT NOT NULL"
287                                                   ")"))
288      {
289           fprintf (stderr, 
290                     "Failed to create table \n"); 
291           GNUNET_MYSQL_statements_invalidate (context);    
292           GNUNET_MYSQL_context_destroy (context);
293           
294           return 1;
295      }
296
297      ret = run_queries (context);
298 /*
299      if(GNUNET_OK != GNUNET_MYSQL_statement_run (context,
300                                                   "DROP TABLE test_my2"))
301      {
302           fprintf (stderr, "Failed to drop table test_my\n");
303           GNUNET_MYSQL_statements_invalidate (context);
304      }
305
306      GNUNET_MYSQL_context_destroy (context);
307 */
308      return ret;
309 }