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