glitch in the license text detected by hyazinthe, thank you!
[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 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
16 /**
17  * @file peerinfo/test_peerinfo_shipped_hellos.c
18  * @brief testcase for shipped HELLOs getting parsed
19  * @author Christian Grothoff
20  * @author Matthias Wachs
21  *
22  */
23 #include "platform.h"
24 #include "gnunet_hello_lib.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_peerinfo_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "peerinfo.h"
29
30 static struct GNUNET_PEERINFO_IteratorContext *ic;
31
32 static struct GNUNET_PEERINFO_Handle *h;
33
34 static int global_ret;
35
36
37 static int
38 addr_cb (void *cls,
39          const struct GNUNET_HELLO_Address *address,
40          struct GNUNET_TIME_Absolute expiration)
41 {
42   unsigned int *addr = cls;
43
44   (*addr) ++;
45   return GNUNET_OK;
46 }
47
48
49 static void
50 process (void *cls,
51          const struct GNUNET_PeerIdentity *peer,
52          const struct GNUNET_HELLO_Message *hello,
53          const char *err_msg)
54 {
55   static unsigned int calls = 0;
56   unsigned int addr;
57
58   if (NULL != err_msg)
59   {
60     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
61                 "Error in communication with PEERINFO service: %s\n",
62                 err_msg);
63   }
64   if (NULL != peer)
65   {
66     addr = 0;
67     if (NULL != hello)
68     {
69       GNUNET_HELLO_iterate_addresses (hello,
70                                       GNUNET_NO,
71                                       &addr_cb,
72                                       &addr);
73       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74                   "Got information about peer %s with %u addresses\n",
75                   GNUNET_i2s (peer),
76                   addr);
77       calls++;
78     }
79     else
80     {
81       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
82                   "Got no HELLP for peer %s\n",
83                   GNUNET_i2s (peer));
84     }
85   }
86   else
87   {
88     if (0 == calls)
89     {
90       fprintf (stderr,
91                "Failed: got no callbacks!\n");
92       global_ret = 1;
93       GNUNET_PEERINFO_disconnect (h);
94       h = NULL;
95     }
96     else
97     {
98       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99                   "Got %u HELLOs in total\n",
100                   calls);
101       global_ret = 0;
102       GNUNET_PEERINFO_disconnect (h);
103       h = NULL;
104     }
105   }
106 }
107
108
109 static void
110 run (void *cls,
111      const struct GNUNET_CONFIGURATION_Handle *cfg,
112      struct GNUNET_TESTING_Peer *peer)
113 {
114   h = GNUNET_PEERINFO_connect (cfg);
115   GNUNET_assert (NULL != h);
116   ic = GNUNET_PEERINFO_iterate (h,
117                                 GNUNET_YES,
118                                 NULL,
119                                 &process,
120                                 cls);
121   GNUNET_assert (NULL != ic);
122 }
123
124
125 int
126 main (int argc,
127       char *argv[])
128 {
129   global_ret = 3;
130   if (0 != GNUNET_TESTING_service_run ("test_peerinfo_shipped_hellos",
131                                        "peerinfo",
132                                        "test_peerinfo_api_data.conf",
133                                        &run, NULL))
134     return 1;
135   return global_ret;
136 }
137
138 /* end of test_peerinfo_shipped_hellos.c */