aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_peer.c
blob: 695e2549bc97533b8dc870db9f839690b0572f58 (plain)
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
/*
     This file is part of GNUnet.
     (C) 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_peer.c
 * @brief testcase for peer.c
 * @author Safey Mohammed
 */

#include "platform.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_peer_lib.h"

#define NUMBER_OF_PEERS 10

#define DEBUG GNUNET_NO

/**
 * A list of Peer ID's to play with 
 */
static struct GNUNET_PeerIdentity pidArr[NUMBER_OF_PEERS];


static void
generatePeerIdList ()
{
  int i;

  for (i = 0; i < NUMBER_OF_PEERS; i++)
    {
      GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
                                        &pidArr[i].hashPubKey);
#if DEBUG
      printf ("Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i]));
#endif
    }
}


static int
check ()
{
  int i;
  GNUNET_PEER_Id pid;
  struct GNUNET_PeerIdentity res;
  GNUNET_PEER_Id ids[] = { 1, 2, 3 };

  GNUNET_assert (0 == GNUNET_PEER_intern (NULL));
  /* Insert Peers into PeerEntry table and hashmap */
  for (i = 0; i < NUMBER_OF_PEERS; i++)
    {
      pid = GNUNET_PEER_intern (&pidArr[i]);
      if (pid != (i + 1))
        {
          fprintf (stderr,
                   "Unexpected Peer ID returned by intern function \n");
          return 1;
        }
    }

  /* Referencing the first 3 peers once again */
  for (i = 0; i < 3; i++)
    {
      pid = GNUNET_PEER_intern (&pidArr[i]);
      if (pid != (i + 1))
        {
          fprintf (stderr,
                   "Unexpected Peer ID returned by intern function \n");
          return 1;
        }
    }

  /* Dereferencing the first 3 peers once [decrementing their reference count] */ 
  GNUNET_PEER_decrement_rcs (ids, 3);  

  /* re-referencing the first 3 peers using the change_rc function */
  for (i = 0; i < 3; i++)   
    GNUNET_PEER_change_rc (i, 1);

  /* Removing the second Peer from the PeerEntry hash map */
  GNUNET_PEER_change_rc (2, -2);

  /* convert the pid of the first PeerEntry into that of the third */
  GNUNET_PEER_resolve (1, &res);
  GNUNET_assert (0 == memcmp (&res, &pidArr[0], sizeof(res)));

  return 0;
}


int
main ()
{
  int i;
  GNUNET_log_setup ("test-peer", "ERROR", NULL);
  for (i=0;i<1;i++)
    {
      generatePeerIdList ();
      if (0 != check ())
	return 1;
    }
  return 0;
}

/* end of test_peer.c */