use NULL value in load_path_suffix to NOT load any files
[oweals/gnunet.git] / src / peerstore / test_peerstore_api_sync.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015 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_sync.c
22  * @brief testcase for peerstore sync-on-disconnect feature. Stores
23  *        a value just before disconnecting, and then checks that
24  *        this value is actually stored.
25  * @author Omar Tarabai
26  * @author Christian Grothoff (minor fix, comments)
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_peerstore_service.h"
32
33 /**
34  * Overall result, 0 for success.
35  */
36 static int ok = 404;
37
38 /**
39  * Configuration we use.
40  */
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43 /**
44  * handle to talk to the peerstore.
45  */
46 static struct GNUNET_PEERSTORE_Handle *h;
47
48 /**
49  * Subsystem we store the value for.
50  */
51 static const char *subsystem = "test_peerstore_api_sync";
52
53 /**
54  * Fake PID under which we store the value.
55  */
56 static struct GNUNET_PeerIdentity pid;
57
58 /**
59  * Test key we're storing the test value under.
60  */
61 static const char *key = "test_peerstore_api_store_key";
62
63 /**
64  * Test value we are storing.
65  */
66 static const char *val = "test_peerstore_api_store_val";
67
68
69 /**
70  * Function that should be called with the result of the
71  * lookup, and finally once with NULL to signal the end
72  * of the iteration.
73  *
74  * Upon the first call, we set "ok" to success. On the
75  * second call (end of iteration) we terminate the test.
76  *
77  * @param cls NULL
78  * @param record the information stored in the peerstore
79  * @param emsg any error message
80  * @return #GNUNET_YES (all good, continue)
81  */
82 static void
83 iterate_cb (void *cls,
84             const struct GNUNET_PEERSTORE_Record *record,
85             const char *emsg)
86 {
87   const char *rec_val;
88
89   GNUNET_break (NULL == emsg);
90   if (NULL == record)
91   {
92     GNUNET_PEERSTORE_disconnect (h,
93                                  GNUNET_NO);
94     GNUNET_SCHEDULER_shutdown ();
95     return;
96   }
97   rec_val = record->value;
98   GNUNET_break (0 == strcmp (rec_val, val));
99   ok = 0;
100 }
101
102
103 /**
104  * Run the 2nd stage of the test where we fetch the
105  * data that should have been stored.
106  *
107  * @param cls NULL
108  */
109 static void
110 test_cont (void *cls)
111 {
112   h = GNUNET_PEERSTORE_connect (cfg);
113   GNUNET_PEERSTORE_iterate (h,
114                             subsystem,
115                             &pid, key,
116                             &iterate_cb,
117                             NULL);
118 }
119
120
121 static void
122 disc_cont (void *cls)
123 {
124   GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
125   h = NULL;
126   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
127                                 &test_cont,
128                                 NULL);
129 }
130
131
132 static void
133 store_cont (void *cls, int success)
134 {
135   ok = success;
136   /* We need to wait a little bit to give the disconnect
137      a chance to actually finish the operation; otherwise,
138      the test may fail non-deterministically if the new
139      connection is faster than the cleanup routine of the
140      old one. */GNUNET_SCHEDULER_add_now (&disc_cont,
141                             NULL);
142 }
143
144
145 /**
146  * Actually run the test.
147  */
148 static void
149 test1 ()
150 {
151   h = GNUNET_PEERSTORE_connect (cfg);
152   GNUNET_PEERSTORE_store (h,
153                           subsystem,
154                           &pid,
155                           key,
156                           val, strlen (val) + 1,
157                           GNUNET_TIME_UNIT_FOREVER_ABS,
158                           GNUNET_PEERSTORE_STOREOPTION_REPLACE,
159                           &store_cont, NULL);
160 }
161
162
163 /**
164  * Initialize globals and launch the test.
165  *
166  * @param cls NULL
167  * @param c configuration to use
168  * @param peer handle to our peer (unused)
169  */
170 static void
171 run (void *cls,
172      const struct GNUNET_CONFIGURATION_Handle *c,
173      struct GNUNET_TESTING_Peer *peer)
174 {
175   cfg = c;
176   memset (&pid, 1, sizeof(pid));
177   test1 ();
178 }
179
180
181 int
182 main (int argc, char *argv[])
183 {
184   if (0 !=
185       GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync",
186                                   "peerstore",
187                                   "peerstore.conf",
188                                   &run, NULL))
189     return 1;
190   if (0 != ok)
191     fprintf (stderr,
192              "Test failed: %d\n",
193              ok);
194   return ok;
195 }
196
197
198 /* end of test_peerstore_api_sync.c */