debug output
[oweals/gnunet.git] / src / nse / test_nse_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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  * @file nse/test_nse_api.c
22  * @brief testcase for nse_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_nse_service.h"
31
32 #define DEBUG_NSE GNUNET_YES
33
34 #define START_ARM GNUNET_YES
35
36 static struct GNUNET_NSE_Handle *h;
37
38 GNUNET_SCHEDULER_TaskIdentifier die_task;
39
40 struct PeerContext
41 {
42   struct GNUNET_CONFIGURATION_Handle *cfg;
43 #if START_ARM
44   struct GNUNET_OS_Process *arm_proc;
45 #endif
46 };
47
48 static struct PeerContext p1;
49
50 /**
51  * Signature of the main function of a task.
52  *
53  * @param cls closure
54  * @param tc context information (why was this task triggered now)
55  */
56 static void end_test (void *cls,
57                       const struct GNUNET_SCHEDULER_TaskContext * tc)
58 {
59   if (h != NULL)
60     {
61       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62                   "Disconnecting from NSE service.\n");
63       GNUNET_NSE_disconnect (h);
64     }
65 }
66
67 /**
68  * Callback to call when network size estimate is updated.
69  *
70  * @param cls unused
71  * @param estimate the value of the current network size estimate
72  * @param std_dev standard deviation (rounded down to nearest integer)
73  *                of the size estimation values seen
74  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
75  */
76 static int
77 check_nse_message (void *cls, double estimate, double std_dev)
78 {
79   int *ok = cls;
80
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
82               "Received NSE message, estimate %f, standard deviation %f.\n");
83   /* Fantastic check below. Expect NaN, the only thing not equal to itself. */
84   if ((estimate != estimate) && (std_dev != std_dev))
85     (*ok) = 0;
86
87   if (die_task != GNUNET_SCHEDULER_NO_TASK)
88     GNUNET_SCHEDULER_cancel(die_task);
89   GNUNET_SCHEDULER_add_now(&end_test, NULL);
90   return GNUNET_OK;
91 }
92
93 static void
94 setup_peer (struct PeerContext *p, const char *cfgname)
95 {
96   p->cfg = GNUNET_CONFIGURATION_create ();
97 #if START_ARM
98   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
99                                         "gnunet-service-arm",
100 #if VERBOSE_ARM
101                                         "-L", "DEBUG",
102 #endif
103                                         "-c", cfgname, NULL);
104 #endif
105   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
106
107 }
108
109 static void
110 stop_arm (struct PeerContext *p)
111 {
112 #if START_ARM
113   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
114     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
115   GNUNET_OS_process_wait (p->arm_proc);
116   GNUNET_OS_process_close (p->arm_proc);
117   p->arm_proc = NULL;
118 #endif
119   GNUNET_CONFIGURATION_destroy (p->cfg);
120 }
121
122 static void
123 run (void *cls,
124      char *const *args,
125      const char *cfgfile,
126      const struct GNUNET_CONFIGURATION_Handle *cfg)
127 {
128   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
129                                            (GNUNET_TIME_UNIT_MINUTES, 1),
130                                            &end_test, NULL);
131
132   setup_peer (&p1, cfgfile);
133   h = GNUNET_NSE_connect (cfg, &check_nse_message, cls);
134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135               "Connecting to NSE service.\n");
136   GNUNET_assert (h != NULL);
137 }
138
139 static int
140 check ()
141 {
142   int ok = 1;
143   char *const argv[] = { "test-nse-api",
144     "-c",
145     "test_nse.conf",
146 #if DEBUG_NSE
147                          "-L", "DEBUG",
148 #else
149                          "-L", "WARNING",
150 #endif
151     NULL
152   };
153   struct GNUNET_GETOPT_CommandLineOption options[] = {
154     GNUNET_GETOPT_OPTION_END
155   };
156
157   GNUNET_PROGRAM_run (5, argv, "test-nse-api", "nohelp",
158                       options, &run, &ok);
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160               "Stopping arm.\n");
161   stop_arm (&p1);
162   return ok;
163 }
164
165 int
166 main (int argc, char *argv[])
167 {
168   int ret;
169
170   GNUNET_log_setup ("test_nse_api",
171 #if DEBUG_NSE
172                     "DEBUG",
173 #else
174                     "WARNING",
175 #endif
176                     NULL);
177   ret = check ();
178
179   return ret;
180 }
181
182 /* end of test_nse_api.c */