c0b6847b6b18eef38275a9a74d2b8f45f0311b48
[oweals/gnunet.git] / src / peerinfo / test_peerinfo_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file peerinfo/test_peerinfo_api.c
23  * @brief testcase for peerinfo_api.c
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - test merging of HELLOs (add same peer twice...)
28  */
29
30 #include "platform.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_program_lib.h"
36 #include "gnunet_time_lib.h"
37
38
39 static int
40 check_it (void *cls,
41           const char *tname,
42           struct GNUNET_TIME_Absolute expiration,
43           const void *addr, size_t addrlen)
44 {
45   unsigned int *agc = cls;
46
47   if (addrlen > 0)
48     {
49       GNUNET_assert (0 == strcmp ("peerinfotest", tname));
50       GNUNET_assert (0 == strncmp ("Address", addr, addrlen));
51       (*agc) -= (1 << (addrlen - 1));
52     }
53   return GNUNET_OK;
54 }
55
56
57 static void
58 process (void *cls,
59          const struct GNUNET_PeerIdentity *peer,
60          const struct GNUNET_HELLO_Message *hello, uint32_t trust)
61 {
62   int *ok = cls;
63   unsigned int agc;
64
65   if (peer == NULL)
66     {
67       GNUNET_assert (peer == NULL);
68       GNUNET_assert (2 == *ok);
69       GNUNET_assert (trust == 0);
70       *ok = 0;
71       return;
72     }
73
74   if (hello != NULL)
75     {
76       GNUNET_assert (3 == *ok);
77       agc = 3;
78       GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_it, &agc);
79       GNUNET_assert (agc == 0);
80       *ok = 2;
81     }
82 }
83
84
85 static size_t
86 address_generator (void *cls, size_t max, void *buf)
87 {
88   size_t *agc = cls;
89   size_t ret;
90
91   if (0 == *agc)
92     return 0;
93   ret = GNUNET_HELLO_add_address ("peerinfotest",
94                                   GNUNET_TIME_relative_to_absolute
95                                   (GNUNET_TIME_UNIT_HOURS), "Address", *agc,
96                                   buf, max);
97   (*agc)--;
98   return ret;
99 }
100
101
102 static void
103 run (void *cls,
104      struct GNUNET_SCHEDULER_Handle *sched,
105      char *const *args,
106      const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
107 {
108   struct GNUNET_HELLO_Message *hello;
109   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
110   size_t agc;
111   struct GNUNET_PeerIdentity pid;
112
113   memset (&pkey, 32, sizeof (pkey));
114   GNUNET_CRYPTO_hash (&pkey, sizeof (pkey), &pid.hashPubKey);
115   agc = 2;
116   hello = GNUNET_HELLO_create (&pkey, &address_generator, &agc);
117   GNUNET_assert (hello != NULL);
118   GNUNET_PEERINFO_add_peer (cfg, sched, &pid, hello);
119   GNUNET_PEERINFO_for_all (cfg,
120                            sched,
121                            NULL,
122                            0,
123                            GNUNET_TIME_relative_multiply
124                            (GNUNET_TIME_UNIT_SECONDS, 15), &process, cls);
125   GNUNET_free (hello);
126 }
127
128
129 static int
130 check ()
131 {
132   int ok = 3;
133   pid_t pid;
134   char *const argv[] = { "test-peerinfo-api",
135     "-c",
136     "test_peerinfo_api_data.conf",
137 #if DEBUG_PEERINFO
138     "-L", "DEBUG",
139 #endif
140     NULL
141   };
142   struct GNUNET_GETOPT_CommandLineOption options[] = {
143     GNUNET_GETOPT_OPTION_END
144   };
145   pid = GNUNET_OS_start_process ("gnunet-service-peerinfo",
146                                  "gnunet-service-peerinfo",
147 #if DEBUG_PEERINFO
148                                  "-L", "DEBUG",
149 #endif
150                                  "-c", "test_peerinfo_api_data.conf", NULL);
151   sleep (1);
152   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
153                       argv, "test-peerinfo-api", "nohelp",
154                       options, &run, &ok);
155   if (0 != PLIBC_KILL (pid, SIGTERM))
156     {
157       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
158       ok = 1;
159     }
160   GNUNET_OS_process_wait(pid);
161   return ok;
162 }
163
164
165 int
166 main (int argc, char *argv[])
167 {
168   int ret = 0;
169
170   ret = check ();
171
172   return ret;
173 }
174
175 /* end of test_peerinfo_api.c */