test_nat_test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2011, 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 nat/test_nat_test.c
  18. * @brief Testcase for NAT testing functions
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_nat_lib.h"
  24. /**
  25. * Time to wait before stopping NAT test, in seconds
  26. */
  27. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
  28. static int ret = 1;
  29. static struct GNUNET_NAT_Test *tst;
  30. static struct GNUNET_SCHEDULER_Task * tsk;
  31. static void
  32. report_result (void *cls,
  33. enum GNUNET_NAT_StatusCode aret)
  34. {
  35. if (GNUNET_NAT_ERROR_TIMEOUT == aret)
  36. fprintf (stderr,
  37. "NAT test timed out\n");
  38. else if (GNUNET_NAT_ERROR_SUCCESS != aret)
  39. fprintf (stderr,
  40. "NAT test reported error %d\n", aret);
  41. else
  42. ret = 0;
  43. GNUNET_NAT_test_stop (tst);
  44. tst = NULL;
  45. GNUNET_SCHEDULER_cancel (tsk);
  46. tsk = NULL;
  47. }
  48. static void
  49. failed_timeout (void *cls)
  50. {
  51. tsk = NULL;
  52. fprintf (stderr,
  53. "NAT test failed to terminate on timeout\n");
  54. ret = 2;
  55. GNUNET_NAT_test_stop (tst);
  56. tst = NULL;
  57. }
  58. /**
  59. * Main function run with scheduler.
  60. */
  61. static void
  62. run (void *cls, char *const *args, const char *cfgfile,
  63. const struct GNUNET_CONFIGURATION_Handle *cfg)
  64. {
  65. tst =
  66. GNUNET_NAT_test_start (cfg, GNUNET_YES, 1285, 1285, TIMEOUT,
  67. &report_result,
  68. NULL);
  69. tsk = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (TIMEOUT,
  70. 2),
  71. &failed_timeout,
  72. NULL);
  73. }
  74. int
  75. main (int argc, char *const argv[])
  76. {
  77. struct GNUNET_GETOPT_CommandLineOption options[] = {
  78. GNUNET_GETOPT_OPTION_END
  79. };
  80. struct GNUNET_OS_Process *gns;
  81. int nat_res;
  82. char *const argv_prog[] = {
  83. "test-nat-test",
  84. "-c",
  85. "test_nat_test_data.conf",
  86. NULL
  87. };
  88. GNUNET_log_setup ("test-nat-test",
  89. "WARNING",
  90. NULL);
  91. nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server", GNUNET_NO, NULL);
  92. if (GNUNET_SYSERR == nat_res)
  93. {
  94. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  95. "Cannot run NAT test: `%s' file not found\n",
  96. "gnunet-nat-server");
  97. return 0;
  98. }
  99. gns = GNUNET_OS_start_process (GNUNET_YES,
  100. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  101. NULL, NULL, NULL,
  102. "gnunet-nat-server",
  103. "gnunet-nat-server",
  104. "-c", "test_nat_test_data.conf",
  105. "12345", NULL);
  106. GNUNET_assert (NULL != gns);
  107. GNUNET_PROGRAM_run (3, argv_prog,
  108. "test-nat-test", "nohelp",
  109. options, &run,
  110. NULL);
  111. GNUNET_break (0 == GNUNET_OS_process_kill (gns, GNUNET_TERM_SIG));
  112. GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (gns));
  113. GNUNET_OS_process_destroy (gns);
  114. if (0 != ret)
  115. fprintf (stderr,
  116. "NAT test failed to report success\n");
  117. return ret;
  118. }
  119. /* end of test_nat_test.c */