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);
196 sizeof (struct GNUNET_HashCode)));
198 GNUNET_CRYPTO_rsa_signature_cmp (sig,
201 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
203 GNUNET_break (strlen (msg) == msg2_len);
208 GNUNET_break (16 == u162);
209 GNUNET_break (32 == u322);
210 GNUNET_break (64 == u642);
211 GNUNET_PQ_cleanup_result (results_select);
214 GNUNET_CRYPTO_rsa_signature_free (sig);
215 GNUNET_CRYPTO_rsa_private_key_free (priv);
216 GNUNET_CRYPTO_rsa_public_key_free (pub);
217 if (GNUNET_OK != ret)
226 const char *const argv[])
232 GNUNET_log_setup ("test-pq",
235 conn = PQconnectdb ("postgres:///gnunetcheck");
236 if (CONNECTION_OK != PQstatus (conn))
239 "Cannot run test, database connection failed: %s\n",
240 PQerrorMessage (conn));
243 return 77; /* signal test was skipped */
246 result = PQexec (conn,
247 "CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
248 " pub BYTEA NOT NULL"
249 ",sig BYTEA NOT NULL"
250 ",abs_time INT8 NOT NULL"
251 ",forever INT8 NOT NULL"
252 ",hash BYTEA NOT NULL CHECK(LENGTH(hash)=64)"
253 ",vsize VARCHAR NOT NULL"
258 if (PGRES_COMMAND_OK != PQresultStatus (result))
261 "Failed to create table: %s\n",
262 PQerrorMessage (conn));
269 postgres_prepare (conn))
275 ret = run_queries (conn);
276 result = PQexec (conn,
277 "DROP TABLE test_pq");
278 if (PGRES_COMMAND_OK != PQresultStatus (result))
281 "Failed to create table: %s\n",
282 PQerrorMessage (conn));
293 /* end of test_pq.c */