perf_gnsrecord_crypto.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2018 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_crypto.c
  18. * @brief testcase for block creation, verification and decryption
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. #include "gnunet_gnsrecord_lib.h"
  23. #define ROUNDS 1000
  24. #define RECORDS 5
  25. #define TEST_RECORD_TYPE 1234
  26. #define TEST_RECORD_DATALEN 123
  27. #define TEST_RECORD_DATA 'a'
  28. #define TEST_REMOVE_RECORD_TYPE 4321
  29. #define TEST_REMOVE_RECORD_DATALEN 255
  30. #define TEST_REMOVE_RECORD_DATA 'b'
  31. static struct GNUNET_GNSRECORD_Data *
  32. create_record (int count)
  33. {
  34. struct GNUNET_GNSRECORD_Data *rd;
  35. rd = GNUNET_new_array (count,
  36. struct GNUNET_GNSRECORD_Data);
  37. for (unsigned int c = 0; c < count; c++)
  38. {
  39. rd[c].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
  40. + 1000000000;
  41. rd[c].record_type = TEST_RECORD_TYPE;
  42. rd[c].data_size = TEST_RECORD_DATALEN;
  43. rd[c].data = GNUNET_malloc (TEST_RECORD_DATALEN);
  44. memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
  45. }
  46. return rd;
  47. }
  48. static void
  49. run (void *cls,
  50. char *const *args,
  51. const char *cfgfile,
  52. const struct GNUNET_CONFIGURATION_Handle *cfg)
  53. {
  54. struct GNUNET_GNSRECORD_Block *block;
  55. struct GNUNET_HashCode query;
  56. struct GNUNET_GNSRECORD_Data *s_rd;
  57. const char *s_name;
  58. struct GNUNET_TIME_Absolute start_time;
  59. struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
  60. struct GNUNET_TIME_Absolute expire;
  61. (void) cls;
  62. (void) args;
  63. (void) cfgfile;
  64. (void) cfg;
  65. expire = GNUNET_TIME_absolute_get ();
  66. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  67. GNUNET_assert (NULL != privkey);
  68. /* test block creation */
  69. s_name = "DUMMY.dummy.gnunet";
  70. s_rd = create_record (RECORDS);
  71. start_time = GNUNET_TIME_absolute_get ();
  72. for (unsigned int i = 0; i < ROUNDS; i++)
  73. {
  74. GNUNET_assert (NULL != (block =
  75. GNUNET_GNSRECORD_block_create2 (privkey,
  76. expire,
  77. s_name,
  78. s_rd,
  79. RECORDS)));
  80. GNUNET_GNSRECORD_query_from_private_key (privkey,
  81. s_name,
  82. &query);
  83. GNUNET_free (block);
  84. }
  85. fprintf (stderr,
  86. "Took %s to produce %u GNS blocks for the DHT\n",
  87. GNUNET_STRINGS_relative_time_to_string (
  88. GNUNET_TIME_absolute_get_duration (start_time),
  89. GNUNET_YES),
  90. ROUNDS);
  91. for (unsigned int i = 0; i < RECORDS; i++)
  92. GNUNET_free ((void *) s_rd[i].data);
  93. GNUNET_free (s_rd);
  94. GNUNET_free (privkey);
  95. }
  96. int
  97. main (int argc, char *argv[])
  98. {
  99. static char *const argvx[] = {
  100. "perf-gnsrecord-crypto",
  101. NULL
  102. };
  103. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  104. GNUNET_GETOPT_OPTION_END
  105. };
  106. if (GNUNET_OK !=
  107. GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1,
  108. argvx,
  109. "perf-gnsrecord-crypto",
  110. "nohelp", options,
  111. &run,
  112. NULL))
  113. return 1;
  114. return 0;
  115. }
  116. /* end of test_gnsrecord_crypto.c */