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