first batch of license fixes (boring)
[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 it
6      under the terms of the GNU General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file my/test_my.c
17  * @brief Tests for convenience MySQL database
18  * @author Christophe Genevey
19  */
20 #include "platform.h"
21 #include <mysql/mysql.h>
22 #include "gnunet_my_lib.h"
23 #include "gnunet_mysql_lib.h"
24 #include "gnunet_util_lib.h"
25
26
27 /**
28   * Run actual test queries.
29   *
30   * @param contexte the current context of mysql
31   * @return 0 on succes
32   */
33 static int
34 run_queries (struct GNUNET_MYSQL_Context *context)
35 {
36   struct GNUNET_CRYPTO_RsaPublicKey *pub = NULL;
37   struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
38   struct GNUNET_CRYPTO_RsaSignature *sig = NULL;;
39   struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
40   struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
41   struct GNUNET_TIME_Absolute abs_time2;
42   struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
43   struct GNUNET_TIME_Absolute forever2;
44   const struct GNUNET_TIME_AbsoluteNBO abs_time_nbo = GNUNET_TIME_absolute_hton (abs_time);
45   struct GNUNET_HashCode hc;
46   struct GNUNET_HashCode hc2;
47   const char msg[] = "hello";
48   void *msg2 = NULL;
49   size_t msg2_len;
50
51   const char msg3[] = "world";
52   char *msg4 = "";
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 = NULL;
64   struct GNUNET_MYSQL_StatementHandle *statements_handle_select = NULL;
65
66   struct GNUNET_CRYPTO_RsaPrivateKey *priv = NULL;
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 = UINT64_MAX;
77
78   memset (&hc, 0, sizeof(hc));
79   memset (&hc2, 0, sizeof(hc2));
80
81   statements_handle_insert
82     = GNUNET_MYSQL_statement_prepare (context,
83                                       "INSERT INTO test_my2 ("
84                                       " pub"
85                                       ",sig"
86                                       ",abs_time"
87                                       ",forever"
88                                       ",abs_time_nbo"
89                                       ",hash"
90                                       ",vsize"
91                                       ",str"
92                                       ",u16"
93                                       ",u32"
94                                       ",u64"
95                                       ") VALUES "
96                                       "( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
97
98   if (NULL == statements_handle_insert)
99   {
100     fprintf (stderr, "Failed to prepared statement INSERT\n");
101     GNUNET_CRYPTO_rsa_signature_free (sig);
102     GNUNET_CRYPTO_rsa_private_key_free (priv);
103     GNUNET_CRYPTO_rsa_public_key_free (pub);
104     return 1;
105   }
106
107   struct GNUNET_MY_QueryParam params_insert[] = {
108     GNUNET_MY_query_param_rsa_public_key (pub),
109     GNUNET_MY_query_param_rsa_signature (sig),
110     GNUNET_MY_query_param_absolute_time (&abs_time),
111     GNUNET_MY_query_param_absolute_time (&forever),
112     GNUNET_MY_query_param_absolute_time_nbo (&abs_time_nbo),
113     GNUNET_MY_query_param_auto_from_type (&hc),
114     GNUNET_MY_query_param_fixed_size (msg, strlen (msg)),
115     GNUNET_MY_query_param_string (msg3),
116     GNUNET_MY_query_param_uint16 (&u16),
117     GNUNET_MY_query_param_uint32 (&u32),
118     GNUNET_MY_query_param_uint64 (&u64),
119     GNUNET_MY_query_param_end
120   };
121
122   if (GNUNET_OK != GNUNET_MY_exec_prepared(context,
123                                           statements_handle_insert,
124                                           params_insert))
125   {
126       fprintf (stderr, "Failed to execute prepared statement INSERT\n");
127       GNUNET_CRYPTO_rsa_signature_free (sig);
128       GNUNET_CRYPTO_rsa_private_key_free (priv);
129       GNUNET_CRYPTO_rsa_public_key_free (pub);
130       return 1;
131   }
132
133   statements_handle_select
134     = GNUNET_MYSQL_statement_prepare (context,
135                                       "SELECT"
136                                       " pub"
137                                       ",sig"
138                                       ",abs_time"
139                                       ",forever"
140                                       ",hash"
141                                       ",vsize"
142                                       ",str"
143                                       ",u16"
144                                       ",u32"
145                                       ",u64"
146                                       " FROM test_my2");
147
148   if (NULL == statements_handle_select)
149   {
150     fprintf(stderr, "Failed to prepared statement SELECT\n");
151     GNUNET_CRYPTO_rsa_signature_free (sig);
152     GNUNET_CRYPTO_rsa_private_key_free (priv);
153     GNUNET_CRYPTO_rsa_public_key_free (pub);
154     return 1;
155   }
156
157   struct GNUNET_MY_QueryParam params_select[] = {
158     GNUNET_MY_query_param_end
159   };
160
161   if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
162                                             statements_handle_select,
163                                             params_select))
164   {
165     fprintf (stderr, "Failed to execute prepared statement SELECT\n");
166     GNUNET_CRYPTO_rsa_signature_free (sig);
167     GNUNET_CRYPTO_rsa_private_key_free (priv);
168     GNUNET_CRYPTO_rsa_public_key_free (pub);
169     return 1;
170   }
171
172   struct GNUNET_MY_ResultSpec results_select[] = {
173     GNUNET_MY_result_spec_rsa_public_key (&pub2),
174     GNUNET_MY_result_spec_rsa_signature (&sig2),
175     GNUNET_MY_result_spec_absolute_time (&abs_time2),
176     GNUNET_MY_result_spec_absolute_time (&forever2),
177     GNUNET_MY_result_spec_auto_from_type (&hc2),
178     GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len),
179     GNUNET_MY_result_spec_string (&msg4),
180     GNUNET_MY_result_spec_uint16 (&u162),
181     GNUNET_MY_result_spec_uint32 (&u322),
182     GNUNET_MY_result_spec_uint64 (&u642),
183     GNUNET_MY_result_spec_end
184   };
185
186   ret = GNUNET_MY_extract_result (statements_handle_select,
187                                   results_select);
188
189   GNUNET_assert (GNUNET_YES == ret);
190   GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
191   GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
192   GNUNET_break (0 ==
193                   memcmp (&hc,
194                             &hc2,
195                             sizeof (struct GNUNET_HashCode)));
196
197   GNUNET_assert (NULL != sig2);
198   GNUNET_assert (NULL != pub2);
199   GNUNET_break (0 ==
200                 GNUNET_CRYPTO_rsa_signature_cmp (sig,
201                                                  sig2));
202   GNUNET_break (0 ==
203                 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
204                                                   pub2));
205
206   GNUNET_break (strlen (msg) == msg2_len);
207   GNUNET_break (0 ==
208                 strncmp (msg,
209                          msg2,
210                          msg2_len));
211
212   GNUNET_break (strlen (msg3) == strlen(msg4));
213   GNUNET_break (0 ==
214                 strcmp (msg3,
215                         msg4));
216
217   GNUNET_break (16 == u162);
218   GNUNET_break (32 == u322);
219   GNUNET_break (UINT64_MAX == u642);
220
221   GNUNET_MY_cleanup_result (results_select);
222
223   GNUNET_CRYPTO_rsa_signature_free (sig);
224   GNUNET_CRYPTO_rsa_private_key_free (priv);
225   GNUNET_CRYPTO_rsa_public_key_free (pub);
226
227   if (GNUNET_OK != ret)
228     return 1;
229
230   return 0;
231 }
232
233
234 int
235 main (int argc, const char *const argv[])
236 {
237   struct GNUNET_CONFIGURATION_Handle *config;
238   struct GNUNET_MYSQL_Context *context;
239   int ret;
240
241   GNUNET_log_setup ("test-my",
242                     "WARNING",
243                     NULL);
244
245   config = GNUNET_CONFIGURATION_create ();
246   if (GNUNET_OK !=
247       GNUNET_CONFIGURATION_parse (config, "test_my.conf"))
248   {
249     fprintf (stderr, "Failed to parse configuaration\n");
250     return 1;
251   }
252
253   context = GNUNET_MYSQL_context_create (config,
254                                          "datastore-mysql");
255   if (NULL == context)
256   {
257     fprintf(stderr, "Failed to connect to database\n");
258     return 77;
259   }
260
261   (void) GNUNET_MYSQL_statement_run (context,
262                                      "DROP TABLE test_my2;");
263
264   if (GNUNET_OK !=
265       GNUNET_MYSQL_statement_run (context,
266                                   "CREATE TABLE IF NOT EXISTS test_my2("
267                                   " pub BLOB NOT NULL"
268                                   ",sig BLOB NOT NULL"
269                                   ",abs_time BIGINT NOT NULL"
270                                   ",forever BIGINT NOT NULL"
271                                   ",abs_time_nbo BIGINT NOT NULL"
272                                   ",hash BLOB NOT NULL CHECK(LENGTH(hash)=64)"
273                                   ",vsize BLOB NOT NULL"
274                                   ",str BLOB NOT NULL"
275                                   ",u16 SMALLINT NOT NULL"
276                                   ",u32 INT NOT NULL"
277                                   ",u64 BIGINT NOT NULL"
278                                   ")"))
279   {
280     fprintf (stderr,
281             "Failed to create table. Database likely not setup correctly.\n");
282     GNUNET_MYSQL_statements_invalidate (context);
283     GNUNET_MYSQL_context_destroy (context);
284
285     return 77;
286   }
287
288   ret = run_queries (context);
289
290   GNUNET_MYSQL_context_destroy (context);
291   GNUNET_free (config);
292
293   return ret;
294 }