-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[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 Affero 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file secretsharing/test_secretsharing_api.c
21  * @brief testcase for the secretsharing api
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_secretsharing_service.h"
27
28
29 static int success;
30
31 static struct GNUNET_SECRETSHARING_Session *keygen;
32
33
34 static void
35 secret_ready_cb (void *cls,
36                  struct GNUNET_SECRETSHARING_Share *my_share,
37                  struct GNUNET_SECRETSHARING_PublicKey *public_key,
38                  unsigned int num_ready_peers,
39                  const struct GNUNET_PeerIdentity *ready_peers)
40 {
41   keygen = NULL;
42   if (num_ready_peers == 1)
43     success = 1;
44   // FIXME: check that our share is valid, which we can do as there's only
45   // one peer.
46   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "secret ready, shutting down\n");
47   GNUNET_SCHEDULER_shutdown ();
48 }
49
50
51 static void
52 handle_shutdown (void *cls)
53 {
54   if (NULL != keygen)
55   {
56     GNUNET_SECRETSHARING_session_destroy (keygen);
57     keygen = NULL;
58   }
59 }
60
61
62 static void
63 run (void *cls,
64      const struct GNUNET_CONFIGURATION_Handle *cfg,
65      struct GNUNET_TESTING_Peer *peer)
66 {
67   struct GNUNET_HashCode session_id;
68   struct GNUNET_TIME_Absolute start;
69   struct GNUNET_TIME_Absolute deadline;
70
71   GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
72
73   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "testing secretsharing api\n");
74
75   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &session_id);
76
77   start = GNUNET_TIME_absolute_get ();
78   deadline = GNUNET_TIME_absolute_add (start, GNUNET_TIME_UNIT_SECONDS);
79
80   keygen = GNUNET_SECRETSHARING_create_session (cfg,
81                                                 0, NULL, /* only the local peer */
82                                                 &session_id,
83                                                 start, deadline,
84                                                 1,
85                                                 secret_ready_cb, NULL);
86
87
88 }
89
90
91 int
92 main (int argc, char **argv)
93 {
94
95   int ret;
96   ret = GNUNET_TESTING_peer_run ("test_secretsharing_api",
97                                  "test_secretsharing.conf",
98                                  &run, NULL);
99   if (0 != ret)
100     return ret;
101   return (GNUNET_YES == success) ? 0 : 1;
102 }