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.
16 * @file peerstore/test_peerstore_api_sync.c
17 * @brief testcase for peerstore sync-on-disconnect feature. Stores
18 * a value just before disconnecting, and then checks that
19 * this value is actually stored.
20 * @author Omar Tarabai
21 * @author Christian Grothoff (minor fix, comments)
24 #include "gnunet_util_lib.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_peerstore_service.h"
29 * Overall result, 0 for success.
34 * Configuration we use.
36 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 * handle to talk to the peerstore.
41 static struct GNUNET_PEERSTORE_Handle *h;
44 * Subsystem we store the value for.
46 static const char *subsystem = "test_peerstore_api_sync";
49 * Fake PID under which we store the value.
51 static struct GNUNET_PeerIdentity pid;
54 * Test key we're storing the test value under.
56 static const char *key = "test_peerstore_api_store_key";
59 * Test value we are storing.
61 static const char *val = "test_peerstore_api_store_val";
65 * Function that should be called with the result of the
66 * lookup, and finally once with NULL to signal the end
69 * Upon the first call, we set "ok" to success. On the
70 * second call (end of iteration) we terminate the test.
73 * @param record the information stored in the peerstore
74 * @param emsg any error message
75 * @return #GNUNET_YES (all good, continue)
78 iterate_cb (void *cls,
79 const struct GNUNET_PEERSTORE_Record *record,
84 GNUNET_break (NULL == emsg);
87 GNUNET_PEERSTORE_disconnect (h,
89 GNUNET_SCHEDULER_shutdown ();
92 rec_val = record->value;
93 GNUNET_break (0 == strcmp (rec_val, val));
99 * Run the 2nd stage of the test where we fetch the
100 * data that should have been stored.
105 test_cont (void *cls)
107 h = GNUNET_PEERSTORE_connect (cfg);
108 GNUNET_PEERSTORE_iterate (h,
111 GNUNET_TIME_UNIT_FOREVER_REL,
117 * Actually run the test.
122 h = GNUNET_PEERSTORE_connect (cfg);
123 GNUNET_PEERSTORE_store (h,
127 val, strlen (val) + 1,
128 GNUNET_TIME_UNIT_FOREVER_ABS,
129 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
131 GNUNET_PEERSTORE_disconnect (h,
134 /* We need to wait a little bit to give the disconnect
135 a chance to actually finish the operation; otherwise,
136 the test may fail non-deterministically if the new
137 connection is faster than the cleanup routine of the
139 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
146 * Initialize globals and launch the test.
149 * @param c configuration to use
150 * @param peer handle to our peer (unused)
154 const struct GNUNET_CONFIGURATION_Handle *c,
155 struct GNUNET_TESTING_Peer *peer)
158 memset (&pid, 1, sizeof (pid));
164 main (int argc, char *argv[])
167 GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync",
169 "test_peerstore_api_data.conf",
179 /* end of test_peerstore_api_sync.c */