test_friend_hello.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 hello/test_friend_hello.c
  18. * @brief test for hello.c
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_hello_lib.h"
  23. static ssize_t
  24. my_addr_gen (void *cls,
  25. size_t max,
  26. void *buf)
  27. {
  28. unsigned int *i = cls;
  29. size_t ret;
  30. struct GNUNET_HELLO_Address address;
  31. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  32. "DEBUG: my_addr_gen called with i = %d\n", *i);
  33. if (0 == *i)
  34. return GNUNET_SYSERR; /* Stop iteration */
  35. memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
  36. address.address = "address_information";
  37. address.transport_name = "test";
  38. address.address_length = *i;
  39. ret =
  40. GNUNET_HELLO_add_address (&address, GNUNET_TIME_absolute_get (), buf,
  41. max);
  42. (*i)--;
  43. return ret;
  44. }
  45. static int
  46. check_addr (void *cls,
  47. const struct GNUNET_HELLO_Address *address,
  48. struct GNUNET_TIME_Absolute expiration)
  49. {
  50. unsigned int *i = cls;
  51. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  52. "DEBUG: check_addr called with i = %d and addrlen = %u\n",
  53. *i, (unsigned int) address->address_length);
  54. GNUNET_assert (address->address_length > 0);
  55. GNUNET_assert (*i & (1 << (address->address_length - 1)));
  56. *i -= (1 << (address->address_length - 1));
  57. GNUNET_assert (0 ==
  58. strncmp ("address_information", address->address,
  59. address->address_length));
  60. GNUNET_assert (0 == strcmp ("test", address->transport_name));
  61. return GNUNET_OK;
  62. }
  63. static int
  64. remove_some (void *cls,
  65. const struct GNUNET_HELLO_Address *address,
  66. struct GNUNET_TIME_Absolute expiration)
  67. {
  68. unsigned int *i = cls;
  69. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  70. "DEBUG: remove_some called with i = %d and addrlen = %u\n",
  71. *i, (unsigned int) address->address_length);
  72. GNUNET_assert (address->address_length > 0);
  73. if (*i & (1 << (address->address_length - 1)))
  74. {
  75. *i -= (1 << (address->address_length - 1));
  76. return GNUNET_NO;
  77. }
  78. return GNUNET_OK;
  79. }
  80. int
  81. main (int argc, char *argv[])
  82. {
  83. struct GNUNET_HELLO_Message *msg1;
  84. struct GNUNET_HELLO_Message *msg2;
  85. struct GNUNET_HELLO_Message *msg3;
  86. struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
  87. struct GNUNET_TIME_Absolute startup_time;
  88. unsigned int i;
  89. GNUNET_log_setup ("test-hello", "DEBUG", NULL);
  90. startup_time = GNUNET_TIME_absolute_get ();
  91. memset (&publicKey, 42, sizeof (publicKey));
  92. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  93. "Testing HELLO creation (without addresses)...\n");
  94. i = 0;
  95. msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
  96. GNUNET_assert (msg1 != NULL);
  97. GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
  98. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  99. "Testing address iteration (empty set)...\n");
  100. GNUNET_assert (NULL ==
  101. GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
  102. &i));
  103. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  104. "Testing HELLO creation (with one address)...\n");
  105. i = 1;
  106. msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
  107. GNUNET_assert (msg2 != NULL);
  108. GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
  109. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  110. "Testing address iteration (one address)...\n");
  111. i = 1;
  112. GNUNET_assert (NULL ==
  113. GNUNET_HELLO_iterate_addresses (msg2, GNUNET_NO, &check_addr,
  114. &i));
  115. GNUNET_assert (i == 0);
  116. GNUNET_free (msg1);
  117. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  118. "Testing HELLO creation (with two addresses)...\n");
  119. i = 2;
  120. msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
  121. GNUNET_assert (msg3 != NULL);
  122. GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
  123. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  124. "Testing address iteration (two addresses)...\n");
  125. i = 3;
  126. GNUNET_assert (NULL ==
  127. GNUNET_HELLO_iterate_addresses (msg3, GNUNET_NO, &check_addr,
  128. &i));
  129. GNUNET_assert (i == 0);
  130. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  131. "Testing HELLO merge...\n");
  132. msg1 = GNUNET_HELLO_merge (msg2, msg3);
  133. GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
  134. i = 3;
  135. GNUNET_assert (NULL ==
  136. GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
  137. &i));
  138. GNUNET_assert (i == 0);
  139. GNUNET_free (msg1);
  140. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  141. "Testing address iteration to copy HELLO...\n");
  142. i = 2;
  143. msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
  144. GNUNET_assert (msg1 != NULL);
  145. GNUNET_assert (i == 0);
  146. i = 1;
  147. GNUNET_assert (NULL ==
  148. GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
  149. &i));
  150. GNUNET_assert (i == 0);
  151. GNUNET_free (msg1);
  152. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  153. "Testing delta address iteration...\n");
  154. i = 2;
  155. GNUNET_HELLO_iterate_new_addresses (msg3, msg2, startup_time, &check_addr,
  156. &i);
  157. GNUNET_assert (i == 0);
  158. GNUNET_free (msg2);
  159. GNUNET_free (msg3);
  160. return 0; /* testcase passed */
  161. }