add integer overflow guards and avoid (unlimited) stack allocation
[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      SPDX-License-Identifier: AGPL3.0-or-later
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
43 static void
44 test3_cont2 (void *cls,
45              const struct GNUNET_PEERSTORE_Record *record,
46              const char *emsg)
47 {
48   if (NULL != emsg)
49     return;
50   if (NULL != record)
51   {
52     GNUNET_assert ((strlen (val3) + 1) == record->value_size);
53     GNUNET_assert (0 == strcmp ((char *) val3,
54                                 (char *) record->value));
55     count++;
56     return;
57   }
58   GNUNET_assert (count == 1);
59   ok = 0;
60   GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
61   GNUNET_SCHEDULER_shutdown ();
62 }
63
64
65 static void
66 test3_cont (void *cls,
67             int success)
68 {
69   if (GNUNET_YES != success)
70     return;
71   count = 0;
72   GNUNET_PEERSTORE_iterate (h,
73                             subsystem,
74                             &pid,
75                             key,
76                             &test3_cont2,
77                             NULL);
78 }
79
80
81 /**
82  * Replace the previous 2 records
83  */
84 static void
85 test3 ()
86 {
87   GNUNET_PEERSTORE_store (h,
88                           subsystem,
89                           &pid,
90                           key,
91                           val3,
92                           strlen (val3) + 1,
93                           GNUNET_TIME_UNIT_FOREVER_ABS,
94                           GNUNET_PEERSTORE_STOREOPTION_REPLACE,
95                           &test3_cont,
96                           NULL);
97 }
98
99
100 static void
101 test2_cont2 (void *cls,
102              const struct GNUNET_PEERSTORE_Record *record,
103              const char *emsg)
104 {
105   if (NULL != emsg)
106     return;
107   if (NULL != record)
108   {
109     GNUNET_assert (((strlen (val1) + 1) == record->value_size) ||
110                    ((strlen (val2) + 1) == record->value_size));
111     GNUNET_assert ((0 == strcmp ((char *) val1, (char *) record->value)) ||
112                    (0 == strcmp ((char *) val2, (char *) record->value)));
113     count++;
114     return;
115   }
116   GNUNET_assert (count == 2);
117   count = 0;
118   test3 ();
119 }
120
121
122 static void
123 test2_cont (void *cls, int success)
124 {
125   if (GNUNET_YES != success)
126     return;
127   count = 0;
128   GNUNET_PEERSTORE_iterate (h,
129                             subsystem,
130                             &pid, key,
131                             &test2_cont2,
132                             NULL);
133 }
134
135
136 /**
137  * Test storing a second value with the same key
138  */
139 void
140 test2 ()
141 {
142   GNUNET_PEERSTORE_store (h,
143                           subsystem,
144                           &pid,
145                           key,
146                           val2,
147                           strlen (val2) + 1,
148                           GNUNET_TIME_UNIT_FOREVER_ABS,
149                           GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
150                           &test2_cont,
151                           NULL);
152 }
153
154
155 static void
156 test1_cont2 (void *cls,
157              const struct GNUNET_PEERSTORE_Record *record,
158              const char *emsg)
159 {
160   if (NULL != emsg)
161     return;
162   if (NULL != record)
163   {
164     GNUNET_assert ((strlen (val1) + 1) == record->value_size);
165     GNUNET_assert (0 == strcmp ((char *) val1, (char *) record->value));
166     count++;
167     return;
168   }
169   GNUNET_assert (count == 1);
170   count = 0;
171   test2 ();
172 }
173
174
175 static void
176 test1_cont (void *cls, int success)
177 {
178   if (GNUNET_YES != success)
179     return;
180   count = 0;
181   GNUNET_PEERSTORE_iterate (h,
182                             subsystem,
183                             &pid,
184                             key,
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
233 /* end of test_peerstore_api_store.c */