2 This file is part of GNUnet
3 (C) 2015, 2016, 2017 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 sqlite3 convenience API
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_sq_lib.h"
31 * @brief Prepare a SQL statement
33 * @param dbh handle to the database
34 * @param zSql SQL statement, UTF-8 encoded
35 * @param[out] ppStmt set to the prepared statement
36 * @return 0 on success
39 sq_prepare (sqlite3 *dbh,
41 sqlite3_stmt **ppStmt)
46 result = sqlite3_prepare_v2 (dbh,
50 (const char **) &dummy);
51 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
52 "Prepared `%s' / %p: %d\n",
61 * Run actual test queries.
63 * @return 0 on success
66 run_queries (sqlite3 *dbh)
68 struct GNUNET_CRYPTO_RsaPublicKey *pub;
69 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
70 struct GNUNET_CRYPTO_RsaSignature *sig;
71 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
72 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
73 struct GNUNET_TIME_Absolute abs_time2;
74 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
75 struct GNUNET_TIME_Absolute forever2;
76 struct GNUNET_HashCode hc;
77 struct GNUNET_HashCode hc2;
79 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
80 const char msg[] = "hello";
82 struct GNUNET_HashCode hmsg;
91 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
92 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
93 memset (&hmsg, 42, sizeof (hmsg));
94 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
99 /* FIXME: test GNUNET_SQ_result_spec_variable_size */
102 "INSERT INTO test_sq ("
113 "($1, $2, $3, $4, $5, $6,"
117 struct GNUNET_SQ_QueryParam params_insert[] = {
118 GNUNET_SQ_query_param_rsa_public_key (pub),
119 GNUNET_SQ_query_param_rsa_signature (sig),
120 GNUNET_SQ_query_param_absolute_time (&abs_time),
121 GNUNET_SQ_query_param_absolute_time (&forever),
122 GNUNET_SQ_query_param_auto_from_type (&hc),
123 GNUNET_SQ_query_param_fixed_size (msg, strlen (msg)),
124 GNUNET_SQ_query_param_uint16 (&u16),
125 GNUNET_SQ_query_param_uint32 (&u32),
126 GNUNET_SQ_query_param_uint64 (&u64),
127 GNUNET_SQ_query_param_end
130 GNUNET_assert (GNUNET_OK ==
131 GNUNET_SQ_bind (stmt,
136 GNUNET_CRYPTO_rsa_signature_free (sig);
137 GNUNET_CRYPTO_rsa_private_key_free (priv);
138 GNUNET_CRYPTO_rsa_public_key_free (pub);
142 sqlite3_finalize (stmt);
156 " ORDER BY abs_time DESC "
160 struct GNUNET_SQ_QueryParam params_select[] = {
161 GNUNET_SQ_query_param_end
163 struct GNUNET_SQ_ResultSpec results_select[] = {
164 GNUNET_SQ_result_spec_rsa_public_key (&pub2),
165 GNUNET_SQ_result_spec_rsa_signature (&sig2),
166 GNUNET_SQ_result_spec_absolute_time (&abs_time2),
167 GNUNET_SQ_result_spec_absolute_time (&forever2),
168 GNUNET_SQ_result_spec_auto_from_type (&hc2),
169 GNUNET_SQ_result_spec_variable_size (&msg2, &msg2_len),
170 GNUNET_SQ_result_spec_uint16 (&u162),
171 GNUNET_SQ_result_spec_uint32 (&u322),
172 GNUNET_SQ_result_spec_uint64 (&u642),
173 GNUNET_SQ_result_spec_end
176 GNUNET_assert (GNUNET_OK ==
177 GNUNET_SQ_bind (stmt,
183 sqlite3_finalize (stmt);
184 GNUNET_CRYPTO_rsa_signature_free (sig);
185 GNUNET_CRYPTO_rsa_private_key_free (priv);
186 GNUNET_CRYPTO_rsa_public_key_free (pub);
189 GNUNET_assert (GNUNET_OK ==
190 GNUNET_SQ_extract_result (stmt,
192 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
193 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
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_SQ_cleanup_result (results_select);
213 sqlite3_finalize (stmt);
215 GNUNET_CRYPTO_rsa_signature_free (sig);
216 GNUNET_CRYPTO_rsa_private_key_free (priv);
217 GNUNET_CRYPTO_rsa_public_key_free (pub);
224 const char *const argv[])
229 GNUNET_log_setup ("test-sq",
233 sqlite3_open ("test.db",
237 "Cannot run test, sqlite3 initialization failed\n");
239 return 77; /* Signal test was skipped... */
244 "CREATE TEMPORARY TABLE IF NOT EXISTS test_sq ("
245 " pub BYTEA NOT NULL"
246 ",sig BYTEA NOT NULL"
247 ",abs_time INT8 NOT NULL"
248 ",forever INT8 NOT NULL"
249 ",hash BYTEA NOT NULL"
250 ",vsize VARCHAR NOT NULL"
258 "Failed to create table\n");
259 GNUNET_break (SQLITE_OK ==
260 sqlite3_close (dbh));
261 if (0 != unlink ("test.db"))
262 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
268 ret = run_queries (dbh);
271 "DROP TABLE test_sq",
275 "Failed to drop table\n");
278 GNUNET_break (SQLITE_OK ==
279 sqlite3_close (dbh));
280 if (0 != unlink ("test.db"))
281 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
288 /* end of test_sq.c */