aboutsummaryrefslogtreecommitdiff
path: root/src/pq/test_pq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/test_pq.c')
-rw-r--r--src/pq/test_pq.c186
1 files changed, 90 insertions, 96 deletions
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c
index 697d8e580..a103aca5d 100644
--- a/src/pq/test_pq.c
+++ b/src/pq/test_pq.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 (C) 2015, 2016 GNUnet e.V. 3 (C) 2015, 2016, 2019 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -23,75 +23,65 @@
23 * @author Christian Grothoff <christian@grothoff.org> 23 * @author Christian Grothoff <christian@grothoff.org>
24 */ 24 */
25#include "platform.h" 25#include "platform.h"
26#include "gnunet_util_lib.h" 26#include "pq.h"
27#include "gnunet_pq_lib.h"
28 27
29 28
30/** 29/**
31 * Setup prepared statements. 30 * Setup prepared statements.
32 * 31 *
33 * @param db_conn connection handle to initialize 32 * @param db database handle to initialize
34 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 33 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
35 */ 34 */
36static int 35static int
37postgres_prepare (PGconn *db_conn) 36postgres_prepare (struct GNUNET_PQ_Context *db)
38{ 37{
39 PGresult *result; 38 struct GNUNET_PQ_PreparedStatement ps[] = {
40 39 GNUNET_PQ_make_prepare ("test_insert",
41#define PREPARE(name, sql, ...) \ 40 "INSERT INTO test_pq ("
42 do { \ 41 " pub"
43 result = PQprepare (db_conn, name, sql, __VA_ARGS__); \ 42 ",sig"
44 if (PGRES_COMMAND_OK != PQresultStatus (result)) \ 43 ",abs_time"
45 { \ 44 ",forever"
46 GNUNET_break (0); \ 45 ",hash"
47 PQclear (result); result = NULL; \ 46 ",vsize"
48 return GNUNET_SYSERR; \ 47 ",u16"
49 } \ 48 ",u32"
50 PQclear (result); result = NULL; \ 49 ",u64"
51 } while (0); 50 ") VALUES "
51 "($1, $2, $3, $4, $5, $6,"
52 "$7, $8, $9);",
53 9),
54 GNUNET_PQ_make_prepare ("test_select",
55 "SELECT"
56 " pub"
57 ",sig"
58 ",abs_time"
59 ",forever"
60 ",hash"
61 ",vsize"
62 ",u16"
63 ",u32"
64 ",u64"
65 " FROM test_pq"
66 " ORDER BY abs_time DESC "
67 " LIMIT 1;",
68 0),
69 GNUNET_PQ_PREPARED_STATEMENT_END
70 };
52 71
53 PREPARE ("test_insert", 72 return GNUNET_PQ_prepare_statements (db,
54 "INSERT INTO test_pq (" 73 ps);
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} 74}
86 75
87 76
88/** 77/**
89 * Run actual test queries. 78 * Run actual test queries.
90 * 79 *
80 * @param db database handle
91 * @return 0 on success 81 * @return 0 on success
92 */ 82 */
93static int 83static int
94run_queries (PGconn *conn) 84run_queries (struct GNUNET_PQ_Context *db)
95{ 85{
96 struct GNUNET_CRYPTO_RsaPublicKey *pub; 86 struct GNUNET_CRYPTO_RsaPublicKey *pub;
97 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL; 87 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
@@ -155,7 +145,7 @@ run_queries (PGconn *conn)
155 GNUNET_PQ_result_spec_end 145 GNUNET_PQ_result_spec_end
156 }; 146 };
157 147
158 result = GNUNET_PQ_exec_prepared (conn, 148 result = GNUNET_PQ_exec_prepared (db,
159 "test_insert", 149 "test_insert",
160 params_insert); 150 params_insert);
161 if (PGRES_COMMAND_OK != PQresultStatus (result)) 151 if (PGRES_COMMAND_OK != PQresultStatus (result))
@@ -171,7 +161,7 @@ run_queries (PGconn *conn)
171 } 161 }
172 162
173 PQclear (result); 163 PQclear (result);
174 result = GNUNET_PQ_exec_prepared (conn, 164 result = GNUNET_PQ_exec_prepared (db,
175 "test_select", 165 "test_select",
176 params_select); 166 params_select);
177 if (1 != 167 if (1 !=
@@ -224,67 +214,71 @@ int
224main (int argc, 214main (int argc,
225 const char *const argv[]) 215 const char *const argv[])
226{ 216{
227 PGconn *conn; 217 struct GNUNET_PQ_ExecuteStatement es[] = {
228 PGresult *result; 218 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
219 " pub BYTEA NOT NULL"
220 ",sig BYTEA NOT NULL"
221 ",abs_time INT8 NOT NULL"
222 ",forever INT8 NOT NULL"
223 ",hash BYTEA NOT NULL CHECK(LENGTH(hash)=64)"
224 ",vsize VARCHAR NOT NULL"
225 ",u16 INT2 NOT NULL"
226 ",u32 INT4 NOT NULL"
227 ",u64 INT8 NOT NULL"
228 ")"),
229 GNUNET_PQ_EXECUTE_STATEMENT_END
230 };
231 struct GNUNET_PQ_Context *db;
229 int ret; 232 int ret;
230 233
231 GNUNET_log_setup ("test-pq", 234 GNUNET_log_setup ("test-pq",
232 "WARNING", 235 "WARNING",
233 NULL); 236 NULL);
234 conn = PQconnectdb ("postgres:///gnunetcheck"); 237 db = GNUNET_PQ_connect ("postgres:///gnunetcheck",
235 if (CONNECTION_OK != PQstatus (conn)) 238 es,
239 NULL);
240 if (CONNECTION_OK != PQstatus (db->conn))
236 { 241 {
237 fprintf (stderr, 242 fprintf (stderr,
238 "Cannot run test, database connection failed: %s\n", 243 "Cannot run test, database connection failed: %s\n",
239 PQerrorMessage (conn)); 244 PQerrorMessage (db->conn));
240 GNUNET_break (0); 245 GNUNET_break (0);
241 PQfinish (conn); 246 GNUNET_PQ_disconnect (db);
242 return 77; /* signal test was skipped */ 247 return 77; /* signal test was skipped */
243 } 248 }
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 != 249 if (GNUNET_OK !=
268 postgres_prepare (conn)) 250 postgres_prepare (db))
269 { 251 {
270 GNUNET_break (0); 252 GNUNET_break (0);
271 PQfinish (conn); 253 GNUNET_PQ_disconnect (db);
272 return 1; 254 return 1;
273 } 255 }
274 ret = run_queries (conn); 256 ret = run_queries (db);
275 result = PQexec (conn, 257#if TEST_RESTART
276 "DROP TABLE test_pq"); 258 fprintf (stderr, "Please restart Postgres database now!\n");
277 if (PGRES_COMMAND_OK != PQresultStatus (result)) 259 sleep (60);
260 ret = run_queries (db);
261 fprintf (stderr, "Result: %d (expect: 1 -- if you restarted the DB)\n", ret);
262 ret = run_queries (db);
263 fprintf (stderr, "Result: %d (expect: 0)\n", ret);
264#endif
278 { 265 {
279 fprintf (stderr, 266 struct GNUNET_PQ_ExecuteStatement es[] = {
280 "Failed to create table: %s\n", 267 GNUNET_PQ_make_execute ("DROP TABLE test_pq"),
281 PQerrorMessage (conn)); 268 GNUNET_PQ_EXECUTE_STATEMENT_END
282 PQclear (result); 269 };
283 PQfinish (conn); 270
284 return 1; 271 if (GNUNET_OK !=
272 GNUNET_PQ_exec_statements (db,
273 es))
274 {
275 fprintf (stderr,
276 "Failed to drop table\n");
277 GNUNET_PQ_disconnect (db);
278 return 1;
279 }
285 } 280 }
286 PQclear (result); 281 GNUNET_PQ_disconnect (db);
287 PQfinish (conn);
288 return ret; 282 return ret;
289} 283}
290 284