test_testbed_api_hosts.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--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 testbed/test_testbed_api_hosts.c
  18. * @brief tests cases for testbed_api_hosts.c
  19. * @author Sree Harsha Totakura
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testbed_service.h"
  24. #include "testbed_api_hosts.h"
  25. #define TIME_REL_SECS(sec) \
  26. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
  27. /**
  28. * configuration handle to use as template configuration while creating hosts
  29. */
  30. static struct GNUNET_CONFIGURATION_Handle *cfg;
  31. /**
  32. * Host we are creating and using
  33. */
  34. static struct GNUNET_TESTBED_Host *host;
  35. /**
  36. * An array of hosts which are loaded from a file
  37. */
  38. static struct GNUNET_TESTBED_Host **hosts;
  39. /**
  40. * Number of hosts in the above list
  41. */
  42. static unsigned int num_hosts;
  43. /**
  44. * Global test status
  45. */
  46. static int status;
  47. /**
  48. * The shutdown task
  49. *
  50. * @param cls NULL
  51. */
  52. static void
  53. do_shutdown (void *cls)
  54. {
  55. GNUNET_TESTBED_host_destroy (host);
  56. while (0 != num_hosts)
  57. {
  58. GNUNET_TESTBED_host_destroy (hosts[num_hosts - 1]);
  59. num_hosts--;
  60. }
  61. GNUNET_free (hosts);
  62. if (NULL != cfg)
  63. {
  64. GNUNET_CONFIGURATION_destroy (cfg);
  65. cfg = NULL;
  66. }
  67. }
  68. /**
  69. * Main run function.
  70. *
  71. * @param cls NULL
  72. * @param args arguments passed to GNUNET_PROGRAM_run
  73. * @param cfgfile the path to configuration file
  74. * @param cfg the configuration file handle
  75. */
  76. static void
  77. run (void *cls, char *const *args, const char *cfgfile,
  78. const struct GNUNET_CONFIGURATION_Handle *config)
  79. {
  80. unsigned int cnt;
  81. cfg = GNUNET_CONFIGURATION_dup (config);
  82. host = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
  83. GNUNET_assert (NULL != host);
  84. GNUNET_assert (0 != GNUNET_TESTBED_host_get_id_ (host));
  85. GNUNET_TESTBED_host_destroy (host);
  86. host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
  87. GNUNET_assert (NULL != host);
  88. GNUNET_assert (0 == GNUNET_TESTBED_host_get_id_ (host));
  89. GNUNET_assert (host == GNUNET_TESTBED_host_lookup_by_id_ (0));
  90. hosts = NULL;
  91. num_hosts = GNUNET_TESTBED_hosts_load_from_file ("sample_hosts.txt", cfg,
  92. &hosts);
  93. GNUNET_assert (7 == num_hosts);
  94. GNUNET_assert (NULL != hosts);
  95. for (cnt = 0; cnt < num_hosts; cnt++)
  96. {
  97. if (cnt < 3)
  98. {
  99. GNUNET_assert (0 == strcmp ("totakura",
  100. GNUNET_TESTBED_host_get_username_
  101. (hosts[cnt])));
  102. GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
  103. GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
  104. }
  105. if (3 == cnt)
  106. {
  107. GNUNET_assert (0 == strcmp ("totakura",
  108. GNUNET_TESTBED_host_get_username_
  109. (hosts[cnt])));
  110. GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
  111. GNUNET_assert (2022 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
  112. }
  113. if (4 == cnt)
  114. {
  115. GNUNET_assert (0 == strcmp ("totakura",
  116. GNUNET_TESTBED_host_get_username_
  117. (hosts[cnt])));
  118. GNUNET_assert (0 == strcmp ("asgard.realm",
  119. GNUNET_TESTBED_host_get_hostname
  120. (hosts[cnt])));
  121. GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
  122. }
  123. if (5 == cnt)
  124. {
  125. GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
  126. GNUNET_assert (0 == strcmp ("rivendal",
  127. GNUNET_TESTBED_host_get_hostname
  128. (hosts[cnt])));
  129. GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
  130. }
  131. if (6 == cnt)
  132. {
  133. GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
  134. GNUNET_assert (0 == strcmp ("rohan",
  135. GNUNET_TESTBED_host_get_hostname
  136. (hosts[cnt])));
  137. GNUNET_assert (561 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
  138. }
  139. }
  140. status = GNUNET_YES;
  141. GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  142. }
  143. int
  144. main (int argc, char **argv)
  145. {
  146. char *const argv2[] = { "test_testbed_api_hosts",
  147. "-c", "test_testbed_api.conf",
  148. NULL };
  149. struct GNUNET_GETOPT_CommandLineOption options[] = {
  150. GNUNET_GETOPT_OPTION_END
  151. };
  152. status = GNUNET_SYSERR;
  153. if (GNUNET_OK !=
  154. GNUNET_PROGRAM_run ((sizeof(argv2) / sizeof(char *)) - 1, argv2,
  155. "test_testbed_api_hosts", "nohelp", options, &run,
  156. NULL))
  157. return 1;
  158. return (GNUNET_OK == status) ? 0 : 1;
  159. }
  160. /* end of test_testbed_api_hosts.c */