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 under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
18 * @brief Tests for Postgres convenience API
19 * @author Christian Grothoff <christian@grothoff.org>
22 #include "gnunet_util_lib.h"
23 #include "gnunet_pq_lib.h"
27 * Setup prepared statements.
29 * @param db_conn connection handle to initialize
30 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
33 postgres_prepare (PGconn *db_conn)
37 #define PREPARE(name, sql, ...) \
39 result = PQprepare (db_conn, name, sql, __VA_ARGS__); \
40 if (PGRES_COMMAND_OK != PQresultStatus (result)) \
43 PQclear (result); result = NULL; \
44 return GNUNET_SYSERR; \
46 PQclear (result); result = NULL; \
49 PREPARE ("test_insert",
50 "INSERT INTO test_pq ("
61 "($1, $2, $3, $4, $5, $6,"
64 PREPARE ("test_select",
76 " ORDER BY abs_time DESC "
85 * Run actual test queries.
87 * @return 0 on success
90 run_queries (PGconn *conn)
92 struct GNUNET_CRYPTO_rsa_PublicKey *pub;
93 struct GNUNET_CRYPTO_rsa_PublicKey *pub2 = NULL;
94 struct GNUNET_CRYPTO_rsa_Signature *sig;
95 struct GNUNET_CRYPTO_rsa_Signature *sig2 = NULL;
96 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
97 struct GNUNET_TIME_Absolute abs_time2;
98 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
99 struct GNUNET_TIME_Absolute forever2;
100 struct GNUNET_HashCode hc;
101 struct GNUNET_HashCode hc2;
104 struct GNUNET_CRYPTO_rsa_PrivateKey *priv;
105 char msg[] = "Hello";
115 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
116 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
117 sig = GNUNET_CRYPTO_rsa_sign (priv,
123 /* FIXME: test GNUNET_PQ_result_spec_variable_size */
125 struct GNUNET_PQ_QueryParam params_insert[] = {
126 GNUNET_PQ_query_param_rsa_public_key (pub),
127 GNUNET_PQ_query_param_rsa_signature (sig),
128 GNUNET_PQ_query_param_absolute_time (&abs_time),
129 GNUNET_PQ_query_param_absolute_time (&forever),
130 GNUNET_PQ_query_param_auto_from_type (&hc),
131 GNUNET_PQ_query_param_fixed_size (msg, strlen (msg)),
132 GNUNET_PQ_query_param_uint16 (&u16),
133 GNUNET_PQ_query_param_uint32 (&u32),
134 GNUNET_PQ_query_param_uint64 (&u64),
135 GNUNET_PQ_query_param_end
137 struct GNUNET_PQ_QueryParam params_select[] = {
138 GNUNET_PQ_query_param_end
140 struct GNUNET_PQ_ResultSpec results_select[] = {
141 GNUNET_PQ_result_spec_rsa_public_key ("pub", &pub2),
142 GNUNET_PQ_result_spec_rsa_signature ("sig", &sig2),
143 GNUNET_PQ_result_spec_absolute_time ("abs_time", &abs_time2),
144 GNUNET_PQ_result_spec_absolute_time ("forever", &forever2),
145 GNUNET_PQ_result_spec_auto_from_type ("hash", &hc2),
146 GNUNET_PQ_result_spec_variable_size ("vsize", &msg2, &msg2_len),
147 GNUNET_PQ_result_spec_uint16 ("u16", &u162),
148 GNUNET_PQ_result_spec_uint32 ("u32", &u322),
149 GNUNET_PQ_result_spec_uint64 ("u64", &u642),
150 GNUNET_PQ_result_spec_end
153 result = GNUNET_PQ_exec_prepared (conn,
156 if (PGRES_COMMAND_OK != PQresultStatus (result))
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 "Database failure: %s\n",
160 PQresultErrorMessage (result));
162 GNUNET_CRYPTO_rsa_signature_free (sig);
163 GNUNET_CRYPTO_rsa_private_key_free (priv);
164 GNUNET_CRYPTO_rsa_public_key_free (pub);
169 result = GNUNET_PQ_exec_prepared (conn,
177 GNUNET_CRYPTO_rsa_signature_free (sig);
178 GNUNET_CRYPTO_rsa_private_key_free (priv);
179 GNUNET_CRYPTO_rsa_public_key_free (pub);
182 ret = GNUNET_PQ_extract_result (result,
185 GNUNET_break (GNUNET_YES == ret);
186 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
187 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
191 sizeof (struct GNUNET_HashCode)));
193 GNUNET_CRYPTO_rsa_signature_cmp (sig,
196 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
198 GNUNET_break (strlen (msg) == msg2_len);
203 GNUNET_break (16 == u162);
204 GNUNET_break (32 == u322);
205 GNUNET_break (64 == u642);
206 GNUNET_PQ_cleanup_result (results_select);
209 GNUNET_CRYPTO_rsa_signature_free (sig);
210 GNUNET_CRYPTO_rsa_private_key_free (priv);
211 GNUNET_CRYPTO_rsa_public_key_free (pub);
212 if (GNUNET_OK != ret)
221 const char *const argv[])
227 GNUNET_log_setup ("test-pq",
230 conn = PQconnectdb ("postgres:///gnunetcheck");
231 if (CONNECTION_OK != PQstatus (conn))
234 "Cannot run test, database connection failed: %s\n",
235 PQerrorMessage (conn));
238 return 0; /* We ignore this type of error... */
241 result = PQexec (conn,
242 "CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
243 " pub BYTEA NOT NULL"
244 ",sig BYTEA NOT NULL"
245 ",abs_time INT8 NOT NULL"
246 ",forever INT8 NOT NULL"
247 ",hash BYTEA NOT NULL CHECK(LENGTH(hash)=64)"
248 ",vsize VARCHAR NOT NULL"
253 if (PGRES_COMMAND_OK != PQresultStatus (result))
256 "Failed to create table: %s\n",
257 PQerrorMessage (conn));
264 postgres_prepare (conn))
270 ret = run_queries (conn);
271 result = PQexec (conn,
272 "DROP TABLE test_pq");
273 if (PGRES_COMMAND_OK != PQresultStatus (result))
276 "Failed to create table: %s\n",
277 PQerrorMessage (conn));
288 /* end of test_pq.c */