-fix format warning
[oweals/gnunet.git] / src / core / test_core_api_send_to_self.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file core/test_core_api_send_to_self.c
23  * @brief test that sending a message to ourselves via CORE works
24  * @author Philipp Toelke
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_constants.h"
32
33 /**
34  * Final status code.
35  */
36 static int ret;
37
38 /**
39  * Handle to the cleanup task.
40  */
41 static struct GNUNET_SCHEDULER_Task *die_task;
42
43 /**
44  * Identity of this peer.
45  */
46 static struct GNUNET_PeerIdentity myself;
47
48 /**
49  * The handle to core
50  */
51 static struct GNUNET_CORE_Handle *core;
52
53
54 /**
55  * Function scheduled as very last function, cleans up after us
56  */
57 static void
58 cleanup (void *cls)
59 {
60   if (NULL != die_task)
61   {
62     GNUNET_SCHEDULER_cancel (die_task);
63     die_task = NULL;
64   }
65   if (NULL != core)
66   {
67     GNUNET_CORE_disconnect (core);
68     core = NULL;
69   }
70   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
71               "Ending test.\n");
72 }
73
74
75 /**
76  * Function scheduled as very last function, cleans up after us
77  */
78 static void
79 do_timeout (void *cls)
80 {
81   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
82               "Test timeout.\n");
83   die_task = NULL;
84   GNUNET_SCHEDULER_shutdown ();
85 }
86
87
88 static int
89 receive (void *cls,
90          const struct GNUNET_PeerIdentity *other,
91          const struct GNUNET_MessageHeader *message)
92 {
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94               "Received message from peer %s\n",
95               GNUNET_i2s (other));
96   GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (message->type));
97   GNUNET_assert (0 == memcmp (other, &myself, sizeof (myself)));
98   GNUNET_SCHEDULER_shutdown ();
99   ret = 0;
100   return GNUNET_OK;
101 }
102
103
104 static size_t
105 send_message (void *cls,
106               size_t size,
107               void *buf)
108 {
109   struct GNUNET_MessageHeader *hdr = buf;
110   if ( (size == 0) || (buf == NULL) )
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113                 "Could not send; got 0 buffer\n");
114     return 0;
115   }
116   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117               "Sending!\n");
118   hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
119   hdr->type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
120   return ntohs (hdr->size);
121 }
122
123
124 static void
125 init (void *cls,
126       const struct GNUNET_PeerIdentity *my_identity)
127 {
128   if (NULL == my_identity)
129   {
130     GNUNET_break (0);
131     return;
132   }
133   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
134               "Correctly connected to CORE; we are the peer %s.\n",
135               GNUNET_i2s (my_identity));
136   memcpy (&myself,
137           my_identity,
138           sizeof (struct GNUNET_PeerIdentity));
139 }
140
141
142 static void
143 connect_cb (void *cls,
144             const struct GNUNET_PeerIdentity *peer)
145 {
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147               "Connected to peer %s.\n",
148               GNUNET_i2s (peer));
149   if (0 == memcmp (peer,
150                    &myself,
151                    sizeof (struct GNUNET_PeerIdentity)))
152   {
153     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154                 "Connected to myself; sending message!\n");
155     GNUNET_CORE_notify_transmit_ready (core,
156                                        GNUNET_YES,
157                                        0,
158                                        GNUNET_TIME_UNIT_FOREVER_REL,
159                                        peer,
160                                        sizeof (struct GNUNET_MessageHeader),
161                                        &send_message, NULL);
162   }
163 }
164
165
166 /**
167  * Main function that will be run by the scheduler.
168  *
169  * @param cls closure
170  * @param cfg configuration
171  */
172 static void
173 run (void *cls,
174      const struct GNUNET_CONFIGURATION_Handle *cfg,
175      struct GNUNET_TESTING_Peer *peer)
176 {
177   const static struct GNUNET_CORE_MessageHandler handlers[] = {
178     { &receive,
179       GNUNET_MESSAGE_TYPE_DUMMY,
180       sizeof (struct GNUNET_MessageHeader) },
181     {NULL, 0, 0}
182   };
183   core =
184     GNUNET_CORE_connect (cfg, NULL, &init,
185                          &connect_cb, NULL, NULL,
186                          0, NULL, 0, handlers);
187   GNUNET_SCHEDULER_add_shutdown (&cleanup,
188                                  NULL);
189   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
190                                            &do_timeout,
191                                            NULL);
192 }
193
194
195 /**
196  * The main function to test sending a message to the local peer via core
197  *
198  * @param argc number of arguments from the command line
199  * @param argv command line arguments
200  * @return 0 ok, 1 on error
201  */
202 int
203 main (int argc, char *argv[])
204 {
205   ret = 1;
206   if (0 != GNUNET_TESTING_peer_run ("test-core-api-send-to-self",
207                                     "test_core_api_peer1.conf",
208                                     &run, NULL))
209     return 1;
210   return ret;
211 }
212
213 /* end of test_core_api_send_to_self.c */