aboutsummaryrefslogtreecommitdiff
path: root/src/exchangedb/test_exchangedb_fees.c
blob: 55f34bd6f94d6c87a0b270f5064ca01ffe6fdc75 (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
/*
  This file is part of TALER
  Copyright (C) 2017 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU 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 exchangedb/test_exchangedb_fees.c
 * @brief test cases for functions in exchangedb/exchangedb_fees.c
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet/gnunet_util_lib.h"
#include "taler_signatures.h"
#include "taler_exchangedb_lib.h"


/**
 * Sign @a af with @a priv
 *
 * @param[in|out] af fee structure to sign
 * @param priv private key to use for signing
 */
static void
sign_af (struct TALER_EXCHANGEDB_AggregateFees *af,
         const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
{
  struct TALER_MasterWireFeePS wf;

  TALER_EXCHANGEDB_fees_2_wf ("test",
                              af,
                              &wf);
  GNUNET_CRYPTO_eddsa_sign (priv,
                            &wf,
                            &af->master_sig.eddsa_signature);
}


int
main (int argc,
      const char *const argv[])
{
  struct GNUNET_CONFIGURATION_Handle *cfg;
  struct TALER_EXCHANGEDB_AggregateFees *af;
  struct TALER_EXCHANGEDB_AggregateFees *n;
  struct TALER_MasterPublicKeyP master_pub;
  struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
  char *tmpdir;
  char *tmpfile = NULL;
  int ret;
  unsigned int year;

  (void) argc;
  (void) argv;
  GNUNET_log_setup ("test-exchangedb-fees",
                    "WARNING",
                    NULL);
  tmpdir = GNUNET_DISK_mkdtemp ("test_exchangedb_fees");
  if (NULL == tmpdir)
    return 77; /* skip test */
  priv = GNUNET_CRYPTO_eddsa_key_create ();
  GNUNET_CRYPTO_eddsa_key_get_public (priv,
                                      &master_pub.eddsa_pub);
  cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_CONFIGURATION_set_value_string (cfg,
                                         "exchangedb",
                                         "WIREFEE_BASE_DIR",
                                         tmpdir);
  GNUNET_asprintf (&tmpfile,
                   "%s/%s.fee",
                   tmpdir,
                   "test");
  ret = 0;
  af = GNUNET_new (struct TALER_EXCHANGEDB_AggregateFees);
  year = GNUNET_TIME_get_current_year ();
  af->start_date = GNUNET_TIME_year_to_time (year);
  af->end_date = GNUNET_TIME_year_to_time (year + 1);
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:1.0",
                                         &af->wire_fee));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:1.0",
                                         &af->closing_fee));
  sign_af (af,
           priv);
  n = GNUNET_new (struct TALER_EXCHANGEDB_AggregateFees);
  n->start_date = GNUNET_TIME_year_to_time (year + 1);
  n->end_date = GNUNET_TIME_year_to_time (year + 2);
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:0.1",
                                         &n->wire_fee));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount ("EUR:0.1",
                                         &n->closing_fee));
  sign_af (n,
           priv);
  af->next = n;

  if (GNUNET_OK !=
      TALER_EXCHANGEDB_fees_write (tmpfile,
                                   "test",
                                   af))
  {
    GNUNET_break (0);
    ret = 1;
  }
  TALER_EXCHANGEDB_fees_free (af);
  GNUNET_free (tmpfile);
  af = TALER_EXCHANGEDB_fees_read (cfg,
                                   "test");
  if (NULL == af)
  {
    GNUNET_break (0);
    ret = 1;
  }
  else
  {
    for (struct TALER_EXCHANGEDB_AggregateFees *p = af;
         NULL != p;
         p = p->next)
    {
      struct TALER_MasterWireFeePS wf;

      TALER_EXCHANGEDB_fees_2_wf ("test",
                                  p,
                                  &wf);
      if (GNUNET_OK !=
          GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_MASTER_WIRE_FEES,
                                      &wf,
                                      &p->master_sig.eddsa_signature,
                                      &master_pub.eddsa_pub))
      {
        GNUNET_break (0);
        ret = 1;
      }
    }
    TALER_EXCHANGEDB_fees_free (af);
  }

  (void) GNUNET_DISK_directory_remove (tmpdir);
  GNUNET_free (tmpdir);
  GNUNET_free (priv);
  GNUNET_CONFIGURATION_destroy (cfg);
  return ret;
}