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