test_gnsrecord_serialization.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 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 gnsrecord/test_gnsrecord_serialization.c
  18. * @brief testcase for gnsrecord_serialization.c
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. #include "gnunet_gnsrecord_lib.h"
  23. #include "gnunet_dnsparser_lib.h"
  24. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
  25. static int res;
  26. static void
  27. run (void *cls,
  28. char *const *args,
  29. const char *cfgfile,
  30. const struct GNUNET_CONFIGURATION_Handle *cfg)
  31. {
  32. size_t len;
  33. int c;
  34. int rd_count = 3;
  35. size_t data_len;
  36. struct GNUNET_GNSRECORD_Data src[rd_count];
  37. memset (src, '\0', rd_count * sizeof(struct GNUNET_GNSRECORD_Data));
  38. data_len = 0;
  39. for (c = 0; c < rd_count; c++)
  40. {
  41. src[c].record_type = GNUNET_DNSPARSER_TYPE_TXT;
  42. src[c].data_size = data_len;
  43. src[c].data = GNUNET_malloc (data_len);
  44. /* Setting data to data_len * record_type */
  45. memset ((char *) src[c].data, 'a', data_len);
  46. data_len += 10;
  47. }
  48. res = 0;
  49. len = GNUNET_GNSRECORD_records_get_size (rd_count, src);
  50. char rd_ser[len];
  51. GNUNET_assert (len ==
  52. GNUNET_GNSRECORD_records_serialize (rd_count,
  53. src,
  54. len,
  55. rd_ser));
  56. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  57. "Serialized data len: %u\n",
  58. (unsigned int) len);
  59. GNUNET_assert (rd_ser != NULL);
  60. {
  61. struct GNUNET_GNSRECORD_Data dst[rd_count];
  62. GNUNET_assert (GNUNET_OK ==
  63. GNUNET_GNSRECORD_records_deserialize (len,
  64. rd_ser,
  65. rd_count,
  66. dst));
  67. GNUNET_assert (dst != NULL);
  68. for (c = 0; c < rd_count; c++)
  69. {
  70. if (src[c].data_size != dst[c].data_size)
  71. {
  72. GNUNET_break (0);
  73. res = 1;
  74. }
  75. if (src[c].expiration_time != dst[c].expiration_time)
  76. {
  77. GNUNET_break (0);
  78. res = 1;
  79. }
  80. if (src[c].flags != dst[c].flags)
  81. {
  82. GNUNET_break (0);
  83. res = 1;
  84. }
  85. if (src[c].record_type != dst[c].record_type)
  86. {
  87. GNUNET_break (0);
  88. res = 1;
  89. }
  90. {
  91. size_t data_size = src[c].data_size;
  92. char data[data_size];
  93. memset (data, 'a', data_size);
  94. if (0 != memcmp (data, dst[c].data, data_size))
  95. {
  96. GNUNET_break (0);
  97. res = 1;
  98. }
  99. if (0 != memcmp (data, src[c].data, data_size))
  100. {
  101. GNUNET_break (0);
  102. res = 1;
  103. }
  104. if (0 != memcmp (src[c].data, dst[c].data, src[c].data_size))
  105. {
  106. GNUNET_break (0);
  107. res = 1;
  108. }
  109. }
  110. }
  111. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c);
  112. }
  113. for (c = 0; c < rd_count; c++)
  114. {
  115. GNUNET_free ((void *) src[c].data);
  116. }
  117. }
  118. int
  119. main (int argcx, char *argvx[])
  120. {
  121. static char *const argv[] = { "test_gnsrecord_serialization",
  122. NULL };
  123. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  124. GNUNET_GETOPT_OPTION_END
  125. };
  126. res = 1;
  127. GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
  128. "test_namestore_record_serialization",
  129. "nohelp", options, &run, &res);
  130. return res;
  131. }
  132. /* end of test_gnsrecord_serialization.c */