test_nse_api.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011 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 nse/test_nse_api.c
  18. * @brief testcase for nse_api.c
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. #include "gnunet_nse_service.h"
  23. #include "gnunet_testing_lib.h"
  24. static struct GNUNET_NSE_Handle *h;
  25. static struct GNUNET_SCHEDULER_Task * die_task;
  26. /**
  27. * Signature of the main function of a task.
  28. *
  29. * @param cls closure
  30. */
  31. static void
  32. end_test (void *cls)
  33. {
  34. if (h != NULL)
  35. {
  36. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from NSE service.\n");
  37. GNUNET_NSE_disconnect (h);
  38. }
  39. }
  40. /**
  41. * Callback to call when network size estimate is updated.
  42. *
  43. * @param cls unused
  44. * @param timestamp time when the estimate was received from the server (or created by the server)
  45. * @param estimate the value of the current network size estimate
  46. * @param std_dev standard deviation (rounded down to nearest integer)
  47. * of the size estimation values seen
  48. *
  49. */
  50. static void
  51. check_nse_message (void *cls, struct GNUNET_TIME_Absolute timestamp,
  52. double estimate, double std_dev)
  53. {
  54. int *ok = cls;
  55. FPRINTF (stderr,
  56. "Received NSE message, estimate %f, standard deviation %f.\n",
  57. estimate, std_dev);
  58. /* Fantastic check below. Expect NaN, the only thing not equal to itself. */
  59. (*ok) = 0;
  60. if (die_task != NULL)
  61. GNUNET_SCHEDULER_cancel (die_task);
  62. die_task = GNUNET_SCHEDULER_add_now (&end_test, NULL);
  63. }
  64. static void
  65. run (void *cls,
  66. const struct GNUNET_CONFIGURATION_Handle *cfg,
  67. struct GNUNET_TESTING_Peer *peer)
  68. {
  69. die_task =
  70. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  71. (GNUNET_TIME_UNIT_MINUTES, 1), &end_test,
  72. NULL);
  73. h = GNUNET_NSE_connect (cfg, &check_nse_message, cls);
  74. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to NSE service.\n");
  75. GNUNET_assert (h != NULL);
  76. }
  77. int
  78. main (int argc, char *argv[])
  79. {
  80. int ok = 1;
  81. if (0 != GNUNET_TESTING_peer_run ("test_nse_api",
  82. "test_nse.conf",
  83. &run, &ok))
  84. return 1;
  85. return ok;
  86. }
  87. /* end of test_nse_api.c */