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