test_peer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_peer.c
  18. * @brief testcase for peer.c
  19. * @author Safey Mohammed
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include <gcrypt.h>
  24. #define NUMBER_OF_PEERS 10
  25. /**
  26. * A list of Peer ID's to play with
  27. */
  28. static struct GNUNET_PeerIdentity pidArr[NUMBER_OF_PEERS];
  29. static void
  30. generatePeerIdList ()
  31. {
  32. for (unsigned int i = 0; i < NUMBER_OF_PEERS; i++)
  33. {
  34. gcry_randomize (&pidArr[i],
  35. sizeof(struct GNUNET_PeerIdentity),
  36. GCRY_STRONG_RANDOM);
  37. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  38. "Peer %u: %s\n",
  39. i,
  40. GNUNET_i2s (&pidArr[i]));
  41. }
  42. }
  43. static int
  44. check ()
  45. {
  46. GNUNET_PEER_Id pid;
  47. struct GNUNET_PeerIdentity res;
  48. GNUNET_PEER_Id ids[] = { 1, 2, 3 };
  49. GNUNET_assert (0 == GNUNET_PEER_intern (NULL));
  50. /* Insert Peers into PeerEntry table and hashmap */
  51. for (unsigned int i = 0; i < NUMBER_OF_PEERS; i++)
  52. {
  53. pid = GNUNET_PEER_intern (&pidArr[i]);
  54. if (pid != (i + 1))
  55. {
  56. fprintf (stderr, "%s",
  57. "Unexpected Peer ID returned by intern function\n");
  58. return 1;
  59. }
  60. }
  61. /* Referencing the first 3 peers once again */
  62. for (unsigned int i = 0; i < 3; i++)
  63. {
  64. pid = GNUNET_PEER_intern (&pidArr[i]);
  65. if (pid != (i + 1))
  66. {
  67. fprintf (stderr, "%s",
  68. "Unexpected Peer ID returned by intern function\n");
  69. return 1;
  70. }
  71. }
  72. /* Dereferencing the first 3 peers once [decrementing their reference count] */
  73. GNUNET_PEER_decrement_rcs (ids, 3);
  74. /* re-referencing the first 3 peers using the change_rc function */
  75. for (unsigned int i = 1; i <= 3; i++)
  76. GNUNET_PEER_change_rc (i, 1);
  77. /* Removing the second Peer from the PeerEntry hash map */
  78. GNUNET_PEER_change_rc (2, -2);
  79. /* convert the pid of the first PeerEntry into that of the third */
  80. GNUNET_PEER_resolve (1,
  81. &res);
  82. GNUNET_assert (0 ==
  83. GNUNET_memcmp (&res,
  84. &pidArr[0]));
  85. /*
  86. * Attempt to convert pid = 0 (which is reserved)
  87. * into a peer identity object, the peer identity memory
  88. * is expected to be set to zero
  89. */GNUNET_log_skip (1, GNUNET_YES);
  90. GNUNET_PEER_resolve (0, &res);
  91. GNUNET_assert (0 ==
  92. GNUNET_is_zero (&res));
  93. /* Removing peer entries 1 and 3 from table using the list decrement function */
  94. /* If count = 0, nothing should be done whatsoever */
  95. GNUNET_PEER_decrement_rcs (ids, 0);
  96. ids[1] = 3;
  97. GNUNET_PEER_decrement_rcs (ids, 2);
  98. GNUNET_PEER_decrement_rcs (ids, 2);
  99. return 0;
  100. }
  101. int
  102. main ()
  103. {
  104. GNUNET_log_setup ("test-peer",
  105. "ERROR",
  106. NULL);
  107. for (unsigned int i = 0; i < 1; i++)
  108. {
  109. generatePeerIdList ();
  110. if (0 != check ())
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. /* end of test_peer.c */