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