aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_pseudonym.c
blob: d358c8210aae1e9564ebfdc74712e9ba6f293940 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
     This file is part of GNUnet.
     (C) 2005, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file util/test_pseudonym.c
 * @brief testcase for pseudonym.c
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_container_lib.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_disk_lib.h"
#include "gnunet_pseudonym_lib.h"

#define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_break(0); goto FAILURE; }

static struct GNUNET_CONTAINER_MetaData *meta;

static GNUNET_HashCode id1;

static int
iter (void *cls,
      const GNUNET_HashCode *
      pseudonym, const struct GNUNET_CONTAINER_MetaData *md, int rating)
{
  int *ok = cls;

  if ((0 == memcmp (pseudonym,
                    &id1,
                    sizeof (GNUNET_HashCode))) &&
      (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
    {
      *ok = GNUNET_NO;
      GNUNET_break (0);
    }
  return GNUNET_OK;
}

static int
noti_callback (void *cls,
               const GNUNET_HashCode *
               pseudonym,
               const struct GNUNET_CONTAINER_MetaData *md, int rating)
{
  int *ret = cls;
  (*ret)++;
  return GNUNET_OK;
}


int
main (int argc, char *argv[])
{
  int ok;
  GNUNET_HashCode rid1;
  GNUNET_HashCode id2;
  GNUNET_HashCode rid2;
  int old;
  int newVal;
  struct GNUNET_CONFIGURATION_Handle *cfg;
  char *name1;
  char *name2;
  int notiCount;

  GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
  ok = GNUNET_YES;
  GNUNET_CRYPTO_random_disable_entropy_gathering ();
  GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
  cfg = GNUNET_CONFIGURATION_create ();
  if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
    {
      GNUNET_CONFIGURATION_destroy (cfg);
      GNUNET_break (0);
      return -1;
    }
  notiCount = 0;
  GNUNET_PSEUDONYM_discovery_callback_register (cfg,
                                                &noti_callback, &notiCount);
  /* ACTUAL TEST CODE */
  old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
  meta = GNUNET_CONTAINER_meta_data_create ();
  GNUNET_CONTAINER_meta_data_insert (meta, EXTRACTOR_TITLE, "test");
  GNUNET_CRYPTO_hash_create_random (&id1);
  GNUNET_PSEUDONYM_add (cfg, &id1, meta);
  CHECK (notiCount == 1);
  GNUNET_PSEUDONYM_add (cfg, &id1, meta);
  CHECK (notiCount == 2);
  newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
  CHECK (old < newVal);
  old = newVal;
  GNUNET_CRYPTO_hash_create_random (&id2);
  GNUNET_PSEUDONYM_add (cfg, &id2, meta);
  CHECK (notiCount == 3);
  newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
  CHECK (old < newVal);
  name2 = GNUNET_PSEUDONYM_id_to_name (cfg, &id2);
  CHECK (name2 != NULL);
  name1 = GNUNET_PSEUDONYM_id_to_name (cfg, &id1);
  CHECK (name1 != NULL);
  CHECK (0 != strcmp (name1, name2));
  CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name2, &rid2));
  CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name1, &rid1));
  CHECK (0 == memcmp (&id1, &rid1, sizeof (GNUNET_HashCode)));
  CHECK (0 == memcmp (&id2, &rid2, sizeof (GNUNET_HashCode)));
  CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 0));
  CHECK (5 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
  CHECK (-5 == GNUNET_PSEUDONYM_rank (cfg, &id1, -10));
  CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
  GNUNET_free (name1);
  GNUNET_free (name2);
  /* END OF TEST CODE */
FAILURE:
  GNUNET_PSEUDONYM_discovery_callback_unregister (&noti_callback, &notiCount);
  GNUNET_CONTAINER_meta_data_destroy (meta);
  GNUNET_CONFIGURATION_destroy (cfg);
  GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
  return (ok == GNUNET_YES) ? 0 : 1;
}

/* end of test_pseudoynm.c */