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