ares-test-parse-naptr.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "ares-test.h"
  2. #include "dns-proto.h"
  3. #include <sstream>
  4. #include <vector>
  5. namespace ares {
  6. namespace test {
  7. TEST_F(LibraryTest, ParseNaptrReplyOK) {
  8. DNSPacket pkt;
  9. pkt.set_qid(0x1234).set_response().set_aa()
  10. .add_question(new DNSQuestion("example.com", T_NAPTR))
  11. .add_answer(new DNSNaptrRR("example.com", 100,
  12. 10, 20, "SP", "service", "regexp", "replace"))
  13. .add_answer(new DNSNaptrRR("example.com", 0x0010,
  14. 11, 21, "SP", "service2", "regexp2", "replace2"));
  15. std::vector<byte> data = pkt.data();
  16. struct ares_naptr_reply* naptr = nullptr;
  17. EXPECT_EQ(ARES_SUCCESS, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
  18. ASSERT_NE(nullptr, naptr);
  19. EXPECT_EQ("SP", std::string((char*)naptr->flags));
  20. EXPECT_EQ("service", std::string((char*)naptr->service));
  21. EXPECT_EQ("regexp", std::string((char*)naptr->regexp));
  22. EXPECT_EQ("replace", std::string((char*)naptr->replacement));
  23. EXPECT_EQ(10, naptr->order);
  24. EXPECT_EQ(20, naptr->preference);
  25. struct ares_naptr_reply* naptr2 = naptr->next;
  26. ASSERT_NE(nullptr, naptr2);
  27. EXPECT_EQ("SP", std::string((char*)naptr2->flags));
  28. EXPECT_EQ("service2", std::string((char*)naptr2->service));
  29. EXPECT_EQ("regexp2", std::string((char*)naptr2->regexp));
  30. EXPECT_EQ("replace2", std::string((char*)naptr2->replacement));
  31. EXPECT_EQ(11, naptr2->order);
  32. EXPECT_EQ(21, naptr2->preference);
  33. EXPECT_EQ(nullptr, naptr2->next);
  34. ares_free_data(naptr);
  35. }
  36. TEST_F(LibraryTest, ParseNaptrReplyErrors) {
  37. DNSPacket pkt;
  38. pkt.set_qid(0x1234).set_response().set_aa()
  39. .add_question(new DNSQuestion("example.com", T_NAPTR))
  40. .add_answer(new DNSNaptrRR("example.com", 100,
  41. 10, 20, "SP", "service", "regexp", "replace"));
  42. std::vector<byte> data;
  43. struct ares_naptr_reply* naptr = nullptr;
  44. // No question.
  45. pkt.questions_.clear();
  46. data = pkt.data();
  47. EXPECT_EQ(ARES_EBADRESP, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
  48. pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
  49. #ifdef DISABLED
  50. // Question != answer
  51. pkt.questions_.clear();
  52. pkt.add_question(new DNSQuestion("Axample.com", T_NAPTR));
  53. data = pkt.data();
  54. EXPECT_EQ(ARES_ENODATA, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
  55. pkt.questions_.clear();
  56. pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
  57. #endif
  58. // Two questions
  59. pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
  60. data = pkt.data();
  61. EXPECT_EQ(ARES_EBADRESP, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
  62. pkt.questions_.clear();
  63. pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
  64. // Wrong sort of answer.
  65. pkt.answers_.clear();
  66. pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
  67. data = pkt.data();
  68. EXPECT_EQ(ARES_SUCCESS, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
  69. EXPECT_EQ(nullptr, naptr);
  70. pkt.answers_.clear();
  71. pkt.add_answer(new DNSNaptrRR("example.com", 100,
  72. 10, 20, "SP", "service", "regexp", "replace"));
  73. // No answer.
  74. pkt.answers_.clear();
  75. data = pkt.data();
  76. EXPECT_EQ(ARES_ENODATA, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
  77. pkt.add_answer(new DNSNaptrRR("example.com", 100,
  78. 10, 20, "SP", "service", "regexp", "replace"));
  79. // Truncated packets.
  80. data = pkt.data();
  81. for (size_t len = 1; len < data.size(); len++) {
  82. int rc = ares_parse_naptr_reply(data.data(), len, &naptr);
  83. EXPECT_TRUE(rc == ARES_EBADRESP || rc == ARES_EBADNAME);
  84. }
  85. }
  86. TEST_F(LibraryTest, ParseNaptrReplyTooShort) {
  87. std::vector<byte> data = {
  88. 0x12, 0x34, // qid
  89. 0x84, // response + query + AA + not-TC + not-RD
  90. 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
  91. 0x00, 0x01, // num questions
  92. 0x00, 0x01, // num answer RRs
  93. 0x00, 0x00, // num authority RRs
  94. 0x00, 0x00, // num additional RRs
  95. // Question
  96. 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  97. 0x03, 'c', 'o', 'm',
  98. 0x00,
  99. 0x00, 0x23, // type NAPTR
  100. 0x00, 0x01, // class IN
  101. // Answer 1
  102. 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  103. 0x03, 'c', 'o', 'm',
  104. 0x00,
  105. 0x00, 0x23, // RR type
  106. 0x00, 0x01, // class IN
  107. 0x01, 0x02, 0x03, 0x04, // TTL
  108. 0x00, 0x01, // rdata length
  109. 0x00, // Too short: expect 2 x int16 and 3 x name (min 1 byte each)
  110. };
  111. struct ares_naptr_reply* naptr = nullptr;
  112. EXPECT_EQ(ARES_EBADRESP, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
  113. }
  114. TEST_F(LibraryTest, ParseNaptrReplyAllocFail) {
  115. DNSPacket pkt;
  116. pkt.set_qid(0x1234).set_response().set_aa()
  117. .add_question(new DNSQuestion("example.com", T_NAPTR))
  118. .add_answer(new DNSNaptrRR("example.com", 100,
  119. 10, 20, "SP", "service", "regexp", "replace"))
  120. .add_answer(new DNSNaptrRR("example.com", 0x0010,
  121. 11, 21, "SP", "service2", "regexp2", "replace2"));
  122. std::vector<byte> data = pkt.data();
  123. struct ares_naptr_reply* naptr = nullptr;
  124. for (int ii = 1; ii <= 13; ii++) {
  125. ClearFails();
  126. SetAllocFail(ii);
  127. EXPECT_EQ(ARES_ENOMEM, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
  128. }
  129. }
  130. } // namespace test
  131. } // namespace ares