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