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