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