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