diff options
Diffstat (limited to 'src/service/peerstore/test_peerstore_api_watch.c')
-rw-r--r-- | src/service/peerstore/test_peerstore_api_watch.c | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/src/service/peerstore/test_peerstore_api_watch.c b/src/service/peerstore/test_peerstore_api_watch.c new file mode 100644 index 000000000..8ce39456f --- /dev/null +++ b/src/service/peerstore/test_peerstore_api_watch.c | |||
@@ -0,0 +1,170 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2013-2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file peerstore/test_peerstore_api_watch.c | ||
22 | * @brief testcase for peerstore watch functionality | ||
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 | |||
30 | static int ok = 1; | ||
31 | |||
32 | static struct GNUNET_PEERSTORE_Handle *h; | ||
33 | |||
34 | static struct GNUNET_PEERSTORE_Monitor *wc; | ||
35 | |||
36 | static struct GNUNET_PEERSTORE_StoreContext *sr; | ||
37 | |||
38 | static char *ss = "test_peerstore_api_watch"; | ||
39 | |||
40 | static char *k = "test_peerstore_api_watch_key"; | ||
41 | |||
42 | static char *val = "test_peerstore_api_watch_val"; | ||
43 | |||
44 | static struct GNUNET_PeerIdentity p; | ||
45 | |||
46 | static void | ||
47 | finish (void *cls) | ||
48 | { | ||
49 | if (NULL != sr) | ||
50 | GNUNET_PEERSTORE_store_cancel (sr); | ||
51 | if (NULL != wc) | ||
52 | GNUNET_PEERSTORE_monitor_stop (wc); | ||
53 | GNUNET_PEERSTORE_disconnect (h); | ||
54 | GNUNET_SCHEDULER_shutdown (); | ||
55 | } | ||
56 | |||
57 | |||
58 | /** | ||
59 | * Continuation called with a status result. | ||
60 | * | ||
61 | * @param cls closure | ||
62 | * @param success #GNUNET_OK or #GNUNET_SYSERR | ||
63 | */ | ||
64 | static void | ||
65 | cont2 (void *cls, int success) | ||
66 | { | ||
67 | sr = NULL; | ||
68 | } | ||
69 | |||
70 | |||
71 | static void | ||
72 | cont (void *cls) | ||
73 | { | ||
74 | sr = GNUNET_PEERSTORE_store (h, | ||
75 | ss, | ||
76 | &p, | ||
77 | k, | ||
78 | val, | ||
79 | strlen (val) + 1, | ||
80 | GNUNET_TIME_UNIT_FOREVER_ABS, | ||
81 | GNUNET_PEERSTORE_STOREOPTION_REPLACE, | ||
82 | &cont2, | ||
83 | NULL); | ||
84 | } | ||
85 | |||
86 | |||
87 | static int initial_iteration = GNUNET_YES; | ||
88 | |||
89 | static void | ||
90 | watch_cb (void *cls, | ||
91 | const struct GNUNET_PEERSTORE_Record *record, | ||
92 | const char *emsg) | ||
93 | { | ||
94 | GNUNET_assert (NULL == emsg); | ||
95 | if (GNUNET_YES == initial_iteration) | ||
96 | { | ||
97 | GNUNET_PEERSTORE_monitor_next (wc, 1); | ||
98 | return; | ||
99 | } | ||
100 | if (NULL == record) | ||
101 | { | ||
102 | GNUNET_break (0); | ||
103 | return; | ||
104 | } | ||
105 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received record: %s\n", | ||
106 | (char*) record->value); | ||
107 | GNUNET_assert (0 == strcmp (val, | ||
108 | (char *) record->value)); | ||
109 | ok = 0; | ||
110 | GNUNET_SCHEDULER_add_now (&finish, NULL); | ||
111 | } | ||
112 | |||
113 | |||
114 | static void | ||
115 | sync_cb (void *cls) | ||
116 | { | ||
117 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &cont, NULL); | ||
118 | initial_iteration = GNUNET_NO; | ||
119 | } | ||
120 | |||
121 | |||
122 | static void | ||
123 | error_cb (void *cls) | ||
124 | { | ||
125 | // Never reach this | ||
126 | GNUNET_assert (0); | ||
127 | } | ||
128 | |||
129 | |||
130 | static void | ||
131 | run (void *cls, | ||
132 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
133 | struct GNUNET_TESTING_Peer *peer) | ||
134 | { | ||
135 | |||
136 | h = GNUNET_PEERSTORE_connect (cfg); | ||
137 | GNUNET_assert (NULL != h); | ||
138 | memset (&p, | ||
139 | 4, | ||
140 | sizeof(p)); | ||
141 | wc = GNUNET_PEERSTORE_monitor_start (cfg, | ||
142 | GNUNET_YES, | ||
143 | ss, | ||
144 | &p, | ||
145 | k, | ||
146 | &error_cb, | ||
147 | NULL, | ||
148 | &sync_cb, | ||
149 | NULL, | ||
150 | &watch_cb, | ||
151 | NULL); | ||
152 | } | ||
153 | |||
154 | |||
155 | int | ||
156 | main (int argc, | ||
157 | char *argv[]) | ||
158 | { | ||
159 | if (0 != | ||
160 | GNUNET_TESTING_service_run ("test-gnunet-peerstore", | ||
161 | "peerstore", | ||
162 | "test_peerstore_api_data.conf", | ||
163 | &run, | ||
164 | NULL)) | ||
165 | return 1; | ||
166 | return ok; | ||
167 | } | ||
168 | |||
169 | |||
170 | /* end of test_peerstore_api_watch.c */ | ||