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