ares-test-parse-aaaa.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, ParseAaaaReplyOK) {
  8. DNSPacket pkt;
  9. pkt.set_qid(0x1234).set_response().set_aa()
  10. .add_question(new DNSQuestion("example.com", T_AAAA))
  11. .add_answer(new DNSAaaaRR("example.com", 100,
  12. {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
  13. 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}))
  14. .add_answer(new DNSARR("example.com", 0x01020304, {2,3,4,5}));
  15. std::vector<byte> data = pkt.data();
  16. struct hostent *host = nullptr;
  17. struct ares_addr6ttl info[5];
  18. int count = 5;
  19. EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
  20. &host, info, &count));
  21. EXPECT_EQ(1, count);
  22. EXPECT_EQ(100, info[0].ttl);
  23. EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
  24. EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
  25. ASSERT_NE(nullptr, host);
  26. std::stringstream ss;
  27. ss << HostEnt(host);
  28. EXPECT_EQ("{'example.com' aliases=[] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str());
  29. ares_free_hostent(host);
  30. // Repeat without providing places to put the results
  31. count = 0;
  32. EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
  33. nullptr, info, &count));
  34. }
  35. TEST_F(LibraryTest, ParseAaaaReplyCname) {
  36. DNSPacket pkt;
  37. pkt.set_qid(0x1234).set_response().set_aa()
  38. .add_question(new DNSQuestion("example.com", T_AAAA))
  39. .add_answer(new DNSCnameRR("example.com", 50, "c.example.com"))
  40. .add_answer(new DNSAaaaRR("c.example.com", 100,
  41. {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
  42. 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
  43. std::vector<byte> data = pkt.data();
  44. struct hostent *host = nullptr;
  45. struct ares_addr6ttl info[5];
  46. int count = 5;
  47. EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
  48. &host, info, &count));
  49. EXPECT_EQ(1, count);
  50. // CNAME TTL overrides AAAA TTL.
  51. EXPECT_EQ(50, info[0].ttl);
  52. EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
  53. EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
  54. ASSERT_NE(nullptr, host);
  55. std::stringstream ss;
  56. ss << HostEnt(host);
  57. EXPECT_EQ("{'c.example.com' aliases=[example.com] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str());
  58. ares_free_hostent(host);
  59. // Repeat without providing a hostent
  60. count = 5;
  61. EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
  62. nullptr, info, &count));
  63. EXPECT_EQ(1, count);
  64. EXPECT_EQ(50, info[0].ttl);
  65. EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
  66. EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
  67. }
  68. TEST_F(LibraryTest, ParseAaaaReplyNoData) {
  69. DNSPacket pkt;
  70. pkt.set_qid(0x1234).set_response().set_aa()
  71. .add_question(new DNSQuestion("example.com", T_AAAA));
  72. std::vector<byte> data = pkt.data();
  73. struct hostent *host = nullptr;
  74. struct ares_addr6ttl info[2];
  75. int count = 2;
  76. EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
  77. &host, info, &count));
  78. EXPECT_EQ(0, count);
  79. EXPECT_EQ(nullptr, host);
  80. // Again but with a CNAME.
  81. pkt.add_answer(new DNSCnameRR("example.com", 200, "c.example.com"));
  82. EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
  83. &host, info, &count));
  84. EXPECT_EQ(0, count);
  85. EXPECT_EQ(nullptr, host);
  86. }
  87. TEST_F(LibraryTest, ParseAaaaReplyErrors) {
  88. DNSPacket pkt;
  89. pkt.set_qid(0x1234).set_response().set_aa()
  90. .add_question(new DNSQuestion("example.com", T_AAAA))
  91. .add_answer(new DNSAaaaRR("example.com", 100,
  92. {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
  93. 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
  94. std::vector<byte> data;
  95. struct hostent *host = nullptr;
  96. struct ares_addr6ttl info[2];
  97. int count = 2;
  98. // No question.
  99. pkt.questions_.clear();
  100. data = pkt.data();
  101. EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), data.size(),
  102. &host, info, &count));
  103. EXPECT_EQ(nullptr, host);
  104. pkt.add_question(new DNSQuestion("example.com", T_AAAA));
  105. // Question != answer
  106. pkt.questions_.clear();
  107. pkt.add_question(new DNSQuestion("Axample.com", T_AAAA));
  108. data = pkt.data();
  109. EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
  110. &host, info, &count));
  111. EXPECT_EQ(nullptr, host);
  112. pkt.questions_.clear();
  113. pkt.add_question(new DNSQuestion("example.com", T_AAAA));
  114. // Two questions.
  115. pkt.add_question(new DNSQuestion("example.com", T_AAAA));
  116. data = pkt.data();
  117. EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), data.size(),
  118. &host, info, &count));
  119. EXPECT_EQ(nullptr, host);
  120. pkt.questions_.clear();
  121. pkt.add_question(new DNSQuestion("example.com", T_AAAA));
  122. // Wrong sort of answer.
  123. pkt.answers_.clear();
  124. pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
  125. data = pkt.data();
  126. EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
  127. &host, info, &count));
  128. EXPECT_EQ(nullptr, host);
  129. pkt.answers_.clear();
  130. pkt.add_answer(new DNSAaaaRR("example.com", 100,
  131. {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
  132. 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
  133. // No answer.
  134. pkt.answers_.clear();
  135. data = pkt.data();
  136. EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
  137. &host, info, &count));
  138. EXPECT_EQ(nullptr, host);
  139. pkt.add_answer(new DNSAaaaRR("example.com", 100,
  140. {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
  141. 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
  142. // Truncated packets.
  143. data = pkt.data();
  144. for (size_t len = 1; len < data.size(); len++) {
  145. EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), len,
  146. &host, info, &count));
  147. EXPECT_EQ(nullptr, host);
  148. EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), len,
  149. nullptr, info, &count));
  150. }
  151. }
  152. TEST_F(LibraryTest, ParseAaaaReplyAllocFail) {
  153. DNSPacket pkt;
  154. pkt.set_qid(0x1234).set_response().set_aa()
  155. .add_question(new DNSQuestion("example.com", T_AAAA))
  156. .add_answer(new DNSCnameRR("example.com", 300, "c.example.com"))
  157. .add_answer(new DNSAaaaRR("c.example.com", 100,
  158. {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
  159. 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
  160. std::vector<byte> data = pkt.data();
  161. struct hostent *host = nullptr;
  162. struct ares_addr6ttl info[2];
  163. int count = 2;
  164. for (int ii = 1; ii <= 8; ii++) {
  165. ClearFails();
  166. SetAllocFail(ii);
  167. EXPECT_EQ(ARES_ENOMEM, ares_parse_aaaa_reply(data.data(), data.size(),
  168. &host, info, &count)) << ii;
  169. EXPECT_EQ(nullptr, host);
  170. }
  171. }
  172. } // namespace test
  173. } // namespace ares