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