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/>.
20 * @brief Tests for Postgres convenience API
21 * @author Christian Grothoff <christian@grothoff.org>
24 #include "gnunet_util_lib.h"
25 #include "gnunet_pq_lib.h"
29 * Setup prepared statements.
31 * @param db_conn connection handle to initialize
32 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
35 postgres_prepare (PGconn *db_conn)
39 #define PREPARE(name, sql, ...) \
41 result = PQprepare (db_conn, name, sql, __VA_ARGS__); \
42 if (PGRES_COMMAND_OK != PQresultStatus (result)) \
45 PQclear (result); result = NULL; \
46 return GNUNET_SYSERR; \
48 PQclear (result); result = NULL; \
51 PREPARE ("test_insert",
52 "INSERT INTO test_pq ("
63 "($1, $2, $3, $4, $5, $6,"
66 PREPARE ("test_select",
78 " ORDER BY abs_time DESC "
87 * Run actual test queries.
89 * @return 0 on success
92 run_queries (PGconn *conn)
94 struct GNUNET_CRYPTO_RsaPublicKey *pub;
95 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
96 struct GNUNET_CRYPTO_RsaSignature *sig;
97 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
98 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
99 struct GNUNET_TIME_Absolute abs_time2;
100 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
101 struct GNUNET_TIME_Absolute forever2;
102 struct GNUNET_HashCode hc;
103 struct GNUNET_HashCode hc2;
106 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
107 const char msg[] = "hello";
109 struct GNUNET_HashCode hmsg;
118 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
119 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
120 memset (&hmsg, 42, sizeof (hmsg));
121 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
126 /* FIXME: test GNUNET_PQ_result_spec_variable_size */
128 struct GNUNET_PQ_QueryParam params_insert[] = {
129 GNUNET_PQ_query_param_rsa_public_key (pub),
130 GNUNET_PQ_query_param_rsa_signature (sig),
131 GNUNET_PQ_query_param_absolute_time (&abs_time),
132 GNUNET_PQ_query_param_absolute_time (&forever),
133 GNUNET_PQ_query_param_auto_from_type (&hc),
134 GNUNET_PQ_query_param_fixed_size (msg, strlen (msg)),
135 GNUNET_PQ_query_param_uint16 (&u16),
136 GNUNET_PQ_query_param_uint32 (&u32),
137 GNUNET_PQ_query_param_uint64 (&u64),
138 GNUNET_PQ_query_param_end
140 struct GNUNET_PQ_QueryParam params_select[] = {
141 GNUNET_PQ_query_param_end
143 struct GNUNET_PQ_ResultSpec results_select[] = {
144 GNUNET_PQ_result_spec_rsa_public_key ("pub", &pub2),
145 GNUNET_PQ_result_spec_rsa_signature ("sig", &sig2),
146 GNUNET_PQ_result_spec_absolute_time ("abs_time", &abs_time2),
147 GNUNET_PQ_result_spec_absolute_time ("forever", &forever2),
148 GNUNET_PQ_result_spec_auto_from_type ("hash", &hc2),
149 GNUNET_PQ_result_spec_variable_size ("vsize", &msg2, &msg2_len),
150 GNUNET_PQ_result_spec_uint16 ("u16", &u162),
151 GNUNET_PQ_result_spec_uint32 ("u32", &u322),
152 GNUNET_PQ_result_spec_uint64 ("u64", &u642),
153 GNUNET_PQ_result_spec_end
156 result = GNUNET_PQ_exec_prepared (conn,
159 if (PGRES_COMMAND_OK != PQresultStatus (result))
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162 "Database failure: %s\n",
163 PQresultErrorMessage (result));
165 GNUNET_CRYPTO_rsa_signature_free (sig);
166 GNUNET_CRYPTO_rsa_private_key_free (priv);
167 GNUNET_CRYPTO_rsa_public_key_free (pub);
172 result = GNUNET_PQ_exec_prepared (conn,
180 GNUNET_CRYPTO_rsa_signature_free (sig);
181 GNUNET_CRYPTO_rsa_private_key_free (priv);
182 GNUNET_CRYPTO_rsa_public_key_free (pub);
185 ret = GNUNET_PQ_extract_result (result,
188 GNUNET_break (GNUNET_YES == ret);
189 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
190 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
194 sizeof (struct GNUNET_HashCode)));
196 GNUNET_CRYPTO_rsa_signature_cmp (sig,
199 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
201 GNUNET_break (strlen (msg) == msg2_len);
206 GNUNET_break (16 == u162);
207 GNUNET_break (32 == u322);
208 GNUNET_break (64 == u642);
209 GNUNET_PQ_cleanup_result (results_select);
212 GNUNET_CRYPTO_rsa_signature_free (sig);
213 GNUNET_CRYPTO_rsa_private_key_free (priv);
214 GNUNET_CRYPTO_rsa_public_key_free (pub);
215 if (GNUNET_OK != ret)
224 const char *const argv[])
230 GNUNET_log_setup ("test-pq",
233 conn = PQconnectdb ("postgres:///gnunetcheck");
234 if (CONNECTION_OK != PQstatus (conn))
237 "Cannot run test, database connection failed: %s\n",
238 PQerrorMessage (conn));
241 return 77; /* signal test was skipped */
244 result = PQexec (conn,
245 "CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
246 " pub BYTEA NOT NULL"
247 ",sig BYTEA NOT NULL"
248 ",abs_time INT8 NOT NULL"
249 ",forever INT8 NOT NULL"
250 ",hash BYTEA NOT NULL CHECK(LENGTH(hash)=64)"
251 ",vsize VARCHAR NOT NULL"
256 if (PGRES_COMMAND_OK != PQresultStatus (result))
259 "Failed to create table: %s\n",
260 PQerrorMessage (conn));
267 postgres_prepare (conn))
273 ret = run_queries (conn);
274 result = PQexec (conn,
275 "DROP TABLE test_pq");
276 if (PGRES_COMMAND_OK != PQresultStatus (result))
279 "Failed to create table: %s\n",
280 PQerrorMessage (conn));
291 /* end of test_pq.c */