peerstore: fixes in watch functionality
[oweals/gnunet.git] / src / peerstore / test_peerstore_api.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.c
22  * @brief testcase for peerstore_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_peerstore_service.h"
28 #include <inttypes.h>
29
30 //TODO: test single cycle of watch, store, iterate
31
32 static int ok = 1;
33
34 static int counter = 0;
35
36 struct GNUNET_PEERSTORE_Handle *h;
37
38 int iterate_cb (void *cls,
39     struct GNUNET_PEERSTORE_Record *record,
40     char *emsg)
41 {
42   if(NULL != emsg)
43   {
44     printf("Error received: %s.\n", emsg);
45     return GNUNET_YES;
46   }
47   printf("Record:\n");
48   if(NULL == record)
49   {
50     GNUNET_assert(counter > 0);
51     counter = 0;
52     printf("END\n");
53     GNUNET_PEERSTORE_disconnect(h);
54     return GNUNET_YES;
55   }
56   printf("Sub system: %s\n", record->sub_system);
57   printf("Peer: %s\n", GNUNET_i2s (record->peer));
58   printf("Key: %s\n", record->key);
59   printf("Value: %.*s\n", (int)record->value_size, (char *)record->value);
60   printf("Expiry: %" PRIu64 "\n", record->expiry->abs_value_us);
61   counter ++;
62
63   return GNUNET_YES;
64 }
65
66 void store_cont(void *cls, int success)
67 {
68   if(GNUNET_OK == success)
69     ok = 0;
70   else
71     ok = 1;
72   printf("Store success: %d\n", success);
73   GNUNET_PEERSTORE_iterate(h, "peerstore-test",
74       NULL,
75       NULL,
76       GNUNET_TIME_UNIT_FOREVER_REL,
77       &iterate_cb,
78       NULL);
79 }
80
81 int watch_cb (void *cls,
82     struct GNUNET_PEERSTORE_Record *record,
83     char *emsg)
84 {
85   if(NULL != emsg)
86   {
87     printf("Error received: %s.\n", emsg);
88     return GNUNET_YES;
89   }
90
91   printf("Watch Record:\n");
92   printf("Sub system: %s\n", record->sub_system);
93   printf("Peer: %s\n", GNUNET_i2s (record->peer));
94   printf("Key: %s\n", record->key);
95   printf("Value: %.*s\n", (int)record->value_size, (char *)record->value);
96   printf("Expiry: %" PRIu64 "\n", record->expiry->abs_value_us);
97   return GNUNET_YES;
98 }
99
100 static void
101 run (void *cls,
102     const struct GNUNET_CONFIGURATION_Handle *cfg,
103     struct GNUNET_TESTING_Peer *peer)
104 {
105   struct GNUNET_PeerIdentity pid;
106   char *val = "peerstore-test-value";
107   size_t val_size = strlen(val);
108   struct GNUNET_TIME_Absolute expiry;
109
110   ok = 1;
111   memset (&pid, 32, sizeof (pid));
112   expiry = GNUNET_TIME_absolute_get();
113   h = GNUNET_PEERSTORE_connect(cfg);
114   GNUNET_assert(NULL != h);
115   GNUNET_PEERSTORE_watch(h,
116       "peerstore-test",
117       &pid,
118       "peerstore-test-key",
119       &watch_cb,
120       NULL);
121   GNUNET_PEERSTORE_store(h,
122       "peerstore-test",
123       &pid,
124       "peerstore-test-key",
125       val,
126       val_size,
127       expiry,
128       &store_cont,
129       NULL);
130 }
131
132 int
133 main (int argc, char *argv[])
134 {
135   if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerstore",
136                  "peerstore",
137                  "test_peerstore_api_data.conf",
138                  &run, NULL))
139     return 1;
140   return ok;
141 }
142
143 /* end of test_peerstore_api.c */