aboutsummaryrefslogtreecommitdiff
path: root/src/service/dht/test_dht_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/dht/test_dht_api.c')
-rw-r--r--src/service/dht/test_dht_api.c193
1 files changed, 193 insertions, 0 deletions
diff --git a/src/service/dht/test_dht_api.c b/src/service/dht/test_dht_api.c
new file mode 100644
index 000000000..0b51477fe
--- /dev/null
+++ b/src/service/dht/test_dht_api.c
@@ -0,0 +1,193 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2015, 2017 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 dht/test_dht_api.c
22 * @brief base test case for dht api
23 * @author Christian Grothoff
24 *
25 * This test case tests DHT api to DUMMY DHT service communication.
26 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_testing_lib.h"
30#include "gnunet_dht_service.h"
31
32
33/**
34 * How long until we really give up on a particular testcase portion?
35 */
36#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
37 60)
38
39static struct GNUNET_DHT_Handle *dht_handle;
40
41static struct GNUNET_DHT_GetHandle *get_handle;
42
43static struct GNUNET_DHT_PutHandle *put_handle;
44
45static int ok = 1;
46
47static struct GNUNET_SCHEDULER_Task *die_task;
48
49
50static void
51do_shutdown (void *cls)
52{
53 if (NULL != die_task)
54 {
55 GNUNET_SCHEDULER_cancel (die_task);
56 die_task = NULL;
57 }
58 if (NULL != put_handle)
59 {
60 GNUNET_DHT_put_cancel (put_handle);
61 put_handle = NULL;
62 }
63 if (NULL != get_handle)
64 {
65 GNUNET_DHT_get_stop (get_handle);
66 get_handle = NULL;
67 }
68 GNUNET_DHT_disconnect (dht_handle);
69 dht_handle = NULL;
70}
71
72
73static void
74end_badly (void *cls)
75{
76 die_task = NULL;
77 fprintf (stderr,
78 "%s",
79 "Ending on an unhappy note.\n");
80 GNUNET_SCHEDULER_shutdown ();
81 ok = 1;
82}
83
84
85static void
86test_get_iterator (void *cls,
87 struct GNUNET_TIME_Absolute exp,
88 const struct GNUNET_HashCode *key,
89 const struct GNUNET_PeerIdentity *trunc_peer,
90 const struct GNUNET_DHT_PathElement *get_path,
91 unsigned int get_path_length,
92 const struct GNUNET_DHT_PathElement *put_path,
93 unsigned int put_path_length,
94 enum GNUNET_BLOCK_Type type,
95 size_t size,
96 const void *data)
97{
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 "test_get_iterator called (we got a result), stopping get request!\n");
100 GNUNET_SCHEDULER_shutdown ();
101 ok = 0;
102}
103
104
105/**
106 * Signature of the main function of a task.
107 *
108 * @param cls closure
109 */
110static void
111test_get (void *cls)
112{
113 struct GNUNET_HashCode hash;
114
115 put_handle = NULL;
116 memset (&hash,
117 42,
118 sizeof(struct GNUNET_HashCode));
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120 "Called test_get!\n");
121 GNUNET_assert (dht_handle != NULL);
122 get_handle = GNUNET_DHT_get_start (dht_handle,
123 GNUNET_BLOCK_TYPE_TEST,
124 &hash,
125 1,
126 GNUNET_DHT_RO_NONE,
127 NULL,
128 0,
129 &test_get_iterator,
130 NULL);
131
132 if (NULL == get_handle)
133 {
134 GNUNET_break (0);
135 ok = 1;
136 GNUNET_SCHEDULER_shutdown ();
137 return;
138 }
139}
140
141
142static void
143run (void *cls,
144 const struct GNUNET_CONFIGURATION_Handle *cfg,
145 struct GNUNET_TESTING_Peer *peer)
146{
147 struct GNUNET_HashCode hash;
148 char *data;
149 size_t data_size = 42;
150
151 GNUNET_assert (ok == 1);
152 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
153 NULL);
154 die_task = GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT,
155 &end_badly,
156 NULL);
157 memset (&hash,
158 42,
159 sizeof(struct GNUNET_HashCode));
160 data = GNUNET_malloc (data_size);
161 memset (data, 43, data_size);
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "Called test_put!\n");
164 dht_handle = GNUNET_DHT_connect (cfg,
165 100);
166 GNUNET_assert (NULL != dht_handle);
167 put_handle = GNUNET_DHT_put (dht_handle,
168 &hash,
169 1,
170 GNUNET_DHT_RO_NONE,
171 GNUNET_BLOCK_TYPE_TEST,
172 data_size,
173 data,
174 GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
175 &test_get,
176 NULL);
177 GNUNET_free (data);
178}
179
180
181int
182main (int argc,
183 char *argv[])
184{
185 if (0 != GNUNET_TESTING_peer_run ("test-dht-api",
186 "test_dht_api_data.conf",
187 &run, NULL))
188 return 1;
189 return ok;
190}
191
192
193/* end of test_dht_api.c */