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 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.
17 * @brief Tests for sqlite3 convenience API
18 * @author Christian Grothoff
21 #include "gnunet_util_lib.h"
22 #include "gnunet_sq_lib.h"
26 * @brief Prepare a SQL statement
28 * @param dbh handle to the database
29 * @param zSql SQL statement, UTF-8 encoded
30 * @param[out] ppStmt set to the prepared statement
31 * @return 0 on success
34 sq_prepare (sqlite3 *dbh,
36 sqlite3_stmt **ppStmt)
41 result = sqlite3_prepare_v2 (dbh,
45 (const char **) &dummy);
46 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
47 "Prepared `%s' / %p: %d\n",
56 * Run actual test queries.
58 * @return 0 on success
61 run_queries (sqlite3 *dbh)
63 struct GNUNET_CRYPTO_RsaPublicKey *pub;
64 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
65 struct GNUNET_CRYPTO_RsaSignature *sig;
66 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
67 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
68 struct GNUNET_TIME_Absolute abs_time2;
69 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
70 struct GNUNET_TIME_Absolute forever2;
71 struct GNUNET_HashCode hc;
72 struct GNUNET_HashCode hc2;
74 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
75 const char msg[] = "hello";
77 struct GNUNET_HashCode hmsg;
86 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
87 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
88 memset (&hmsg, 42, sizeof (hmsg));
89 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
94 /* FIXME: test GNUNET_SQ_result_spec_variable_size */
97 "INSERT INTO test_sq ("
108 "($1, $2, $3, $4, $5, $6,"
112 struct GNUNET_SQ_QueryParam params_insert[] = {
113 GNUNET_SQ_query_param_rsa_public_key (pub),
114 GNUNET_SQ_query_param_rsa_signature (sig),
115 GNUNET_SQ_query_param_absolute_time (&abs_time),
116 GNUNET_SQ_query_param_absolute_time (&forever),
117 GNUNET_SQ_query_param_auto_from_type (&hc),
118 GNUNET_SQ_query_param_fixed_size (msg, strlen (msg)),
119 GNUNET_SQ_query_param_uint16 (&u16),
120 GNUNET_SQ_query_param_uint32 (&u32),
121 GNUNET_SQ_query_param_uint64 (&u64),
122 GNUNET_SQ_query_param_end
125 GNUNET_assert (GNUNET_OK ==
126 GNUNET_SQ_bind (stmt,
131 GNUNET_CRYPTO_rsa_signature_free (sig);
132 GNUNET_CRYPTO_rsa_private_key_free (priv);
133 GNUNET_CRYPTO_rsa_public_key_free (pub);
137 sqlite3_finalize (stmt);
151 " ORDER BY abs_time DESC "
155 struct GNUNET_SQ_QueryParam params_select[] = {
156 GNUNET_SQ_query_param_end
158 struct GNUNET_SQ_ResultSpec results_select[] = {
159 GNUNET_SQ_result_spec_rsa_public_key (&pub2),
160 GNUNET_SQ_result_spec_rsa_signature (&sig2),
161 GNUNET_SQ_result_spec_absolute_time (&abs_time2),
162 GNUNET_SQ_result_spec_absolute_time (&forever2),
163 GNUNET_SQ_result_spec_auto_from_type (&hc2),
164 GNUNET_SQ_result_spec_variable_size (&msg2, &msg2_len),
165 GNUNET_SQ_result_spec_uint16 (&u162),
166 GNUNET_SQ_result_spec_uint32 (&u322),
167 GNUNET_SQ_result_spec_uint64 (&u642),
168 GNUNET_SQ_result_spec_end
171 GNUNET_assert (GNUNET_OK ==
172 GNUNET_SQ_bind (stmt,
178 sqlite3_finalize (stmt);
179 GNUNET_CRYPTO_rsa_signature_free (sig);
180 GNUNET_CRYPTO_rsa_private_key_free (priv);
181 GNUNET_CRYPTO_rsa_public_key_free (pub);
184 GNUNET_assert (GNUNET_OK ==
185 GNUNET_SQ_extract_result (stmt,
187 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
188 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
192 sizeof (struct GNUNET_HashCode)));
194 GNUNET_CRYPTO_rsa_signature_cmp (sig,
197 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
199 GNUNET_break (strlen (msg) == msg2_len);
204 GNUNET_break (16 == u162);
205 GNUNET_break (32 == u322);
206 GNUNET_break (64 == u642);
207 GNUNET_SQ_cleanup_result (results_select);
209 sqlite3_finalize (stmt);
211 GNUNET_CRYPTO_rsa_signature_free (sig);
212 GNUNET_CRYPTO_rsa_private_key_free (priv);
213 GNUNET_CRYPTO_rsa_public_key_free (pub);
220 const char *const argv[])
225 GNUNET_log_setup ("test-sq",
229 sqlite3_open ("test.db",
233 "Cannot run test, sqlite3 initialization failed\n");
235 return 77; /* Signal test was skipped... */
240 "CREATE TEMPORARY TABLE IF NOT EXISTS test_sq ("
241 " pub BYTEA NOT NULL"
242 ",sig BYTEA NOT NULL"
243 ",abs_time INT8 NOT NULL"
244 ",forever INT8 NOT NULL"
245 ",hash BYTEA NOT NULL"
246 ",vsize VARCHAR NOT NULL"
254 "Failed to create table\n");
255 GNUNET_break (SQLITE_OK ==
256 sqlite3_close (dbh));
257 if (0 != unlink ("test.db"))
258 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
264 ret = run_queries (dbh);
267 "DROP TABLE test_sq",
271 "Failed to drop table\n");
274 GNUNET_break (SQLITE_OK ==
275 sqlite3_close (dbh));
276 if (0 != unlink ("test.db"))
277 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
284 /* end of test_sq.c */