aboutsummaryrefslogtreecommitdiff
path: root/src/sensor/test_pow_sign.c
blob: c4d8e24118bd0ff4c6c927f6dd29befa44590061 (plain) (blame)
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
  /*
   * This file is part of GNUnet.
   * Copyright (C)
   *
   * GNUnet 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.
   *
   * GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   * Boston, MA 02111-1307, USA.
   */
/**
 * @file sensor/test_pow_sign.c
 * @brief testcase for proof-of-work and signature library functions
 */
#include <inttypes.h>
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_sensor_util_lib.h"
#include "gnunet_testbed_service.h"
#include "gnunet_signatures.h"

/**
 * Number of peers to start for the test
 */
#define NUM_PEERS 1

/**
 * Size of the message exchanged
 */
#define MSG_SIZE 1024

/**
 * Number of matching bits to use for generating proof-of-work
 */
#define MATCHING_BITS 5

/**
 * Test timeout
 */
#define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)

/**
 * Test name
 */
static const char *testname = "test_pow_sign";

/**
 * Name of GNUNET config file used in this test
 */
static const char *cfg_filename = "test_pow_sign.conf";

/**
 * Status of the test to be returned by main()
 */
static int ok = 1;

/**
 * Task used to shutdown / expire the test
 */
static struct GNUNET_SCHEDULER_Task * shutdown_task;

/**
 * Message to be exchanged
 */
static char msg[MSG_SIZE];

/**
 * Private key of sending peer
 */
static struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;

/**
 * Public key of sending peer
 */
static struct GNUNET_CRYPTO_EddsaPublicKey *public_key;


/**
 * Shutdown task
 *
 * @param cls Closure (unused)
 * @param tc Task context (unused)
 */
static void
do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  if (NULL != private_key)
  {
    GNUNET_free (private_key);
    private_key = NULL;
  }
  if (NULL != public_key)
  {
    GNUNET_free (public_key);
    public_key = NULL;
  }
  GNUNET_SCHEDULER_shutdown ();
}


static void
pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block)
{
  void *response;
  struct GNUNET_TIME_Absolute end_time;
  struct GNUNET_TIME_Relative duration;

  end_time = GNUNET_TIME_absolute_get();
  duration = GNUNET_TIME_absolute_get_difference (block->timestamp, end_time);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received block:\n" "pow: %" PRIu64 ".\n", block->pow);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Block generation toke %s.\n",
              GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO));
  /* Test that the block is valid */
  GNUNET_assert (MSG_SIZE ==
                 GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
                                                       public_key, &response));
  GNUNET_assert (0 == memcmp (msg, response, MSG_SIZE));
  /* Modify the payload and test that verification returns invalid */
  block->pow++;
  GNUNET_assert (0 ==
                 GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
                                                       public_key, &response));
  ok = 0;
  GNUNET_SCHEDULER_cancel (shutdown_task);
  GNUNET_SCHEDULER_add_now (do_shutdown, NULL);
}


/**
 * Callback to be called when the requested peer information is available
 *
 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
 * @param op the operation this callback corresponds to
 * @param pinfo the result; will be NULL if the operation has failed
 * @param emsg error message if the operation has failed; will be NULL if the
 *          operation is successfull
 */
static void
peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
              const struct GNUNET_TESTBED_PeerInformation *pinfo,
              const char *emsg)
{
  struct GNUNET_TIME_Absolute timestamp;

  /* generate random data block */
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, msg, MSG_SIZE);
  /* get private and public keys */
  private_key =
      GNUNET_CRYPTO_eddsa_key_create_from_configuration (pinfo->result.cfg);
  GNUNET_assert (NULL != private_key);
  public_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPublicKey);

  GNUNET_CRYPTO_eddsa_key_get_public (private_key, public_key);
  /* create pow and sign */
  timestamp = GNUNET_TIME_absolute_get ();
  GNUNET_SENSOR_crypto_pow_sign (msg, MSG_SIZE, &timestamp, public_key,
                                 private_key, MATCHING_BITS, &pow_cb, NULL);
  GNUNET_TESTBED_operation_done (op);
}


/**
 * Signature of a main function for a testcase.
 *
 * @param cls closure
 * @param h the run handle
 * @param num_peers number of peers in 'peers'
 * @param peers handle to peers run in the testbed.  NULL upon timeout (see
 *          GNUNET_TESTBED_test_run()).
 * @param links_succeeded the number of overlay link connection attempts that
 *          succeeded
 * @param links_failed the number of overlay link connection attempts that
 *          failed
 * @see GNUNET_TESTBED_test_run()
 */
static void
test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h,
             unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
             unsigned int links_succeeded, unsigned int links_failed)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%d peers started. %d links succeeded. %d links failed.\n",
              num_peers, links_succeeded, links_failed);
  GNUNET_assert (NUM_PEERS == num_peers);
  GNUNET_assert (0 == links_failed);
  /* Schedule test timeout */
  shutdown_task =
      GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &do_shutdown, NULL);
  GNUNET_TESTBED_peer_get_information (peers[0],
                                       GNUNET_TESTBED_PIT_CONFIGURATION,
                                       &peer_info_cb, peers[0]);
}


int
main (int argc, char *argv[])
{
  GNUNET_log_setup (testname, "INFO", NULL);
  if (GNUNET_OK ==
      GNUNET_TESTBED_test_run (testname, cfg_filename, NUM_PEERS, 0, NULL, NULL,
                               &test_master, NULL))
    return ok;
  return 1;
}