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