first batch of license fixes (boring)
[oweals/gnunet.git] / src / peerinfo / test_peerinfo_api.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 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_api.c
18  * @brief testcase for peerinfo_api.c
19  * @author Christian Grothoff
20  *
21  * TODO:
22  * - test merging of HELLOs (add same peer twice...)
23  */
24 #include "platform.h"
25 #include "gnunet_hello_lib.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_peerinfo_service.h"
28 #include "gnunet_testing_lib.h"
29 #include "peerinfo.h"
30
31 static struct GNUNET_PEERINFO_IteratorContext *ic;
32
33 static struct GNUNET_PEERINFO_Handle *h;
34
35 static unsigned int retries;
36
37 static int global_ret;
38
39
40 static int
41 check_it (void *cls, const struct GNUNET_HELLO_Address *address,
42           struct GNUNET_TIME_Absolute expiration)
43 {
44   unsigned int *agc = cls;
45
46   if (address != NULL)
47   {
48     GNUNET_assert (0 == strcmp ("peerinfotest", address->transport_name));
49     GNUNET_assert (0 ==
50                    strncmp ("Address", address->address,
51                             address->address_length));
52     (*agc) -= (1 << (address->address_length - 1));
53   }
54   return GNUNET_OK;
55 }
56
57
58 static ssize_t
59 address_generator (void *cls, size_t max, void *buf)
60 {
61   size_t *agc = cls;
62   ssize_t ret;
63   struct GNUNET_HELLO_Address address;
64
65   if (0 == *agc)
66     return GNUNET_SYSERR; /* Done */
67   memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
68   address.address = "Address";
69   address.transport_name = "peerinfotest";
70   address.address_length = *agc;
71   ret =
72       GNUNET_HELLO_add_address (&address,
73                                 GNUNET_TIME_relative_to_absolute
74                                 (GNUNET_TIME_UNIT_HOURS), buf, max);
75   (*agc)--;
76   return ret;
77 }
78
79 struct GNUNET_PeerIdentity pid;
80
81 static void
82 add_peer ()
83 {
84   struct GNUNET_HELLO_Message *h2;
85   size_t agc;
86
87   agc = 2;
88   memset (&pid, 32, sizeof (pid));
89   h2 = GNUNET_HELLO_create (&pid.public_key, &address_generator, &agc, GNUNET_NO);
90   GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
91   GNUNET_free (h2);
92
93 }
94
95
96 static void
97 process (void *cls, const struct GNUNET_PeerIdentity *peer,
98          const struct GNUNET_HELLO_Message *hello, const char *err_msg)
99 {
100   unsigned int agc;
101
102   if (err_msg != NULL)
103   {
104     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
105                 _("Error in communication with PEERINFO service\n"));
106   }
107
108   if (peer == NULL)
109   {
110     ic = NULL;
111     if ((3 == global_ret) && (retries < 50))
112     {
113       /* try again */
114       retries++;
115       add_peer ();
116       ic = GNUNET_PEERINFO_iterate (h, GNUNET_NO, NULL,
117                                     &process,
118                                     cls);
119       return;
120     }
121     GNUNET_assert (peer == NULL);
122     GNUNET_assert (2 == global_ret);
123     GNUNET_PEERINFO_disconnect (h);
124     h = NULL;
125     global_ret = 0;
126     return;
127   }
128   if (hello != NULL)
129   {
130     GNUNET_assert (3 == global_ret);
131     agc = 3;
132     GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
133                                     &check_it, &agc);
134     GNUNET_assert (agc == 0);
135     global_ret = 2;
136   }
137 }
138
139
140 static void
141 run (void *cls,
142      const struct GNUNET_CONFIGURATION_Handle *cfg,
143      struct GNUNET_TESTING_Peer *peer)
144 {
145   h = GNUNET_PEERINFO_connect (cfg);
146   GNUNET_assert (NULL != h);
147   add_peer ();
148   ic = GNUNET_PEERINFO_iterate (h, GNUNET_NO, &pid,
149                                 &process, cls);
150 }
151
152
153 int
154 main (int argc, char *argv[])
155 {
156   global_ret = 3;
157   if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerinfo",
158                                        "peerinfo",
159                                        "test_peerinfo_api_data.conf",
160                                        &run, NULL))
161     return 1;
162   return global_ret;
163 }
164
165 /* end of test_peerinfo_api.c */