2 This file is part of GNUnet
3 (C) 2015, 2016, 2019, 2020 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>
30 * Setup prepared statements.
32 * @param db database handle to initialize
33 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
36 postgres_prepare (struct GNUNET_PQ_Context *db)
38 struct GNUNET_PQ_PreparedStatement ps[] = {
39 GNUNET_PQ_make_prepare ("test_insert",
40 "INSERT INTO test_pq ("
51 "($1, $2, $3, $4, $5, $6,"
54 GNUNET_PQ_make_prepare ("test_select",
66 " ORDER BY abs_time DESC "
69 GNUNET_PQ_PREPARED_STATEMENT_END
72 return GNUNET_PQ_prepare_statements (db,
78 * Run actual test queries.
80 * @param db database handle
81 * @return 0 on success
84 run_queries (struct GNUNET_PQ_Context *db)
86 struct GNUNET_CRYPTO_RsaPublicKey *pub;
87 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
88 struct GNUNET_CRYPTO_RsaSignature *sig;
89 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
90 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
91 struct GNUNET_TIME_Absolute abs_time2;
92 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
93 struct GNUNET_TIME_Absolute forever2;
94 struct GNUNET_HashCode hc;
95 struct GNUNET_HashCode hc2;
98 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
99 const char msg[] = "hello";
101 struct GNUNET_HashCode hmsg;
110 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
111 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
112 memset (&hmsg, 42, sizeof(hmsg));
113 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
118 /* FIXME: test GNUNET_PQ_result_spec_variable_size */
120 struct GNUNET_PQ_QueryParam params_insert[] = {
121 GNUNET_PQ_query_param_rsa_public_key (pub),
122 GNUNET_PQ_query_param_rsa_signature (sig),
123 GNUNET_PQ_query_param_absolute_time (&abs_time),
124 GNUNET_PQ_query_param_absolute_time (&forever),
125 GNUNET_PQ_query_param_auto_from_type (&hc),
126 GNUNET_PQ_query_param_fixed_size (msg, strlen (msg)),
127 GNUNET_PQ_query_param_uint16 (&u16),
128 GNUNET_PQ_query_param_uint32 (&u32),
129 GNUNET_PQ_query_param_uint64 (&u64),
130 GNUNET_PQ_query_param_end
132 struct GNUNET_PQ_QueryParam params_select[] = {
133 GNUNET_PQ_query_param_end
135 struct GNUNET_PQ_ResultSpec results_select[] = {
136 GNUNET_PQ_result_spec_rsa_public_key ("pub", &pub2),
137 GNUNET_PQ_result_spec_rsa_signature ("sig", &sig2),
138 GNUNET_PQ_result_spec_absolute_time ("abs_time", &abs_time2),
139 GNUNET_PQ_result_spec_absolute_time ("forever", &forever2),
140 GNUNET_PQ_result_spec_auto_from_type ("hash", &hc2),
141 GNUNET_PQ_result_spec_variable_size ("vsize", &msg2, &msg2_len),
142 GNUNET_PQ_result_spec_uint16 ("u16", &u162),
143 GNUNET_PQ_result_spec_uint32 ("u32", &u322),
144 GNUNET_PQ_result_spec_uint64 ("u64", &u642),
145 GNUNET_PQ_result_spec_end
148 result = GNUNET_PQ_exec_prepared (db,
151 if (PGRES_COMMAND_OK != PQresultStatus (result))
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154 "Database failure: %s\n",
155 PQresultErrorMessage (result));
157 GNUNET_CRYPTO_rsa_signature_free (sig);
158 GNUNET_CRYPTO_rsa_private_key_free (priv);
159 GNUNET_CRYPTO_rsa_public_key_free (pub);
164 result = GNUNET_PQ_exec_prepared (db,
172 GNUNET_CRYPTO_rsa_signature_free (sig);
173 GNUNET_CRYPTO_rsa_private_key_free (priv);
174 GNUNET_CRYPTO_rsa_public_key_free (pub);
177 ret = GNUNET_PQ_extract_result (result,
180 GNUNET_break (GNUNET_YES == ret);
181 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
182 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
187 GNUNET_CRYPTO_rsa_signature_cmp (sig,
190 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
192 GNUNET_break (strlen (msg) == msg2_len);
197 GNUNET_break (16 == u162);
198 GNUNET_break (32 == u322);
199 GNUNET_break (64 == u642);
200 GNUNET_PQ_cleanup_result (results_select);
203 GNUNET_CRYPTO_rsa_signature_free (sig);
204 GNUNET_CRYPTO_rsa_private_key_free (priv);
205 GNUNET_CRYPTO_rsa_public_key_free (pub);
206 if (GNUNET_OK != ret)
215 const char *const argv[])
217 struct GNUNET_PQ_ExecuteStatement es[] = {
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"
229 GNUNET_PQ_EXECUTE_STATEMENT_END
231 struct GNUNET_PQ_Context *db;
234 GNUNET_log_setup ("test-pq",
237 db = GNUNET_PQ_connect ("postgres:///gnunetcheck",
244 "Cannot run test, database connection failed\n");
247 if (CONNECTION_OK != PQstatus (db->conn))
250 "Cannot run test, database connection failed: %s\n",
251 PQerrorMessage (db->conn));
253 GNUNET_PQ_disconnect (db);
254 return 77; /* signal test was skipped */
257 postgres_prepare (db))
260 GNUNET_PQ_disconnect (db);
263 ret = run_queries (db);
265 fprintf (stderr, "Please restart Postgres database now!\n");
267 ret = run_queries (db);
268 fprintf (stderr, "Result: %d (expect: 1 -- if you restarted the DB)\n", ret);
269 ret = run_queries (db);
270 fprintf (stderr, "Result: %d (expect: 0)\n", ret);
273 struct GNUNET_PQ_ExecuteStatement es[] = {
274 GNUNET_PQ_make_execute ("DROP TABLE test_pq"),
275 GNUNET_PQ_EXECUTE_STATEMENT_END
279 GNUNET_PQ_exec_statements (db,
283 "Failed to drop table\n");
284 GNUNET_PQ_disconnect (db);
288 GNUNET_PQ_disconnect (db);
293 /* end of test_pq.c */