test_gnsrecord_serialization.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file gnsrecord/test_gnsrecord_serialization.c
  19. * @brief testcase for gnsrecord_serialization.c
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_gnsrecord_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, char *const *args, const char *cfgfile,
  28. const struct GNUNET_CONFIGURATION_Handle *cfg)
  29. {
  30. size_t len;
  31. int c;
  32. int rd_count = 3;
  33. size_t data_len;
  34. struct GNUNET_GNSRECORD_Data src[rd_count];
  35. memset(src, '\0', rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
  36. data_len = 0;
  37. for (c = 0; c < rd_count; c++)
  38. {
  39. src[c].record_type = c+1;
  40. src[c].data_size = data_len;
  41. src[c].data = GNUNET_malloc (data_len);
  42. /* Setting data to data_len * record_type */
  43. memset ((char *) src[c].data, 'a', data_len);
  44. data_len += 10;
  45. }
  46. res = 0;
  47. len = GNUNET_GNSRECORD_records_get_size(rd_count, src);
  48. char rd_ser[len];
  49. GNUNET_assert (len == GNUNET_GNSRECORD_records_serialize(rd_count, src, len, rd_ser));
  50. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serialized data len: %u\n",len);
  51. GNUNET_assert (rd_ser != NULL);
  52. struct GNUNET_GNSRECORD_Data dst[rd_count];
  53. GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_records_deserialize (len, rd_ser, rd_count, dst));
  54. GNUNET_assert (dst != NULL);
  55. for (c = 0; c < rd_count; c++)
  56. {
  57. if (src[c].data_size != dst[c].data_size)
  58. {
  59. GNUNET_break (0);
  60. res = 1;
  61. }
  62. if (src[c].expiration_time != dst[c].expiration_time)
  63. {
  64. GNUNET_break (0);
  65. res = 1;
  66. }
  67. if (src[c].flags != dst[c].flags)
  68. {
  69. GNUNET_break (0);
  70. res = 1;
  71. }
  72. if (src[c].record_type != dst[c].record_type)
  73. {
  74. GNUNET_break (0);
  75. res = 1;
  76. }
  77. size_t data_size = src[c].data_size;
  78. char data[data_size];
  79. memset (data, 'a', data_size);
  80. if (0 != memcmp (data, dst[c].data, data_size))
  81. {
  82. GNUNET_break (0);
  83. res = 1;
  84. }
  85. if (0 != memcmp (data, src[c].data, data_size))
  86. {
  87. GNUNET_break (0);
  88. res = 1;
  89. }
  90. if (0 != memcmp (src[c].data, dst[c].data, src[c].data_size))
  91. {
  92. GNUNET_break (0);
  93. res = 1;
  94. }
  95. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c);
  96. }
  97. for (c = 0; c < rd_count; c++)
  98. {
  99. GNUNET_free ((void *)src[c].data);
  100. }
  101. }
  102. int
  103. main (int argcx, char *argvx[])
  104. {
  105. static char *const argv[] = { "test_gnsrecord_serialization",
  106. NULL
  107. };
  108. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  109. GNUNET_GETOPT_OPTION_END
  110. };
  111. res = 1;
  112. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_record_serialization",
  113. "nohelp", options, &run, &res);
  114. return res;
  115. }
  116. /* end of test_gnsrecord_serialization.c */