-fix format warning
[oweals/gnunet.git] / src / peerinfo / test_peerinfo_shipped_hellos.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2009 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file peerinfo/test_peerinfo_shipped_hellos.c
23  * @brief testcase for shipped HELLOs getting parsed
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  *
27  */
28 #include "platform.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_testing_lib.h"
33 #include "peerinfo.h"
34
35 static struct GNUNET_PEERINFO_IteratorContext *ic;
36
37 static struct GNUNET_PEERINFO_Handle *h;
38
39 static int global_ret;
40
41 static int
42 addr_cb (void *cls,
43          const struct GNUNET_HELLO_Address *address,
44          struct GNUNET_TIME_Absolute expiration)
45 {
46   int *addr = cls;
47
48   (*addr) ++;
49   return GNUNET_OK;
50 }
51
52
53 static void
54 process (void *cls,
55          const struct GNUNET_PeerIdentity *peer,
56          const struct GNUNET_HELLO_Message *hello,
57          const char *err_msg)
58 {
59   static unsigned int calls = 0;
60   int addr;
61
62   if (err_msg != NULL)
63   {
64     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
65                 _("Error in communication with PEERINFO service\n"));
66   }
67   if (NULL != peer)
68   {
69     addr = 0;
70     if (NULL != hello)
71     {
72       GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &addr_cb, &addr);
73       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74                   "Got information about peer `%s' with %u addresses \n",
75                   GNUNET_i2s (peer), addr);
76       calls++;
77     }
78     else
79       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
80                   "Fail: Got information about peer `%s' without HELLO \n",
81                   GNUNET_i2s (peer));
82   }
83   else
84   {
85     if (0 == calls)
86     {
87       fprintf (stderr,
88                "Failed: got no callbacks!\n");
89       global_ret = 1;
90     }
91     else
92       {
93         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94                     "Got %u callbacks\n", calls);
95         global_ret = 0;
96       }
97   }
98 }
99
100
101 static void
102 run (void *cls,
103      const struct GNUNET_CONFIGURATION_Handle *cfg,
104      struct GNUNET_TESTING_Peer *peer)
105 {
106   h = GNUNET_PEERINFO_connect (cfg);
107   GNUNET_assert (NULL != h);
108   ic = GNUNET_PEERINFO_iterate (h, GNUNET_YES, NULL,
109                                 GNUNET_TIME_relative_multiply
110                                 (GNUNET_TIME_UNIT_SECONDS, 15), &process, cls);
111 }
112
113
114 int
115 main (int argc, char *argv[])
116 {
117   global_ret = 3;
118   if (0 != GNUNET_TESTING_service_run ("test_peerinfo_shipped_hellos",
119                                        "peerinfo",
120                                        "test_peerinfo_api_data.conf",
121                                        &run, NULL))
122     return 1;
123   return global_ret;
124 }
125
126 /* end of test_peerinfo_shipped_hellos.c */