test_resolver_api.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009 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 resolver/test_resolver_api.c
  18. * @brief testcase for resolver_api.c
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. #include "gnunet_resolver_service.h"
  23. #include "resolver.h"
  24. static int disable_rootserver_check;
  25. /**
  26. * Using DNS root servers to check gnunet's resolver service
  27. * a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
  28. * For more information have a look at IANA's website http://www.root-servers.org/
  29. */
  30. #define ROOTSERVER_NAME "a.root-servers.net"
  31. #define ROOTSERVER_IP "198.41.0.4"
  32. static void
  33. check_hostname (void *cls,
  34. const struct sockaddr *sa,
  35. socklen_t salen)
  36. {
  37. int *ok = cls;
  38. if (0 == salen)
  39. {
  40. (*ok) &= ~8;
  41. return;
  42. }
  43. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  44. "Got IP address `%s' for our host.\n",
  45. GNUNET_a2s (sa, salen));
  46. }
  47. static void
  48. check_localhost_num (void *cls,
  49. const char *hostname)
  50. {
  51. int *ok = cls;
  52. if (hostname == NULL)
  53. return;
  54. if (0 == strcmp (hostname, "127.0.0.1"))
  55. {
  56. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  57. "Received correct hostname `%s'.\n",
  58. hostname);
  59. (*ok) &= ~4;
  60. }
  61. else
  62. {
  63. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  64. "Received invalid hostname `%s'.\n",
  65. hostname);
  66. GNUNET_break (0);
  67. }
  68. }
  69. static void
  70. check_localhost (void *cls,
  71. const char *hostname)
  72. {
  73. int *ok = cls;
  74. if (NULL == hostname)
  75. return;
  76. if (0 == strcmp (hostname, "localhost"))
  77. {
  78. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  79. "Received correct hostname `%s'.\n",
  80. hostname);
  81. (*ok) &= ~2;
  82. }
  83. else
  84. {
  85. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  86. "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
  87. hostname);
  88. }
  89. }
  90. static void
  91. check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
  92. {
  93. int *ok = cls;
  94. const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
  95. if (NULL == sa)
  96. return;
  97. GNUNET_assert (sizeof(struct sockaddr_in) == salen);
  98. if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
  99. {
  100. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  101. "Received correct address.\n");
  102. (*ok) &= ~1;
  103. }
  104. else
  105. {
  106. char buf[INET_ADDRSTRLEN];
  107. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  108. "Received incorrect address `%s'.\n",
  109. inet_ntop (AF_INET,
  110. &sai->sin_addr,
  111. buf,
  112. sizeof(buf)));
  113. GNUNET_break (0);
  114. }
  115. }
  116. static void
  117. check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
  118. {
  119. int *ok = cls;
  120. const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
  121. if (NULL == sa)
  122. return;
  123. GNUNET_assert (sizeof(struct sockaddr_in) == salen);
  124. if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
  125. {
  126. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  127. "Received correct rootserver ip address.\n");
  128. (*ok) &= ~1;
  129. }
  130. else
  131. {
  132. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  133. "Received incorrect rootserver ip address.\n");
  134. GNUNET_break (0);
  135. }
  136. }
  137. static void
  138. check_rootserver_name (void *cls,
  139. const char *hostname)
  140. {
  141. int *ok = cls;
  142. if (NULL == hostname)
  143. return;
  144. if (0 == strcmp (hostname, ROOTSERVER_NAME))
  145. {
  146. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  147. "Received correct rootserver hostname `%s'.\n",
  148. hostname);
  149. (*ok) &= ~2;
  150. }
  151. else
  152. {
  153. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  154. "Received invalid rootserver hostname `%s', expected `%s'\n",
  155. hostname,
  156. ROOTSERVER_NAME);
  157. GNUNET_break (disable_rootserver_check);
  158. }
  159. }
  160. static void
  161. run (void *cls, char *const *args, const char *cfgfile,
  162. const struct GNUNET_CONFIGURATION_Handle *cfg)
  163. {
  164. int *ok = cls;
  165. struct sockaddr_in sa;
  166. struct GNUNET_TIME_Relative timeout =
  167. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
  168. int count_ips = 0;
  169. char *own_fqdn;
  170. const char *rootserver_name = ROOTSERVER_NAME;
  171. struct hostent *rootserver;
  172. struct in_addr rootserver_addr;
  173. memset (&sa, 0, sizeof(sa));
  174. sa.sin_family = AF_INET;
  175. #if HAVE_SOCKADDR_IN_SIN_LEN
  176. sa.sin_len = (u_char) sizeof(sa);
  177. #endif
  178. sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  179. /*
  180. * Looking up our own fqdn
  181. */
  182. own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
  183. /* can't really check, only thing we can safely
  184. compare against is our own identical logic... */
  185. GNUNET_free_non_null (own_fqdn);
  186. /*
  187. * Testing non-local DNS resolution
  188. * DNS rootserver to test: a.root-servers.net - 198.41.0.4
  189. */
  190. rootserver = gethostbyname (rootserver_name);
  191. if (NULL == rootserver)
  192. {
  193. /* Error: resolving ip addresses does not work */
  194. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  195. _ ("gethostbyname() could not lookup IP address: %s\n"),
  196. hstrerror (h_errno));
  197. fprintf (stderr,
  198. "%s",
  199. "System seems to be off-line, will not run all DNS tests\n");
  200. *ok = 0; /* mark test as passing anyway */
  201. return;
  202. }
  203. /* Counting returned IP addresses */
  204. while (NULL != rootserver->h_addr_list[count_ips])
  205. count_ips++;
  206. if (count_ips > 1)
  207. {
  208. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  209. "IP received range for root name server, but a root name server has only 1 IP\n");
  210. GNUNET_break (0);
  211. }
  212. /* Comparing to resolved address to the address the root name server should have */
  213. if (0 !=
  214. strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
  215. ROOTSERVER_IP))
  216. {
  217. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  218. "IP received and IP for root name server differ\n");
  219. GNUNET_break (0);
  220. }
  221. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  222. "System's own forward name resolution is working\n");
  223. /* Resolve the same using GNUNET */
  224. GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
  225. &check_rootserver_ip, cls);
  226. GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
  227. &check_rootserver_ip, cls);
  228. /*
  229. * Success: forward lookups work as expected
  230. * Next step: reverse lookups
  231. */
  232. if (1 != inet_pton (AF_INET,
  233. ROOTSERVER_IP,
  234. &rootserver_addr))
  235. {
  236. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  237. "Could not transform root name server IP address\n");
  238. GNUNET_break (0);
  239. }
  240. rootserver =
  241. gethostbyaddr ((const void *) &rootserver_addr,
  242. sizeof(rootserver_addr),
  243. AF_INET);
  244. if (NULL == rootserver)
  245. {
  246. /* Error: resolving IP addresses does not work */
  247. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  248. "gethostbyaddr() could not lookup hostname: %s\n",
  249. hstrerror (h_errno));
  250. disable_rootserver_check = GNUNET_YES;
  251. }
  252. else
  253. {
  254. if (0 != strcmp (rootserver->h_name,
  255. ROOTSERVER_NAME))
  256. {
  257. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  258. "Received hostname and hostname for root name server differ\n");
  259. disable_rootserver_check = GNUNET_YES;
  260. }
  261. }
  262. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  263. "System's own reverse name resolution is working\n");
  264. /* Resolve the same using GNUNET */
  265. memset (&sa, 0, sizeof(sa));
  266. sa.sin_family = AF_INET;
  267. #if HAVE_SOCKADDR_IN_SIN_LEN
  268. sa.sin_len = (u_char) sizeof(sa);
  269. #endif
  270. inet_aton (ROOTSERVER_IP, &sa.sin_addr);
  271. GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
  272. sizeof(struct sockaddr), GNUNET_YES, timeout,
  273. &check_rootserver_name, cls);
  274. memset (&sa, 0, sizeof(sa));
  275. sa.sin_family = AF_INET;
  276. #if HAVE_SOCKADDR_IN_SIN_LEN
  277. sa.sin_len = (u_char) sizeof(sa);
  278. #endif
  279. sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  280. GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
  281. GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
  282. sizeof(struct sockaddr), GNUNET_YES, timeout,
  283. &check_localhost, cls);
  284. GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
  285. sizeof(struct sockaddr), GNUNET_NO, timeout,
  286. &check_localhost_num, cls);
  287. GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
  288. }
  289. int
  290. main (int argc, char *argv[])
  291. {
  292. int ok = 1 + 2 + 4 + 8;
  293. char *fn;
  294. struct GNUNET_OS_Process *proc;
  295. char *const argvx[] = {
  296. "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
  297. };
  298. struct GNUNET_GETOPT_CommandLineOption options[] =
  299. { GNUNET_GETOPT_OPTION_END };
  300. GNUNET_log_setup ("test-resolver-api",
  301. "WARNING",
  302. NULL);
  303. fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
  304. proc = GNUNET_OS_start_process (GNUNET_YES,
  305. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  306. NULL, NULL, NULL,
  307. fn,
  308. "gnunet-service-resolver",
  309. "-c", "test_resolver_api_data.conf", NULL);
  310. GNUNET_assert (NULL != proc);
  311. GNUNET_free (fn);
  312. GNUNET_assert (GNUNET_OK ==
  313. GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1,
  314. argvx, "test-resolver-api", "nohelp",
  315. options, &run, &ok));
  316. if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
  317. {
  318. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  319. ok = 1;
  320. }
  321. GNUNET_OS_process_wait (proc);
  322. GNUNET_OS_process_destroy (proc);
  323. proc = NULL;
  324. if (0 != ok)
  325. fprintf (stderr, "Missed some resolutions: %u\n", ok);
  326. return ok;
  327. }
  328. /* end of test_resolver_api.c */