glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / testbed / test_testbed_underlay.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 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
16 /**
17  * @file testbed/test_testbed_underlay.c
18  * @brief testcase binary for testing testbed underlay restrictions
19  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20  */
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_testbed_service.h"
25
26
27 /**
28  * Number of peers we start in this test case
29  */
30 #define NUM_PEERS 3
31
32 /**
33  * Result of this test case
34  */
35 static int result;
36
37 static struct GNUNET_TESTBED_Operation *op;
38
39
40 /**
41  * Shutdown testcase
42  *
43  * @param cls NULL
44  * @param tc scheduler task context
45  */
46 static void
47 do_shutdown (void *cls)
48 {
49   if (NULL != op)
50     GNUNET_TESTBED_operation_done (op);
51   op = NULL;
52 }
53
54
55 /**
56  * Callback to be called when an operation is completed
57  *
58  * @param cls the callback closure from functions generating an operation
59  * @param op the operation that has been finished
60  * @param emsg error message in case the operation has failed; will be NULL if
61  *          operation has executed successfully.
62  */
63 static void
64 overlay_connect_status (void *cls,
65                         struct GNUNET_TESTBED_Operation *op_,
66                         const char *emsg)
67 {
68   GNUNET_assert (op_ == op);
69   GNUNET_TESTBED_operation_done (op);
70   op = NULL;
71   if (NULL == emsg)
72     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers 0 and 2 should not get connected\n");
73   else
74   {
75     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers 0 and 2 not connected: %s.  Success!\n", emsg);
76     result = GNUNET_OK;
77   }
78   GNUNET_SCHEDULER_shutdown ();
79 }
80
81
82
83 /**
84  * Signature of a main function for a testcase.
85  *
86  * @param cls closure
87  * @param h the run handle
88  * @param num_peers number of peers in 'peers'
89  * @param peers_ handle to peers run in the testbed
90  * @param links_succeeded the number of overlay link connection attempts that
91  *          succeeded
92  * @param links_failed the number of overlay link connection attempts that
93  *          failed
94  */
95 static void
96 test_master (void *cls,
97              struct GNUNET_TESTBED_RunHandle *h,
98              unsigned int num_peers,
99              struct GNUNET_TESTBED_Peer **peers_,
100              unsigned int links_succeeded,
101              unsigned int links_failed)
102 {
103   GNUNET_assert (NULL == cls);
104   if (NULL == peers_)
105   {
106     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test due to timeout\n");
107     GNUNET_SCHEDULER_shutdown ();
108     return;
109   }
110   GNUNET_assert (NUM_PEERS == num_peers);
111   op = GNUNET_TESTBED_overlay_connect (NULL,
112                                        &overlay_connect_status,
113                                        NULL,
114                                        peers_[0],
115                                        peers_[2]);
116   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
117                                                                60),
118                                 &do_shutdown, NULL);
119 }
120
121
122 #ifndef PATH_MAX
123 /**
124  * Assumed maximum path length (for the log file name).
125  */
126 #define PATH_MAX 4096
127 #endif
128
129
130 /**
131  * Main function
132  */
133 int
134 main (int argc, char **argv)
135 {
136   struct GNUNET_CONFIGURATION_Handle *cfg;
137   char pwd[PATH_MAX];
138   char *dbfile;
139   uint64_t event_mask;
140
141   result = GNUNET_SYSERR;
142   event_mask = 0;
143   cfg = GNUNET_CONFIGURATION_create ();
144   GNUNET_assert (GNUNET_YES ==
145                  GNUNET_CONFIGURATION_parse (cfg,
146                                              "test_testbed_underlay.conf.in"));
147   if (NULL == getcwd (pwd, PATH_MAX))
148     return 1;
149   GNUNET_assert (0 < GNUNET_asprintf (&dbfile, "%s/%s", pwd,
150                                       "test-underlay.sqlite"));
151   GNUNET_CONFIGURATION_set_value_string (cfg, "TESTBED-UNDERLAY","DBFILE", dbfile);
152   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write
153                  (cfg, "test_testbed_underlay.conf"));
154   GNUNET_CONFIGURATION_destroy (cfg);
155   cfg = NULL;
156   GNUNET_free (dbfile);
157   dbfile = NULL;
158   (void) GNUNET_TESTBED_test_run ("test_testbed_underlay",
159                                   "test_testbed_underlay.conf", NUM_PEERS,
160                                   event_mask, NULL, NULL,
161                                   &test_master, NULL);
162   (void) unlink ("test_testbed_underlay.conf");
163   if (GNUNET_OK != result)
164     return 1;
165   return 0;
166 }