-fix
[oweals/gnunet.git] / src / consensus / test_consensus_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file consensus/test_consensus_api.c
23  * @brief testcase for consensus_api.c
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_consensus_service.h"
28 #include "gnunet_testing_lib.h"
29
30
31 static struct GNUNET_CONSENSUS_Handle *consensus;
32
33 static struct GNUNET_HashCode session_id;
34
35
36 static int
37 conclude_done (void *cls, const struct GNUNET_CONSENSUS_Group *group)
38 {
39   if (NULL == group)
40   {
41     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "conclude over\n");
42     GNUNET_SCHEDULER_shutdown ();
43     return GNUNET_NO;
44   }
45   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "concluded\n");
46   return GNUNET_YES;
47 }
48
49 static int
50 on_new_element (void *cls,
51                 struct GNUNET_CONSENSUS_Element *element)
52 {
53   GNUNET_assert (0);
54   return GNUNET_YES;
55 }
56
57 static void
58 insert_done (void *cls, int success)
59 {
60   /* make sure cb is only called once */
61   static int called = GNUNET_NO;
62   GNUNET_assert (GNUNET_NO == called);
63   called = GNUNET_YES;
64   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "insert done\n");
65   GNUNET_CONSENSUS_conclude (consensus, GNUNET_TIME_UNIT_SECONDS, 0, &conclude_done, NULL);
66 }
67
68
69 /**
70  * Signature of the main function of a task.
71  *
72  * @param cls closure
73  * @param tc context information (why was this task triggered now)
74  */
75 static void
76 on_shutdown (void *cls,
77           const struct GNUNET_SCHEDULER_TaskContext *tc)
78 {
79   if (NULL != consensus)
80   {
81     GNUNET_CONSENSUS_destroy (consensus);
82     consensus = NULL;
83   }
84 }
85
86
87 static void
88 run (void *cls, 
89      const struct GNUNET_CONFIGURATION_Handle *cfg,
90      struct GNUNET_TESTING_Peer *peer)
91 {
92   char *str = "foo";
93
94   struct GNUNET_CONSENSUS_Element el1 = {"foo", 4, 0};
95   struct GNUNET_CONSENSUS_Element el2 = {"bar", 4, 0};
96
97   GNUNET_log_setup ("test_consensus_api",
98                     "INFO",
99                     NULL);
100
101   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "testing consensus api\n");
102
103   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &on_shutdown, NULL);
104
105   GNUNET_CRYPTO_hash (str, strlen (str), &session_id);
106   consensus = GNUNET_CONSENSUS_create (cfg, 0, NULL, &session_id, on_new_element, &consensus);
107   GNUNET_assert (consensus != NULL);
108
109   GNUNET_CONSENSUS_insert (consensus, &el1, NULL, &consensus);
110   GNUNET_CONSENSUS_insert (consensus, &el2, &insert_done, &consensus);
111 }
112
113
114 int
115 main (int argc, char **argv)
116 {
117   int ret;
118
119   ret = GNUNET_TESTING_peer_run ("test_consensus_api",
120                                  "test_consensus.conf",
121                                  &run, NULL);
122   return ret;
123 }
124