test_gnsrecord_crypto.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_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_dnsparser_lib.h"
  23. #include "gnunet_gnsrecord_lib.h"
  24. #define RECORDS 5
  25. #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
  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_CRYPTO_EcdsaPrivateKey *privkey;
  32. static struct GNUNET_GNSRECORD_Data *s_rd;
  33. static char *s_name;
  34. static int res;
  35. static struct GNUNET_GNSRECORD_Data *
  36. create_record (int count)
  37. {
  38. struct GNUNET_GNSRECORD_Data *rd;
  39. rd = GNUNET_new_array (count, struct GNUNET_GNSRECORD_Data);
  40. for (unsigned int c = 0; c < count; c++)
  41. {
  42. rd[c].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
  43. + 1000000000;
  44. rd[c].record_type = TEST_RECORD_TYPE;
  45. rd[c].data_size = TEST_RECORD_DATALEN;
  46. rd[c].data = GNUNET_malloc (TEST_RECORD_DATALEN);
  47. memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
  48. }
  49. return rd;
  50. }
  51. static void
  52. rd_decrypt_cb (void *cls,
  53. unsigned int rd_count,
  54. const struct GNUNET_GNSRECORD_Data *rd)
  55. {
  56. char rd_cmp_data[TEST_RECORD_DATALEN];
  57. GNUNET_assert (RECORDS == rd_count);
  58. GNUNET_assert (NULL != rd);
  59. memset (rd_cmp_data,
  60. 'a',
  61. TEST_RECORD_DATALEN);
  62. for (unsigned int c = 0; c < rd_count; c++)
  63. {
  64. GNUNET_assert (TEST_RECORD_TYPE == rd[c].record_type);
  65. GNUNET_assert (TEST_RECORD_DATALEN == rd[c].data_size);
  66. GNUNET_assert (0 == memcmp (&rd_cmp_data,
  67. rd[c].data,
  68. TEST_RECORD_DATALEN));
  69. }
  70. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  71. "Block was decrypted successfully \n");
  72. res = 0;
  73. }
  74. static void
  75. run (void *cls,
  76. char *const *args,
  77. const char *cfgfile,
  78. const struct GNUNET_CONFIGURATION_Handle *cfg)
  79. {
  80. struct GNUNET_GNSRECORD_Block *block;
  81. struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  82. struct GNUNET_HashCode query_pub;
  83. struct GNUNET_HashCode query_priv;
  84. struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get ();
  85. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  86. GNUNET_assert (NULL != privkey);
  87. /* get public key */
  88. GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
  89. &pubkey);
  90. /* test query derivation */
  91. GNUNET_GNSRECORD_query_from_private_key (privkey,
  92. "testlabel",
  93. &query_priv);
  94. GNUNET_GNSRECORD_query_from_public_key (&pubkey,
  95. "testlabel",
  96. &query_pub);
  97. GNUNET_assert (0 == memcmp (&query_priv,
  98. &query_pub,
  99. sizeof(struct GNUNET_HashCode)));
  100. /* create record */
  101. s_name = "DUMMY.dummy.gnunet";
  102. s_rd = create_record (RECORDS);
  103. /* Create block */
  104. GNUNET_assert (NULL != (block =
  105. GNUNET_GNSRECORD_block_create (privkey,
  106. expire,
  107. s_name,
  108. s_rd,
  109. RECORDS)));
  110. GNUNET_assert (GNUNET_OK ==
  111. GNUNET_GNSRECORD_block_verify (block));
  112. GNUNET_assert (GNUNET_OK ==
  113. GNUNET_GNSRECORD_block_decrypt (block,
  114. &pubkey,
  115. s_name,
  116. &rd_decrypt_cb,
  117. s_name));
  118. GNUNET_free (block);
  119. GNUNET_free (privkey);
  120. }
  121. int
  122. main (int argc, char *argv[])
  123. {
  124. static char *const argvx[] = {
  125. "test-gnsrecord-crypto",
  126. NULL
  127. };
  128. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  129. GNUNET_GETOPT_OPTION_END
  130. };
  131. res = 1;
  132. GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1,
  133. argvx,
  134. "test-gnsrecord-crypto",
  135. "nohelp", options,
  136. &run, &res);
  137. return res;
  138. }
  139. /* end of test_gnsrecord_crypto.c */