Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2026 Taler Systems SA
4 :
5 : TALER 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.
8 :
9 : TALER 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.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file exchangedb/test_known_coins.c
18 : * @brief tests for the exchangedb functions whose primary table is
19 : * `known_coins`
20 : * @author Christian Grothoff
21 : *
22 : * Covers #TALER_EXCHANGEDB_do_insert_known_coins(),
23 : * #TALER_EXCHANGEDB_get_known_coin(),
24 : * #TALER_EXCHANGEDB_get_coin_denomination(),
25 : * #TALER_EXCHANGEDB_get_signature_for_known_coin(),
26 : * #TALER_EXCHANGEDB_get_denomination_proof_for_known_coin() and
27 : * #TALER_EXCHANGEDB_get_count_known_coins().
28 : *
29 : * `known_coins` references `denominations`, so a denomination is created
30 : * first with TDB_denom(). do_insert_known_coins() is the interesting one:
31 : * it is idempotent, but only for a coin that comes back with the *same*
32 : * denomination and age commitment -- the conflicting cases are what its
33 : * negative per-coin status codes are for. It takes a whole batch of
34 : * coins in one round trip, so the last check mixes all cases in one call
35 : * and verifies that every coin is reported at its own position.
36 : */
37 : #include "test_common.h"
38 : #include "exchange-database/do_insert_known_coins.h"
39 : #include "exchange-database/get_known_coin.h"
40 : #include "exchange-database/get_coin_denomination.h"
41 : #include "exchange-database/get_signature_for_known_coin.h"
42 : #include "exchange-database/get_denomination_proof_for_known_coin.h"
43 : #include "exchange-database/get_count_known_coins.h"
44 : #include "exchange-database/start.h"
45 : #include "exchange-database/rollback.h"
46 :
47 :
48 : /**
49 : * Make a single @a coin known via the batch function.
50 : *
51 : * @param pg the database context
52 : * @param coin the coin to make known
53 : * @param[out] res outcome for the coin
54 : * @return database status, #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT on success
55 : */
56 : static enum GNUNET_DB_QueryStatus
57 8 : insert_one (struct TALER_EXCHANGEDB_PostgresContext *pg,
58 : const struct TALER_CoinPublicInfo *coin,
59 : struct TALER_EXCHANGEDB_CoinKnownResult *res)
60 : {
61 8 : const struct TALER_CoinPublicInfo *coins[1] = { coin };
62 :
63 8 : return TALER_EXCHANGEDB_do_insert_known_coins (pg,
64 : 1,
65 : coins,
66 : res);
67 : }
68 :
69 :
70 : /**
71 : * Nothing is known about a coin that was never inserted.
72 : *
73 : * @param pg the database context
74 : * @return 0 on success
75 : */
76 : static int
77 1 : check_empty (struct TALER_EXCHANGEDB_PostgresContext *pg)
78 : {
79 : struct TALER_CoinSpendPublicKeyP coin_pub;
80 : struct TALER_CoinPublicInfo info;
81 : struct TALER_DenominationHashP h_denom_pub;
82 1 : struct TALER_DenominationPublicKey denom_pub = { 0 };
83 1 : struct TALER_DenominationSignature denom_sig = { 0 };
84 : struct TALER_AgeCommitmentHashP h_age_commitment;
85 : bool no_age_commitment;
86 : uint64_t known_coin_id;
87 :
88 1 : TDB_FILL (coin_pub,
89 : 1);
90 1 : TDB_FILL (h_denom_pub,
91 : 1);
92 1 : FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
93 : TALER_EXCHANGEDB_get_known_coin (pg,
94 : &coin_pub,
95 : &info));
96 1 : FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
97 : TALER_EXCHANGEDB_get_coin_denomination (pg,
98 : &coin_pub,
99 : &known_coin_id,
100 : &h_denom_pub));
101 1 : FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
102 : TALER_EXCHANGEDB_get_signature_for_known_coin (pg,
103 : &coin_pub,
104 : &denom_pub,
105 : &denom_sig));
106 1 : FAILIF (NULL != denom_pub.bsign_pub_key);
107 1 : FAILIF (NULL != denom_sig.unblinded_sig);
108 1 : FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
109 : TALER_EXCHANGEDB_get_denomination_proof_for_known_coin (
110 : pg,
111 : &coin_pub,
112 : &denom_pub,
113 : &denom_sig,
114 : &h_age_commitment,
115 : &no_age_commitment));
116 1 : FAILIF (NULL != denom_pub.bsign_pub_key);
117 1 : FAILIF (NULL != denom_sig.unblinded_sig);
118 : /* a denomination that does not exist has no coins */
119 1 : FAILIF (0 != TALER_EXCHANGEDB_get_count_known_coins (pg,
120 : &h_denom_pub));
121 1 : return 0;
122 : }
123 :
124 :
125 : /**
126 : * A coin of an unknown denomination cannot be made known.
127 : *
128 : * @param pg the database context
129 : * @return 0 on success
130 : */
131 : static int
132 1 : check_unknown_denomination (struct TALER_EXCHANGEDB_PostgresContext *pg)
133 : {
134 : struct TALER_CoinPublicInfo coin;
135 : struct TALER_EXCHANGEDB_CoinKnownResult res;
136 :
137 1 : memset (&coin,
138 : 0,
139 : sizeof (coin));
140 1 : TDB_FILL (coin.coin_pub,
141 : 2);
142 1 : TDB_FILL (coin.denom_pub_hash,
143 : 2);
144 1 : coin.no_age_commitment = true;
145 1 : TDB_denom_sig (2,
146 : &coin.denom_sig);
147 : /* no denomination row, so nothing is inserted and no coin is found
148 : either: the row for the coin has no known_coin_id, a hard error */
149 1 : FAILIF_C (GNUNET_DB_STATUS_HARD_ERROR !=
150 : insert_one (pg,
151 : &coin,
152 : &res),
153 : TALER_denom_sig_free (&coin.denom_sig));
154 1 : TALER_denom_sig_free (&coin.denom_sig);
155 1 : FAILIF (0 != TDB_count (pg,
156 : "FROM known_coins"));
157 1 : return 0;
158 : }
159 :
160 :
161 : /**
162 : * Making a coin known stores it, and every lookup finds it.
163 : *
164 : * @param pg the database context
165 : * @return 0 on success
166 : */
167 : static int
168 1 : check_insert_and_lookup (struct TALER_EXCHANGEDB_PostgresContext *pg)
169 : {
170 : struct TDB_Denom denom;
171 : struct TALER_CoinPublicInfo coin;
172 : struct TALER_CoinPublicInfo got;
173 : struct TALER_DenominationHashP h_denom_pub;
174 1 : struct TALER_DenominationPublicKey denom_pub = { 0 };
175 1 : struct TALER_DenominationSignature denom_sig = { 0 };
176 : struct TALER_AgeCommitmentHashP h_age_commitment;
177 1 : bool no_age_commitment = false;
178 1 : uint64_t known_coin_id = 0;
179 1 : uint64_t id2 = 0;
180 :
181 1 : TDB_denom (pg,
182 : 10,
183 : "5",
184 : "0.1",
185 : &denom);
186 1 : TDB_coin (pg,
187 : &denom,
188 : 20,
189 : &coin,
190 : &known_coin_id);
191 1 : FAILIF_C (0 == known_coin_id,
192 : TDB_coin_free (&coin); TDB_denom_free (&denom));
193 :
194 1 : memset (&got,
195 : 0,
196 : sizeof (got));
197 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
198 : TALER_EXCHANGEDB_get_known_coin (pg,
199 : &coin.coin_pub,
200 : &got),
201 : TDB_coin_free (&coin); TDB_denom_free (&denom));
202 1 : FAILIF_C (0 != GNUNET_memcmp (&got.denom_pub_hash,
203 : &denom.h_denom_pub),
204 : TALER_denom_sig_free (&got.denom_sig);
205 : TDB_coin_free (&coin); TDB_denom_free (&denom));
206 1 : FAILIF_C (! got.no_age_commitment,
207 : TALER_denom_sig_free (&got.denom_sig);
208 : TDB_coin_free (&coin); TDB_denom_free (&denom));
209 1 : FAILIF_C (0 != TALER_denom_sig_cmp (&got.denom_sig,
210 : &coin.denom_sig),
211 : TALER_denom_sig_free (&got.denom_sig);
212 : TDB_coin_free (&coin); TDB_denom_free (&denom));
213 1 : TALER_denom_sig_free (&got.denom_sig);
214 :
215 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
216 : TALER_EXCHANGEDB_get_coin_denomination (pg,
217 : &coin.coin_pub,
218 : &id2,
219 : &h_denom_pub),
220 : TDB_coin_free (&coin); TDB_denom_free (&denom));
221 1 : FAILIF_C (id2 != known_coin_id,
222 : TDB_coin_free (&coin); TDB_denom_free (&denom));
223 1 : FAILIF_C (0 != GNUNET_memcmp (&h_denom_pub,
224 : &denom.h_denom_pub),
225 : TDB_coin_free (&coin); TDB_denom_free (&denom));
226 :
227 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
228 : TALER_EXCHANGEDB_get_signature_for_known_coin (pg,
229 : &coin.coin_pub,
230 : &denom_pub,
231 : &denom_sig),
232 : TDB_coin_free (&coin); TDB_denom_free (&denom));
233 1 : FAILIF_C (0 != TALER_denom_pub_cmp (&denom_pub,
234 : &denom.pub),
235 : TALER_denom_pub_free (&denom_pub);
236 : TALER_denom_sig_free (&denom_sig);
237 : TDB_coin_free (&coin); TDB_denom_free (&denom));
238 1 : FAILIF_C (0 != TALER_denom_sig_cmp (&denom_sig,
239 : &coin.denom_sig),
240 : TALER_denom_pub_free (&denom_pub);
241 : TALER_denom_sig_free (&denom_sig);
242 : TDB_coin_free (&coin); TDB_denom_free (&denom));
243 1 : TALER_denom_pub_free (&denom_pub);
244 1 : TALER_denom_sig_free (&denom_sig);
245 :
246 : /* the same in one lookup, together with the (absent) age commitment */
247 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
248 : TALER_EXCHANGEDB_get_denomination_proof_for_known_coin (
249 : pg,
250 : &coin.coin_pub,
251 : &denom_pub,
252 : &denom_sig,
253 : &h_age_commitment,
254 : &no_age_commitment),
255 : TDB_coin_free (&coin); TDB_denom_free (&denom));
256 1 : FAILIF_C ( (0 != TALER_denom_pub_cmp (&denom_pub,
257 : &denom.pub)) ||
258 : (0 != TALER_denom_sig_cmp (&denom_sig,
259 : &coin.denom_sig)) ||
260 : (! no_age_commitment),
261 : TALER_denom_pub_free (&denom_pub);
262 : TALER_denom_sig_free (&denom_sig);
263 : TDB_coin_free (&coin); TDB_denom_free (&denom));
264 1 : TALER_denom_pub_free (&denom_pub);
265 1 : TALER_denom_sig_free (&denom_sig);
266 :
267 1 : FAILIF_C (1 != TALER_EXCHANGEDB_get_count_known_coins (pg,
268 : &denom.h_denom_pub),
269 : TDB_coin_free (&coin); TDB_denom_free (&denom));
270 1 : TDB_coin_free (&coin);
271 1 : TDB_denom_free (&denom);
272 1 : return 0;
273 : }
274 :
275 :
276 : /**
277 : * Re-inserting the same coin is a no-op; re-inserting it under a different
278 : * denomination or with an age commitment is a conflict.
279 : *
280 : * @param pg the database context
281 : * @return 0 on success
282 : */
283 : static int
284 1 : check_conflicts (struct TALER_EXCHANGEDB_PostgresContext *pg)
285 : {
286 : struct TDB_Denom denom;
287 : struct TDB_Denom other;
288 : struct TALER_CoinPublicInfo coin;
289 : struct TALER_EXCHANGEDB_CoinKnownResult res;
290 :
291 1 : TDB_denom (pg,
292 : 10,
293 : "5",
294 : "0.1",
295 : &denom);
296 1 : TDB_denom (pg,
297 : 11,
298 : "5",
299 : "0.1",
300 : &other);
301 1 : memset (&coin,
302 : 0,
303 : sizeof (coin));
304 1 : TDB_FILL (coin.coin_pub,
305 : 20);
306 1 : coin.denom_pub_hash = denom.h_denom_pub;
307 1 : coin.no_age_commitment = true;
308 1 : TDB_denom_sig (20,
309 : &coin.denom_sig);
310 :
311 : /* the coin from the previous check: already present, no conflict */
312 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
313 : insert_one (pg,
314 : &coin,
315 : &res),
316 : TALER_denom_sig_free (&coin.denom_sig);
317 : TDB_denom_free (&denom); TDB_denom_free (&other));
318 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_PRESENT != res.status,
319 : TALER_denom_sig_free (&coin.denom_sig);
320 : TDB_denom_free (&denom); TDB_denom_free (&other));
321 1 : FAILIF_C (0 == res.known_coin_id,
322 : TALER_denom_sig_free (&coin.denom_sig);
323 : TDB_denom_free (&denom); TDB_denom_free (&other));
324 1 : FAILIF_C (! res.no_age_commitment,
325 : TALER_denom_sig_free (&coin.denom_sig);
326 : TDB_denom_free (&denom); TDB_denom_free (&other));
327 :
328 : /* same coin key, different denomination: conflict, and the stored
329 : denomination is handed back so the caller can report it */
330 1 : coin.denom_pub_hash = other.h_denom_pub;
331 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
332 : insert_one (pg,
333 : &coin,
334 : &res),
335 : TALER_denom_sig_free (&coin.denom_sig);
336 : TDB_denom_free (&denom); TDB_denom_free (&other));
337 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_DENOM_CONFLICT != res.status,
338 : TALER_denom_sig_free (&coin.denom_sig);
339 : TDB_denom_free (&denom); TDB_denom_free (&other));
340 1 : FAILIF_C (0 != GNUNET_memcmp (&res.h_denom_pub,
341 : &denom.h_denom_pub),
342 : TALER_denom_sig_free (&coin.denom_sig);
343 : TDB_denom_free (&denom); TDB_denom_free (&other));
344 :
345 : /* same coin key and denomination, but now with an age commitment where
346 : the stored row has none: the caller should have passed none */
347 1 : coin.denom_pub_hash = denom.h_denom_pub;
348 1 : coin.no_age_commitment = false;
349 1 : TDB_FILL (coin.h_age_commitment,
350 : 21);
351 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
352 : insert_one (pg,
353 : &coin,
354 : &res),
355 : TALER_denom_sig_free (&coin.denom_sig);
356 : TDB_denom_free (&denom); TDB_denom_free (&other));
357 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_AGE_CONFLICT_EXPECTED_NULL != res.status,
358 : TALER_denom_sig_free (&coin.denom_sig);
359 : TDB_denom_free (&denom); TDB_denom_free (&other));
360 1 : TALER_denom_sig_free (&coin.denom_sig);
361 :
362 : /* the conflicting attempts did not add anything */
363 1 : FAILIF_C (1 != TALER_EXCHANGEDB_get_count_known_coins (pg,
364 : &denom.h_denom_pub),
365 : TDB_denom_free (&denom); TDB_denom_free (&other));
366 1 : FAILIF_C (0 != TALER_EXCHANGEDB_get_count_known_coins (pg,
367 : &other.h_denom_pub),
368 : TDB_denom_free (&denom); TDB_denom_free (&other));
369 1 : TDB_denom_free (&denom);
370 1 : TDB_denom_free (&other);
371 1 : return 0;
372 : }
373 :
374 :
375 : /**
376 : * A coin with an age commitment round-trips, and the mirror-image
377 : * conflict is reported.
378 : *
379 : * @param pg the database context
380 : * @return 0 on success
381 : */
382 : static int
383 1 : check_age_commitment (struct TALER_EXCHANGEDB_PostgresContext *pg)
384 : {
385 : struct TDB_Denom denom;
386 : struct TALER_CoinPublicInfo coin;
387 : struct TALER_CoinPublicInfo got;
388 : struct TALER_EXCHANGEDB_CoinKnownResult res;
389 1 : struct TALER_DenominationPublicKey denom_pub = { 0 };
390 1 : struct TALER_DenominationSignature denom_sig = { 0 };
391 : struct TALER_AgeCommitmentHashP h_age_commitment;
392 1 : bool no_age_commitment = true;
393 :
394 1 : TDB_denom (pg,
395 : 10,
396 : "5",
397 : "0.1",
398 : &denom);
399 1 : memset (&coin,
400 : 0,
401 : sizeof (coin));
402 1 : TDB_FILL (coin.coin_pub,
403 : 30);
404 1 : coin.denom_pub_hash = denom.h_denom_pub;
405 1 : coin.no_age_commitment = false;
406 1 : TDB_FILL (coin.h_age_commitment,
407 : 31);
408 1 : TDB_denom_sig (30,
409 : &coin.denom_sig);
410 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
411 : insert_one (pg,
412 : &coin,
413 : &res),
414 : TALER_denom_sig_free (&coin.denom_sig);
415 : TDB_denom_free (&denom));
416 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_ADDED != res.status,
417 : TALER_denom_sig_free (&coin.denom_sig);
418 : TDB_denom_free (&denom));
419 1 : FAILIF_C (0 == res.known_coin_id,
420 : TALER_denom_sig_free (&coin.denom_sig);
421 : TDB_denom_free (&denom));
422 1 : memset (&got,
423 : 0,
424 : sizeof (got));
425 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
426 : TALER_EXCHANGEDB_get_known_coin (pg,
427 : &coin.coin_pub,
428 : &got),
429 : TALER_denom_sig_free (&coin.denom_sig);
430 : TDB_denom_free (&denom));
431 1 : FAILIF_C (got.no_age_commitment,
432 : TALER_denom_sig_free (&got.denom_sig);
433 : TALER_denom_sig_free (&coin.denom_sig);
434 : TDB_denom_free (&denom));
435 1 : FAILIF_C (0 != GNUNET_memcmp (&got.h_age_commitment,
436 : &coin.h_age_commitment),
437 : TALER_denom_sig_free (&got.denom_sig);
438 : TALER_denom_sig_free (&coin.denom_sig);
439 : TDB_denom_free (&denom));
440 1 : TALER_denom_sig_free (&got.denom_sig);
441 : /* the age commitment hash is part of the proof of the denomination */
442 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
443 : TALER_EXCHANGEDB_get_denomination_proof_for_known_coin (
444 : pg,
445 : &coin.coin_pub,
446 : &denom_pub,
447 : &denom_sig,
448 : &h_age_commitment,
449 : &no_age_commitment),
450 : TALER_denom_sig_free (&coin.denom_sig);
451 : TDB_denom_free (&denom));
452 1 : TALER_denom_pub_free (&denom_pub);
453 1 : TALER_denom_sig_free (&denom_sig);
454 1 : FAILIF_C ( (no_age_commitment) ||
455 : (0 != GNUNET_memcmp (&h_age_commitment,
456 : &coin.h_age_commitment)),
457 : TALER_denom_sig_free (&coin.denom_sig);
458 : TDB_denom_free (&denom));
459 :
460 : /* a different age commitment for the same coin is a conflict... */
461 1 : TDB_FILL (coin.h_age_commitment,
462 : 32);
463 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
464 : insert_one (pg,
465 : &coin,
466 : &res),
467 : TALER_denom_sig_free (&coin.denom_sig);
468 : TDB_denom_free (&denom));
469 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_AGE_CONFLICT_VALUE_DIFFERS != res.status,
470 : TALER_denom_sig_free (&coin.denom_sig);
471 : TDB_denom_free (&denom));
472 : /* the stored age commitment is handed back for the error report */
473 1 : FAILIF_C (res.no_age_commitment,
474 : TALER_denom_sig_free (&coin.denom_sig);
475 : TDB_denom_free (&denom));
476 1 : FAILIF_C (0 != GNUNET_memcmp (&res.h_age_commitment,
477 : &got.h_age_commitment),
478 : TALER_denom_sig_free (&coin.denom_sig);
479 : TDB_denom_free (&denom));
480 : /* ...and so is no age commitment at all, where one is on file */
481 1 : coin.no_age_commitment = true;
482 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
483 : insert_one (pg,
484 : &coin,
485 : &res),
486 : TALER_denom_sig_free (&coin.denom_sig);
487 : TDB_denom_free (&denom));
488 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_AGE_CONFLICT_EXPECTED_NON_NULL != res.status,
489 : TALER_denom_sig_free (&coin.denom_sig);
490 : TDB_denom_free (&denom));
491 1 : TALER_denom_sig_free (&coin.denom_sig);
492 :
493 : /* two coins of this denomination by now */
494 1 : FAILIF_C (2 != TALER_EXCHANGEDB_get_count_known_coins (pg,
495 : &denom.h_denom_pub),
496 : TDB_denom_free (&denom));
497 1 : TDB_denom_free (&denom);
498 1 : return 0;
499 : }
500 :
501 :
502 : /**
503 : * One call with a mixed batch: two new coins (one with an age
504 : * commitment), one already known, one with a conflicting denomination
505 : * and one with a conflicting age commitment. Every coin must be
506 : * reported at its own position, the new ones must be inserted, and a
507 : * batch with a repeated coin or an unknown denomination is refused
508 : * without inserting anything.
509 : *
510 : * @param pg the database context
511 : * @return 0 on success
512 : */
513 : static int
514 1 : check_batch (struct TALER_EXCHANGEDB_PostgresContext *pg)
515 : {
516 : struct TDB_Denom denom;
517 : struct TDB_Denom other;
518 : struct TALER_CoinPublicInfo coins[5];
519 : const struct TALER_CoinPublicInfo *pcoins[5];
520 : struct TALER_EXCHANGEDB_CoinKnownResult res[5];
521 : struct TALER_DenominationHashP dh;
522 : uint64_t id;
523 : uint64_t count_before;
524 1 : int ret = 1;
525 :
526 1 : TDB_denom (pg,
527 : 10,
528 : "5",
529 : "0.1",
530 : &denom);
531 1 : TDB_denom (pg,
532 : 11,
533 : "5",
534 : "0.1",
535 : &other);
536 1 : memset (coins,
537 : 0,
538 : sizeof (coins));
539 6 : for (unsigned int i = 0; i < 5; i++)
540 : {
541 5 : coins[i].no_age_commitment = true;
542 5 : coins[i].denom_pub_hash = denom.h_denom_pub;
543 5 : pcoins[i] = &coins[i];
544 : }
545 : /* [0]: new, no age commitment */
546 1 : TDB_FILL (coins[0].coin_pub,
547 : 40);
548 1 : TDB_denom_sig (40,
549 : &coins[0].denom_sig);
550 : /* [1]: new, with age commitment */
551 1 : TDB_FILL (coins[1].coin_pub,
552 : 41);
553 1 : coins[1].no_age_commitment = false;
554 1 : TDB_FILL (coins[1].h_age_commitment,
555 : 410);
556 1 : TDB_denom_sig (41,
557 : &coins[1].denom_sig);
558 : /* [2]: known from check_insert_and_lookup (seed 20), same data */
559 1 : TDB_FILL (coins[2].coin_pub,
560 : 20);
561 1 : TDB_denom_sig (20,
562 : &coins[2].denom_sig);
563 : /* [3]: known (seed 20 again, different key below), other denomination */
564 1 : TDB_FILL (coins[3].coin_pub,
565 : 20);
566 1 : coins[3].denom_pub_hash = other.h_denom_pub;
567 1 : TDB_denom_sig (20,
568 : &coins[3].denom_sig);
569 : /* [4]: known from check_age_commitment (seed 30), wrong age commitment */
570 1 : TDB_FILL (coins[4].coin_pub,
571 : 30);
572 1 : coins[4].no_age_commitment = false;
573 1 : TDB_FILL (coins[4].h_age_commitment,
574 : 33);
575 1 : TDB_denom_sig (30,
576 : &coins[4].denom_sig);
577 :
578 1 : count_before = TDB_count (pg,
579 : "FROM known_coins");
580 :
581 : /* [2] and [3] share a key: refused, nothing inserted */
582 1 : FAILIF_C (GNUNET_DB_STATUS_HARD_ERROR !=
583 : TALER_EXCHANGEDB_do_insert_known_coins (pg,
584 : 5,
585 : pcoins,
586 : res),
587 : goto cleanup);
588 1 : FAILIF_C (count_before != TDB_count (pg,
589 : "FROM known_coins"),
590 : goto cleanup);
591 :
592 : /* give [3] its own key: a coin known under the other denomination */
593 : {
594 : struct TALER_EXCHANGEDB_CoinKnownResult r1;
595 1 : struct TALER_CoinPublicInfo tmp = coins[3];
596 :
597 1 : TDB_FILL (tmp.coin_pub,
598 : 43);
599 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
600 : insert_one (pg,
601 : &tmp,
602 : &r1),
603 : goto cleanup);
604 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_ADDED != r1.status,
605 : goto cleanup);
606 1 : count_before++;
607 1 : TDB_FILL (coins[3].coin_pub,
608 : 43);
609 1 : coins[3].denom_pub_hash = denom.h_denom_pub;
610 : }
611 :
612 : /* a new coin of an unknown denomination anywhere in the batch: the
613 : batch is refused. The INSERT itself is one statement, so this is
614 : done inside a transaction as the callers do it, and the rollback
615 : removes the other new coin again. */
616 1 : TDB_FILL (coins[0].denom_pub_hash,
617 : 44);
618 1 : FAILIF_C (GNUNET_OK !=
619 : TALER_EXCHANGEDB_start (pg,
620 : "test unknown denomination"),
621 : goto cleanup);
622 1 : FAILIF_C (GNUNET_DB_STATUS_HARD_ERROR !=
623 : TALER_EXCHANGEDB_do_insert_known_coins (pg,
624 : 5,
625 : pcoins,
626 : res),
627 : TALER_EXCHANGEDB_rollback (pg); goto cleanup);
628 1 : TALER_EXCHANGEDB_rollback (pg);
629 1 : FAILIF_C (count_before != TDB_count (pg,
630 : "FROM known_coins"),
631 : goto cleanup);
632 1 : coins[0].denom_pub_hash = denom.h_denom_pub;
633 :
634 : /* now the mixed batch goes through, with one row per coin */
635 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
636 : TALER_EXCHANGEDB_do_insert_known_coins (pg,
637 : 5,
638 : pcoins,
639 : res),
640 : goto cleanup);
641 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_ADDED != res[0].status,
642 : goto cleanup);
643 1 : FAILIF_C (0 == res[0].known_coin_id,
644 : goto cleanup);
645 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_ADDED != res[1].status,
646 : goto cleanup);
647 1 : FAILIF_C (0 == res[1].known_coin_id,
648 : goto cleanup);
649 : /* for added coins, the result reflects what was stored */
650 1 : FAILIF_C (! res[0].no_age_commitment,
651 : goto cleanup);
652 1 : FAILIF_C (0 != GNUNET_memcmp (&res[0].h_denom_pub,
653 : &denom.h_denom_pub),
654 : goto cleanup);
655 1 : FAILIF_C (res[1].no_age_commitment,
656 : goto cleanup);
657 1 : FAILIF_C (0 != GNUNET_memcmp (&res[1].h_age_commitment,
658 : &coins[1].h_age_commitment),
659 : goto cleanup);
660 1 : FAILIF_C (res[0].known_coin_id == res[1].known_coin_id,
661 : goto cleanup);
662 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_PRESENT != res[2].status,
663 : goto cleanup);
664 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
665 : TALER_EXCHANGEDB_get_coin_denomination (pg,
666 : &coins[2].coin_pub,
667 : &id,
668 : &dh),
669 : goto cleanup);
670 1 : FAILIF_C (id != res[2].known_coin_id,
671 : goto cleanup);
672 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_DENOM_CONFLICT != res[3].status,
673 : goto cleanup);
674 1 : FAILIF_C (0 != GNUNET_memcmp (&res[3].h_denom_pub,
675 : &other.h_denom_pub),
676 : goto cleanup);
677 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_AGE_CONFLICT_VALUE_DIFFERS != res[4].status,
678 : goto cleanup);
679 1 : FAILIF_C (res[4].no_age_commitment,
680 : goto cleanup);
681 1 : FAILIF_C (0 == GNUNET_memcmp (&res[4].h_age_commitment,
682 : &coins[4].h_age_commitment),
683 : goto cleanup);
684 : /* exactly the two new coins were added */
685 1 : FAILIF_C (count_before + 2 != TDB_count (pg,
686 : "FROM known_coins"),
687 : goto cleanup);
688 : {
689 : struct TALER_CoinPublicInfo got;
690 :
691 1 : memset (&got,
692 : 0,
693 : sizeof (got));
694 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
695 : TALER_EXCHANGEDB_get_known_coin (pg,
696 : &coins[1].coin_pub,
697 : &got),
698 : goto cleanup);
699 1 : FAILIF_C (got.no_age_commitment,
700 : TALER_denom_sig_free (&got.denom_sig); goto cleanup);
701 1 : FAILIF_C (0 != GNUNET_memcmp (&got.h_age_commitment,
702 : &coins[1].h_age_commitment),
703 : TALER_denom_sig_free (&got.denom_sig); goto cleanup);
704 1 : FAILIF_C (0 != TALER_denom_sig_cmp (&got.denom_sig,
705 : &coins[1].denom_sig),
706 : TALER_denom_sig_free (&got.denom_sig); goto cleanup);
707 1 : TALER_denom_sig_free (&got.denom_sig);
708 : }
709 :
710 : /* running the same batch again changes nothing: the new coins are
711 : now merely present, the conflicts are reported as before */
712 1 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
713 : TALER_EXCHANGEDB_do_insert_known_coins (pg,
714 : 5,
715 : pcoins,
716 : res),
717 : goto cleanup);
718 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_PRESENT != res[0].status,
719 : goto cleanup);
720 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_PRESENT != res[1].status,
721 : goto cleanup);
722 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_PRESENT != res[2].status,
723 : goto cleanup);
724 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_DENOM_CONFLICT != res[3].status,
725 : goto cleanup);
726 1 : FAILIF_C (TALER_EXCHANGEDB_CKS_AGE_CONFLICT_VALUE_DIFFERS != res[4].status,
727 : goto cleanup);
728 1 : FAILIF_C (count_before + 2 != TDB_count (pg,
729 : "FROM known_coins"),
730 : goto cleanup);
731 1 : ret = 0;
732 1 : cleanup:
733 6 : for (unsigned int i = 0; i < 5; i++)
734 5 : TALER_denom_sig_free (&coins[i].denom_sig);
735 1 : TDB_denom_free (&denom);
736 1 : TDB_denom_free (&other);
737 1 : return ret;
738 : }
739 :
740 :
741 : /**
742 : * Both ways out of do_insert_known_coins() release the closures its
743 : * query parameters allocate. Nothing to assert here: the leak shows up
744 : * when the test runs under valgrind (meson test --setup=valgrind).
745 : *
746 : * @param pg the database context
747 : * @return 0 on success
748 : */
749 : static int
750 1 : check_no_leak (struct TALER_EXCHANGEDB_PostgresContext *pg)
751 : {
752 : struct TDB_Denom denom;
753 : struct TALER_CoinPublicInfo coins[2];
754 : const struct TALER_CoinPublicInfo *pcoins[2];
755 : const struct TALER_CoinPublicInfo *dups[2];
756 : struct TALER_EXCHANGEDB_CoinKnownResult res[2];
757 1 : int ret = 1;
758 :
759 1 : TDB_denom (pg,
760 : 10,
761 : "5",
762 : "0.1",
763 : &denom);
764 1 : memset (coins,
765 : 0,
766 : sizeof (coins));
767 3 : for (unsigned int i = 0; i < 2; i++)
768 : {
769 2 : coins[i].no_age_commitment = true;
770 2 : coins[i].denom_pub_hash = denom.h_denom_pub;
771 2 : TDB_FILL (coins[i].coin_pub,
772 : 50 + i);
773 2 : TDB_denom_sig (50 + i,
774 : &coins[i].denom_sig);
775 2 : pcoins[i] = &coins[i];
776 2 : dups[i] = &coins[0];
777 : }
778 : /* the batch goes through: first inserted, then found */
779 3 : for (unsigned int i = 0; i < 2; i++)
780 : {
781 2 : FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
782 : TALER_EXCHANGEDB_do_insert_known_coins (pg,
783 : 2,
784 : pcoins,
785 : res),
786 : goto cleanup);
787 2 : FAILIF_C ( (i == 0)
788 : ? (TALER_EXCHANGEDB_CKS_ADDED != res[1].status)
789 : : (TALER_EXCHANGEDB_CKS_PRESENT != res[1].status),
790 : goto cleanup);
791 : }
792 : /* the batch is refused before the query for repeating a coin */
793 1 : FAILIF_C (GNUNET_DB_STATUS_HARD_ERROR !=
794 : TALER_EXCHANGEDB_do_insert_known_coins (pg,
795 : 2,
796 : dups,
797 : res),
798 : goto cleanup);
799 1 : ret = 0;
800 1 : cleanup:
801 3 : for (unsigned int i = 0; i < 2; i++)
802 2 : TALER_denom_sig_free (&coins[i].denom_sig);
803 1 : TDB_denom_free (&denom);
804 1 : return ret;
805 : }
806 :
807 :
808 : /**
809 : * The checks to run, in order.
810 : */
811 : static const struct TDB_Test tests[] = {
812 : { "known-coins-empty",
813 : &check_empty },
814 : { "known-coins-unknown-denomination",
815 : &check_unknown_denomination },
816 : { "known-coins-insert-and-lookup",
817 : &check_insert_and_lookup },
818 : { "known-coins-conflicts",
819 : &check_conflicts },
820 : { "known-coins-age-commitment",
821 : &check_age_commitment },
822 : { "known-coins-batch",
823 : &check_batch },
824 : { "known-coins-no-leak",
825 : &check_no_leak },
826 : { NULL, NULL }
827 : };
828 :
829 :
830 : int
831 1 : main (int argc,
832 : char *const *argv)
833 : {
834 1 : return TDB_main (argc,
835 : argv,
836 : "test-known-coins",
837 : "Tests for the exchangedb `known_coins' table",
838 : tests);
839 : }
840 :
841 :
842 : /* end of test_known_coins.c */
|