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
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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)
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_peerstore_service.h"
34 * Overall result, 0 for success.
39 * Configuration we use.
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44 * handle to talk to the peerstore.
46 static struct GNUNET_PEERSTORE_Handle *h;
49 * Subsystem we store the value for.
51 static const char *subsystem = "test_peerstore_api_sync";
54 * Fake PID under which we store the value.
56 static struct GNUNET_PeerIdentity pid;
59 * Test key we're storing the test value under.
61 static const char *key = "test_peerstore_api_store_key";
64 * Test value we are storing.
66 static const char *val = "test_peerstore_api_store_val";
70 * Function that should be called with the result of the
71 * lookup, and finally once with NULL to signal the end
74 * Upon the first call, we set "ok" to success. On the
75 * second call (end of iteration) we terminate the test.
78 * @param record the information stored in the peerstore
79 * @param emsg any error message
80 * @return #GNUNET_YES (all good, continue)
83 iterate_cb (void *cls,
84 const struct GNUNET_PEERSTORE_Record *record,
89 GNUNET_break (NULL == emsg);
92 GNUNET_PEERSTORE_disconnect (h,
94 GNUNET_SCHEDULER_shutdown ();
97 rec_val = record->value;
98 GNUNET_break (0 == strcmp (rec_val, val));
104 * Run the 2nd stage of the test where we fetch the
105 * data that should have been stored.
110 test_cont (void *cls)
112 h = GNUNET_PEERSTORE_connect (cfg);
113 GNUNET_PEERSTORE_iterate (h,
116 GNUNET_TIME_UNIT_FOREVER_REL,
122 * Actually run the test.
127 h = GNUNET_PEERSTORE_connect (cfg);
128 GNUNET_PEERSTORE_store (h,
132 val, strlen (val) + 1,
133 GNUNET_TIME_UNIT_FOREVER_ABS,
134 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
136 GNUNET_PEERSTORE_disconnect (h,
139 /* We need to wait a little bit to give the disconnect
140 a chance to actually finish the operation; otherwise,
141 the test may fail non-deterministically if the new
142 connection is faster than the cleanup routine of the
144 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
151 * Initialize globals and launch the test.
154 * @param c configuration to use
155 * @param peer handle to our peer (unused)
159 const struct GNUNET_CONFIGURATION_Handle *c,
160 struct GNUNET_TESTING_Peer *peer)
163 memset (&pid, 1, sizeof (pid));
169 main (int argc, char *argv[])
172 GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync",
174 "test_peerstore_api_data.conf",
184 /* end of test_peerstore_api_sync.c */