dns-proto-test.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "ares-test.h"
  2. #include "dns-proto.h"
  3. #include <vector>
  4. namespace ares {
  5. namespace test {
  6. TEST(DNSProto, EncodeQuestions) {
  7. DNSPacket pkt;
  8. pkt.set_qid(0x1234).set_response().set_aa()
  9. .add_question(new DNSQuestion("example.com.", T_A))
  10. .add_question(new DNSQuestion("www.example.com", T_AAAA, C_CHAOS));
  11. std::vector<byte> data = {
  12. 0x12, 0x34, // qid
  13. 0x84, // response + query + AA + not-TC + not-RD
  14. 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
  15. 0x00, 0x02, // num questions
  16. 0x00, 0x00, // num answer RRs
  17. 0x00, 0x00, // num authority RRs
  18. 0x00, 0x00, // num additional RRs
  19. // Question 1
  20. 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  21. 0x03, 'c', 'o', 'm',
  22. 0x00,
  23. 0x00, 0x01, // type A
  24. 0x00, 0x01, // class IN
  25. // Question 2
  26. 0x03, 'w', 'w', 'w',
  27. 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  28. 0x03, 'c', 'o', 'm',
  29. 0x00,
  30. 0x00, 0x1C, // type AAAA = 28
  31. 0x00, 0x03, // class CHAOS = 3
  32. };
  33. EXPECT_EQ(data, pkt.data());
  34. }
  35. TEST(DNSProto, EncodeSingleNameAnswers) {
  36. DNSPacket pkt;
  37. pkt.qid_ = 0x1234;
  38. pkt.response_ = true;
  39. pkt.aa_ = true;
  40. pkt.opcode_ = O_QUERY;
  41. pkt.add_answer(new DNSCnameRR("example.com", 0x01020304, "other.com."));
  42. pkt.add_auth(new DNSPtrRR("www.example.com", 0x01020304, "www.other.com"));
  43. std::vector<byte> data = {
  44. 0x12, 0x34, // qid
  45. 0x84, // response + query + AA + not-TC + not-RD
  46. 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
  47. 0x00, 0x00, // num questions
  48. 0x00, 0x01, // num answer RRs
  49. 0x00, 0x01, // num authority RRs
  50. 0x00, 0x00, // num additional RRs
  51. // Answer 1
  52. 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  53. 0x03, 'c', 'o', 'm',
  54. 0x00,
  55. 0x00, 0x05, // RR type
  56. 0x00, 0x01, // class IN
  57. 0x01, 0x02, 0x03, 0x04, // TTL
  58. 0x00, 0x0B, // rdata length
  59. 0x05, 'o', 't', 'h', 'e', 'r',
  60. 0x03, 'c', 'o', 'm',
  61. 0x00,
  62. // Authority 1
  63. 0x03, 'w', 'w', 'w',
  64. 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  65. 0x03, 'c', 'o', 'm',
  66. 0x00,
  67. 0x00, 0x0c, // RR type
  68. 0x00, 0x01, // class IN
  69. 0x01, 0x02, 0x03, 0x04, // TTL
  70. 0x00, 0x0F, // rdata length
  71. 0x03, 'w', 'w', 'w',
  72. 0x05, 'o', 't', 'h', 'e', 'r',
  73. 0x03, 'c', 'o', 'm',
  74. 0x00,
  75. };
  76. EXPECT_EQ(data, pkt.data());
  77. }
  78. TEST(DNSProto, EncodeAddressAnswers) {
  79. DNSPacket pkt;
  80. pkt.qid_ = 0x1234;
  81. pkt.response_ = true;
  82. pkt.aa_ = true;
  83. pkt.opcode_ = O_QUERY;
  84. std::vector<byte> addrv4 = {0x02, 0x03, 0x04, 0x05};
  85. pkt.add_answer(new DNSARR("example.com", 0x01020304, addrv4));
  86. byte addrv6[16] = {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
  87. 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04};
  88. pkt.add_additional(new DNSAaaaRR("www.example.com", 0x01020304, addrv6, 16));
  89. std::vector<byte> data = {
  90. 0x12, 0x34, // qid
  91. 0x84, // response + query + AA + not-TC + not-RD
  92. 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
  93. 0x00, 0x00, // num questions
  94. 0x00, 0x01, // num answer RRs
  95. 0x00, 0x00, // num authority RRs
  96. 0x00, 0x01, // num additional RRs
  97. // Answer 1
  98. 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  99. 0x03, 'c', 'o', 'm',
  100. 0x00,
  101. 0x00, 0x01, // RR type
  102. 0x00, 0x01, // class IN
  103. 0x01, 0x02, 0x03, 0x04, // TTL
  104. 0x00, 0x04, // rdata length
  105. 0x02, 0x03, 0x04, 0x05,
  106. // Additional 1
  107. 0x03, 'w', 'w', 'w',
  108. 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  109. 0x03, 'c', 'o', 'm',
  110. 0x00,
  111. 0x00, 0x1c, // RR type
  112. 0x00, 0x01, // class IN
  113. 0x01, 0x02, 0x03, 0x04, // TTL
  114. 0x00, 0x10, // rdata length
  115. 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
  116. 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04
  117. };
  118. EXPECT_EQ(data, pkt.data());
  119. }
  120. } // namespace test
  121. } // namespace ares