2 This file is part of GNUnet
3 (C) 2015, 2016 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @brief Tests for Postgres convenience API
23 * @author Christian Grothoff <christian@grothoff.org>
26 #include "gnunet_util_lib.h"
27 #include "gnunet_pq_lib.h"
31 * Setup prepared statements.
33 * @param db_conn connection handle to initialize
34 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
37 postgres_prepare(PGconn *db_conn)
41 #define PREPARE(name, sql, ...) \
43 result = PQprepare(db_conn, name, sql, __VA_ARGS__); \
44 if (PGRES_COMMAND_OK != PQresultStatus(result)) \
47 PQclear(result); result = NULL; \
48 return GNUNET_SYSERR; \
50 PQclear(result); result = NULL; \
53 PREPARE("test_insert",
54 "INSERT INTO test_pq ("
65 "($1, $2, $3, $4, $5, $6,"
68 PREPARE("test_select",
80 " ORDER BY abs_time DESC "
89 * Run actual test queries.
91 * @return 0 on success
94 run_queries(PGconn *conn)
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;
108 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
109 const char msg[] = "hello";
111 struct GNUNET_HashCode hmsg;
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,
128 /* FIXME: test GNUNET_PQ_result_spec_variable_size */
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
142 struct GNUNET_PQ_QueryParam params_select[] = {
143 GNUNET_PQ_query_param_end
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
158 result = GNUNET_PQ_exec_prepared(conn,
161 if (PGRES_COMMAND_OK != PQresultStatus(result))
163 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
164 "Database failure: %s\n",
165 PQresultErrorMessage(result));
167 GNUNET_CRYPTO_rsa_signature_free(sig);
168 GNUNET_CRYPTO_rsa_private_key_free(priv);
169 GNUNET_CRYPTO_rsa_public_key_free(pub);
174 result = GNUNET_PQ_exec_prepared(conn,
182 GNUNET_CRYPTO_rsa_signature_free(sig);
183 GNUNET_CRYPTO_rsa_private_key_free(priv);
184 GNUNET_CRYPTO_rsa_public_key_free(pub);
187 ret = GNUNET_PQ_extract_result(result,
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);
197 GNUNET_CRYPTO_rsa_signature_cmp(sig,
200 GNUNET_CRYPTO_rsa_public_key_cmp(pub,
202 GNUNET_break(strlen(msg) == msg2_len);
207 GNUNET_break(16 == u162);
208 GNUNET_break(32 == u322);
209 GNUNET_break(64 == u642);
210 GNUNET_PQ_cleanup_result(results_select);
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)
225 const char *const argv[])
231 GNUNET_log_setup("test-pq",
234 conn = PQconnectdb("postgres:///gnunetcheck");
235 if (CONNECTION_OK != PQstatus(conn))
238 "Cannot run test, database connection failed: %s\n",
239 PQerrorMessage(conn));
242 return 77; /* signal test was skipped */
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"
257 if (PGRES_COMMAND_OK != PQresultStatus(result))
260 "Failed to create table: %s\n",
261 PQerrorMessage(conn));
268 postgres_prepare(conn))
274 ret = run_queries(conn);
275 result = PQexec(conn,
276 "DROP TABLE test_pq");
277 if (PGRES_COMMAND_OK != PQresultStatus(result))
280 "Failed to create table: %s\n",
281 PQerrorMessage(conn));
292 /* end of test_pq.c */