2 This file is part of GNUnet.
3 Copyright (C) 2015 GNUnet e.V.
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.
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.
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/>.
19 * @file peerstore/test_peerstore_api_sync.c
20 * @brief testcase for peerstore sync-on-disconnect feature. Stores
21 * a value just before disconnecting, and then checks that
22 * this value is actually stored.
23 * @author Omar Tarabai
24 * @author Christian Grothoff (minor fix, comments)
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_peerstore_service.h"
32 * Overall result, 0 for success.
37 * Configuration we use.
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42 * handle to talk to the peerstore.
44 static struct GNUNET_PEERSTORE_Handle *h;
47 * Subsystem we store the value for.
49 static const char *subsystem = "test_peerstore_api_sync";
52 * Fake PID under which we store the value.
54 static struct GNUNET_PeerIdentity pid;
57 * Test key we're storing the test value under.
59 static const char *key = "test_peerstore_api_store_key";
62 * Test value we are storing.
64 static const char *val = "test_peerstore_api_store_val";
68 * Function that should be called with the result of the
69 * lookup, and finally once with NULL to signal the end
72 * Upon the first call, we set "ok" to success. On the
73 * second call (end of iteration) we terminate the test.
76 * @param record the information stored in the peerstore
77 * @param emsg any error message
78 * @return #GNUNET_YES (all good, continue)
81 iterate_cb (void *cls,
82 const struct GNUNET_PEERSTORE_Record *record,
87 GNUNET_break (NULL == emsg);
90 GNUNET_PEERSTORE_disconnect (h,
92 GNUNET_SCHEDULER_shutdown ();
95 rec_val = record->value;
96 GNUNET_break (0 == strcmp (rec_val, val));
102 * Run the 2nd stage of the test where we fetch the
103 * data that should have been stored.
108 test_cont (void *cls)
110 h = GNUNET_PEERSTORE_connect (cfg);
111 GNUNET_PEERSTORE_iterate (h,
114 GNUNET_TIME_UNIT_FOREVER_REL,
120 * Actually run the test.
125 h = GNUNET_PEERSTORE_connect (cfg);
126 GNUNET_PEERSTORE_store (h,
130 val, strlen (val) + 1,
131 GNUNET_TIME_UNIT_FOREVER_ABS,
132 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
134 GNUNET_PEERSTORE_disconnect (h,
137 /* We need to wait a little bit to give the disconnect
138 a chance to actually finish the operation; otherwise,
139 the test may fail non-deterministically if the new
140 connection is faster than the cleanup routine of the
142 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
149 * Initialize globals and launch the test.
152 * @param c configuration to use
153 * @param peer handle to our peer (unused)
157 const struct GNUNET_CONFIGURATION_Handle *c,
158 struct GNUNET_TESTING_Peer *peer)
161 memset (&pid, 1, sizeof (pid));
167 main (int argc, char *argv[])
170 GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync",
172 "test_peerstore_api_data.conf",
182 /* end of test_peerstore_api_sync.c */