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