794a2b04867e9da412d9e8e37d59306d1e767fa6
[oweals/gnunet.git] / src / peerstore / test_peerstore_api_store.c
1 /*
2      This file is part of GNUnet.
3      (C)
4
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.
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      General Public License for more details.
14
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file peerstore/test_peerstore_api_store.c
22  * @brief testcase for peerstore store operation
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_peerstore_service.h"
28
29 static int ok = 1;
30
31 static struct GNUNET_PEERSTORE_Handle *h;
32
33 static char *subsystem = "test_peerstore_api_store";
34 static struct GNUNET_PeerIdentity pid;
35 static char *key = "test_peerstore_api_store_key";
36 static char *val1 = "test_peerstore_api_store_val1";
37 static char *val2 = "test_peerstore_api_store_val2-";
38 static char *val3 = "test_peerstore_api_store_val3--";
39
40 static int count = 0;
41
42 static int
43 test3_cont2 (void *cls,
44              const struct GNUNET_PEERSTORE_Record *record,
45              const char *emsg)
46 {
47   if (NULL != emsg)
48     return GNUNET_NO;
49   if (NULL != record)
50   {
51     GNUNET_assert ((strlen (val3) + 1) == record->value_size);
52     GNUNET_assert (0 == strcmp ((char *) val3, (char *) record->value));
53     count++;
54     return GNUNET_YES;
55   }
56   GNUNET_assert (count == 1);
57   ok = 0;
58   GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
59   GNUNET_SCHEDULER_shutdown ();
60   return GNUNET_YES;
61 }
62
63
64 static void
65 test3_cont (void *cls, int success)
66 {
67   if (GNUNET_YES != success)
68     return;
69   count = 0;
70   GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS,
71                             &test3_cont2, NULL);
72 }
73
74
75 /**
76  * Replace the previous 2 records
77  */
78 static void
79 test3 ()
80 {
81   GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val3, strlen (val3) + 1,
82                           GNUNET_TIME_UNIT_FOREVER_ABS,
83                           GNUNET_PEERSTORE_STOREOPTION_REPLACE, &test3_cont,
84                           NULL);
85 }
86
87
88 static int
89 test2_cont2 (void *cls,
90              const struct GNUNET_PEERSTORE_Record *record,
91              const char *emsg)
92 {
93   if (NULL != emsg)
94     return GNUNET_NO;
95   if (NULL != record)
96   {
97     GNUNET_assert (((strlen (val1) + 1) == record->value_size) ||
98                    ((strlen (val2) + 1) == record->value_size));
99     GNUNET_assert ((0 == strcmp ((char *) val1, (char *) record->value)) ||
100                    (0 == strcmp ((char *) val2, (char *) record->value)));
101     count++;
102     return GNUNET_YES;
103   }
104   GNUNET_assert (count == 2);
105   count = 0;
106   test3 ();
107   return GNUNET_YES;
108 }
109
110
111 static void
112 test2_cont (void *cls, int success)
113 {
114   if (GNUNET_YES != success)
115     return;
116   count = 0;
117   GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS,
118                             &test2_cont2, NULL);
119 }
120
121
122 /**
123  * Test storing a second value with the same key
124  */
125 void
126 test2 ()
127 {
128   GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val2, strlen (val2) + 1,
129                           GNUNET_TIME_UNIT_FOREVER_ABS,
130                           GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, &test2_cont,
131                           NULL);
132 }
133
134
135 static int
136 test1_cont2 (void *cls,
137              const struct GNUNET_PEERSTORE_Record *record,
138              const char *emsg)
139 {
140   if (NULL != emsg)
141     return GNUNET_NO;
142   if (NULL != record)
143   {
144     GNUNET_assert ((strlen (val1) + 1) == record->value_size);
145     GNUNET_assert (0 == strcmp ((char *) val1, (char *) record->value));
146     count++;
147     return GNUNET_YES;
148   }
149   GNUNET_assert (count == 1);
150   count = 0;
151   test2 ();
152   return GNUNET_YES;
153 }
154
155
156 static void
157 test1_cont (void *cls, int success)
158 {
159   if (GNUNET_YES != success)
160     return;
161   count = 0;
162   GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS,
163                             &test1_cont2, NULL);
164 }
165
166
167 /**
168  * Store a single record
169  */
170 static void
171 test1 ()
172 {
173   GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val1, strlen (val1) + 1,
174                           GNUNET_TIME_UNIT_FOREVER_ABS,
175                           GNUNET_PEERSTORE_STOREOPTION_REPLACE, &test1_cont,
176                           NULL);
177 }
178
179
180 static void
181 run (void *cls,
182      const struct GNUNET_CONFIGURATION_Handle *cfg,
183      struct GNUNET_TESTING_Peer *peer)
184 {
185   h = GNUNET_PEERSTORE_connect (cfg);
186   GNUNET_assert (NULL != h);
187   memset (&pid, 1, sizeof (pid));
188   test1 ();
189 }
190
191
192 int
193 main (int argc, char *argv[])
194 {
195   if (0 !=
196       GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore",
197                                   "test_peerstore_api_data.conf", &run, NULL))
198     return 1;
199   return ok;
200 }
201
202 /* end of test_peerstore_api_store.c */