fix bad free
[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 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
19 /**
20  * @file core/test_core_api_send_to_self.c
21  * @brief test that sending a message to ourselves via CORE works
22  * @author Philipp Toelke
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_constants.h"
31
32 /**
33  * Final status code.
34  */
35 static int ret;
36
37 /**
38  * Handle to the cleanup task.
39  */
40 static struct GNUNET_SCHEDULER_Task *die_task;
41
42 /**
43  * Identity of this peer.
44  */
45 static struct GNUNET_PeerIdentity myself;
46
47 /**
48  * The handle to core
49  */
50 static struct GNUNET_CORE_Handle *core;
51
52
53 /**
54  * Function scheduled as very last function, cleans up after us
55  */
56 static void
57 cleanup (void *cls)
58 {
59   if (NULL != die_task)
60   {
61     GNUNET_SCHEDULER_cancel (die_task);
62     die_task = NULL;
63   }
64   if (NULL != core)
65   {
66     GNUNET_CORE_disconnect (core);
67     core = NULL;
68   }
69   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70               "Ending test.\n");
71 }
72
73
74 /**
75  * Function scheduled as very last function, cleans up after us
76  */
77 static void
78 do_timeout (void *cls)
79 {
80   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
81               "Test timeout.\n");
82   die_task = NULL;
83   GNUNET_SCHEDULER_shutdown ();
84 }
85
86
87 static void
88 handle_test (void *cls,
89              const struct GNUNET_MessageHeader *message)
90 {
91   GNUNET_SCHEDULER_shutdown ();
92   ret = 0;
93 }
94
95
96 static void
97 init (void *cls,
98       const struct GNUNET_PeerIdentity *my_identity)
99 {
100   if (NULL == my_identity)
101   {
102     GNUNET_break (0);
103     return;
104   }
105   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
106               "Correctly connected to CORE; we are the peer %s.\n",
107               GNUNET_i2s (my_identity));
108   GNUNET_memcpy (&myself,
109                  my_identity,
110                  sizeof (struct GNUNET_PeerIdentity));
111 }
112
113
114 static void *
115 connect_cb (void *cls,
116             const struct GNUNET_PeerIdentity *peer,
117             struct GNUNET_MQ_Handle *mq)
118 {
119   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120               "Connected to peer %s.\n",
121               GNUNET_i2s (peer));
122   if (0 == memcmp (peer,
123                    &myself,
124                    sizeof (struct GNUNET_PeerIdentity)))
125   {
126     struct GNUNET_MQ_Envelope *env;
127     struct GNUNET_MessageHeader *msg;
128
129     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130                 "Connected to myself; sending message!\n");
131     env = GNUNET_MQ_msg (msg,
132                          GNUNET_MESSAGE_TYPE_DUMMY);
133     GNUNET_MQ_send (mq,
134                     env);
135   }
136   return NULL;
137 }
138
139
140 /**
141  * Main function that will be run by the scheduler.
142  *
143  * @param cls closure
144  * @param cfg configuration
145  */
146 static void
147 run (void *cls,
148      const struct GNUNET_CONFIGURATION_Handle *cfg,
149      struct GNUNET_TESTING_Peer *peer)
150 {
151   struct GNUNET_MQ_MessageHandler handlers[] = {
152     GNUNET_MQ_hd_fixed_size (test,
153                              GNUNET_MESSAGE_TYPE_DUMMY,
154                              struct GNUNET_MessageHeader,
155                              NULL),
156     GNUNET_MQ_handler_end ()
157   };
158
159   core =
160     GNUNET_CORE_connect (cfg,
161                          NULL,
162                          &init,
163                          &connect_cb,
164                          NULL,
165                          handlers);
166   GNUNET_SCHEDULER_add_shutdown (&cleanup,
167                                  NULL);
168   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
169                                            &do_timeout,
170                                            NULL);
171 }
172
173
174 /**
175  * The main function to test sending a message to the local peer via core
176  *
177  * @param argc number of arguments from the command line
178  * @param argv command line arguments
179  * @return 0 ok, 1 on error
180  */
181 int
182 main (int argc, char *argv[])
183 {
184   ret = 1;
185   if (0 != GNUNET_TESTING_peer_run ("test-core-api-send-to-self",
186                                     "test_core_api_peer1.conf",
187                                     &run, NULL))
188     return 1;
189   return ret;
190 }
191
192 /* end of test_core_api_send_to_self.c */