paragraph for gnunet devs that don't know how to use the web
[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 /**
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)
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_peerstore_service.h"
30
31 /**
32  * Overall result, 0 for success.
33  */
34 static int ok = 404;
35
36 /**
37  * Configuration we use.
38  */
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
40
41 /**
42  * handle to talk to the peerstore.
43  */
44 static struct GNUNET_PEERSTORE_Handle *h;
45
46 /**
47  * Subsystem we store the value for.
48  */
49 static const char *subsystem = "test_peerstore_api_sync";
50
51 /**
52  * Fake PID under which we store the value.
53  */
54 static struct GNUNET_PeerIdentity pid;
55
56 /**
57  * Test key we're storing the test value under.
58  */
59 static const char *key = "test_peerstore_api_store_key";
60
61 /**
62  * Test value we are storing.
63  */
64 static const char *val = "test_peerstore_api_store_val";
65
66
67 /**
68  * Function that should be called with the result of the
69  * lookup, and finally once with NULL to signal the end
70  * of the iteration.
71  *
72  * Upon the first call, we set "ok" to success. On the
73  * second call (end of iteration) we terminate the test.
74  *
75  * @param cls NULL
76  * @param record the information stored in the peerstore
77  * @param emsg any error message
78  * @return #GNUNET_YES (all good, continue)
79  */
80 static void
81 iterate_cb (void *cls,
82             const struct GNUNET_PEERSTORE_Record *record,
83             const char *emsg)
84 {
85   const char *rec_val;
86
87   GNUNET_break (NULL == emsg);
88   if (NULL == record)
89   {
90     GNUNET_PEERSTORE_disconnect (h,
91                                  GNUNET_NO);
92     GNUNET_SCHEDULER_shutdown ();
93     return;
94   }
95   rec_val = record->value;
96   GNUNET_break (0 == strcmp (rec_val, val));
97   ok = 0;
98 }
99
100
101 /**
102  * Run the 2nd stage of the test where we fetch the
103  * data that should have been stored.
104  *
105  * @param cls NULL
106  */
107 static void
108 test_cont (void *cls)
109 {
110   h = GNUNET_PEERSTORE_connect (cfg);
111   GNUNET_PEERSTORE_iterate (h,
112                             subsystem,
113                             &pid, key,
114                             GNUNET_TIME_UNIT_FOREVER_REL,
115                             &iterate_cb, NULL);
116 }
117
118
119 /**
120  * Actually run the test.
121  */
122 static void
123 test1 ()
124 {
125   h = GNUNET_PEERSTORE_connect (cfg);
126   GNUNET_PEERSTORE_store (h,
127                           subsystem,
128                           &pid,
129                           key,
130                           val, strlen (val) + 1,
131                           GNUNET_TIME_UNIT_FOREVER_ABS,
132                           GNUNET_PEERSTORE_STOREOPTION_REPLACE,
133                           NULL, NULL);
134   GNUNET_PEERSTORE_disconnect (h,
135                                GNUNET_YES);
136   h = NULL;
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
141      old one. */
142   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
143                                 &test_cont,
144                                 NULL);
145 }
146
147
148 /**
149  * Initialize globals and launch the test.
150  *
151  * @param cls NULL
152  * @param c configuration to use
153  * @param peer handle to our peer (unused)
154  */
155 static void
156 run (void *cls,
157      const struct GNUNET_CONFIGURATION_Handle *c,
158      struct GNUNET_TESTING_Peer *peer)
159 {
160   cfg = c;
161   memset (&pid, 1, sizeof (pid));
162   test1 ();
163 }
164
165
166 int
167 main (int argc, char *argv[])
168 {
169   if (0 !=
170       GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync",
171                                   "peerstore",
172                                   "test_peerstore_api_data.conf",
173                                   &run, NULL))
174     return 1;
175   if (0 != ok)
176     fprintf (stderr,
177              "Test failed: %d\n",
178              ok);
179   return ok;
180 }
181
182 /* end of test_peerstore_api_sync.c */