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