first batch of license fixes (boring)
[oweals/gnunet.git] / src / secretsharing / test_secretsharing_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2014 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14 */
15
16 /**
17  * @file secretsharing/test_secretsharing_api.c
18  * @brief testcase for the secretsharing api
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22 #include "gnunet_testing_lib.h"
23 #include "gnunet_secretsharing_service.h"
24
25
26 static int success;
27
28 static struct GNUNET_SECRETSHARING_Session *keygen;
29
30
31 static void
32 secret_ready_cb (void *cls,
33                  struct GNUNET_SECRETSHARING_Share *my_share,
34                  struct GNUNET_SECRETSHARING_PublicKey *public_key,
35                  unsigned int num_ready_peers,
36                  const struct GNUNET_PeerIdentity *ready_peers)
37 {
38   keygen = NULL;
39   if (num_ready_peers == 1)
40     success = 1;
41   // FIXME: check that our share is valid, which we can do as there's only
42   // one peer.
43   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "secret ready, shutting down\n");
44   GNUNET_SCHEDULER_shutdown ();
45 }
46
47
48 static void
49 handle_shutdown (void *cls)
50 {
51   if (NULL != keygen)
52   {
53     GNUNET_SECRETSHARING_session_destroy (keygen);
54     keygen = NULL;
55   }
56 }
57
58
59 static void
60 run (void *cls,
61      const struct GNUNET_CONFIGURATION_Handle *cfg,
62      struct GNUNET_TESTING_Peer *peer)
63 {
64   struct GNUNET_HashCode session_id;
65   struct GNUNET_TIME_Absolute start;
66   struct GNUNET_TIME_Absolute deadline;
67
68   GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
69
70   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "testing secretsharing api\n");
71
72   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &session_id);
73
74   start = GNUNET_TIME_absolute_get ();
75   deadline = GNUNET_TIME_absolute_add (start, GNUNET_TIME_UNIT_SECONDS);
76
77   keygen = GNUNET_SECRETSHARING_create_session (cfg,
78                                                 0, NULL, /* only the local peer */
79                                                 &session_id,
80                                                 start, deadline,
81                                                 1,
82                                                 secret_ready_cb, NULL);
83
84
85 }
86
87
88 int
89 main (int argc, char **argv)
90 {
91
92   int ret;
93   ret = GNUNET_TESTING_peer_run ("test_secretsharing_api",
94                                  "test_secretsharing.conf",
95                                  &run, NULL);
96   if (0 != ret)
97     return ret;
98   return (GNUNET_YES == success) ? 0 : 1;
99 }