written function cleanup
[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   if (GNUNET_OK != GNUNET_MY_exec_prepared(context,
112                                           statements_handle_insert,
113                                           params_insert))
114   {
115       fprintf (stderr, "Failed to execute prepared statement INSERT\n");
116       return 1;
117   }
118
119   statements_handle_select = GNUNET_MYSQL_statement_prepare (context,
120                                                               "SELECT"
121                                                               " pub"
122                                                               ",sig"
123                                                               ",abs_time"
124                                                               ",forever"
125                                                               ",hash"
126                                                               ",vsize"
127                                                               ",u16"
128                                                               ",u32"
129                                                               ",u64"
130                                                               " FROM test_my2");
131
132   if (NULL == statements_handle_select)
133   {
134     fprintf(stderr, "Failed to prepared statement SELECT\n");
135     return 1;
136   }
137
138   struct GNUNET_MY_QueryParam params_select[] = {
139         GNUNET_MY_query_param_end
140   };
141
142   if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
143                                             statements_handle_select,
144                                             params_select))
145   {
146           fprintf (stderr, "Failed to execute prepared statement SELECT\n");
147           return 1;
148   }
149
150   struct GNUNET_MY_ResultSpec results_select[] = {
151         GNUNET_MY_result_spec_rsa_public_key (&pub2),
152         GNUNET_MY_result_spec_rsa_signature (&sig2),
153         GNUNET_MY_result_spec_absolute_time (&abs_time2),
154         GNUNET_MY_result_spec_absolute_time (&forever2),
155         GNUNET_MY_result_spec_auto_from_type (&hc2),
156         GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len),
157         GNUNET_MY_result_spec_uint16 (&u162),
158         GNUNET_MY_result_spec_uint32 (&u322),
159         GNUNET_MY_result_spec_uint64 (&u642),
160         GNUNET_MY_result_spec_end
161   };
162
163   ret = GNUNET_MY_extract_result (statements_handle_select,
164                                   results_select);
165
166   GNUNET_break (GNUNET_YES == ret);
167   GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
168   GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
169   GNUNET_break (0 ==
170                   memcmp (&hc,
171                             &hc2,
172                             sizeof (struct GNUNET_HashCode)));
173
174   GNUNET_break (0 ==
175             GNUNET_CRYPTO_rsa_signature_cmp (sig,
176                                  sig2));
177   GNUNET_break (0 ==
178             GNUNET_CRYPTO_rsa_public_key_cmp (pub,
179                                   pub2));
180
181   GNUNET_break (strlen (msg) == msg2_len);
182   GNUNET_break (0 ==
183             strncmp (msg,
184                   msg2,
185                   msg2_len));
186   GNUNET_break (16 == u162);
187   GNUNET_break (32 == u322);
188   GNUNET_break (64 == u642);
189
190   GNUNET_MY_cleanup_result (results_select);
191
192   GNUNET_CRYPTO_rsa_signature_free (sig);
193   GNUNET_CRYPTO_rsa_private_key_free (priv);
194   GNUNET_CRYPTO_rsa_public_key_free (pub);     
195      
196   if (GNUNET_OK != ret)
197       return 1;
198
199   return 0;
200 }
201
202
203 int
204 main (int argc, const char * const argv[])
205 {
206   struct GNUNET_CONFIGURATION_Handle *config;
207   struct GNUNET_MYSQL_Context *context;
208   int ret;
209
210   GNUNET_log_setup (  "test-my",
211                          "WARNING",
212                          NULL);
213
214   config = GNUNET_CONFIGURATION_create ();
215   if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf"))
216   {
217       fprintf (stderr, "Failed to parse configuaration\n");
218       return 1;
219   }
220
221   context = GNUNET_MYSQL_context_create (config,
222                                              "datastore-mysql");
223   if (NULL == context)
224   {
225       fprintf(stderr, "Failed to connect to database\n");
226       return 77;
227   }
228
229   (void) GNUNET_MYSQL_statement_run (context,
230                                         "DROP TABLE test_my2;");
231   if (GNUNET_OK != GNUNET_MYSQL_statement_run (context,
232                                                   "CREATE TABLE IF NOT EXISTS test_my2("
233                                                   " pub BLOB NOT NULL"
234                                                   ",sig BLOB NOT NULL"
235                                                   ",abs_time BIGINT NOT NULL"
236                                                   ",forever BIGINT NOT NULL"
237                                                   ",hash BLOB NOT NULL CHECK(LENGTH(hash)=64)"
238                                                   ",vsize BLOB NOT NULL"
239                                                   ",u16 SMALLINT NOT NULL"
240                                                   ",u32 INT NOT NULL"
241                                                   ",u64 BIGINT NOT NULL"
242                                                   ")"))
243   {
244       fprintf (stderr,
245                 "Failed to create table \n");
246       GNUNET_MYSQL_statements_invalidate (context);
247       GNUNET_MYSQL_context_destroy (context);
248
249       return 1;
250   }
251
252   ret = run_queries (context);
253
254   return ret;
255 }