tst-resolv-binary.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Test handling of binary domain names with res_send.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <resolv.h>
  16. #include <string.h>
  17. #include <support/check.h>
  18. #include <support/resolv_test.h>
  19. static void
  20. response (const struct resolv_response_context *ctx,
  21. struct resolv_response_builder *b,
  22. const char *qname, uint16_t qclass, uint16_t qtype)
  23. {
  24. TEST_COMPARE (qclass, C_IN);
  25. TEST_COMPARE (qtype, T_TXT);
  26. TEST_VERIFY (strlen (qname) <= 255);
  27. struct resolv_response_flags flags = { 0 };
  28. resolv_response_init (b, flags);
  29. resolv_response_add_question (b, qname, qclass, qtype);
  30. resolv_response_section (b, ns_s_an);
  31. resolv_response_open_record (b, qname, qclass, T_TXT, 0x12345678);
  32. unsigned char qnamelen = strlen (qname);
  33. resolv_response_add_data (b, &qnamelen, 1);
  34. resolv_response_add_data (b, qname, qnamelen);
  35. resolv_response_close_record (b);
  36. }
  37. static int
  38. do_test (void)
  39. {
  40. struct resolv_test *aux = resolv_test_start
  41. ((struct resolv_redirect_config)
  42. {
  43. .response_callback = response,
  44. });
  45. for (int b = 0; b <= 255; ++b)
  46. {
  47. unsigned char query[] =
  48. {
  49. b, b, /* Transaction ID. */
  50. 1, 0, /* Query with RD flag. */
  51. 0, 1, /* One question. */
  52. 0, 0, 0, 0, 0, 0, /* The other sections are empty. */
  53. 1, b, 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0,
  54. 0, T_TXT, /* TXT query. */
  55. 0, 1, /* Class IN. */
  56. };
  57. unsigned char response[512];
  58. int ret = res_send (query, sizeof (query), response, sizeof (response));
  59. char expected_name[20];
  60. /* The name is uncompressed in the query, so we can reference it
  61. directly. */
  62. TEST_VERIFY_EXIT (ns_name_ntop (query + 12, expected_name,
  63. sizeof (expected_name)) >= 0);
  64. TEST_COMPARE (ret,
  65. (ssize_t) sizeof (query)
  66. + 2 /* Compression reference. */
  67. + 2 + 2 + 4 + 2 /* Type, class, TTL, RDATA length. */
  68. + 1 /* Pascal-style string length. */
  69. + strlen (expected_name));
  70. /* Mark as answer, with recursion available, and one answer. */
  71. query[2] = 0x81;
  72. query[3] = 0x80;
  73. query[7] = 1;
  74. /* Prefix of the response must match the query. */
  75. TEST_COMPARE (memcmp (response, query, sizeof (query)), 0);
  76. /* The actual answer follows, starting with the compression
  77. reference. */
  78. unsigned char *p = response + sizeof (query);
  79. TEST_COMPARE (*p++, 0xc0);
  80. TEST_COMPARE (*p++, 0x0c);
  81. /* Type and class. */
  82. TEST_COMPARE (*p++, 0);
  83. TEST_COMPARE (*p++, T_TXT);
  84. TEST_COMPARE (*p++, 0);
  85. TEST_COMPARE (*p++, C_IN);
  86. /* TTL. */
  87. TEST_COMPARE (*p++, 0x12);
  88. TEST_COMPARE (*p++, 0x34);
  89. TEST_COMPARE (*p++, 0x56);
  90. TEST_COMPARE (*p++, 0x78);
  91. /* RDATA length. */
  92. TEST_COMPARE (*p++, 0);
  93. TEST_COMPARE (*p++, 1 + strlen (expected_name));
  94. /* RDATA. */
  95. TEST_COMPARE (*p++, strlen (expected_name));
  96. TEST_COMPARE (memcmp (p, expected_name, strlen (expected_name)), 0);
  97. }
  98. resolv_test_end (aux);
  99. return 0;
  100. }
  101. #include <support/test-driver.c>