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