-fix format warning
[oweals/gnunet.git] / src / peerstore / test_peerstore_api_iterate.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file peerstore/test_peerstore_api_iterate.c
22  * @brief testcase for peerstore iteration 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 static struct GNUNET_PEERSTORE_IterateContext *ic;
33
34 static char *ss = "test_peerstore_api_iterate";
35 static struct GNUNET_PeerIdentity p1;
36 static struct GNUNET_PeerIdentity p2;
37 static char *k1 = "test_peerstore_api_iterate_key1";
38 static char *k2 = "test_peerstore_api_iterate_key2";
39 static char *k3 = "test_peerstore_api_iterate_key3";
40 static char *val = "test_peerstore_api_iterate_val";
41 static int count = 0;
42
43
44 static void
45 iter3_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record,
46           const char *emsg)
47 {
48   if (NULL != emsg)
49   {
50     GNUNET_PEERSTORE_iterate_cancel (ic);
51     return;
52   }
53   if (NULL != record)
54   {
55     count++;
56     return;
57   }
58   GNUNET_assert (count == 3);
59   ok = 0;
60   GNUNET_PEERSTORE_disconnect (h, GNUNET_NO);
61   GNUNET_SCHEDULER_shutdown ();
62 }
63
64
65 static void
66 iter2_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record,
67           const char *emsg)
68 {
69   if (NULL != emsg)
70   {
71     GNUNET_PEERSTORE_iterate_cancel (ic);
72     return;
73   }
74   if (NULL != record)
75   {
76     count++;
77     return;
78   }
79   GNUNET_assert (count == 2);
80   count = 0;
81   ic = GNUNET_PEERSTORE_iterate (h, ss, NULL, NULL, GNUNET_TIME_UNIT_FOREVER_REL,
82                             iter3_cb, NULL);
83 }
84
85
86 static void
87 iter1_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record,
88           const char *emsg)
89 {
90   if (NULL != emsg)
91   {
92     GNUNET_PEERSTORE_iterate_cancel (ic);
93     return;
94   }
95   if (NULL != record)
96   {
97     count++;
98     return;
99   }
100   GNUNET_assert (count == 1);
101   count = 0;
102   ic = GNUNET_PEERSTORE_iterate (h, ss, &p1, NULL, GNUNET_TIME_UNIT_FOREVER_REL,
103                             iter2_cb, NULL);
104 }
105
106
107 static void
108 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
109      struct GNUNET_TESTING_Peer *peer)
110 {
111   h = GNUNET_PEERSTORE_connect (cfg);
112   GNUNET_assert (NULL != h);
113   memset (&p1, 1, sizeof (p1));
114   memset (&p2, 2, sizeof (p2));
115   GNUNET_PEERSTORE_store (h, ss, &p1, k1, val, strlen (val) + 1,
116                           GNUNET_TIME_UNIT_FOREVER_ABS,
117                           GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL);
118   GNUNET_PEERSTORE_store (h, ss, &p1, k2, val, strlen (val) + 1,
119                           GNUNET_TIME_UNIT_FOREVER_ABS,
120                           GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL);
121   GNUNET_PEERSTORE_store (h, ss, &p2, k3, val, strlen (val) + 1,
122                           GNUNET_TIME_UNIT_FOREVER_ABS,
123                           GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL);
124   ic = GNUNET_PEERSTORE_iterate (h, ss, &p1, k1, GNUNET_TIME_UNIT_FOREVER_REL,
125                             iter1_cb, NULL);
126 }
127
128
129 int
130 main (int argc, char *argv[])
131 {
132   if (0 !=
133       GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore",
134                                   "test_peerstore_api_data.conf", &run, NULL))
135     return 1;
136   return ok;
137 }
138
139 /* end of test_peerstore_api_iterate.c */