test_secretsharing_api.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2014 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 secretsharing/test_secretsharing_api.c
  18. * @brief testcase for the secretsharing api
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. #include "gnunet_testing_lib.h"
  23. #include "gnunet_secretsharing_service.h"
  24. static int success;
  25. static struct GNUNET_SECRETSHARING_Session *keygen;
  26. static void
  27. secret_ready_cb (void *cls,
  28. struct GNUNET_SECRETSHARING_Share *my_share,
  29. struct GNUNET_SECRETSHARING_PublicKey *public_key,
  30. unsigned int num_ready_peers,
  31. const struct GNUNET_PeerIdentity *ready_peers)
  32. {
  33. keygen = NULL;
  34. if (num_ready_peers == 1)
  35. success = 1;
  36. // FIXME: check that our share is valid, which we can do as there's only
  37. // one peer.
  38. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "secret ready, shutting down\n");
  39. GNUNET_SCHEDULER_shutdown ();
  40. }
  41. static void
  42. handle_shutdown (void *cls)
  43. {
  44. if (NULL != keygen)
  45. {
  46. GNUNET_SECRETSHARING_session_destroy (keygen);
  47. keygen = NULL;
  48. }
  49. }
  50. static void
  51. run (void *cls,
  52. const struct GNUNET_CONFIGURATION_Handle *cfg,
  53. struct GNUNET_TESTING_Peer *peer)
  54. {
  55. struct GNUNET_HashCode session_id;
  56. struct GNUNET_TIME_Absolute start;
  57. struct GNUNET_TIME_Absolute deadline;
  58. GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
  59. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "testing secretsharing api\n");
  60. GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &session_id);
  61. start = GNUNET_TIME_absolute_get ();
  62. deadline = GNUNET_TIME_absolute_add (start, GNUNET_TIME_UNIT_SECONDS);
  63. keygen = GNUNET_SECRETSHARING_create_session (cfg,
  64. 0, NULL, /* only the local peer */
  65. &session_id,
  66. start, deadline,
  67. 1,
  68. secret_ready_cb, NULL);
  69. }
  70. int
  71. main (int argc, char **argv)
  72. {
  73. int ret;
  74. ret = GNUNET_TESTING_peer_run ("test_secretsharing_api",
  75. "test_secretsharing.conf",
  76. &run, NULL);
  77. if (0 != ret)
  78. return ret;
  79. return (GNUNET_YES == success) ? 0 : 1;
  80. }