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