9c257f2e93e46fc9733687380cf952514379d3b5
[oweals/gnunet.git] / src / consensus / test_consensus_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 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 consensus/test_consensus_api.c
21  * @brief testcase for consensus_api.c
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_consensus_service.h"
26 #include "gnunet_testing_lib.h"
27
28
29 static struct GNUNET_CONSENSUS_Handle *consensus;
30
31 static struct GNUNET_HashCode session_id;
32
33 static unsigned int elements_received;
34
35
36 static void
37 conclude_done (void *cls)
38 {
39   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "conclude over\n");
40   if (2 != elements_received)
41     GNUNET_assert (0);
42   GNUNET_SCHEDULER_shutdown ();
43 }
44
45 static void
46 on_new_element (void *cls,
47                 const struct GNUNET_SET_Element *element)
48 {
49   elements_received++;
50 }
51
52 static void
53 insert_done (void *cls, int success)
54 {
55   /* make sure cb is only called once */
56   static int called = GNUNET_NO;
57   GNUNET_assert (GNUNET_NO == called);
58   called = GNUNET_YES;
59   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "insert done\n");
60   GNUNET_CONSENSUS_conclude (consensus, &conclude_done, NULL);
61 }
62
63
64 /**
65  * Signature of the main function of a task.
66  *
67  * @param cls closure
68  */
69 static void
70 on_shutdown (void *cls)
71 {
72   if (NULL != consensus)
73   {
74     GNUNET_CONSENSUS_destroy (consensus);
75     consensus = NULL;
76   }
77 }
78
79
80 static void
81 run (void *cls,
82      const struct GNUNET_CONFIGURATION_Handle *cfg,
83      struct GNUNET_TESTING_Peer *peer)
84 {
85   char *str = "foo";
86
87   struct GNUNET_SET_Element el1 = {4, 0, "foo"};
88   struct GNUNET_SET_Element el2 = {5, 0, "quux"};
89
90   GNUNET_log_setup ("test_consensus_api",
91                     "INFO",
92                     NULL);
93   GNUNET_SCHEDULER_add_shutdown (&on_shutdown, NULL);
94
95   GNUNET_CRYPTO_hash (str, strlen (str), &session_id);
96   consensus = GNUNET_CONSENSUS_create (cfg, 0, NULL, &session_id,
97       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
98       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES),
99       on_new_element, &consensus);
100   GNUNET_assert (consensus != NULL);
101
102   GNUNET_CONSENSUS_insert (consensus, &el1, NULL, &consensus);
103   GNUNET_CONSENSUS_insert (consensus, &el2, &insert_done, &consensus);
104 }
105
106
107 int
108 main (int argc, char **argv)
109 {
110   return GNUNET_TESTING_peer_run ("test_consensus_api",
111                                   "test_consensus.conf",
112                                   &run, NULL);
113 }