summaryrefslogtreecommitdiff
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.c382
1 files changed, 191 insertions, 191 deletions
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c
index 67cf32733..df54580d2 100644
--- a/src/pq/test_pq.c
+++ b/src/pq/test_pq.c
@@ -1,22 +1,22 @@
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 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
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/test_pq.c 21 * @file pq/test_pq.c
22 * @brief Tests for Postgres convenience API 22 * @brief Tests for Postgres convenience API
@@ -34,52 +34,52 @@
34 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 34 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
35 */ 35 */
36static int 36static int
37postgres_prepare (PGconn *db_conn) 37postgres_prepare(PGconn *db_conn)
38{ 38{
39 PGresult *result; 39 PGresult *result;
40 40
41#define PREPARE(name, sql, ...) \ 41#define PREPARE(name, sql, ...) \
42 do { \ 42 do { \
43 result = PQprepare (db_conn, name, sql, __VA_ARGS__); \ 43 result = PQprepare(db_conn, name, sql, __VA_ARGS__); \
44 if (PGRES_COMMAND_OK != PQresultStatus (result)) \ 44 if (PGRES_COMMAND_OK != PQresultStatus(result)) \
45 { \ 45 { \
46 GNUNET_break (0); \ 46 GNUNET_break(0); \
47 PQclear (result); result = NULL; \ 47 PQclear(result); result = NULL; \
48 return GNUNET_SYSERR; \ 48 return GNUNET_SYSERR; \
49 } \ 49 } \
50 PQclear (result); result = NULL; \ 50 PQclear(result); result = NULL; \
51 } while (0); 51 } while (0);
52 52
53 PREPARE ("test_insert", 53 PREPARE("test_insert",
54 "INSERT INTO test_pq (" 54 "INSERT INTO test_pq ("
55 " pub" 55 " pub"
56 ",sig" 56 ",sig"
57 ",abs_time" 57 ",abs_time"
58 ",forever" 58 ",forever"
59 ",hash" 59 ",hash"
60 ",vsize" 60 ",vsize"
61 ",u16" 61 ",u16"
62 ",u32" 62 ",u32"
63 ",u64" 63 ",u64"
64 ") VALUES " 64 ") VALUES "
65 "($1, $2, $3, $4, $5, $6," 65 "($1, $2, $3, $4, $5, $6,"
66 "$7, $8, $9);", 66 "$7, $8, $9);",
67 9, NULL); 67 9, NULL);
68 PREPARE ("test_select", 68 PREPARE("test_select",
69 "SELECT" 69 "SELECT"
70 " pub" 70 " pub"
71 ",sig" 71 ",sig"
72 ",abs_time" 72 ",abs_time"
73 ",forever" 73 ",forever"
74 ",hash" 74 ",hash"
75 ",vsize" 75 ",vsize"
76 ",u16" 76 ",u16"
77 ",u32" 77 ",u32"
78 ",u64" 78 ",u64"
79 " FROM test_pq" 79 " FROM test_pq"
80 " ORDER BY abs_time DESC " 80 " ORDER BY abs_time DESC "
81 " LIMIT 1;", 81 " LIMIT 1;",
82 0, NULL); 82 0, NULL);
83 return GNUNET_OK; 83 return GNUNET_OK;
84#undef PREPARE 84#undef PREPARE
85} 85}
@@ -91,13 +91,13 @@ postgres_prepare (PGconn *db_conn)
91 * @return 0 on success 91 * @return 0 on success
92 */ 92 */
93static int 93static int
94run_queries (PGconn *conn) 94run_queries(PGconn *conn)
95{ 95{
96 struct GNUNET_CRYPTO_RsaPublicKey *pub; 96 struct GNUNET_CRYPTO_RsaPublicKey *pub;
97 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL; 97 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
98 struct GNUNET_CRYPTO_RsaSignature *sig; 98 struct GNUNET_CRYPTO_RsaSignature *sig;
99 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL; 99 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
100 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get (); 100 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get();
101 struct GNUNET_TIME_Absolute abs_time2; 101 struct GNUNET_TIME_Absolute abs_time2;
102 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS; 102 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
103 struct GNUNET_TIME_Absolute forever2; 103 struct GNUNET_TIME_Absolute forever2;
@@ -117,102 +117,102 @@ run_queries (PGconn *conn)
117 uint64_t u64; 117 uint64_t u64;
118 uint64_t u642; 118 uint64_t u642;
119 119
120 priv = GNUNET_CRYPTO_rsa_private_key_create (1024); 120 priv = GNUNET_CRYPTO_rsa_private_key_create(1024);
121 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); 121 pub = GNUNET_CRYPTO_rsa_private_key_get_public(priv);
122 memset (&hmsg, 42, sizeof (hmsg)); 122 memset(&hmsg, 42, sizeof(hmsg));
123 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, 123 sig = GNUNET_CRYPTO_rsa_sign_fdh(priv,
124 &hmsg); 124 &hmsg);
125 u16 = 16; 125 u16 = 16;
126 u32 = 32; 126 u32 = 32;
127 u64 = 64; 127 u64 = 64;
128 /* FIXME: test GNUNET_PQ_result_spec_variable_size */ 128 /* FIXME: test GNUNET_PQ_result_spec_variable_size */
129 { 129 {
130 struct GNUNET_PQ_QueryParam params_insert[] = { 130 struct GNUNET_PQ_QueryParam params_insert[] = {
131 GNUNET_PQ_query_param_rsa_public_key (pub), 131 GNUNET_PQ_query_param_rsa_public_key(pub),
132 GNUNET_PQ_query_param_rsa_signature (sig), 132 GNUNET_PQ_query_param_rsa_signature(sig),
133 GNUNET_PQ_query_param_absolute_time (&abs_time), 133 GNUNET_PQ_query_param_absolute_time(&abs_time),
134 GNUNET_PQ_query_param_absolute_time (&forever), 134 GNUNET_PQ_query_param_absolute_time(&forever),
135 GNUNET_PQ_query_param_auto_from_type (&hc), 135 GNUNET_PQ_query_param_auto_from_type(&hc),
136 GNUNET_PQ_query_param_fixed_size (msg, strlen (msg)), 136 GNUNET_PQ_query_param_fixed_size(msg, strlen(msg)),
137 GNUNET_PQ_query_param_uint16 (&u16), 137 GNUNET_PQ_query_param_uint16(&u16),
138 GNUNET_PQ_query_param_uint32 (&u32), 138 GNUNET_PQ_query_param_uint32(&u32),
139 GNUNET_PQ_query_param_uint64 (&u64), 139 GNUNET_PQ_query_param_uint64(&u64),
140 GNUNET_PQ_query_param_end 140 GNUNET_PQ_query_param_end
141 }; 141 };
142 struct GNUNET_PQ_QueryParam params_select[] = { 142 struct GNUNET_PQ_QueryParam params_select[] = {
143 GNUNET_PQ_query_param_end 143 GNUNET_PQ_query_param_end
144 }; 144 };
145 struct GNUNET_PQ_ResultSpec results_select[] = { 145 struct GNUNET_PQ_ResultSpec results_select[] = {
146 GNUNET_PQ_result_spec_rsa_public_key ("pub", &pub2), 146 GNUNET_PQ_result_spec_rsa_public_key("pub", &pub2),
147 GNUNET_PQ_result_spec_rsa_signature ("sig", &sig2), 147 GNUNET_PQ_result_spec_rsa_signature("sig", &sig2),
148 GNUNET_PQ_result_spec_absolute_time ("abs_time", &abs_time2), 148 GNUNET_PQ_result_spec_absolute_time("abs_time", &abs_time2),
149 GNUNET_PQ_result_spec_absolute_time ("forever", &forever2), 149 GNUNET_PQ_result_spec_absolute_time("forever", &forever2),
150 GNUNET_PQ_result_spec_auto_from_type ("hash", &hc2), 150 GNUNET_PQ_result_spec_auto_from_type("hash", &hc2),
151 GNUNET_PQ_result_spec_variable_size ("vsize", &msg2, &msg2_len), 151 GNUNET_PQ_result_spec_variable_size("vsize", &msg2, &msg2_len),
152 GNUNET_PQ_result_spec_uint16 ("u16", &u162), 152 GNUNET_PQ_result_spec_uint16("u16", &u162),
153 GNUNET_PQ_result_spec_uint32 ("u32", &u322), 153 GNUNET_PQ_result_spec_uint32("u32", &u322),
154 GNUNET_PQ_result_spec_uint64 ("u64", &u642), 154 GNUNET_PQ_result_spec_uint64("u64", &u642),
155 GNUNET_PQ_result_spec_end 155 GNUNET_PQ_result_spec_end
156 }; 156 };
157 157
158 result = GNUNET_PQ_exec_prepared (conn, 158 result = GNUNET_PQ_exec_prepared(conn,
159 "test_insert", 159 "test_insert",
160 params_insert); 160 params_insert);
161 if (PGRES_COMMAND_OK != PQresultStatus (result)) 161 if (PGRES_COMMAND_OK != PQresultStatus(result))
162 { 162 {
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 163 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
164 "Database failure: %s\n", 164 "Database failure: %s\n",
165 PQresultErrorMessage (result)); 165 PQresultErrorMessage(result));
166 PQclear (result); 166 PQclear(result);
167 GNUNET_CRYPTO_rsa_signature_free (sig); 167 GNUNET_CRYPTO_rsa_signature_free(sig);
168 GNUNET_CRYPTO_rsa_private_key_free (priv); 168 GNUNET_CRYPTO_rsa_private_key_free(priv);
169 GNUNET_CRYPTO_rsa_public_key_free (pub); 169 GNUNET_CRYPTO_rsa_public_key_free(pub);
170 return 1; 170 return 1;
171 } 171 }
172 172
173 PQclear (result); 173 PQclear(result);
174 result = GNUNET_PQ_exec_prepared (conn, 174 result = GNUNET_PQ_exec_prepared(conn,
175 "test_select", 175 "test_select",
176 params_select); 176 params_select);
177 if (1 != 177 if (1 !=
178 PQntuples (result)) 178 PQntuples(result))
179 { 179 {
180 GNUNET_break (0); 180 GNUNET_break(0);
181 PQclear (result); 181 PQclear(result);
182 GNUNET_CRYPTO_rsa_signature_free (sig); 182 GNUNET_CRYPTO_rsa_signature_free(sig);
183 GNUNET_CRYPTO_rsa_private_key_free (priv); 183 GNUNET_CRYPTO_rsa_private_key_free(priv);
184 GNUNET_CRYPTO_rsa_public_key_free (pub); 184 GNUNET_CRYPTO_rsa_public_key_free(pub);
185 return 1; 185 return 1;
186 } 186 }
187 ret = GNUNET_PQ_extract_result (result, 187 ret = GNUNET_PQ_extract_result(result,
188 results_select, 188 results_select,
189 0); 189 0);
190 GNUNET_break (GNUNET_YES == ret); 190 GNUNET_break(GNUNET_YES == ret);
191 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us); 191 GNUNET_break(abs_time.abs_value_us == abs_time2.abs_value_us);
192 GNUNET_break (forever.abs_value_us == forever2.abs_value_us); 192 GNUNET_break(forever.abs_value_us == forever2.abs_value_us);
193 GNUNET_break (0 == 193 GNUNET_break(0 ==
194 GNUNET_memcmp (&hc, 194 GNUNET_memcmp(&hc,
195 &hc2)); 195 &hc2));
196 GNUNET_break (0 == 196 GNUNET_break(0 ==
197 GNUNET_CRYPTO_rsa_signature_cmp (sig, 197 GNUNET_CRYPTO_rsa_signature_cmp(sig,
198 sig2)); 198 sig2));
199 GNUNET_break (0 == 199 GNUNET_break(0 ==
200 GNUNET_CRYPTO_rsa_public_key_cmp (pub, 200 GNUNET_CRYPTO_rsa_public_key_cmp(pub,
201 pub2)); 201 pub2));
202 GNUNET_break (strlen (msg) == msg2_len); 202 GNUNET_break(strlen(msg) == msg2_len);
203 GNUNET_break (0 == 203 GNUNET_break(0 ==
204 strncmp (msg, 204 strncmp(msg,
205 msg2, 205 msg2,
206 msg2_len)); 206 msg2_len));
207 GNUNET_break (16 == u162); 207 GNUNET_break(16 == u162);
208 GNUNET_break (32 == u322); 208 GNUNET_break(32 == u322);
209 GNUNET_break (64 == u642); 209 GNUNET_break(64 == u642);
210 GNUNET_PQ_cleanup_result (results_select); 210 GNUNET_PQ_cleanup_result(results_select);
211 PQclear (result); 211 PQclear(result);
212 } 212 }
213 GNUNET_CRYPTO_rsa_signature_free (sig); 213 GNUNET_CRYPTO_rsa_signature_free(sig);
214 GNUNET_CRYPTO_rsa_private_key_free (priv); 214 GNUNET_CRYPTO_rsa_private_key_free(priv);
215 GNUNET_CRYPTO_rsa_public_key_free (pub); 215 GNUNET_CRYPTO_rsa_public_key_free(pub);
216 if (GNUNET_OK != ret) 216 if (GNUNET_OK != ret)
217 return 1; 217 return 1;
218 218
@@ -221,70 +221,70 @@ run_queries (PGconn *conn)
221 221
222 222
223int 223int
224main (int argc, 224main(int argc,
225 const char *const argv[]) 225 const char *const argv[])
226{ 226{
227 PGconn *conn; 227 PGconn *conn;
228 PGresult *result; 228 PGresult *result;
229 int ret; 229 int ret;
230 230
231 GNUNET_log_setup ("test-pq", 231 GNUNET_log_setup("test-pq",
232 "WARNING", 232 "WARNING",
233 NULL); 233 NULL);
234 conn = PQconnectdb ("postgres:///gnunetcheck"); 234 conn = PQconnectdb("postgres:///gnunetcheck");
235 if (CONNECTION_OK != PQstatus (conn)) 235 if (CONNECTION_OK != PQstatus(conn))
236 { 236 {
237 fprintf (stderr, 237 fprintf(stderr,
238 "Cannot run test, database connection failed: %s\n", 238 "Cannot run test, database connection failed: %s\n",
239 PQerrorMessage (conn)); 239 PQerrorMessage(conn));
240 GNUNET_break (0); 240 GNUNET_break(0);
241 PQfinish (conn); 241 PQfinish(conn);
242 return 77; /* signal test was skipped */ 242 return 77; /* signal test was skipped */
243 } 243 }
244 244
245 result = PQexec (conn, 245 result = PQexec(conn,
246 "CREATE TEMPORARY TABLE IF NOT EXISTS test_pq (" 246 "CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
247 " pub BYTEA NOT NULL" 247 " pub BYTEA NOT NULL"
248 ",sig BYTEA NOT NULL" 248 ",sig BYTEA NOT NULL"
249 ",abs_time INT8 NOT NULL" 249 ",abs_time INT8 NOT NULL"
250 ",forever INT8 NOT NULL" 250 ",forever INT8 NOT NULL"
251 ",hash BYTEA NOT NULL CHECK(LENGTH(hash)=64)" 251 ",hash BYTEA NOT NULL CHECK(LENGTH(hash)=64)"
252 ",vsize VARCHAR NOT NULL" 252 ",vsize VARCHAR NOT NULL"
253 ",u16 INT2 NOT NULL" 253 ",u16 INT2 NOT NULL"
254 ",u32 INT4 NOT NULL" 254 ",u32 INT4 NOT NULL"
255 ",u64 INT8 NOT NULL" 255 ",u64 INT8 NOT NULL"
256 ")"); 256 ")");
257 if (PGRES_COMMAND_OK != PQresultStatus (result)) 257 if (PGRES_COMMAND_OK != PQresultStatus(result))
258 { 258 {
259 fprintf (stderr, 259 fprintf(stderr,
260 "Failed to create table: %s\n", 260 "Failed to create table: %s\n",
261 PQerrorMessage (conn)); 261 PQerrorMessage(conn));
262 PQclear (result); 262 PQclear(result);
263 PQfinish (conn); 263 PQfinish(conn);
264 return 1; 264 return 1;
265 } 265 }
266 PQclear (result); 266 PQclear(result);
267 if (GNUNET_OK != 267 if (GNUNET_OK !=
268 postgres_prepare (conn)) 268 postgres_prepare(conn))
269 { 269 {
270 GNUNET_break (0); 270 GNUNET_break(0);
271 PQfinish (conn); 271 PQfinish(conn);
272 return 1; 272 return 1;
273 } 273 }
274 ret = run_queries (conn); 274 ret = run_queries(conn);
275 result = PQexec (conn, 275 result = PQexec(conn,
276 "DROP TABLE test_pq"); 276 "DROP TABLE test_pq");
277 if (PGRES_COMMAND_OK != PQresultStatus (result)) 277 if (PGRES_COMMAND_OK != PQresultStatus(result))
278 { 278 {
279 fprintf (stderr, 279 fprintf(stderr,
280 "Failed to create table: %s\n", 280 "Failed to create table: %s\n",
281 PQerrorMessage (conn)); 281 PQerrorMessage(conn));
282 PQclear (result); 282 PQclear(result);
283 PQfinish (conn); 283 PQfinish(conn);
284 return 1; 284 return 1;
285 } 285 }
286 PQclear (result); 286 PQclear(result);
287 PQfinish (conn); 287 PQfinish(conn);
288 return ret; 288 return ret;
289} 289}
290 290