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/>.
20 * @brief Tests for sqlite3 convenience API
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
25 #include "gnunet_sq_lib.h"
29 * @brief Prepare a SQL statement
31 * @param dbh handle to the database
32 * @param zSql SQL statement, UTF-8 encoded
33 * @param[out] ppStmt set to the prepared statement
34 * @return 0 on success
37 sq_prepare (sqlite3 *dbh,
39 sqlite3_stmt **ppStmt)
44 result = sqlite3_prepare_v2 (dbh,
48 (const char **) &dummy);
49 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
50 "Prepared `%s' / %p: %d\n",
59 * Run actual test queries.
61 * @return 0 on success
64 run_queries (sqlite3 *dbh)
66 struct GNUNET_CRYPTO_RsaPublicKey *pub;
67 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
68 struct GNUNET_CRYPTO_RsaSignature *sig;
69 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
70 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
71 struct GNUNET_TIME_Absolute abs_time2;
72 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
73 struct GNUNET_TIME_Absolute forever2;
74 struct GNUNET_HashCode hc;
75 struct GNUNET_HashCode hc2;
77 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
78 const char msg[] = "hello";
80 struct GNUNET_HashCode hmsg;
89 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
90 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
91 memset (&hmsg, 42, sizeof (hmsg));
92 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
97 /* FIXME: test GNUNET_SQ_result_spec_variable_size */
100 "INSERT INTO test_sq ("
111 "($1, $2, $3, $4, $5, $6,"
115 struct GNUNET_SQ_QueryParam params_insert[] = {
116 GNUNET_SQ_query_param_rsa_public_key (pub),
117 GNUNET_SQ_query_param_rsa_signature (sig),
118 GNUNET_SQ_query_param_absolute_time (&abs_time),
119 GNUNET_SQ_query_param_absolute_time (&forever),
120 GNUNET_SQ_query_param_auto_from_type (&hc),
121 GNUNET_SQ_query_param_fixed_size (msg, strlen (msg)),
122 GNUNET_SQ_query_param_uint16 (&u16),
123 GNUNET_SQ_query_param_uint32 (&u32),
124 GNUNET_SQ_query_param_uint64 (&u64),
125 GNUNET_SQ_query_param_end
128 GNUNET_assert (GNUNET_OK ==
129 GNUNET_SQ_bind (stmt,
134 GNUNET_CRYPTO_rsa_signature_free (sig);
135 GNUNET_CRYPTO_rsa_private_key_free (priv);
136 GNUNET_CRYPTO_rsa_public_key_free (pub);
140 sqlite3_finalize (stmt);
154 " ORDER BY abs_time DESC "
158 struct GNUNET_SQ_QueryParam params_select[] = {
159 GNUNET_SQ_query_param_end
161 struct GNUNET_SQ_ResultSpec results_select[] = {
162 GNUNET_SQ_result_spec_rsa_public_key (&pub2),
163 GNUNET_SQ_result_spec_rsa_signature (&sig2),
164 GNUNET_SQ_result_spec_absolute_time (&abs_time2),
165 GNUNET_SQ_result_spec_absolute_time (&forever2),
166 GNUNET_SQ_result_spec_auto_from_type (&hc2),
167 GNUNET_SQ_result_spec_variable_size (&msg2, &msg2_len),
168 GNUNET_SQ_result_spec_uint16 (&u162),
169 GNUNET_SQ_result_spec_uint32 (&u322),
170 GNUNET_SQ_result_spec_uint64 (&u642),
171 GNUNET_SQ_result_spec_end
174 GNUNET_assert (GNUNET_OK ==
175 GNUNET_SQ_bind (stmt,
181 sqlite3_finalize (stmt);
182 GNUNET_CRYPTO_rsa_signature_free (sig);
183 GNUNET_CRYPTO_rsa_private_key_free (priv);
184 GNUNET_CRYPTO_rsa_public_key_free (pub);
187 GNUNET_assert (GNUNET_OK ==
188 GNUNET_SQ_extract_result (stmt,
190 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
191 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
195 sizeof (struct GNUNET_HashCode)));
197 GNUNET_CRYPTO_rsa_signature_cmp (sig,
200 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
202 GNUNET_break (strlen (msg) == msg2_len);
207 GNUNET_break (16 == u162);
208 GNUNET_break (32 == u322);
209 GNUNET_break (64 == u642);
210 GNUNET_SQ_cleanup_result (results_select);
212 sqlite3_finalize (stmt);
214 GNUNET_CRYPTO_rsa_signature_free (sig);
215 GNUNET_CRYPTO_rsa_private_key_free (priv);
216 GNUNET_CRYPTO_rsa_public_key_free (pub);
223 const char *const argv[])
228 GNUNET_log_setup ("test-sq",
232 sqlite3_open ("test.db",
236 "Cannot run test, sqlite3 initialization failed\n");
238 return 77; /* Signal test was skipped... */
243 "CREATE TEMPORARY TABLE IF NOT EXISTS test_sq ("
244 " pub BYTEA NOT NULL"
245 ",sig BYTEA NOT NULL"
246 ",abs_time INT8 NOT NULL"
247 ",forever INT8 NOT NULL"
248 ",hash BYTEA NOT NULL"
249 ",vsize VARCHAR NOT NULL"
257 "Failed to create table\n");
258 GNUNET_break (SQLITE_OK ==
259 sqlite3_close (dbh));
260 if (0 != unlink ("test.db"))
261 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
267 ret = run_queries (dbh);
270 "DROP TABLE test_sq",
274 "Failed to drop table\n");
277 GNUNET_break (SQLITE_OK ==
278 sqlite3_close (dbh));
279 if (0 != unlink ("test.db"))
280 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
287 /* end of test_sq.c */