indentation
[oweals/gnunet.git] / src / util / test_pseudonym.c
1 /*
2      This file is part of GNUnet.
3      (C) 2005, 2006, 2008, 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 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/test_pseudonym.c
23  * @brief testcase for pseudonym.c
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_pseudonym_lib.h"
33
34 #define CHECK(a) do { if (!(a)) { ok = GNUNET_NO; GNUNET_break(0); goto FAILURE; } } while (0)
35
36 static struct GNUNET_CONTAINER_MetaData *meta;
37
38 static GNUNET_HashCode id1;
39
40 static int
41 iter (void *cls,
42       const GNUNET_HashCode *
43       pseudonym, const struct GNUNET_CONTAINER_MetaData *md, int rating)
44 {
45   int *ok = cls;
46
47   if ((0 == memcmp (pseudonym,
48                     &id1,
49                     sizeof (GNUNET_HashCode))) &&
50       (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
51   {
52     *ok = GNUNET_NO;
53     GNUNET_break (0);
54   }
55   return GNUNET_OK;
56 }
57
58 static int
59 noti_callback (void *cls,
60                const GNUNET_HashCode *
61                pseudonym,
62                const struct GNUNET_CONTAINER_MetaData *md, int rating)
63 {
64   int *ret = cls;
65
66   (*ret)++;
67   return GNUNET_OK;
68 }
69
70 static int
71 fake_noti_callback (void *cls,
72                     const GNUNET_HashCode *
73                     pseudonym,
74                     const struct GNUNET_CONTAINER_MetaData *md, int rating)
75 {
76   int *ret = cls;
77
78   (*ret)++;
79   return GNUNET_OK;
80 }
81
82 static int
83 false_callback (void *cls,
84                 const GNUNET_HashCode * pseudonym,
85                 const struct GNUNET_CONTAINER_MetaData *md, int rating)
86 {
87   return GNUNET_OK;
88 }
89
90 int
91 main (int argc, char *argv[])
92 {
93   int ok;
94   GNUNET_HashCode rid1;
95   GNUNET_HashCode id2;
96   GNUNET_HashCode rid2;
97   GNUNET_HashCode fid;
98   GNUNET_HashCode id3;
99
100   int old;
101   int newVal;
102   struct GNUNET_CONFIGURATION_Handle *cfg;
103   char *name1;
104   char *name2;
105   char *name3;
106   char *noname;
107   int notiCount, fakenotiCount;
108   int count;
109   static char m[1024 * 1024 * 10];
110
111   memset (m, 'b', sizeof (m));
112   m[sizeof (m) - 1] = '\0';
113
114   GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
115   ok = GNUNET_YES;
116   GNUNET_CRYPTO_random_disable_entropy_gathering ();
117   (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
118   cfg = GNUNET_CONFIGURATION_create ();
119   if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
120   {
121     GNUNET_CONFIGURATION_destroy (cfg);
122     GNUNET_break (0);
123     return -1;
124   }
125   notiCount = 0;
126   fakenotiCount = 0;
127   count = 0;
128   GNUNET_PSEUDONYM_discovery_callback_register (cfg,
129                                                 &fake_noti_callback,
130                                                 &fakenotiCount);
131   GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback,
132                                                 &notiCount);
133   GNUNET_PSEUDONYM_discovery_callback_unregister (&false_callback, &count);
134   GNUNET_PSEUDONYM_discovery_callback_unregister (&fake_noti_callback,
135                                                   &fakenotiCount);
136
137   /* ACTUAL TEST CODE */
138   old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
139   meta = GNUNET_CONTAINER_meta_data_create ();
140   GNUNET_CONTAINER_meta_data_insert (meta,
141                                      "<test>",
142                                      EXTRACTOR_METATYPE_TITLE,
143                                      EXTRACTOR_METAFORMAT_UTF8,
144                                      "text/plain", "test", strlen ("test") + 1);
145   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id1);
146   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
147   CHECK (notiCount == 1);
148   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
149   CHECK (notiCount == 2);
150   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
151   CHECK (old < newVal);
152   old = newVal;
153   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id2);
154   GNUNET_PSEUDONYM_add (cfg, &id2, meta);
155   CHECK (notiCount == 3);
156   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
157   CHECK (old < newVal);
158   GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (meta,
159                                                                  "<test>",
160                                                                  EXTRACTOR_METATYPE_COMMENT,
161                                                                  EXTRACTOR_METAFORMAT_UTF8,
162                                                                  "text/plain",
163                                                                  m,
164                                                                  strlen (m) +
165                                                                  1));
166   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id3);
167   GNUNET_PSEUDONYM_add (cfg, &id3, meta);
168   name3 = GNUNET_PSEUDONYM_id_to_name (cfg, &id3);
169   name2 = GNUNET_PSEUDONYM_id_to_name (cfg, &id2);
170   CHECK (name2 != NULL);
171   name1 = GNUNET_PSEUDONYM_id_to_name (cfg, &id1);
172   CHECK (name1 != NULL);
173   CHECK (0 != strcmp (name1, name2));
174   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, "fake", &rid2));
175   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name2, &rid2));
176   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name1, &rid1));
177   CHECK (0 == memcmp (&id1, &rid1, sizeof (GNUNET_HashCode)));
178   CHECK (0 == memcmp (&id2, &rid2, sizeof (GNUNET_HashCode)));
179
180   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &fid);
181   GNUNET_log_skip (1, GNUNET_NO);
182   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &fid, 0));
183   GNUNET_log_skip (0, GNUNET_YES);
184   noname = GNUNET_PSEUDONYM_id_to_name (cfg, &fid);
185   CHECK (noname != NULL);
186   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 0));
187   CHECK (5 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
188   CHECK (-5 == GNUNET_PSEUDONYM_rank (cfg, &id1, -10));
189   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
190   GNUNET_free (name1);
191   GNUNET_free (name2);
192   GNUNET_free (name3);
193   GNUNET_free (noname);
194   /* END OF TEST CODE */
195 FAILURE:
196   GNUNET_PSEUDONYM_discovery_callback_unregister (&noti_callback, &notiCount);
197   GNUNET_CONTAINER_meta_data_destroy (meta);
198   GNUNET_CONFIGURATION_destroy (cfg);
199   GNUNET_break (GNUNET_OK ==
200                 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));
201   return (ok == GNUNET_YES) ? 0 : 1;
202 }
203
204 /* end of test_pseudoynm.c */