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 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 sqlite3 convenience API
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_sq_lib.h"
27 * @brief Prepare a SQL statement
29 * @param dbh handle to the database
30 * @param zSql SQL statement, UTF-8 encoded
31 * @param[out] ppStmt set to the prepared statement
32 * @return 0 on success
35 sq_prepare (sqlite3 *dbh,
37 sqlite3_stmt **ppStmt)
42 result = sqlite3_prepare_v2 (dbh,
46 (const char **) &dummy);
47 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
48 "Prepared `%s' / %p: %d\n",
57 * Run actual test queries.
59 * @return 0 on success
62 run_queries (sqlite3 *dbh)
64 struct GNUNET_CRYPTO_RsaPublicKey *pub;
65 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
66 struct GNUNET_CRYPTO_RsaSignature *sig;
67 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
68 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
69 struct GNUNET_TIME_Absolute abs_time2;
70 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
71 struct GNUNET_TIME_Absolute forever2;
72 struct GNUNET_HashCode hc;
73 struct GNUNET_HashCode hc2;
75 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
76 const char msg[] = "hello";
78 struct GNUNET_HashCode hmsg;
87 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
88 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
89 memset (&hmsg, 42, sizeof (hmsg));
90 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
95 /* FIXME: test GNUNET_SQ_result_spec_variable_size */
98 "INSERT INTO test_sq ("
109 "($1, $2, $3, $4, $5, $6,"
113 struct GNUNET_SQ_QueryParam params_insert[] = {
114 GNUNET_SQ_query_param_rsa_public_key (pub),
115 GNUNET_SQ_query_param_rsa_signature (sig),
116 GNUNET_SQ_query_param_absolute_time (&abs_time),
117 GNUNET_SQ_query_param_absolute_time (&forever),
118 GNUNET_SQ_query_param_auto_from_type (&hc),
119 GNUNET_SQ_query_param_fixed_size (msg, strlen (msg)),
120 GNUNET_SQ_query_param_uint16 (&u16),
121 GNUNET_SQ_query_param_uint32 (&u32),
122 GNUNET_SQ_query_param_uint64 (&u64),
123 GNUNET_SQ_query_param_end
126 GNUNET_assert (GNUNET_OK ==
127 GNUNET_SQ_bind (stmt,
132 GNUNET_CRYPTO_rsa_signature_free (sig);
133 GNUNET_CRYPTO_rsa_private_key_free (priv);
134 GNUNET_CRYPTO_rsa_public_key_free (pub);
138 sqlite3_finalize (stmt);
152 " ORDER BY abs_time DESC "
156 struct GNUNET_SQ_QueryParam params_select[] = {
157 GNUNET_SQ_query_param_end
159 struct GNUNET_SQ_ResultSpec results_select[] = {
160 GNUNET_SQ_result_spec_rsa_public_key (&pub2),
161 GNUNET_SQ_result_spec_rsa_signature (&sig2),
162 GNUNET_SQ_result_spec_absolute_time (&abs_time2),
163 GNUNET_SQ_result_spec_absolute_time (&forever2),
164 GNUNET_SQ_result_spec_auto_from_type (&hc2),
165 GNUNET_SQ_result_spec_variable_size (&msg2, &msg2_len),
166 GNUNET_SQ_result_spec_uint16 (&u162),
167 GNUNET_SQ_result_spec_uint32 (&u322),
168 GNUNET_SQ_result_spec_uint64 (&u642),
169 GNUNET_SQ_result_spec_end
172 GNUNET_assert (GNUNET_OK ==
173 GNUNET_SQ_bind (stmt,
179 sqlite3_finalize (stmt);
180 GNUNET_CRYPTO_rsa_signature_free (sig);
181 GNUNET_CRYPTO_rsa_private_key_free (priv);
182 GNUNET_CRYPTO_rsa_public_key_free (pub);
185 GNUNET_assert (GNUNET_OK ==
186 GNUNET_SQ_extract_result (stmt,
188 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
189 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
193 sizeof (struct GNUNET_HashCode)));
195 GNUNET_CRYPTO_rsa_signature_cmp (sig,
198 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
200 GNUNET_break (strlen (msg) == msg2_len);
205 GNUNET_break (16 == u162);
206 GNUNET_break (32 == u322);
207 GNUNET_break (64 == u642);
208 GNUNET_SQ_cleanup_result (results_select);
210 sqlite3_finalize (stmt);
212 GNUNET_CRYPTO_rsa_signature_free (sig);
213 GNUNET_CRYPTO_rsa_private_key_free (priv);
214 GNUNET_CRYPTO_rsa_public_key_free (pub);
221 const char *const argv[])
226 GNUNET_log_setup ("test-sq",
230 sqlite3_open ("test.db",
234 "Cannot run test, sqlite3 initialization failed\n");
236 return 77; /* Signal test was skipped... */
241 "CREATE TEMPORARY TABLE IF NOT EXISTS test_sq ("
242 " pub BYTEA NOT NULL"
243 ",sig BYTEA NOT NULL"
244 ",abs_time INT8 NOT NULL"
245 ",forever INT8 NOT NULL"
246 ",hash BYTEA NOT NULL"
247 ",vsize VARCHAR NOT NULL"
255 "Failed to create table\n");
261 ret = run_queries (dbh);
264 "DROP TABLE test_sq",
268 "Failed to drop table\n");
279 /* end of test_sq.c */