- fixed malloc size
[oweals/gnunet.git] / src / set / test_set_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 set/test_set_api.c
23  * @brief testcase for set_api.c
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_set_service.h"
29
30
31 static struct GNUNET_PeerIdentity local_id;
32
33 static struct GNUNET_HashCode app_id;
34 static struct GNUNET_SET_Handle *set1;
35 static struct GNUNET_SET_Handle *set2;
36 static struct GNUNET_SET_ListenHandle *listen_handle;
37 const static struct GNUNET_CONFIGURATION_Handle *config;
38
39 int num_done;
40
41
42 static void
43 result_cb_set1 (void *cls, const struct GNUNET_SET_Element *element,
44                 enum GNUNET_SET_Status status)
45 {
46   switch (status)
47   {
48     case GNUNET_SET_STATUS_OK:
49       printf ("set 1: got element\n");
50       break;
51     case GNUNET_SET_STATUS_FAILURE:
52       printf ("set 1: failure\n");
53       break;
54     case GNUNET_SET_STATUS_DONE:
55       printf ("set 1: done\n");
56       GNUNET_SET_destroy (set1);
57       break;
58     default:
59       GNUNET_assert (0);
60   }
61 }
62
63
64 static void
65 result_cb_set2 (void *cls, const struct GNUNET_SET_Element *element,
66            enum GNUNET_SET_Status status)
67 {
68   switch (status)
69   {
70     case GNUNET_SET_STATUS_OK:
71       printf ("set 2: got element\n");
72       break;
73     case GNUNET_SET_STATUS_FAILURE:
74       printf ("set 2: failure\n");
75       break;
76     case GNUNET_SET_STATUS_DONE:
77       printf ("set 2: done\n");
78       GNUNET_SET_destroy (set2);
79       break;
80     default:
81       GNUNET_assert (0);
82   }
83 }
84
85
86 static void
87 listen_cb (void *cls,
88            const struct GNUNET_PeerIdentity *other_peer,
89            const struct GNUNET_MessageHeader *context_msg,
90            struct GNUNET_SET_Request *request)
91 {
92   struct GNUNET_SET_OperationHandle *oh;
93
94   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "listen cb called\n");
95   GNUNET_SET_listen_cancel (listen_handle);
96
97   oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED, result_cb_set2, NULL);
98   GNUNET_SET_commit (oh, set2);
99 }
100
101
102 /**
103  * Start the set operation.
104  *
105  * @param cls closure, unused
106  */
107 static void
108 start (void *cls)
109 {
110   struct GNUNET_SET_OperationHandle *oh;
111
112   listen_handle = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
113                                      &app_id, listen_cb, NULL);
114   oh = GNUNET_SET_prepare (&local_id, &app_id, NULL, 42,
115                            GNUNET_SET_RESULT_ADDED,
116                            result_cb_set1, NULL);
117   GNUNET_SET_commit (oh, set1);
118 }
119
120
121 /**
122  * Initialize the second set, continue
123  *
124  * @param cls closure, unused
125  */
126 static void
127 init_set2 (void *cls)
128 {
129   struct GNUNET_SET_Element element;
130
131
132   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
133
134   element.data = "hello";
135   element.size = strlen(element.data);
136   GNUNET_SET_add_element (set2, &element, NULL, NULL);
137   element.data = "quux";
138   element.size = strlen(element.data);
139   GNUNET_SET_add_element (set2, &element, start, NULL);
140 }
141
142
143 /**
144  * Initialize the first set, continue.
145  */
146 static void
147 init_set1 (void)
148 {
149   struct GNUNET_SET_Element element;
150
151   element.data = "hello";
152   element.size = strlen(element.data);
153   GNUNET_SET_add_element (set1, &element, NULL, NULL);
154   element.data = "bar";
155   element.size = strlen(element.data);
156   GNUNET_SET_add_element (set1, &element, init_set2, NULL);
157
158   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
159 }
160
161
162 /**
163  * Signature of the 'main' function for a (single-peer) testcase that
164  * is run using 'GNUNET_TESTING_peer_run'.
165  * 
166  * @param cls closure
167  * @param cfg configuration of the peer that was started
168  * @param peer identity of the peer that was created
169  */
170 static void
171 run (void *cls,
172      const struct GNUNET_CONFIGURATION_Handle *cfg,
173      struct GNUNET_TESTING_Peer *peer)
174 {
175
176   config = cfg;
177   GNUNET_CRYPTO_get_host_identity (cfg, &local_id);
178   printf ("my id (from CRYPTO): %s\n", GNUNET_h2s (&local_id.hashPubKey));
179   GNUNET_TESTING_peer_get_identity (peer, &local_id);
180   printf ("my id (from TESTING): %s\n", GNUNET_h2s (&local_id.hashPubKey));
181   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
182   set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
183   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
184   init_set1 ();
185 }
186
187 int
188 main (int argc, char **argv)
189 {
190   int ret;
191
192   ret = GNUNET_TESTING_peer_run ("test_set_api",
193                                  "test_set.conf",
194                                  &run, NULL);
195   return ret;
196 }
197