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