glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / pq / test_pq.c
1 /*
2   This file is part of GNUnet
3   (C) 2015, 2016 GNUnet e.V.
4
5   GNUnet is free software: you can redistribute it and/or modify it
6   under the terms of the GNU Affero General Public License as published
7   by the Free Software Foundation, either version 3 of the License,
8   or (at your 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   Affero General Public License for more details.
14 */
15 /**
16  * @file pq/test_pq.c
17  * @brief Tests for Postgres convenience API
18  * @author Christian Grothoff <christian@grothoff.org>
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22 #include "gnunet_pq_lib.h"
23
24
25 /**
26  * Setup prepared statements.
27  *
28  * @param db_conn connection handle to initialize
29  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
30  */
31 static int
32 postgres_prepare (PGconn *db_conn)
33 {
34   PGresult *result;
35
36 #define PREPARE(name, sql, ...)                                 \
37   do {                                                          \
38     result = PQprepare (db_conn, name, sql, __VA_ARGS__);       \
39     if (PGRES_COMMAND_OK != PQresultStatus (result))            \
40     {                                                           \
41       GNUNET_break (0);                                         \
42       PQclear (result); result = NULL;                          \
43       return GNUNET_SYSERR;                                     \
44     }                                                           \
45     PQclear (result); result = NULL;                            \
46   } while (0);
47
48   PREPARE ("test_insert",
49            "INSERT INTO test_pq ("
50            " pub"
51            ",sig"
52            ",abs_time"
53            ",forever"
54            ",hash"
55            ",vsize"
56            ",u16"
57            ",u32"
58            ",u64"
59            ") VALUES "
60            "($1, $2, $3, $4, $5, $6,"
61             "$7, $8, $9);",
62            9, NULL);
63   PREPARE ("test_select",
64            "SELECT"
65            " pub"
66            ",sig"
67            ",abs_time"
68            ",forever"
69            ",hash"
70            ",vsize"
71            ",u16"
72            ",u32"
73            ",u64"
74            " FROM test_pq"
75            " ORDER BY abs_time DESC "
76            " LIMIT 1;",
77            0, NULL);
78   return GNUNET_OK;
79 #undef PREPARE
80 }
81
82
83 /**
84  * Run actual test queries.
85  *
86  * @return 0 on success
87  */
88 static int
89 run_queries (PGconn *conn)
90 {
91   struct GNUNET_CRYPTO_RsaPublicKey *pub;
92   struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
93   struct GNUNET_CRYPTO_RsaSignature *sig;
94   struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
95   struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
96   struct GNUNET_TIME_Absolute abs_time2;
97   struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
98   struct GNUNET_TIME_Absolute forever2;
99   struct GNUNET_HashCode hc;
100   struct GNUNET_HashCode hc2;
101   PGresult *result;
102   int ret;
103   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
104   const char msg[] = "hello";
105   void *msg2;
106   struct GNUNET_HashCode hmsg;
107   size_t msg2_len;
108   uint16_t u16;
109   uint16_t u162;
110   uint32_t u32;
111   uint32_t u322;
112   uint64_t u64;
113   uint64_t u642;
114
115   priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
116   pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
117   memset (&hmsg, 42, sizeof (hmsg));
118   sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
119                                     &hmsg);
120   u16 = 16;
121   u32 = 32;
122   u64 = 64;
123   /* FIXME: test GNUNET_PQ_result_spec_variable_size */
124   {
125     struct GNUNET_PQ_QueryParam params_insert[] = {
126       GNUNET_PQ_query_param_rsa_public_key (pub),
127       GNUNET_PQ_query_param_rsa_signature (sig),
128       GNUNET_PQ_query_param_absolute_time (&abs_time),
129       GNUNET_PQ_query_param_absolute_time (&forever),
130       GNUNET_PQ_query_param_auto_from_type (&hc),
131       GNUNET_PQ_query_param_fixed_size (msg, strlen (msg)),
132       GNUNET_PQ_query_param_uint16 (&u16),
133       GNUNET_PQ_query_param_uint32 (&u32),
134       GNUNET_PQ_query_param_uint64 (&u64),
135       GNUNET_PQ_query_param_end
136     };
137     struct GNUNET_PQ_QueryParam params_select[] = {
138       GNUNET_PQ_query_param_end
139     };
140     struct GNUNET_PQ_ResultSpec results_select[] = {
141       GNUNET_PQ_result_spec_rsa_public_key ("pub", &pub2),
142       GNUNET_PQ_result_spec_rsa_signature ("sig", &sig2),
143       GNUNET_PQ_result_spec_absolute_time ("abs_time", &abs_time2),
144       GNUNET_PQ_result_spec_absolute_time ("forever", &forever2),
145       GNUNET_PQ_result_spec_auto_from_type ("hash", &hc2),
146       GNUNET_PQ_result_spec_variable_size ("vsize", &msg2, &msg2_len),
147       GNUNET_PQ_result_spec_uint16 ("u16", &u162),
148       GNUNET_PQ_result_spec_uint32 ("u32", &u322),
149       GNUNET_PQ_result_spec_uint64 ("u64", &u642),
150       GNUNET_PQ_result_spec_end
151     };
152
153     result = GNUNET_PQ_exec_prepared (conn,
154                                      "test_insert",
155                                      params_insert);
156     if (PGRES_COMMAND_OK != PQresultStatus (result))
157     {
158       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159                   "Database failure: %s\n",
160                   PQresultErrorMessage (result));
161       PQclear (result);
162       GNUNET_CRYPTO_rsa_signature_free (sig);
163       GNUNET_CRYPTO_rsa_private_key_free (priv);
164       GNUNET_CRYPTO_rsa_public_key_free (pub);
165       return 1;
166     }
167
168     PQclear (result);
169     result = GNUNET_PQ_exec_prepared (conn,
170                                       "test_select",
171                                       params_select);
172     if (1 !=
173         PQntuples (result))
174     {
175       GNUNET_break (0);
176       PQclear (result);
177       GNUNET_CRYPTO_rsa_signature_free (sig);
178       GNUNET_CRYPTO_rsa_private_key_free (priv);
179       GNUNET_CRYPTO_rsa_public_key_free (pub);
180       return 1;
181     }
182     ret = GNUNET_PQ_extract_result (result,
183                                    results_select,
184                                    0);
185     GNUNET_break (GNUNET_YES == ret);
186     GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
187     GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
188     GNUNET_break (0 ==
189                   memcmp (&hc,
190                           &hc2,
191                           sizeof (struct GNUNET_HashCode)));
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     GNUNET_break (strlen (msg) == msg2_len);
199     GNUNET_break (0 ==
200                   strncmp (msg,
201                            msg2,
202                            msg2_len));
203     GNUNET_break (16 == u162);
204     GNUNET_break (32 == u322);
205     GNUNET_break (64 == u642);
206     GNUNET_PQ_cleanup_result (results_select);
207     PQclear (result);
208   }
209   GNUNET_CRYPTO_rsa_signature_free (sig);
210   GNUNET_CRYPTO_rsa_private_key_free (priv);
211   GNUNET_CRYPTO_rsa_public_key_free (pub);
212   if (GNUNET_OK != ret)
213     return 1;
214
215   return 0;
216 }
217
218
219 int
220 main (int argc,
221       const char *const argv[])
222 {
223   PGconn *conn;
224   PGresult *result;
225   int ret;
226
227   GNUNET_log_setup ("test-pq",
228                     "WARNING",
229                     NULL);
230   conn = PQconnectdb ("postgres:///gnunetcheck");
231   if (CONNECTION_OK != PQstatus (conn))
232   {
233     fprintf (stderr,
234              "Cannot run test, database connection failed: %s\n",
235              PQerrorMessage (conn));
236     GNUNET_break (0);
237     PQfinish (conn);
238     return 77; /* signal test was skipped */
239   }
240
241   result = PQexec (conn,
242                    "CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
243                    " pub BYTEA NOT NULL"
244                    ",sig BYTEA NOT NULL"
245                    ",abs_time INT8 NOT NULL"
246                    ",forever INT8 NOT NULL"
247                    ",hash BYTEA NOT NULL CHECK(LENGTH(hash)=64)"
248                    ",vsize VARCHAR NOT NULL"
249                    ",u16 INT2 NOT NULL"
250                    ",u32 INT4 NOT NULL"
251                    ",u64 INT8 NOT NULL"
252                    ")");
253   if (PGRES_COMMAND_OK != PQresultStatus (result))
254   {
255     fprintf (stderr,
256              "Failed to create table: %s\n",
257              PQerrorMessage (conn));
258     PQclear (result);
259     PQfinish (conn);
260     return 1;
261   }
262   PQclear (result);
263   if (GNUNET_OK !=
264       postgres_prepare (conn))
265   {
266     GNUNET_break (0);
267     PQfinish (conn);
268     return 1;
269   }
270   ret = run_queries (conn);
271   result = PQexec (conn,
272                    "DROP TABLE test_pq");
273   if (PGRES_COMMAND_OK != PQresultStatus (result))
274   {
275     fprintf (stderr,
276              "Failed to create table: %s\n",
277              PQerrorMessage (conn));
278     PQclear (result);
279     PQfinish (conn);
280     return 1;
281   }
282   PQclear (result);
283   PQfinish (conn);
284   return ret;
285 }
286
287
288 /* end of test_pq.c */