- moved timeout handling responsibility from for nat tests from caller to the library
[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 struct GNUNET_PEERSTORE_Handle *h;
32
33 char *ss = "test_peerstore_api_iterate";
34 struct GNUNET_PeerIdentity p1;
35 struct GNUNET_PeerIdentity p2;
36 char *k1 = "test_peerstore_api_iterate_key1";
37 char *k2 = "test_peerstore_api_iterate_key2";
38 char *k3 = "test_peerstore_api_iterate_key3";
39 char *val = "test_peerstore_api_iterate_val";
40 int count = 0;
41
42 static int
43 iter3_cb(void *cls,
44     struct GNUNET_PEERSTORE_Record *record,
45     char *emsg)
46 {
47   if(NULL != emsg)
48     return GNUNET_NO;
49   if(NULL != record)
50   {
51     count++;
52     return GNUNET_YES;
53   }
54   GNUNET_assert(count == 3);
55   ok = 0;
56   GNUNET_PEERSTORE_disconnect(h);
57   GNUNET_SCHEDULER_shutdown();
58   return GNUNET_YES;
59 }
60
61 static int
62 iter2_cb(void *cls,
63     struct GNUNET_PEERSTORE_Record *record,
64     char *emsg)
65 {
66   if(NULL != emsg)
67     return GNUNET_NO;
68   if(NULL != record)
69   {
70     count++;
71     return GNUNET_YES;
72   }
73   GNUNET_assert(count == 2);
74   count = 0;
75   GNUNET_PEERSTORE_iterate(h,
76       ss,
77       NULL,
78       NULL,
79       GNUNET_TIME_UNIT_FOREVER_REL,
80       iter3_cb,
81       NULL);
82   return GNUNET_YES;
83 }
84
85 static int
86 iter1_cb(void *cls,
87     struct GNUNET_PEERSTORE_Record *record,
88     char *emsg)
89 {
90   if(NULL != emsg)
91     return GNUNET_NO;
92   if(NULL != record)
93   {
94     count++;
95     return GNUNET_YES;
96   }
97   GNUNET_assert(count == 1);
98   count = 0;
99   GNUNET_PEERSTORE_iterate(h,
100       ss,
101       &p1,
102       NULL,
103       GNUNET_TIME_UNIT_FOREVER_REL,
104       iter2_cb,
105       NULL);
106   return GNUNET_YES;
107 }
108
109 static void
110 run (void *cls,
111     const struct GNUNET_CONFIGURATION_Handle *cfg,
112     struct GNUNET_TESTING_Peer *peer)
113 {
114   h = GNUNET_PEERSTORE_connect(cfg);
115   GNUNET_assert(NULL != h);
116   memset (&p1, 1, sizeof (p1));
117   memset (&p2, 2, sizeof (p2));
118   GNUNET_PEERSTORE_store(h,
119       ss,
120       &p1,
121       k1,
122       val,
123       strlen(val) + 1,
124       GNUNET_TIME_UNIT_FOREVER_ABS,
125       GNUNET_PEERSTORE_STOREOPTION_REPLACE,
126       NULL,
127       NULL);
128   GNUNET_PEERSTORE_store(h,
129       ss,
130       &p1,
131       k2,
132       val,
133       strlen(val) + 1,
134       GNUNET_TIME_UNIT_FOREVER_ABS,
135       GNUNET_PEERSTORE_STOREOPTION_REPLACE,
136       NULL,
137       NULL);
138   GNUNET_PEERSTORE_store(h,
139       ss,
140       &p2,
141       k3,
142       val,
143       strlen(val) + 1,
144       GNUNET_TIME_UNIT_FOREVER_ABS,
145       GNUNET_PEERSTORE_STOREOPTION_REPLACE,
146       NULL,
147       NULL);
148   GNUNET_PEERSTORE_iterate(h,
149       ss,
150       &p1,
151       k1,
152       GNUNET_TIME_UNIT_FOREVER_REL,
153       iter1_cb,
154       NULL);
155 }
156
157 int
158 main (int argc, char *argv[])
159 {
160   if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerstore",
161                  "peerstore",
162                  "test_peerstore_api_data.conf",
163                  &run, NULL))
164     return 1;
165   return ok;
166 }
167
168 /* end of test_peerstore_api_iterate.c */