added seeding function to the api
[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_store.c
22  * @brief testcase for peerstore store operation
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 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 static int
43 test3_cont2 (void *cls, const struct GNUNET_PEERSTORE_Record *record,
44              const 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, GNUNET_YES);
58   GNUNET_SCHEDULER_shutdown ();
59   return GNUNET_YES;
60 }
61
62
63 static void
64 test3_cont (void *cls, int success)
65 {
66   if (GNUNET_YES != success)
67     return;
68   count = 0;
69   GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS,
70                             &test3_cont2, NULL);
71 }
72
73
74 /**
75  * Replace the previous 2 records
76  */
77 static void
78 test3 ()
79 {
80   GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val3, strlen (val3) + 1,
81                           GNUNET_TIME_UNIT_FOREVER_ABS,
82                           GNUNET_PEERSTORE_STOREOPTION_REPLACE, &test3_cont,
83                           NULL);
84 }
85
86
87 static int
88 test2_cont2 (void *cls, const struct GNUNET_PEERSTORE_Record *record,
89              const char *emsg)
90 {
91   if (NULL != emsg)
92     return GNUNET_NO;
93   if (NULL != record)
94   {
95     GNUNET_assert (((strlen (val1) + 1) == record->value_size) ||
96                    ((strlen (val2) + 1) == record->value_size));
97     GNUNET_assert ((0 == strcmp ((char *) val1, (char *) record->value)) ||
98                    (0 == strcmp ((char *) val2, (char *) record->value)));
99     count++;
100     return GNUNET_YES;
101   }
102   GNUNET_assert (count == 2);
103   count = 0;
104   test3 ();
105   return GNUNET_YES;
106 }
107
108
109 static void
110 test2_cont (void *cls, int success)
111 {
112   if (GNUNET_YES != success)
113     return;
114   count = 0;
115   GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS,
116                             &test2_cont2, NULL);
117 }
118
119
120 /**
121  * Test storing a second value with the same key
122  */
123 void
124 test2 ()
125 {
126   GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val2, strlen (val2) + 1,
127                           GNUNET_TIME_UNIT_FOREVER_ABS,
128                           GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, &test2_cont,
129                           NULL);
130 }
131
132
133 static int
134 test1_cont2 (void *cls, const struct GNUNET_PEERSTORE_Record *record,
135              const char *emsg)
136 {
137   if (NULL != emsg)
138     return GNUNET_NO;
139   if (NULL != record)
140   {
141     GNUNET_assert ((strlen (val1) + 1) == record->value_size);
142     GNUNET_assert (0 == strcmp ((char *) val1, (char *) record->value));
143     count++;
144     return GNUNET_YES;
145   }
146   GNUNET_assert (count == 1);
147   count = 0;
148   test2 ();
149   return GNUNET_YES;
150 }
151
152
153 static void
154 test1_cont (void *cls, int success)
155 {
156   if (GNUNET_YES != success)
157     return;
158   count = 0;
159   GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS,
160                             &test1_cont2, NULL);
161 }
162
163
164 /**
165  * Store a single record
166  */
167 static void
168 test1 ()
169 {
170   GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val1, strlen (val1) + 1,
171                           GNUNET_TIME_UNIT_FOREVER_ABS,
172                           GNUNET_PEERSTORE_STOREOPTION_REPLACE, &test1_cont,
173                           NULL);
174 }
175
176
177 static void
178 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
179      struct GNUNET_TESTING_Peer *peer)
180 {
181   h = GNUNET_PEERSTORE_connect (cfg);
182   GNUNET_assert (NULL != h);
183   memset (&pid, 1, sizeof (pid));
184   test1 ();
185 }
186
187
188 int
189 main (int argc, char *argv[])
190 {
191   if (0 !=
192       GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore",
193                                   "test_peerstore_api_data.conf", &run, NULL))
194     return 1;
195   return ok;
196 }
197
198 /* end of test_peerstore_api_store.c */