aboutsummaryrefslogtreecommitdiff
path: root/src/lib/sq/test_sq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/sq/test_sq.c')
-rw-r--r--src/lib/sq/test_sq.c291
1 files changed, 291 insertions, 0 deletions
diff --git a/src/lib/sq/test_sq.c b/src/lib/sq/test_sq.c
new file mode 100644
index 000000000..292de5f34
--- /dev/null
+++ b/src/lib/sq/test_sq.c
@@ -0,0 +1,291 @@
1/*
2 This file is part of GNUnet
3 (C) 2015, 2016, 2017 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file sq/test_sq.c
22 * @brief Tests for sqlite3 convenience API
23 * @author Christian Grothoff
24 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include "gnunet_sq_lib.h"
28
29
30/**
31 * @brief Prepare a SQL statement
32 *
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
37 */
38static int
39sq_prepare (sqlite3 *dbh,
40 const char *zSql,
41 sqlite3_stmt **ppStmt)
42{
43 char *dummy;
44 int result;
45
46 result = sqlite3_prepare_v2 (dbh,
47 zSql,
48 strlen (zSql),
49 ppStmt,
50 (const char **) &dummy);
51 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
52 "Prepared `%s' / %p: %d\n",
53 zSql,
54 *ppStmt,
55 result);
56 return result;
57}
58
59
60/**
61 * Run actual test queries.
62 *
63 * @return 0 on success
64 */
65static int
66run_queries (sqlite3 *dbh)
67{
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;
78 sqlite3_stmt *stmt;
79 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
80 const char msg[] = "hello";
81 void *msg2;
82 struct GNUNET_HashCode hmsg;
83 size_t msg2_len;
84 uint16_t u16;
85 uint16_t u162;
86 uint32_t u32;
87 uint32_t u322;
88 uint64_t u64;
89 uint64_t u642;
90
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,
95 &hmsg,
96 sizeof (hmsg));
97 u16 = 16;
98 u32 = 32;
99 u64 = 64;
100 /* FIXME: test GNUNET_SQ_result_spec_variable_size */
101
102 sq_prepare (dbh,
103 "INSERT INTO test_sq ("
104 " pub"
105 ",sig"
106 ",abs_time"
107 ",forever"
108 ",hash"
109 ",vsize"
110 ",u16"
111 ",u32"
112 ",u64"
113 ") VALUES "
114 "($1, $2, $3, $4, $5, $6,"
115 "$7, $8, $9);",
116 &stmt);
117 {
118 struct GNUNET_SQ_QueryParam params_insert[] = {
119 GNUNET_SQ_query_param_rsa_public_key (pub),
120 GNUNET_SQ_query_param_rsa_signature (sig),
121 GNUNET_SQ_query_param_absolute_time (&abs_time),
122 GNUNET_SQ_query_param_absolute_time (&forever),
123 GNUNET_SQ_query_param_auto_from_type (&hc),
124 GNUNET_SQ_query_param_fixed_size (msg, strlen (msg)),
125 GNUNET_SQ_query_param_uint16 (&u16),
126 GNUNET_SQ_query_param_uint32 (&u32),
127 GNUNET_SQ_query_param_uint64 (&u64),
128 GNUNET_SQ_query_param_end
129 };
130
131 GNUNET_assert (GNUNET_OK ==
132 GNUNET_SQ_bind (stmt,
133 params_insert));
134 if (SQLITE_DONE !=
135 sqlite3_step (stmt))
136 {
137 GNUNET_CRYPTO_rsa_signature_free (sig);
138 GNUNET_CRYPTO_rsa_private_key_free (priv);
139 GNUNET_CRYPTO_rsa_public_key_free (pub);
140 return 1;
141 }
142 sqlite3_reset (stmt);
143 }
144 sqlite3_finalize (stmt);
145
146 sq_prepare (dbh,
147 "SELECT"
148 " pub"
149 ",sig"
150 ",abs_time"
151 ",forever"
152 ",hash"
153 ",vsize"
154 ",u16"
155 ",u32"
156 ",u64"
157 " FROM test_sq"
158 " ORDER BY abs_time DESC "
159 " LIMIT 1;",
160 &stmt);
161 {
162 struct GNUNET_SQ_QueryParam params_select[] = {
163 GNUNET_SQ_query_param_end
164 };
165 struct GNUNET_SQ_ResultSpec results_select[] = {
166 GNUNET_SQ_result_spec_rsa_public_key (&pub2),
167 GNUNET_SQ_result_spec_rsa_signature (&sig2),
168 GNUNET_SQ_result_spec_absolute_time (&abs_time2),
169 GNUNET_SQ_result_spec_absolute_time (&forever2),
170 GNUNET_SQ_result_spec_auto_from_type (&hc2),
171 GNUNET_SQ_result_spec_variable_size (&msg2, &msg2_len),
172 GNUNET_SQ_result_spec_uint16 (&u162),
173 GNUNET_SQ_result_spec_uint32 (&u322),
174 GNUNET_SQ_result_spec_uint64 (&u642),
175 GNUNET_SQ_result_spec_end
176 };
177
178 GNUNET_assert (GNUNET_OK ==
179 GNUNET_SQ_bind (stmt,
180 params_select));
181 if (SQLITE_ROW !=
182 sqlite3_step (stmt))
183 {
184 GNUNET_break (0);
185 sqlite3_finalize (stmt);
186 GNUNET_CRYPTO_rsa_signature_free (sig);
187 GNUNET_CRYPTO_rsa_private_key_free (priv);
188 GNUNET_CRYPTO_rsa_public_key_free (pub);
189 return 1;
190 }
191 GNUNET_assert (GNUNET_OK ==
192 GNUNET_SQ_extract_result (stmt,
193 results_select));
194 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
195 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
196 GNUNET_break (0 ==
197 GNUNET_memcmp (&hc,
198 &hc2));
199 GNUNET_break (0 ==
200 GNUNET_CRYPTO_rsa_signature_cmp (sig,
201 sig2));
202 GNUNET_break (0 ==
203 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
204 pub2));
205 GNUNET_break (strlen (msg) == msg2_len);
206 GNUNET_break (0 ==
207 strncmp (msg,
208 msg2,
209 msg2_len));
210 GNUNET_break (16 == u162);
211 GNUNET_break (32 == u322);
212 GNUNET_break (64 == u642);
213 GNUNET_SQ_cleanup_result (results_select);
214 sqlite3_reset (stmt);
215 }
216 sqlite3_finalize (stmt);
217
218 GNUNET_CRYPTO_rsa_signature_free (sig);
219 GNUNET_CRYPTO_rsa_private_key_free (priv);
220 GNUNET_CRYPTO_rsa_public_key_free (pub);
221 return 0;
222}
223
224
225int
226main (int argc,
227 const char *const argv[])
228{
229 sqlite3 *dbh;
230 int ret;
231
232 GNUNET_log_setup ("test-sq",
233 "WARNING",
234 NULL);
235 if (SQLITE_OK !=
236 sqlite3_open ("test.db",
237 &dbh))
238 {
239 fprintf (stderr,
240 "Cannot run test, sqlite3 initialization failed\n");
241 GNUNET_break (0);
242 return 77; /* Signal test was skipped... */
243 }
244
245 if (SQLITE_OK !=
246 sqlite3_exec (dbh,
247 "CREATE TEMPORARY TABLE IF NOT EXISTS test_sq ("
248 " pub BYTEA NOT NULL"
249 ",sig BYTEA NOT NULL"
250 ",abs_time INT8 NOT NULL"
251 ",forever INT8 NOT NULL"
252 ",hash BYTEA NOT NULL"
253 ",vsize VARCHAR NOT NULL"
254 ",u16 INT2 NOT NULL"
255 ",u32 INT4 NOT NULL"
256 ",u64 INT8 NOT NULL"
257 ")",
258 NULL, NULL, NULL))
259 {
260 fprintf (stderr,
261 "Failed to create table\n");
262 GNUNET_break (SQLITE_OK ==
263 sqlite3_close (dbh));
264 if (0 != unlink ("test.db"))
265 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
266 "unlink",
267 "test.db");
268 return 1;
269 }
270
271 ret = run_queries (dbh);
272 if (SQLITE_OK !=
273 sqlite3_exec (dbh,
274 "DROP TABLE test_sq",
275 NULL, NULL, NULL))
276 {
277 fprintf (stderr,
278 "Failed to drop table\n");
279 ret = 1;
280 }
281 GNUNET_break (SQLITE_OK ==
282 sqlite3_close (dbh));
283 if (0 != unlink ("test.db"))
284 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
285 "unlink",
286 "test.db");
287 return ret;
288}
289
290
291/* end of test_sq.c */