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