making GNUNET_SCHEDULER_cancel() perform in O(1) instead of O(n) to help or even...
[oweals/gnunet.git] / src / multicast / test_multicast.c
1 /*
2  * This file is part of GNUnet
3  * (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file multicast/test_multicast.c
23  * @brief Tests for the Multicast API.
24  * @author Gabor X Toth
25  */
26
27 #include <inttypes.h>
28
29 #include "platform.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_common.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_testing_lib.h"
34 #include "gnunet_multicast_service.h"
35
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37
38 /**
39  * Return value from 'main'.
40  */
41 static int res;
42
43 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 /**
46  * Handle for task for timeout termination.
47  */
48 static struct GNUNET_SCHEDULER_Task * end_badly_task;
49
50
51 /**
52  * Clean up all resources used.
53  */
54 static void
55 cleanup ()
56 {
57
58 }
59
60
61 /**
62  * Terminate the test case (failure).
63  *
64  * @param cls NULL
65  * @param tc scheduler context
66  */
67 static void
68 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70   res = 1;
71   cleanup ();
72   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
73 }
74
75
76 /**
77  * Terminate the test case (success).
78  *
79  * @param cls NULL
80  * @param tc scheduler context
81  */
82 static void
83 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 {
85   res = 0;
86   cleanup ();
87   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
88 }
89
90
91 /**
92  * Finish the test case (successfully).
93  */
94 static void
95 end ()
96 {
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
98
99   if (end_badly_task != NULL)
100   {
101     GNUNET_SCHEDULER_cancel (end_badly_task);
102     end_badly_task = NULL;
103   }
104   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
105                                 &end_normally, NULL);
106 }
107
108
109 /**
110  * Main function of the test, run from scheduler.
111  *
112  * @param cls NULL
113  * @param cfg configuration we use (also to connect to Multicast service)
114  * @param peer handle to access more of the peer (not used)
115  */
116 static void
117 #if DEBUG_TEST_MULTICAST
118 run (void *cls, char *const *args, const char *cfgfile,
119      const struct GNUNET_CONFIGURATION_Handle *c)
120 #else
121 run (void *cls,
122      const struct GNUNET_CONFIGURATION_Handle *c,
123      struct GNUNET_TESTING_Peer *peer)
124 #endif
125 {
126   cfg = c;
127   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
128
129   /* FIXME: add tests */
130
131   end ();
132 }
133
134
135 int
136 main (int argc, char *argv[])
137 {
138   res = 1;
139 #if DEBUG_TEST_MULTICAST
140   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
141     GNUNET_GETOPT_OPTION_END
142   };
143   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-multicast",
144                                        "test-multicast [options]",
145                                        opts, &run, NULL))
146     return 1;
147 #else
148   if (0 != GNUNET_TESTING_peer_run ("test-multicast", "test_multicast.conf", &run, NULL))
149     return 1;
150 #endif
151   return res;
152 }
153
154 /* end of test_multicast.c */