paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / nse / test_nse_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file nse/test_nse_api.c
20  * @brief testcase for nse_api.c
21  */
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_nse_service.h"
25 #include "gnunet_testing_lib.h"
26
27
28 static struct GNUNET_NSE_Handle *h;
29
30 static struct GNUNET_SCHEDULER_Task * die_task;
31
32
33 /**
34  * Signature of the main function of a task.
35  *
36  * @param cls closure
37  */
38 static void
39 end_test (void *cls)
40 {
41   if (h != NULL)
42   {
43     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from NSE service.\n");
44     GNUNET_NSE_disconnect (h);
45   }
46 }
47
48 /**
49  * Callback to call when network size estimate is updated.
50  *
51  * @param cls unused
52  * @param timestamp time when the estimate was received from the server (or created by the server)
53  * @param estimate the value of the current network size estimate
54  * @param std_dev standard deviation (rounded down to nearest integer)
55  *                of the size estimation values seen
56  *
57  */
58 static void
59 check_nse_message (void *cls, struct GNUNET_TIME_Absolute timestamp,
60                    double estimate, double std_dev)
61 {
62   int *ok = cls;
63
64   FPRINTF (stderr,
65            "Received NSE message, estimate %f, standard deviation %f.\n",
66            estimate, std_dev);
67   /* Fantastic check below. Expect NaN, the only thing not equal to itself. */
68   (*ok) = 0;
69   if (die_task != NULL)
70     GNUNET_SCHEDULER_cancel (die_task);
71   die_task = GNUNET_SCHEDULER_add_now (&end_test, NULL);
72 }
73
74
75 static void
76 run (void *cls,
77      const struct GNUNET_CONFIGURATION_Handle *cfg,
78      struct GNUNET_TESTING_Peer *peer)
79 {
80   die_task =
81       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
82                                     (GNUNET_TIME_UNIT_MINUTES, 1), &end_test,
83                                     NULL);
84
85   h = GNUNET_NSE_connect (cfg, &check_nse_message, cls);
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to NSE service.\n");
87   GNUNET_assert (h != NULL);
88 }
89
90
91 int
92 main (int argc, char *argv[])
93 {
94   int ok = 1;
95
96   if (0 != GNUNET_TESTING_peer_run ("test_nse_api",
97                                     "test_nse.conf",
98                                     &run, &ok))
99     return 1;
100   return ok;
101 }
102
103 /* end of test_nse_api.c */