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