-more file name fixes
[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, const GNUNET_HashCode * pseudonym,
42       const struct GNUNET_CONTAINER_MetaData *md, int rating)
43 {
44   int *ok = cls;
45
46   if ((0 == memcmp (pseudonym, &id1, sizeof (GNUNET_HashCode))) &&
47       (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
48   {
49     *ok = GNUNET_NO;
50     GNUNET_break (0);
51   }
52   return GNUNET_OK;
53 }
54
55 static int
56 noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
57                const struct GNUNET_CONTAINER_MetaData *md, int rating)
58 {
59   int *ret = cls;
60
61   (*ret)++;
62   return GNUNET_OK;
63 }
64
65 static int
66 fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
67                     const struct GNUNET_CONTAINER_MetaData *md, int rating)
68 {
69   int *ret = cls;
70
71   (*ret)++;
72   return GNUNET_OK;
73 }
74
75 static int
76 false_callback (void *cls, const GNUNET_HashCode * pseudonym,
77                 const struct GNUNET_CONTAINER_MetaData *md, int rating)
78 {
79   return GNUNET_OK;
80 }
81
82 int
83 main (int argc, char *argv[])
84 {
85   int ok;
86   GNUNET_HashCode rid1;
87   GNUNET_HashCode id2;
88   GNUNET_HashCode rid2;
89   GNUNET_HashCode fid;
90   GNUNET_HashCode id3;
91
92   int old;
93   int newVal;
94   struct GNUNET_CONFIGURATION_Handle *cfg;
95   char *name1;
96   char *name2;
97   char *name3;
98   char *noname;
99   int notiCount, fakenotiCount;
100   int count;
101   static char m[1024 * 1024 * 10];
102
103   memset (m, 'b', sizeof (m));
104   m[sizeof (m) - 1] = '\0';
105
106   GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
107   ok = GNUNET_YES;
108   GNUNET_CRYPTO_random_disable_entropy_gathering ();
109   (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
110   cfg = GNUNET_CONFIGURATION_create ();
111   if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
112   {
113     GNUNET_CONFIGURATION_destroy (cfg);
114     GNUNET_break (0);
115     return -1;
116   }
117   notiCount = 0;
118   fakenotiCount = 0;
119   count = 0;
120   GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback,
121                                                 &fakenotiCount);
122   GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback,
123                                                 &notiCount);
124   GNUNET_PSEUDONYM_discovery_callback_unregister (&false_callback, &count);
125   GNUNET_PSEUDONYM_discovery_callback_unregister (&fake_noti_callback,
126                                                   &fakenotiCount);
127
128   /* ACTUAL TEST CODE */
129   old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
130   meta = GNUNET_CONTAINER_meta_data_create ();
131   GNUNET_CONTAINER_meta_data_insert (meta, "<test>", EXTRACTOR_METATYPE_TITLE,
132                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
133                                      "test", strlen ("test") + 1);
134   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id1);
135   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
136   CHECK (notiCount == 1);
137   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
138   CHECK (notiCount == 2);
139   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
140   CHECK (old < newVal);
141   old = newVal;
142   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id2);
143   GNUNET_PSEUDONYM_add (cfg, &id2, meta);
144   CHECK (notiCount == 3);
145   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
146   CHECK (old < newVal);
147   GNUNET_assert (GNUNET_OK ==
148                  GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
149                                                     EXTRACTOR_METATYPE_COMMENT,
150                                                     EXTRACTOR_METAFORMAT_UTF8,
151                                                     "text/plain", m,
152                                                     strlen (m) + 1));
153   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id3);
154   GNUNET_PSEUDONYM_add (cfg, &id3, meta);
155   name3 = GNUNET_PSEUDONYM_id_to_name (cfg, &id3);
156   name2 = GNUNET_PSEUDONYM_id_to_name (cfg, &id2);
157   CHECK (name2 != NULL);
158   name1 = GNUNET_PSEUDONYM_id_to_name (cfg, &id1);
159   CHECK (name1 != NULL);
160   CHECK (0 != strcmp (name1, name2));
161   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, "fake", &rid2));
162   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name2, &rid2));
163   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name1, &rid1));
164   CHECK (0 == memcmp (&id1, &rid1, sizeof (GNUNET_HashCode)));
165   CHECK (0 == memcmp (&id2, &rid2, sizeof (GNUNET_HashCode)));
166
167   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &fid);
168   GNUNET_log_skip (1, GNUNET_NO);
169   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &fid, 0));
170   GNUNET_log_skip (0, GNUNET_YES);
171   noname = GNUNET_PSEUDONYM_id_to_name (cfg, &fid);
172   CHECK (noname != NULL);
173   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 0));
174   CHECK (5 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
175   CHECK (-5 == GNUNET_PSEUDONYM_rank (cfg, &id1, -10));
176   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
177   GNUNET_free (name1);
178   GNUNET_free (name2);
179   GNUNET_free (name3);
180   GNUNET_free (noname);
181   /* END OF TEST CODE */
182 FAILURE:
183   GNUNET_PSEUDONYM_discovery_callback_unregister (&noti_callback, &notiCount);
184   GNUNET_CONTAINER_meta_data_destroy (meta);
185   GNUNET_CONFIGURATION_destroy (cfg);
186   GNUNET_break (GNUNET_OK ==
187                 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));
188   return (ok == GNUNET_YES) ? 0 : 1;
189 }
190
191 /* end of test_pseudoynm.c */