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