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