starting major change towards implementing #2564, this breaks some FS tests and FS...
[oweals/gnunet.git] / src / util / test_pseudonym.c
1 /*
2      This file is part of GNUnet.
3      (C) 2005--2013 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 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_disk_lib.h"
31 #include "gnunet_pseudonym_lib.h"
32
33 #define CHECK(a) do { if (!(a)) { ok = GNUNET_NO; GNUNET_break(0); goto FAILURE; } } while (0)
34
35 static struct GNUNET_CONTAINER_MetaData *meta;
36
37 static struct GNUNET_PseudonymIdentifier id1;
38
39
40 static int
41 iter (void *cls, const struct GNUNET_PseudonymIdentifier * pseudonym,
42       const char *name, const char *unique_name,
43       const struct GNUNET_CONTAINER_MetaData *md, int32_t rating)
44 {
45   int *ok = cls;
46
47   if ((0 == memcmp (pseudonym, &id1, sizeof (struct GNUNET_PseudonymIdentifier))) &&
48       (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
49   {
50     *ok = GNUNET_NO;
51     GNUNET_break (0);
52   }
53   return GNUNET_OK;
54 }
55
56
57 static int
58 noti_callback (void *cls, const struct GNUNET_PseudonymIdentifier * pseudonym,
59                const char *name, const char *unique_name,
60                const struct GNUNET_CONTAINER_MetaData *md, int32_t rating)
61 {
62   int *ret = cls;
63
64   (*ret)++;
65   return GNUNET_OK;
66 }
67
68
69 static int
70 fake_noti_callback (void *cls, const struct GNUNET_PseudonymIdentifier * pseudonym,
71                     const char *name, const char *unique_name,
72                     const struct GNUNET_CONTAINER_MetaData *md, int32_t rating)
73 {
74   int *ret = cls;
75
76   (*ret)++;
77   return GNUNET_OK;
78 }
79
80
81 static void
82 create_pseu (struct GNUNET_PseudonymIdentifier *pseu)
83 {
84   struct GNUNET_PseudonymHandle *ph;
85
86   ph = GNUNET_PSEUDONYM_create (NULL);
87   GNUNET_PSEUDONYM_get_identifier (ph, pseu);
88   GNUNET_PSEUDONYM_destroy (ph);
89 }
90
91
92 /**
93  * Testcase for meta data / ranking IO routines.
94  */
95 static int
96 test_io ()
97 {
98   int ok;
99   struct GNUNET_PseudonymIdentifier rid1;
100   struct GNUNET_PseudonymIdentifier id2;
101   struct GNUNET_PseudonymIdentifier rid2;
102   struct GNUNET_PseudonymIdentifier fid;
103   struct GNUNET_PseudonymIdentifier id3;
104   int old;
105   int newVal;
106   struct GNUNET_CONFIGURATION_Handle *cfg;
107   char *name1;
108   char *name2;
109   char *name3;
110   char *name1_unique;
111   char *name2_unique;
112   char *noname;
113   int noname_is_a_dup;
114   int notiCount, fakenotiCount;
115   static char m[1024 * 1024 * 10];
116   struct GNUNET_PSEUDONYM_DiscoveryHandle *dh1;
117   struct GNUNET_PSEUDONYM_DiscoveryHandle *dh2;
118
119   memset (m, 'b', sizeof (m));
120   m[sizeof (m) - 1] = '\0';
121
122   GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
123   ok = GNUNET_YES;
124   (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
125   cfg = GNUNET_CONFIGURATION_create ();
126   if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
127   {
128     GNUNET_CONFIGURATION_destroy (cfg);
129     GNUNET_break (0);
130     return -1;
131   }
132   notiCount = 0;
133   fakenotiCount = 0;
134   dh1 = GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback,
135                                                       &fakenotiCount);
136   dh2 = GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback,
137                                                       &notiCount);
138   GNUNET_PSEUDONYM_discovery_callback_unregister (dh1);
139
140   /* ACTUAL TEST CODE */
141   old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
142   meta = GNUNET_CONTAINER_meta_data_create ();
143   GNUNET_CONTAINER_meta_data_insert (meta, "<test>", EXTRACTOR_METATYPE_TITLE,
144                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
145                                      "test", strlen ("test") + 1);
146   create_pseu (&id1);
147   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
148   CHECK (notiCount == 1);
149   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
150   CHECK (notiCount == 2);
151   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
152   CHECK (old < newVal);
153   old = newVal;
154   create_pseu (&id2);
155   GNUNET_PSEUDONYM_add (cfg, &id2, meta);
156   CHECK (notiCount == 3);
157   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
158   CHECK (old < newVal);
159   GNUNET_assert (GNUNET_OK ==
160                  GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
161                                                     EXTRACTOR_METATYPE_COMMENT,
162                                                     EXTRACTOR_METAFORMAT_UTF8,
163                                                     "text/plain", m,
164                                                     strlen (m) + 1));
165   create_pseu (&id3);
166   GNUNET_PSEUDONYM_add (cfg, &id3, meta);
167   GNUNET_PSEUDONYM_get_info (cfg, &id3, NULL, NULL, &name3, NULL);
168   CHECK (name3 != NULL);
169   GNUNET_PSEUDONYM_get_info (cfg, &id2, NULL, NULL, &name2, NULL);
170   CHECK (name2 != NULL);
171   GNUNET_PSEUDONYM_get_info (cfg, &id1, NULL, NULL, &name1, NULL);
172   CHECK (name1 != NULL);
173   CHECK (0 == strcmp (name1, name2));
174   name1_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id1, name1, NULL);
175   name2_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id2, name2, NULL);
176   CHECK (0 != strcmp (name1_unique, name2_unique));
177   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, "fake", &rid2));
178   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name2, &rid2));
179   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name1, &rid1));
180   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name2_unique, &rid2));
181   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name1_unique, &rid1));
182   CHECK (0 == memcmp (&id1, &rid1, sizeof (struct GNUNET_PseudonymIdentifier)));
183   CHECK (0 == memcmp (&id2, &rid2, sizeof (struct GNUNET_PseudonymIdentifier)));
184
185   create_pseu (&fid);
186   GNUNET_log_skip (1, GNUNET_NO);
187   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &fid, 0));
188   GNUNET_log_skip (0, GNUNET_NO);
189   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_get_info (cfg, &fid, NULL, NULL, &noname, &noname_is_a_dup));
190   CHECK (noname != NULL);
191   CHECK (noname_is_a_dup == GNUNET_YES);
192   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 0));
193   CHECK (5 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
194   CHECK (-5 == GNUNET_PSEUDONYM_rank (cfg, &id1, -10));
195   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
196   GNUNET_free (name1);
197   GNUNET_free (name2);
198   GNUNET_free (name1_unique);
199   GNUNET_free (name2_unique);
200   GNUNET_free (name3);
201   GNUNET_free (noname);
202   /* END OF TEST CODE */
203 FAILURE:
204   GNUNET_PSEUDONYM_discovery_callback_unregister (dh2);
205   GNUNET_CONTAINER_meta_data_destroy (meta);
206   GNUNET_CONFIGURATION_destroy (cfg);
207   GNUNET_break (GNUNET_OK ==
208                 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));
209   return (ok == GNUNET_YES) ? 0 : 1;
210 }
211
212
213 static int
214 test_crypto ()
215 {
216   struct GNUNET_PseudonymHandle *ph;
217
218   ph = GNUNET_PSEUDONYM_create (NULL);
219   // FIXME: call sign, verify APIs...
220   GNUNET_PSEUDONYM_destroy (ph);  
221   return 0;
222 }
223
224
225 int
226 main (int argc, char *argv[])
227 {
228   if (0 != test_io ())
229     return 1;
230   if (0 != test_crypto ())
231     return 1;
232   
233   return 0;
234 }
235
236
237 /* end of test_pseudoynm.c */