067d70a6e7fdf5396518f5053775580d05ac6ebc
[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      const struct GNUNET_CRYPTO_RsaPublicKey *pub;
41      struct GNUNET_CRYPTO_RsaSignature *sig;
42      struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
43      struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
44      struct GNUNET_HashCode hc;
45      const char msg[] = "hello";
46      uint16_t u16;
47      uint32_t u32;
48      uint64_t u64;
49
50      struct GNUNET_MYSQL_StatementHandle *statements_handle_insert;
51 //     struct GNUNET_MYSQL_StatementHandle *statements_handle_select;
52
53      struct GNUNET_CRYPTO_RsaPrivateKey *priv;
54      struct GNUNET_HashCode hmsg;
55
56      priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
57      pub =  GNUNET_CRYPTO_rsa_private_key_get_public (priv);
58      memset (&hmsg, 42, sizeof(hmsg));
59      sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
60                                         &hmsg);
61      u16 = 16;
62      u32 = 32;
63      u64 = 64;
64
65 //   FIXE THE INSERT QUERY  
66      statements_handle_insert = GNUNET_MYSQL_statement_prepare (context,
67                                         "INSERT INTO test_my ("
68                                         " pub"
69                                         ",sig"
70                                         ",abs_time"
71                                         ",forever"
72                                         ",hash"
73                                         ",vsize"
74                                         ",u16"
75                                         ",u32"
76                                         ",u64"
77                                         ") VALUES "
78                                         "(?, ?, ?, ?, ?, ?,"
79                                         "?, ?, ?)");
80
81      if (NULL == statements_handle_insert)
82      {
83           fprintf (stderr, "Failed to prepared statement INSERT\n");
84           return 1;
85      }
86
87      //ERROR WITH MSG
88      struct GNUNET_MY_QueryParam params_insert[] = {
89           GNUNET_MY_query_param_rsa_public_key (pub),
90           GNUNET_MY_query_param_rsa_signature (sig),
91           GNUNET_MY_query_param_absolute_time (&abs_time),
92           GNUNET_MY_query_param_absolute_time (&forever),
93           GNUNET_MY_query_param_auto_from_type (&hc),
94           GNUNET_MY_query_param_fixed_size (msg, strlen (msg)),
95           GNUNET_MY_query_param_uint16 (&u16),
96           GNUNET_MY_query_param_uint32 (&u32),
97           GNUNET_MY_query_param_uint64 (&u64),
98           GNUNET_MY_query_param_end
99      };
100
101      fprintf(stderr, " u16 : %u\n", (unsigned)params_insert[6].data);
102      fprintf(stderr, " &u16 : %u\n", (unsigned)&u16);
103
104       //FAIL HERE
105      if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
106                                              statements_handle_insert,
107                                              params_insert))
108      {
109           fprintf (stderr, 
110                     "Failed to execute prepared statement\n");
111           return 22;
112      }
113
114 /*   NOT THE GOOD FUNCTION -> TO FIXE 
115      statements_handle_select = GNUNET_MYSQL_statement_prepare (context,
116                                                                  "SELECT"
117                                                                  " pub"
118                                                                  ",sig"
119                                                                  ",abs_time"
120                                                                  ",forever"
121                                                                  ",hash"
122                                                                  ",vsize"
123                                                                  ",u16"
124                                                                  ",u32"
125                                                                  ",u64"
126                                                                  " FROM test_my"
127                                                                  " ORDER BY abs_time DESC "
128                                                                  " LIMIT 1;");
129
130      if (NULL == statements_handle_select)
131      {
132           fprintf(stderr, "Failed to prepared statement SELECT\n");
133           return 1;
134      }
135
136      struct GNUNET_MY_QueryParam params_select[] = {
137           GNUNET_MY_query_param_end
138      };
139
140      if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
141                                              statements_handle_select,
142                                              params_select))
143      {
144           fprintf (stderr, "Failed to execute prepared statement\n");
145           return 22;
146      }
147 */
148      return 0;
149 }
150
151
152 int 
153 main (int argc, const char * const argv[])
154 {
155      struct GNUNET_CONFIGURATION_Handle *config;
156      struct GNUNET_MYSQL_Context *context;
157
158      int ret;
159
160      GNUNET_log_setup (  "test-my",
161                          "WARNING",
162                          NULL);
163
164      config = GNUNET_CONFIGURATION_create ();
165      if (NULL == config)
166      {
167           fprintf (stderr, "Failed to create a configuration\n");
168           return 1;
169      }
170
171      if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf"))
172      {
173           fprintf (stderr, "Failed to parse configuaration\n");
174           return 1;
175      }
176
177      context = GNUNET_MYSQL_context_create (config, 
178                                              "datastore-mysql");
179      if (NULL == context)
180      {
181           fprintf(stderr, "Failed to connect to database\n");
182           return 77;
183      }
184
185      if (GNUNET_OK != GNUNET_MYSQL_statement_run (context,
186                                                   "CREATE TABLE test_my("
187                                                   "pub INT NOT NULL"
188                                                   ", sig INT NOT NULL"
189                                                   ", abs_time BIGINT NOT NULL"
190                                                   ", forever BIGINT NOT NULL"
191                                                   ", hash INT NOT NULL CHECK(LENGTH(hash)=64)"
192                                                   ", vsize VARCHAR(32) NOT NULL"
193                                                   ", u16 SMALLINT NOT NULL"
194                                                   ", u32 INT NOT NULL"
195                                                   ", u64 BIGINT NOT NULL"
196                                                   ")"))
197      {
198           fprintf (stderr, 
199                     "Failed to create table \n"); 
200           GNUNET_MYSQL_statements_invalidate (context);    
201           GNUNET_MYSQL_context_destroy (context);
202           
203           return 1;
204      }
205
206      ret = run_queries (context);
207
208      if(GNUNET_OK != GNUNET_MYSQL_statement_run (context,
209                                                   "DROP TABLE test_my"))
210      {
211           fprintf (stderr, "Failed to drop table test_my\n");
212           GNUNET_MYSQL_statements_invalidate (context);
213      }
214
215      GNUNET_MYSQL_context_destroy (context);
216
217      return ret;
218 }