af15b6ab2dcdcb3b7976ea13644482ab01575391
[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_my2 ("
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                                         ",hash"
116                                         ",u16"
117                                         ",u32"
118                                         ",u64"
119                                         ") VALUES "
120                                         "( ?, ?, ?, ?, ?, ?)");
121
122       struct GNUNET_MY_QueryParam params_insert[] = {
123           GNUNET_MY_query_param_absolute_time (&abs_time),
124           GNUNET_MY_query_param_absolute_time (&forever),
125           GNUNET_MY_query_param_auto_from_type (&hc),
126           GNUNET_MY_query_param_uint16 (&u16),
127           GNUNET_MY_query_param_uint32 (&u32),
128           GNUNET_MY_query_param_uint64 (&u64),
129           GNUNET_MY_query_param_end
130      };
131 */
132      if (GNUNET_OK != GNUNET_MY_exec_prepared(context,
133                                              statements_handle_insert,
134                                              params_insert))
135      {
136           fprintf (stderr, "Failed to execute prepared statement INSERT\n");
137           return 1;
138      }
139
140
141
142 /*   statements_handle_select = GNUNET_MYSQL_statement_prepare (context,
143                                                                  "SELECT"
144                                                                  " pub"
145                                                                  ",sig"
146                                                                  ",abs_time"
147                                                                  ",forever"
148                                                                  ",hash"
149                                                                  ",vsize"
150                                                                  ",u16"
151                                                                  ",u32"
152                                                                  ",u64"
153                                                                  " FROM test_my"
154                                                                  " ORDER BY abs_time DESC "
155                                                                  " LIMIT 1;");
156
157 */ 
158      statements_handle_select = GNUNET_MYSQL_statement_prepare (context,
159                                                                  "SELECT"
160                                                                  //" pub"
161                                                                  //" sig"
162                                                                  " abs_time"
163                                                                  ",forever"
164                                                                  ",hash"
165                                                                  //" vsize"
166                                                                  ",u16"
167                                                                  ",u32"
168                                                                  ",u64"
169                                                                  " FROM test_my2");
170
171      if (NULL == statements_handle_select)
172      {
173           fprintf(stderr, "Failed to prepared statement SELECT\n");
174           return 1;
175      }
176
177      struct GNUNET_MY_QueryParam params_select[] = {
178           GNUNET_MY_query_param_end
179      };
180
181      if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
182                                              statements_handle_select,
183                                              params_select))
184      {
185           fprintf (stderr, "Failed to execute prepared statement SELECT\n");
186           return 1;
187      }
188
189 /*
190      struct GNUNET_MY_ResultSpec results_select[] = {
191           GNUNET_MY_result_spec_rsa_public_key (&pub2),
192           GNUNET_MY_result_spec_rsa_signature (&sig2),
193           GNUNET_MY_result_spec_absolute_time (&abs_time2),
194           GNUNET_MY_result_spec_absolute_time (&forever2),
195           GNUNET_MY_result_spec_auto_from_type (&hc2),
196           GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len),
197           GNUNET_MY_result_spec_uint16 (&u162),
198           GNUNET_MY_result_spec_uint32 (&u322),
199           GNUNET_MY_result_spec_uint64 (&u642),
200           GNUNET_MY_result_spec_end
201      };
202 */
203      struct GNUNET_MY_ResultSpec results_select[] = {
204           //GNUNET_MY_result_spec_rsa_public_key (&pub2),
205           //GNUNET_MY_result_spec_rsa_signature (&sig2),
206           GNUNET_MY_result_spec_absolute_time (&abs_time2),
207           GNUNET_MY_result_spec_absolute_time (&forever2),
208           GNUNET_MY_result_spec_auto_from_type (&hc2),
209           //GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len),
210           GNUNET_MY_result_spec_uint16 (&u162),
211           GNUNET_MY_result_spec_uint32 (&u322),
212           GNUNET_MY_result_spec_uint64 (&u642),
213           GNUNET_MY_result_spec_end
214      };
215
216      ret = GNUNET_MY_extract_result (statements_handle_select,
217                                      results_select);
218
219      GNUNET_break (GNUNET_YES == ret);
220      GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
221      GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
222      GNUNET_break (0 ==
223                     memcmp (&hc,
224                             &hc2,
225                             sizeof (struct GNUNET_HashCode)));
226
227      GNUNET_break (16 == u162);
228      GNUNET_break (32 == u322);
229      GNUNET_break (64 == u642);
230
231      if (GNUNET_OK != ret)
232      {
233           fprintf(stderr, "Failed to extract result\n");
234           return 1;
235      }
236
237      return 0;
238 }
239
240
241 int 
242 main (int argc, const char * const argv[])
243 {
244      struct GNUNET_CONFIGURATION_Handle *config;
245      struct GNUNET_MYSQL_Context *context;
246
247      int ret;
248
249      GNUNET_log_setup (  "test-my",
250                          "WARNING",
251                          NULL);
252
253      config = GNUNET_CONFIGURATION_create ();
254      if (NULL == config)
255      {
256           fprintf (stderr, "Failed to create a configuration\n");
257           return 1;
258      }
259
260      if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf"))
261      {
262           fprintf (stderr, "Failed to parse configuaration\n");
263           return 1;
264      }
265
266      context = GNUNET_MYSQL_context_create (config, 
267                                              "datastore-mysql");
268      if (NULL == context)
269      {
270           fprintf(stderr, "Failed to connect to database\n");
271           return 77;
272      }
273
274      if (GNUNET_OK != GNUNET_MYSQL_statement_run (context,
275                                                   "CREATE TABLE test_my2("
276                                                   "pub BLOB NOT NULL"
277                                                   ", sig BLOB NOT NULL"
278                                                   ", abs_time BIGINT NOT NULL"
279                                                   ", forever BIGINT NOT NULL"
280                                                   ", hash BLOB NOT NULL CHECK(LENGTH(hash)=64)"
281                                                   ", vsize BLOB NOT NULL"
282                                                   ", u16 SMALLINT NOT NULL"
283                                                   ", u32 INT NOT NULL"
284                                                   ", u64 BIGINT NOT NULL"
285                                                   ")"))
286      {
287           fprintf (stderr, 
288                     "Failed to create table \n"); 
289           GNUNET_MYSQL_statements_invalidate (context);    
290           GNUNET_MYSQL_context_destroy (context);
291           
292           return 1;
293      }
294
295 /*     if (GNUNET_OK != GNUNET_MYSQL_statement_run (context,
296                                                   "CREATE TABLE test_my2("
297                                                   " abs_time BIGINT NOT NULL"
298                                                   ", forever BIGINT NOT NULL"
299                                                   ", hash VARCHAR(32) NOT NULL CHECK(LENGTH(hash)=64)"
300                                                   ", u16 SMALLINT NOT NULL"
301                                                   ", u32 INT NOT NULL"
302                                                   ", u64 BIGINT NOT NULL"
303                                                   ")"))
304      {
305           fprintf (stderr, 
306                     "Failed to create table \n"); 
307           GNUNET_MYSQL_statements_invalidate (context);    
308           GNUNET_MYSQL_context_destroy (context);
309           
310           return 1;
311      }
312 */
313      ret = run_queries (context);
314
315 /*     if(GNUNET_OK != GNUNET_MYSQL_statement_run (context,
316                                                   "DROP TABLE test_my2"))
317      {
318           fprintf (stderr, "Failed to drop table test_my\n");
319           GNUNET_MYSQL_statements_invalidate (context);
320      }
321
322      GNUNET_MYSQL_context_destroy (context);
323 */
324      return ret;
325 }