Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / peerstore / test_peerstore_api_store.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file peerstore/test_peerstore_api_store.c
22  * @brief testcase for peerstore store operation
23  */
24 #include "platform.h"
25 #include "gnunet_peerstore_service.h"
26 #include "gnunet_testing_lib.h"
27
28
29 static int ok = 1;
30
31 static struct GNUNET_PEERSTORE_Handle *h;
32
33 static char *subsystem = "test_peerstore_api_store";
34 static struct GNUNET_PeerIdentity pid;
35 static char *key = "test_peerstore_api_store_key";
36 static char *val1 = "test_peerstore_api_store_val1";
37 static char *val2 = "test_peerstore_api_store_val2-";
38 static char *val3 = "test_peerstore_api_store_val3--";
39
40 static int count = 0;
41
42
43 static void
44 test3_cont2 (void *cls,
45              const struct GNUNET_PEERSTORE_Record *record,
46              const char *emsg)
47 {
48   if (NULL != emsg)
49     return;
50   if (NULL != record)
51   {
52     GNUNET_assert ((strlen (val3) + 1) == record->value_size);
53     GNUNET_assert (0 == strcmp ((char *) val3,
54                                 (char *) record->value));
55     count++;
56     return;
57   }
58   GNUNET_assert (count == 1);
59   ok = 0;
60   GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
61   GNUNET_SCHEDULER_shutdown ();
62 }
63
64
65 static void
66 test3_cont (void *cls,
67             int success)
68 {
69   if (GNUNET_YES != success)
70     return;
71   count = 0;
72   GNUNET_PEERSTORE_iterate (h,
73                             subsystem,
74                             &pid,
75                             key,
76                             GNUNET_TIME_UNIT_SECONDS,
77                             &test3_cont2,
78                             NULL);
79 }
80
81
82 /**
83  * Replace the previous 2 records
84  */
85 static void
86 test3 ()
87 {
88   GNUNET_PEERSTORE_store (h,
89                           subsystem,
90                           &pid,
91                           key,
92                           val3,
93                           strlen (val3) + 1,
94                           GNUNET_TIME_UNIT_FOREVER_ABS,
95                           GNUNET_PEERSTORE_STOREOPTION_REPLACE,
96                           &test3_cont,
97                           NULL);
98 }
99
100
101 static void
102 test2_cont2 (void *cls,
103              const struct GNUNET_PEERSTORE_Record *record,
104              const char *emsg)
105 {
106   if (NULL != emsg)
107     return;
108   if (NULL != record)
109   {
110     GNUNET_assert (((strlen (val1) + 1) == record->value_size) ||
111                    ((strlen (val2) + 1) == record->value_size));
112     GNUNET_assert ((0 == strcmp ((char *) val1, (char *) record->value)) ||
113                    (0 == strcmp ((char *) val2, (char *) record->value)));
114     count++;
115     return;
116   }
117   GNUNET_assert (count == 2);
118   count = 0;
119   test3 ();
120 }
121
122
123 static void
124 test2_cont (void *cls, int success)
125 {
126   if (GNUNET_YES != success)
127     return;
128   count = 0;
129   GNUNET_PEERSTORE_iterate (h,
130                             subsystem,
131                             &pid, key,
132                             GNUNET_TIME_UNIT_SECONDS,
133                             &test2_cont2, NULL);
134 }
135
136
137 /**
138  * Test storing a second value with the same key
139  */
140 void
141 test2 ()
142 {
143   GNUNET_PEERSTORE_store (h,
144                           subsystem,
145                           &pid,
146                           key,
147                           val2,
148                           strlen (val2) + 1,
149                           GNUNET_TIME_UNIT_FOREVER_ABS,
150                           GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
151                           &test2_cont,
152                           NULL);
153 }
154
155
156 static void
157 test1_cont2 (void *cls,
158              const struct GNUNET_PEERSTORE_Record *record,
159              const char *emsg)
160 {
161   if (NULL != emsg)
162     return;
163   if (NULL != record)
164   {
165     GNUNET_assert ((strlen (val1) + 1) == record->value_size);
166     GNUNET_assert (0 == strcmp ((char *) val1, (char *) record->value));
167     count++;
168     return;
169   }
170   GNUNET_assert (count == 1);
171   count = 0;
172   test2 ();
173 }
174
175
176 static void
177 test1_cont (void *cls, int success)
178 {
179   if (GNUNET_YES != success)
180     return;
181   count = 0;
182   GNUNET_PEERSTORE_iterate (h,
183                             subsystem,
184                             &pid,
185                             key,
186                             GNUNET_TIME_UNIT_SECONDS,
187                             &test1_cont2,
188                             NULL);
189 }
190
191
192 /**
193  * Store a single record
194  */
195 static void
196 test1 ()
197 {
198   GNUNET_PEERSTORE_store (h,
199                           subsystem,
200                           &pid,
201                           key,
202                           val1,
203                           strlen (val1) + 1,
204                           GNUNET_TIME_UNIT_FOREVER_ABS,
205                           GNUNET_PEERSTORE_STOREOPTION_REPLACE,
206                           &test1_cont,
207                           NULL);
208 }
209
210
211 static void
212 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
213      struct GNUNET_TESTING_Peer *peer)
214 {
215   h = GNUNET_PEERSTORE_connect (cfg);
216   GNUNET_assert (NULL != h);
217   memset (&pid, 1, sizeof (pid));
218   test1 ();
219 }
220
221
222 int
223 main (int argc, char *argv[])
224 {
225   if (0 !=
226       GNUNET_TESTING_service_run ("test-gnunet-peerstore",
227                                   "peerstore",
228                                   "test_peerstore_api_data.conf",
229                                   &run, NULL))
230     return 1;
231   return ok;
232 }
233
234 /* end of test_peerstore_api_store.c */