summaryrefslogtreecommitdiff
path: root/src/backenddb/test_merchantdb.c
blob: 9940c5ea6c090b5f96118f707027c60b1a5d0b96 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
/*
  This file is part of TALER
  (C) 2014-2017, 2020 Taler Systems SA

  TALER 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.

  TALER 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 General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file merchant/test_merchantdb.c
 * @brief testcase for merchant's postgres db plugin
 * @author Marcello Stanisci
 * @author Christian Grothoff
 */
#include "platform.h"
#include <taler/taler_util.h>
#include <taler/taler_json_lib.h>
#include "taler_merchantdb_lib.h"


/**
 * 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 TALER_MERCHANTDB_Plugin *plugin;

#define TEST_RET_ON_FAIL(test) if (0 != test) \
  { \
    return 1; \
  }


/**
 * Instance settings along with corresponding accounts
 */
struct InstanceWithAccounts
{
  /**
   * Pointer to the instance settings
   */
  const struct TALER_MERCHANTDB_InstanceSettings *instance;

  /**
   * Length of the array of accounts
   */
  unsigned int accounts_length;

  /**
   * Pointer to the array of accounts
   */
  const struct TALER_MERCHANTDB_AccountDetails *accounts;

};


/**
 * Closure for testing instance lookup
 */
struct TestLookupInstances_Closure
{
  /**
   * Number of instances to compare to
   */
  unsigned int instances_to_cmp_length;

  /**
   * Pointer to array of instances
   */
  const struct InstanceWithAccounts *instances_to_cmp;

  /**
   * Pointer to array of number of matches for each instance
   */
  unsigned int *results_matching;

  /**
   * Total number of results returned
   */
  unsigned int results_length;
};


static int
check_instances_equal (const struct TALER_MERCHANTDB_InstanceSettings *a,
                       const struct TALER_MERCHANTDB_InstanceSettings *b)
{
  if ((0 != strcmp (a->id,
                    b->id)) ||
      (0 != strcmp (a->name,
                    b->name)) ||
      (1 != json_equal (a->address,
                        b->address)) ||
      (1 != json_equal (a->jurisdiction,
                        b->jurisdiction)) ||
      (GNUNET_OK != TALER_amount_cmp_currency (&a->default_max_deposit_fee,
                                               &b->default_max_deposit_fee)) ||
      (0 != TALER_amount_cmp (&a->default_max_deposit_fee,
                              &b->default_max_deposit_fee)) ||
      (GNUNET_OK != TALER_amount_cmp_currency (&a->default_max_wire_fee,
                                               &b->default_max_wire_fee)) ||
      (0 != TALER_amount_cmp (&a->default_max_wire_fee,
                              &b->default_max_wire_fee)) ||
      (a->default_wire_fee_amortization != b->default_wire_fee_amortization) ||
      (a->default_wire_transfer_delay.rel_value_us !=
       b->default_wire_transfer_delay.rel_value_us) ||
      (a->default_pay_delay.rel_value_us != b->default_pay_delay.rel_value_us))
    return 1;
  return 0;
}


static int
check_accounts_equal (const struct TALER_MERCHANTDB_AccountDetails *a,
                      const struct TALER_MERCHANTDB_AccountDetails *b)
{
  if ((0 != GNUNET_CRYPTO_hash_cmp (&a->h_wire,
                                    &b->h_wire)) ||
      (0 != GNUNET_CRYPTO_hash_cmp (&a->salt,
                                    &b->salt)) ||
      (0 != strcmp (a->payto_uri,
                    b->payto_uri)) ||
      (a->active != b->active))
    return 1;
  return 0;
}


static void
lookup_instances_cb (void *cls,
                     const struct TALER_MerchantPublicKeyP *merchant_pub,
                     const struct TALER_MerchantPrivateKeyP *merchant_priv,
                     const struct TALER_MERCHANTDB_InstanceSettings *is,
                     unsigned int accounts_length,
                     const struct TALER_MERCHANTDB_AccountDetails accounts[])
{
  if (cls == NULL)
    return;
  struct TestLookupInstances_Closure *cmp = cls;
  cmp->results_length += 1;
  /* Look through the closure and test each instance for equality */
  for (unsigned int i = 0; cmp->instances_to_cmp_length > i; ++i)
  {
    if (0 != check_instances_equal (cmp->instances_to_cmp[i].instance,
                                    is))
      continue;
    if (accounts_length != cmp->instances_to_cmp[i].accounts_length)
      continue;
    /* Count matches between the accounts found and accounts in cls */
    int accounts_matching[accounts_length];
    for (unsigned int j = 0; accounts_length > j; ++j)
      accounts_matching[j] = 0;
    for (unsigned int j = 0; accounts_length > j; ++j)
    {
      for (unsigned int k = 0; accounts_length > k; ++k)
      {
        if (0 == check_accounts_equal (&cmp->instances_to_cmp[i].accounts[j],
                                       &accounts[k]))
          accounts_matching[j] += 1;
      }
    }
    /* Each account from the lookup should match with one and only one from cls */
    bool accounts_match = true;
    for (unsigned int j = 0; accounts_length > j; ++j)
      if (1 != accounts_matching[j])
        accounts_match = false;
    if (true == accounts_match)
      cmp->results_matching[i] += 1;
  }
}


static int
test_insert_instance (const struct TALER_MerchantPublicKeyP *merchant_pub,
                      const struct TALER_MerchantPrivateKeyP *merchant_priv,
                      const struct TALER_MERCHANTDB_InstanceSettings *is)
{
  if (1 != plugin->insert_instance (plugin->cls,
                                    merchant_pub,
                                    merchant_priv,
                                    is))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Instance insertion failed\n");
    return 1;
  }
  return 0;
}


static int
test_lookup_instances (bool active_only,
                       unsigned int instances_length,
                       struct InstanceWithAccounts instances[])
{
  struct TestLookupInstances_Closure cls;
  cls.instances_to_cmp_length = instances_length;
  cls.instances_to_cmp = instances;
  unsigned int results_matching[instances_length];
  for (unsigned int i = 0; instances_length > i; ++i)
    results_matching[i] = 0;
  cls.results_matching = results_matching;
  cls.results_length = 0;
  if (0 > plugin->lookup_instances (plugin->cls,
                                    active_only,
                                    &lookup_instances_cb,
                                    &cls))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup instances failed\n");
    return 1;
  }
  if (instances_length != cls.results_length)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup instances failed: incorrect number of results\n");
    return 1;
  }
  for (unsigned int i = 0; instances_length > i; ++i)
  {
    if (1 != cls.results_matching[i])
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Lookup instances failed: mismatched data\n");
      return 1;
    }
  }
  return 0;
}


static int
test_insert_account (const char *instance_id,
                     const struct TALER_MERCHANTDB_AccountDetails *account)
{
  if (1 != plugin->insert_account (plugin->cls,
                                   instance_id,
                                   account))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Insert account failed\n");
    return 1;
  }
  return 0;
}


/**
 * Closure for instance tests
 */
struct TestInstances_Closure
{
  /**
   * The instance settings
   */
  struct TALER_MERCHANTDB_InstanceSettings is;

  /**
   * The instance public key
   */
  struct TALER_MerchantPublicKeyP merchant_pub;

  /**
   * The instance private key
   */
  struct TALER_MerchantPrivateKeyP merchant_priv;

  /**
   * The accounts array
   */
  struct TALER_MERCHANTDB_AccountDetails accounts[2];
};


/**
 * Sets up the data structures used in the instance tests
 */
static void
pre_test_instances (struct TestInstances_Closure *cls)
{
  GNUNET_CRYPTO_eddsa_key_create (&cls->merchant_priv.eddsa_priv);
  GNUNET_CRYPTO_eddsa_key_get_public (&cls->merchant_priv.eddsa_priv,
                                      &cls->merchant_pub.eddsa_pub);
  cls->is.id = "test_inst";
  cls->is.name = "Test";
  cls->is.address = json_array ();
  GNUNET_assert (NULL != cls->is.address);
  GNUNET_assert (0 == json_array_append (cls->is.address,
                                         json_string ("123 Example St")));
  cls->is.jurisdiction = json_array ();
  GNUNET_assert (NULL != cls->is.jurisdiction);
  GNUNET_assert (0 == json_array_append (cls->is.jurisdiction,
                                         json_string ("Ohio")));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:1200.40",
                                         &cls->is.default_max_deposit_fee));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:1200.40",
                                         &cls->is.default_max_wire_fee));
  cls->is.default_wire_fee_amortization = 1;
  cls->is.default_wire_transfer_delay = GNUNET_TIME_relative_get_minute_ ();
  cls->is.default_pay_delay = GNUNET_TIME_relative_get_second_ ();

  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG,
                                    &cls->accounts[0].h_wire);
  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG,
                                    &cls->accounts[0].salt);
  cls->accounts[0].payto_uri = "payto://x-taler-bank/bank.demo.taler.net/4";
  cls->accounts[0].active = true;

  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG,
                                    &cls->accounts[1].h_wire);
  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG,
                                    &cls->accounts[1].salt);
  cls->accounts[1].payto_uri = "payto://other-bank/bank.demo.other.net/4";
  cls->accounts[1].active = true;
}


/**
 * Handles all teardown after testing
 */
static void
post_test_instances (struct TestInstances_Closure *cls)
{
  json_decref (cls->is.address);
  json_decref (cls->is.jurisdiction);
}


/**
 * Function that tests instances.
 *
 * @param cls closure with config
 */
static int
run_test_instances (struct TestInstances_Closure *cls)
{
  /* Test inserting an instance */
  TEST_RET_ON_FAIL (test_insert_instance (&cls->merchant_pub,
                                          &cls->merchant_priv,
                                          &cls->is));

  /* Test lookup instances- is our new instance there? */
  struct InstanceWithAccounts instances[1];
  instances[0].accounts_length = 0;
  instances[0].accounts = NULL;
  instances[0].instance = &cls->is;
  TEST_RET_ON_FAIL (test_lookup_instances (false,
                                           1,
                                           instances));

  /* Test update instance */
  cls->is.name = "Test - updated";
  if (0 > plugin->update_instance (plugin->cls,
                                   &cls->is))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Update instance failed\n");
    return 1;
  }
  TEST_RET_ON_FAIL (test_lookup_instances (false,
                                           1,
                                           instances));

  /* Test account creation */
  TEST_RET_ON_FAIL (test_insert_account ("test_inst", &cls->accounts[0]));

  /* Test accounts from instance lookup */
  instances[0].accounts_length = 1;
  instances[0].accounts = cls->accounts;
  TEST_RET_ON_FAIL (test_lookup_instances (false,
                                           1,
                                           instances));

  /* Test account inactivation */
  if (0 > plugin->inactivate_account (plugin->cls,
                                      &cls->accounts[0].h_wire))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Inactivate account failed\n");
    return 1;
  }
  cls->accounts[0].active = false;
  TEST_RET_ON_FAIL (test_lookup_instances (false,
                                           1,
                                           instances));

  /* Test multiple accounts */

  /* Test instance private key deletion */
  if (0 > plugin->delete_instance_private_key (plugin->cls,
                                               "test_inst"))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Delete instance private key failed\n");
    return 1;
  }
  TEST_RET_ON_FAIL (test_lookup_instances (true,
                                           0,
                                           NULL));
  TEST_RET_ON_FAIL (test_lookup_instances (false,
                                           1,
                                           instances));

  /* Test instance deletion */

  return 0;
}


/**
 * Function that tests instances.
 *
 * @param cls closure with config
 */
static int
test_instances (void *cls)
{
  struct TestInstances_Closure test_cls;
  pre_test_instances (&test_cls);
  int test_result = run_test_instances (&test_cls);
  post_test_instances (&test_cls);
  return test_result;
}


/**
 * Number of products detected.
 */
static int product_count;


/**
 * An array of product ids found from the lookup method.
 */
static char *products_found[8];


static void
free_product (struct TALER_MERCHANTDB_ProductDetails *pd)
{
  GNUNET_free (pd->description);
  json_decref (pd->description_i18n);
  GNUNET_free (pd->unit);
  json_decref (pd->image);
  json_decref (pd->address);
}


static void
lookup_products_cb (void *cls,
                    const char *product_id)
{
  product_count += 1;
  if (8 >= product_count)
  {
    products_found[product_count - 1] =
      GNUNET_malloc (sizeof(char) * (strlen (product_id) + 1));
    strcpy (products_found[product_count - 1],
            product_id);
  }
}


static int
check_products_equal (const struct TALER_MERCHANTDB_ProductDetails *a,
                      const struct TALER_MERCHANTDB_ProductDetails *b)
{
  if ((0 != strcmp (a->description,
                    b->description)) ||
      (1 != json_equal (a->description_i18n,
                        b->description_i18n)) ||
      (0 != strcmp (a->unit,
                    b->unit)) ||
      (GNUNET_OK != TALER_amount_cmp_currency (&a->price,
                                               &b->price)) ||
      (0 != TALER_amount_cmp (&a->price,
                              &b->price)) ||
      (1 != json_equal (a->taxes,
                        b->taxes)) ||
      (a->total_stock != b->total_stock) ||
      (a->total_sold != b->total_sold) ||
      (a->total_lost != b->total_lost) ||
      (1 != json_equal (a->image,
                        b->image)) ||
      (1 != json_equal (a->address,
                        b->address)) ||
      (a->next_restock.abs_value_us != b->next_restock.abs_value_us))
    return 1;
  return 0;
}


static int
test_insert_product (const char *is,
                     const char *pd_id,
                     const struct TALER_MERCHANTDB_ProductDetails *pd)
{
  if (0 > plugin->insert_product (plugin->cls,
                                  is,
                                  pd_id,
                                  pd))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Insert product failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  return 0;
}


static int
test_lookup_product (const char *is,
                     const char *pd_id,
                     struct TALER_MERCHANTDB_ProductDetails *pd)
{
  if (0 > plugin->lookup_product (plugin->cls,
                                  is,
                                  pd_id,
                                  pd))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup product failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  return 0;
}


static int
test_delete_product (const char *is,
                     const char *pd)
{
  if (0 > plugin->delete_product (plugin->cls,
                                  is,
                                  pd))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Delete product failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  return 0;
}


/**
 * Function that tests products.
 *
 * @param cls closure with config
 */
static int
test_products (void *cls)
{
  /* Test making an instance */
  struct TALER_MerchantPublicKeyP merchant_pub;
  struct TALER_MerchantPrivateKeyP merchant_priv;
  GNUNET_CRYPTO_eddsa_key_create (&merchant_priv.eddsa_priv);
  GNUNET_CRYPTO_eddsa_key_get_public (&merchant_priv.eddsa_priv,
                                      &merchant_pub.eddsa_pub);
  struct TALER_MERCHANTDB_InstanceSettings is;
  is.id = "test_instance_0";
  is.name = "Test";
  is.address = json_array ();
  json_array_append (is.address,
                     json_string ("123 Example St"));
  is.jurisdiction = json_array ();
  json_array_append (is.jurisdiction,
                     json_string ("Ohio"));
  TALER_string_to_amount ("EUR:1200.40",
                          &is.default_max_deposit_fee);
  TALER_string_to_amount ("EUR:1200.40",
                          &is.default_max_wire_fee);
  is.default_wire_fee_amortization = 1;
  is.default_wire_transfer_delay = GNUNET_TIME_relative_get_minute_ ();
  is.default_pay_delay = GNUNET_TIME_relative_get_second_ ();
  TEST_RET_ON_FAIL (test_insert_instance (&merchant_pub,
                                          &merchant_priv,
                                          &is));

  /* Test creating a product */
  struct TALER_MERCHANTDB_ProductDetails pd;
  pd.description = "This is a test product";
  pd.description_i18n = json_array ();
  pd.unit = "boxes";
  TALER_string_to_amount ("EUR:120.40",
                          &pd.price);
  pd.taxes = json_array ();
  pd.total_stock = 55;
  pd.total_sold = 0;
  pd.total_lost = 0;
  pd.image = json_array ();
  pd.address = json_array ();
  pd.next_restock = GNUNET_TIME_absolute_get_zero_ ();
  TEST_RET_ON_FAIL (test_insert_product ("test_instance_0",
                                         "is_0_pd_0",
                                         &pd));

  /* Test lookup of individual products */
  struct TALER_MERCHANTDB_ProductDetails lookup_pd;
  TEST_RET_ON_FAIL (test_lookup_product ("test_instance_0",
                                         "is_0_pd_0",
                                         &lookup_pd));
  if (0 != check_products_equal (&pd,
                                 &lookup_pd))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup product failed: incorrect product returned\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  free_product (&lookup_pd);

  /* Make sure it fails correctly for products that don't exist */
  if (0 != plugin->lookup_product (plugin->cls,
                                   "test_instance_0",
                                   "fictional_product",
                                   &lookup_pd))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup product failed: product returned where there was none\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }

  /* Test product update */
  pd.description = "This is a test product that has been updated!";
  if (0 > plugin->update_product (plugin->cls,
                                  "test_instance_0",
                                  "is_0_pd_0",
                                  &pd))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Update product failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  TEST_RET_ON_FAIL (test_lookup_product ("test_instance_0",
                                         "is_0_pd_0",
                                         &lookup_pd));
  if (0 != check_products_equal (&pd,
                                 &lookup_pd))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Update product failed: lookup returns old product\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  free_product (&lookup_pd);

  /* Test collective product lookup */
  struct TALER_MERCHANTDB_ProductDetails pd1 = pd;
  pd1.description = "This is another product.";
  pd1.unit = "cans";
  TALER_string_to_amount ("EUR:4.95",
                          &pd1.price);
  TEST_RET_ON_FAIL (test_insert_product ("test_instance_0",
                                         "is_0_pd_1",
                                         &pd1));
  product_count = 0;
  if (0 > plugin->lookup_products (plugin->cls,
                                   "test_instance_0",
                                   &lookup_products_cb,
                                   cls))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup products failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  if (2 != product_count)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup products failed: incorrect number of products found\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  if (! (((0 == strcmp ("is_0_pd_0",
                        products_found[0])) &&
          (0 == strcmp ("is_0_pd_1",
                        products_found[1]))) ||
         ((0 == strcmp ("is_0_pd_1",
                        products_found[0])) &&
          (0 == strcmp ("is_0_pd_0",
                        products_found[1])))))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup products failed: incorrect product ids found\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  GNUNET_free (products_found[0]);
  GNUNET_free (products_found[1]);

  /* Test product deletion */
  TEST_RET_ON_FAIL (test_delete_product ("test_instance_0",
                                         "is_0_pd_0"));
  product_count = 0;
  if (0 > plugin->lookup_products (plugin->cls,
                                   "test_instance_0",
                                   &lookup_products_cb,
                                   cls))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup products failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  GNUNET_free (products_found[0]);
  if (1 != product_count)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup products/delete product failed: incorrect number of products after deletion\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  TEST_RET_ON_FAIL (test_delete_product ("test_instance_0",
                                         "is_0_pd_1"));
  product_count = 0;
  if (0 > plugin->lookup_products (plugin->cls,
                                   "test_instance_0",
                                   &lookup_products_cb,
                                   cls))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup products failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  if (0 != product_count)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup products/delete product failed: incorrect number of products after deletion\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }

  json_decref (pd.description_i18n);
  json_decref (pd.taxes);
  json_decref (pd.image);
  json_decref (pd.address);

  /* Clean up: delete the instance */
  /* This test currently FAILS */
  /*
  if (0 > plugin->purge_instance(plugin->cls,
                                 "test_instance_0")) {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Purge instance failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }*/json_decref (is.address);
  json_decref (is.jurisdiction);

  return 0;
}


/**
 * Number of orders detected.
 */
static int order_count;


/**
 * An array of order ids found from the lookup method.
 */
static char *orders_found[8];


/**
 * An array of order serials found from the lookup method.
 */
static uint64_t order_serials[8];


/**
 * An array of order timestamps found from the lookup method.
 */
static struct GNUNET_TIME_Absolute order_timestamps[8];


static void
lookup_orders_cb (void *cls,
                  const char *order_id,
                  uint64_t order_serial,
                  struct GNUNET_TIME_Absolute timestamp)
{
  order_count += 1;
  if (8 >= order_count)
  {
    orders_found[order_count - 1] =
      GNUNET_malloc (sizeof(char) * (strlen (order_id) + 1));
    strcpy (orders_found[order_count - 1],
            order_id);
    order_serials[order_count - 1] = order_serial;
    order_timestamps[order_count - 1] = timestamp;
  }
}


static int
test_insert_order (const char *instance_id,
                   const char *order_id,
                   struct GNUNET_TIME_Absolute pay_deadline,
                   const json_t *contract_terms)
{
  if (0 > plugin->insert_order (plugin->cls,
                                instance_id,
                                order_id,
                                pay_deadline,
                                contract_terms))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Insert order failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  return 0;
}


static int
test_lookup_order (const char *is,
                   const char *od,
                   json_t **contract_terms)
{
  if (0 > plugin->lookup_order (plugin->cls,
                                is,
                                od,
                                contract_terms))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup order failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  return 0;
}


static int
test_lookup_orders (const char *is,
                    const struct TALER_MERCHANTDB_OrderFilter *of)
{
  if (0 > plugin->lookup_orders (plugin->cls,
                                 is,
                                 of,
                                 &lookup_orders_cb,
                                 NULL))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup orders failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  return 0;
}


static int
test_delete_order (const char *is,
                   const char *od)
{
  if (0 > plugin->delete_order (plugin->cls,
                                is,
                                od))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Delete order failed\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  return 0;
}


/**
 * Function that tests orders.
 *
 * @param cls closure with config
 */
static int
test_orders (void *cls)
{
  struct TALER_MerchantPublicKeyP merchant_pub;
  struct TALER_MerchantPrivateKeyP merchant_priv;
  GNUNET_CRYPTO_eddsa_key_create (&merchant_priv.eddsa_priv);
  GNUNET_CRYPTO_eddsa_key_get_public (&merchant_priv.eddsa_priv,
                                      &merchant_pub.eddsa_pub);
  struct TALER_MERCHANTDB_InstanceSettings is;
  is.id = "test_instance_2";
  is.name = "Test";
  is.address = json_array ();
  json_array_append (is.address,
                     json_string ("123 Example St"));
  is.jurisdiction = json_array ();
  json_array_append (is.jurisdiction,
                     json_string ("Ohio"));
  TALER_string_to_amount ("EUR:1200.40",
                          &is.default_max_deposit_fee);
  TALER_string_to_amount ("EUR:1200.40",
                          &is.default_max_wire_fee);
  is.default_wire_fee_amortization = 1;
  is.default_wire_transfer_delay = GNUNET_TIME_relative_get_minute_ ();
  is.default_pay_delay = GNUNET_TIME_relative_get_second_ ();
  TEST_RET_ON_FAIL (test_insert_instance (&merchant_pub,
                                          &merchant_priv,
                                          &is));

  /* Test insert order */
  struct GNUNET_TIME_Absolute time_0 = GNUNET_TIME_absolute_get_zero_ ();
  json_t *terms_0 = json_array ();
  json_array_append (terms_0,
                     json_string ("contract"));
  TEST_RET_ON_FAIL (test_insert_order ("test_instance_2",
                                       "is_2_or_0",
                                       time_0,
                                       terms_0));

  /* Test lookup order */
  json_t *lookup_terms;
  TEST_RET_ON_FAIL (test_lookup_order ("test_instance_2",
                                       "is_2_or_0",
                                       &lookup_terms));
  if (1 != json_equal (terms_0,
                       lookup_terms))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup order failed: contract terms returned do not match\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  json_decref (lookup_terms);

  /* Make sure it fails correctly for products that don't exist */
  if (0 != plugin->lookup_order (plugin->cls,
                                 "test_instance_2",
                                 "fictional_order",
                                 &lookup_terms))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup order failed: order returned where there was none\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }

  /* Test lookups for multiple orders */
  TEST_RET_ON_FAIL (test_insert_order ("test_instance_2",
                                       "is_2_or_1",
                                       time_0,
                                       terms_0));
  struct TALER_MERCHANTDB_OrderFilter filter;
  filter.paid = TALER_MERCHANTDB_YNA_ALL;
  filter.refunded = TALER_MERCHANTDB_YNA_ALL;
  filter.wired = TALER_MERCHANTDB_YNA_ALL;
  filter.date = GNUNET_TIME_absolute_get_zero_ ();
  filter.start_row = 0;
  filter.delta = 8;
  order_count = 0;
  TEST_RET_ON_FAIL (test_lookup_orders ("test_instance_2",
                                        &filter));
  if (2 != order_count)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup orders failed: incorrect number of orders found\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  if (! (((0 == strcmp ("is_2_or_0",
                        orders_found[0])) &&
          (0 == strcmp ("is_2_or_1",
                        orders_found[1]))) ||
         ((0 == strcmp ("is_2_or_1",
                        orders_found[0])) &&
          (0 == strcmp ("is_2_or_0",
                        orders_found[1])))))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Lookup orders failed: incorrect order ids found\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  GNUNET_free (orders_found[0]);
  GNUNET_free (orders_found[1]);

  /* Test delete order */
  TEST_RET_ON_FAIL (test_delete_order ("test_instance_2",
                                       "is_2_or_0"));
  order_count = 0;
  TEST_RET_ON_FAIL (test_lookup_orders ("test_instance_2",
                                        &filter));
  if (1 != order_count)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Delete order failed: order still present in database\n");
    plugin->drop_tables (plugin->cls);
    return 1;
  }
  GNUNET_free (orders_found[0]);

  return 0;
}


/**
 * Function that runs all tests and returns upon error.
 */
static int
run_tests (void *cls)
{
  /* Test instances */
  if (0 != test_instances (cls))
    return 1;

  /* Test products */
  if (0 != test_products (cls))
    return 1;

  /* Test orders */
  if (0 != test_orders (cls))
    return 1;

  return 0;
}


/**
 * 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;
  /* Data for 'store_payment()' */

  /* Load the plugin */
  if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg)))
  {
    result = 77;
    return;
  }

  /* Run the preflight */
  plugin->preflight (plugin->cls);

  result = run_tests (cls);

  /* Test dropping tables */
  if (GNUNET_OK != plugin->drop_tables (plugin->cls))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Dropping tables failed\n");
    result = 77;
    return;
  }

  /* Unload the plugin */
  TALER_MERCHANTDB_plugin_unload (plugin);
  if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Plugin unload failed\n");
    result = 77;
    return;
  }

  GNUNET_break (GNUNET_OK ==
                plugin->drop_tables (plugin->cls));
  TALER_MERCHANTDB_plugin_unload (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;
  }
  GNUNET_log_setup (argv[0], "DEBUG", NULL);
  plugin_name++;
  (void) GNUNET_asprintf (&testname,
                          "test-merchantdb-%s",
                          plugin_name);
  (void) GNUNET_asprintf (&config_filename,
                          "%s.conf",
                          testname);
  cfg = GNUNET_CONFIGURATION_create ();
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_parse (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_merchantdb.c */