summaryrefslogtreecommitdiff
path: root/src/stasis/test_anastasis_db.c
blob: 8f118271043d2833cbd51b5be6f9e45c2a254b9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
  This file is part of Anastasis
  Copyright (C) 2020, 2021 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file anastasis/test_anastasis_db.c
 * @brief testcase for anastasis postgres db plugin
 * @author Marcello Stanisci
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis_database_lib.h"
#include "anastasis_util_lib.h"
#include <gnunet/gnunet_signatures.h>


#define FAILIF(cond)                            \
  do {                                          \
    if (! (cond)) { break;}                       \
    GNUNET_break (0);                           \
    goto drop;                                     \
  } while (0)

#define RND_BLK(ptr)                                                    \
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, ptr, sizeof (*ptr))

/**
 * Global return value for the test.  Initially -1, set to 0 upon
 * completion.   Other values indicate some kind of error.
 */
static int result;

/**
 * Handle to the plugin we are testing.
 */
static struct ANASTASIS_DatabasePlugin *plugin;


/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure with config
 */
static void
run (void *cls)
{
  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  struct TALER_Amount amount;
  struct ANASTASIS_PaymentSecretP paymentSecretP;
  struct ANASTASIS_CRYPTO_AccountPublicKeyP accountPubP;
  struct ANASTASIS_AccountSignatureP accountSig;
  struct ANASTASIS_AccountSignatureP res_account_sig;
  struct GNUNET_HashCode recoveryDataHash;
  struct GNUNET_HashCode res_recovery_data_hash;
  struct GNUNET_HashCode r;
  struct GNUNET_TIME_Relative rel_time;
  struct ANASTASIS_CRYPTO_TruthUUIDP truth_uuid;
  struct ANASTASIS_CRYPTO_EncryptedKeyShareP key_share;
  unsigned int post_counter;
  char *mime_type;
  char *method;
  uint32_t docVersion;
  uint32_t res_version;
  size_t recoverydatasize;
  void *res_recovery_data = NULL;
  struct ANASTASIS_CRYPTO_EncryptedKeyShareP res_key_share;
  bool paid;
  bool valid_counter;
  uint32_t recversion = 1;
  unsigned char aes_gcm_tag[16];
  const char *recovery_data = "RECOVERY_DATA";
  uint64_t challenge_code = 1234;
  struct GNUNET_HashCode c_hash;
  struct ANASTASIS_UploadSignaturePS usp = {
    .purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST),
    .purpose.size = htonl (sizeof (usp))
  };

  if (NULL == (plugin = ANASTASIS_DB_plugin_load (cfg)))
  {
    result = 77;
    return;
  }
  if (GNUNET_OK != plugin->drop_tables (plugin->cls))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Dropping tables failed\n");
    result = 77;
    return;
  }
  ANASTASIS_DB_plugin_unload (plugin);
  if (NULL == (plugin = ANASTASIS_DB_plugin_load (cfg)))
  {
    result = 77;
    return;
  }

  GNUNET_CRYPTO_hash (recovery_data,
                      strlen (recovery_data),
                      &recoveryDataHash);
  RND_BLK (&paymentSecretP);
  RND_BLK (&aes_gcm_tag);
  post_counter = 2;
  mime_type = "Picture";
  method = "Method";
  TALER_string_to_amount ("EUR:30",&amount);

  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
                              &truth_uuid,
                              sizeof (truth_uuid));
  rel_time = GNUNET_TIME_UNIT_MONTHS;

  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:1",
                                         &amount));

  memset (&key_share, 1, sizeof (key_share));
  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
          plugin->store_truth (plugin->cls,
                               &truth_uuid,
                               &key_share,
                               mime_type,
                               "encrypted_truth",
                               strlen ("encrypted_truth"),
                               method,
                               rel_time));

  FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
          plugin->check_payment_identifier (plugin->cls,
                                            &paymentSecretP,
                                            &paid,
                                            &valid_counter));

  memset (&accountPubP, 2, sizeof (accountPubP));
  memset (&accountSig, 3, sizeof (accountSig));
  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
          plugin->record_recdoc_payment (plugin->cls,
                                         &accountPubP,
                                         post_counter,
                                         &paymentSecretP,
                                         &amount));
  {
    struct GNUNET_TIME_Absolute res_time;

    FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
            plugin->increment_lifetime (plugin->cls,
                                        &accountPubP,
                                        &paymentSecretP,
                                        rel_time,
                                        &res_time));
  }
  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
          plugin->check_payment_identifier (plugin->cls,
                                            &paymentSecretP,
                                            &paid,
                                            &valid_counter));
  FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
          plugin->check_challenge_payment (plugin->cls,
                                           &paymentSecretP,
                                           &truth_uuid,
                                           &paid));
  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
          plugin->record_challenge_payment (plugin->cls,
                                            &truth_uuid,
                                            &paymentSecretP,
                                            &amount));
  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
          plugin->update_challenge_payment (plugin->cls,
                                            &truth_uuid,
                                            &paymentSecretP));
  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
          plugin->check_challenge_payment (plugin->cls,
                                           &paymentSecretP,
                                           &truth_uuid,
                                           &paid));
  FAILIF (! paid);
  FAILIF (ANASTASIS_DB_STORE_STATUS_SUCCESS !=
          plugin->store_recovery_document (plugin->cls,
                                           &accountPubP,
                                           &accountSig,
                                           &recoveryDataHash,
                                           recovery_data,
                                           strlen (recovery_data),
                                           &paymentSecretP,
                                           &docVersion));
  {
    uint32_t vrs;
    struct GNUNET_TIME_Absolute exp;

    FAILIF (ANASTASIS_DB_ACCOUNT_STATUS_VALID_HASH_RETURNED !=
            plugin->lookup_account (plugin->cls,
                                    &accountPubP,
                                    &exp,
                                    &r,
                                    &vrs));
  }
  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
          plugin->get_key_share (plugin->cls,
                                 &truth_uuid,
                                 &res_key_share));
  FAILIF (0 !=
          GNUNET_memcmp (&res_key_share,
                         &key_share));

  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
          plugin->get_recovery_document (plugin->cls,
                                         &accountPubP,
                                         recversion,
                                         &res_account_sig,
                                         &res_recovery_data_hash,
                                         &recoverydatasize,
                                         &res_recovery_data));
  FAILIF (0 != memcmp (res_recovery_data,
                       recovery_data,
                       strlen (recovery_data)));
  GNUNET_free (res_recovery_data);

  FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
          plugin->get_latest_recovery_document (plugin->cls,
                                                &accountPubP,
                                                &res_account_sig,
                                                &res_recovery_data_hash,
                                                &recoverydatasize,
                                                &res_recovery_data,
                                                &res_version));
  FAILIF (0 != memcmp (res_recovery_data,
                       recovery_data,
                       strlen (recovery_data)));
  GNUNET_free (res_recovery_data);

  {
    struct GNUNET_TIME_Absolute rt;

    FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
            plugin->create_challenge_code (plugin->cls,
                                           &truth_uuid,
                                           GNUNET_TIME_UNIT_HOURS,
                                           GNUNET_TIME_UNIT_DAYS,
                                           3, /* retry counter */
                                           &rt,
                                           &challenge_code));
    FAILIF (0 != rt.abs_value_us);
  }
  {
    struct GNUNET_TIME_Absolute rt;
    uint64_t c2;

    FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
            plugin->create_challenge_code (plugin->cls,
                                           &truth_uuid,
                                           GNUNET_TIME_UNIT_HOURS,
                                           GNUNET_TIME_UNIT_DAYS,
                                           3, /* retry counter */
                                           &rt,
                                           &c2));
    FAILIF (c2 != challenge_code);
  }
  ANASTASIS_hash_answer (123,
                         &c_hash);
  {
    bool sat;

    FAILIF (ANASTASIS_DB_CODE_STATUS_CHALLENGE_CODE_MISMATCH !=
            plugin->verify_challenge_code (plugin->cls,
                                           &truth_uuid,
                                           &c_hash,
                                           &sat));

    ANASTASIS_hash_answer (challenge_code,
                           &c_hash);
    FAILIF (ANASTASIS_DB_CODE_STATUS_VALID_CODE_STORED !=
            plugin->verify_challenge_code (plugin->cls,
                                           &truth_uuid,
                                           &c_hash,
                                           &sat));
  }
  if (-1 == result)
    result = 0;

drop:
  GNUNET_break (GNUNET_OK ==
                plugin->drop_tables (plugin->cls));
  ANASTASIS_DB_plugin_unload (plugin);
  if (NULL != plugin)
  {
    plugin = NULL;
  }
}


int
main (int argc,
      char *const argv[])
{
  const char *plugin_name;
  char *config_filename;
  char *testname;
  struct GNUNET_CONFIGURATION_Handle *cfg;

  result = -1;
  if (NULL == (plugin_name = strrchr (argv[0], (int) '-')))
  {
    GNUNET_break (0);
    return -1;
  }
  /* FIRST get the libtalerutil initialization out
      of the way. Then throw that one away, and force
      the SYNC defaults to be used! */
  (void) TALER_project_data_default ();
  GNUNET_OS_init (ANASTASIS_project_data_default ());
  GNUNET_log_setup (argv[0], "DEBUG", NULL);
  plugin_name++;
  GNUNET_asprintf (&testname,
                   "%s",
                   plugin_name);
  GNUNET_asprintf (&config_filename,
                   "test_anastasis_db_%s.conf",
                   testname);
  cfg = GNUNET_CONFIGURATION_create ();
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_load (cfg,
                                 config_filename))
  {
    GNUNET_break (0);
    GNUNET_free (config_filename);
    GNUNET_free (testname);
    return 2;
  }
  GNUNET_SCHEDULER_run (&run,
                        cfg);
  GNUNET_CONFIGURATION_destroy (cfg);
  GNUNET_free (config_filename);
  GNUNET_free (testname);
  return result;
}


/* end of test_anastasis_db.c */