2 This file is part of GNUnet.
3 (C) 2005, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file util/test_pseudonym.c
23 * @brief testcase for pseudonym.c
24 * @author Christian Grothoff
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"
34 #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_break(0); goto FAILURE; }
36 static struct GNUNET_CONTAINER_MetaData *meta;
38 static GNUNET_HashCode id1;
42 const GNUNET_HashCode *
43 pseudonym, const struct GNUNET_CONTAINER_MetaData *md, int rating)
47 if ((0 == memcmp (pseudonym,
49 sizeof (GNUNET_HashCode))) &&
50 (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
59 noti_callback (void *cls,
60 const GNUNET_HashCode *
62 const struct GNUNET_CONTAINER_MetaData *md, int rating)
70 fake_noti_callback (void *cls,
71 const GNUNET_HashCode *
73 const struct GNUNET_CONTAINER_MetaData *md, int rating)
81 false_callback (void *cls,
82 const GNUNET_HashCode *
84 const struct GNUNET_CONTAINER_MetaData *md, int rating)
89 main (int argc, char *argv[])
101 struct GNUNET_CONFIGURATION_Handle *cfg;
106 int notiCount,fakenotiCount;
108 static char m[1024 * 1024 * 10];
109 memset (m, 'b', sizeof (m));
110 m[sizeof (m) - 1] = '\0';
112 GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
114 GNUNET_CRYPTO_random_disable_entropy_gathering ();
115 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
116 cfg = GNUNET_CONFIGURATION_create ();
117 if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
119 GNUNET_CONFIGURATION_destroy (cfg);
126 GNUNET_PSEUDONYM_discovery_callback_register (cfg,
127 &fake_noti_callback, &fakenotiCount);
128 GNUNET_PSEUDONYM_discovery_callback_register (cfg,
129 ¬i_callback, ¬iCount);
130 GNUNET_PSEUDONYM_discovery_callback_unregister (&false_callback, &count);
131 GNUNET_PSEUDONYM_discovery_callback_unregister (&fake_noti_callback, &fakenotiCount);
133 /* ACTUAL TEST CODE */
134 old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
135 meta = GNUNET_CONTAINER_meta_data_create ();
136 GNUNET_CONTAINER_meta_data_insert (meta, EXTRACTOR_TITLE,"test");
137 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id1);
138 GNUNET_PSEUDONYM_add (cfg, &id1, meta);
139 CHECK (notiCount == 1);
140 GNUNET_PSEUDONYM_add (cfg, &id1, meta);
141 CHECK (notiCount == 2);
142 newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
143 CHECK (old < newVal);
145 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id2);
146 GNUNET_PSEUDONYM_add (cfg, &id2, meta);
147 CHECK (notiCount == 3);
148 newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
149 CHECK (old < newVal);
150 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (meta, EXTRACTOR_COMMENT, m));
151 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id3);
152 GNUNET_PSEUDONYM_add (cfg, &id3, meta);
153 GNUNET_log_skip (1, GNUNET_NO);
154 name3 = GNUNET_PSEUDONYM_id_to_name (cfg, &id3);
155 GNUNET_log_skip (0, GNUNET_YES);
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)));
167 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &fid);
168 CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &fid, 0));
169 noname = GNUNET_PSEUDONYM_id_to_name (cfg, &fid);
170 CHECK (noname != NULL);
171 CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 0));
172 CHECK (5 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
173 CHECK (-5 == GNUNET_PSEUDONYM_rank (cfg, &id1, -10));
174 CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
178 GNUNET_free (noname);
179 /* END OF TEST CODE */
181 GNUNET_PSEUDONYM_discovery_callback_unregister (¬i_callback, ¬iCount);
182 GNUNET_CONTAINER_meta_data_destroy (meta);
183 GNUNET_CONFIGURATION_destroy (cfg);
184 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
185 return (ok == GNUNET_YES) ? 0 : 1;
188 /* end of test_pseudoynm.c */