[T106][ZXW-22]7520V3SCV2.01.01.02P42U09_VEC_V0.8_AP_VEC origin source commit

Change-Id: Ic6e05d89ecd62fc34f82b23dcf306c93764aec4b
diff --git a/ap/app/c-ares/src/test/dns-proto-test.cc b/ap/app/c-ares/src/test/dns-proto-test.cc
new file mode 100644
index 0000000..0c36a0c
--- /dev/null
+++ b/ap/app/c-ares/src/test/dns-proto-test.cc
@@ -0,0 +1,131 @@
+#include "ares-test.h"
+#include "dns-proto.h"
+
+#include <vector>
+
+namespace ares {
+namespace test {
+
+TEST(DNSProto, EncodeQuestions) {
+  DNSPacket pkt;
+  pkt.set_qid(0x1234).set_response().set_aa()
+    .add_question(new DNSQuestion("example.com.", ns_t_a))
+    .add_question(new DNSQuestion("www.example.com", ns_t_aaaa, ns_c_chaos));
+
+  std::vector<byte> data = {
+    0x12, 0x34,  // qid
+    0x84, // response + query + AA + not-TC + not-RD
+    0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
+    0x00, 0x02,  // num questions
+    0x00, 0x00,  // num answer RRs
+    0x00, 0x00,  // num authority RRs
+    0x00, 0x00,  // num additional RRs
+    // Question 1
+    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
+    0x03, 'c', 'o', 'm',
+    0x00,
+    0x00, 0x01,  // type A
+    0x00, 0x01,  // class IN
+    // Question 2
+    0x03, 'w', 'w', 'w',
+    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
+    0x03, 'c', 'o', 'm',
+    0x00,
+    0x00, 0x1C,  // type AAAA = 28
+    0x00, 0x03,  // class CHAOS = 3
+  };
+  EXPECT_EQ(data, pkt.data());
+}
+
+TEST(DNSProto, EncodeSingleNameAnswers) {
+  DNSPacket pkt;
+  pkt.qid_ = 0x1234;
+  pkt.response_ = true;
+  pkt.aa_ = true;
+  pkt.opcode_ = ns_o_query;
+  pkt.add_answer(new DNSCnameRR("example.com", 0x01020304, "other.com."));
+  pkt.add_auth(new DNSPtrRR("www.example.com", 0x01020304, "www.other.com"));
+
+  std::vector<byte> data = {
+    0x12, 0x34,  // qid
+    0x84, // response + query + AA + not-TC + not-RD
+    0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
+    0x00, 0x00,  // num questions
+    0x00, 0x01,  // num answer RRs
+    0x00, 0x01,  // num authority RRs
+    0x00, 0x00,  // num additional RRs
+    // Answer 1
+    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
+    0x03, 'c', 'o', 'm',
+    0x00,
+    0x00, 0x05,  // RR type
+    0x00, 0x01,  // class IN
+    0x01, 0x02, 0x03, 0x04, // TTL
+    0x00, 0x0B,  // rdata length
+    0x05, 'o', 't', 'h', 'e', 'r',
+    0x03, 'c', 'o', 'm',
+    0x00,
+    // Authority 1
+    0x03, 'w', 'w', 'w',
+    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
+    0x03, 'c', 'o', 'm',
+    0x00,
+    0x00, 0x0c,  // RR type
+    0x00, 0x01,  // class IN
+    0x01, 0x02, 0x03, 0x04, // TTL
+    0x00, 0x0F,  // rdata length
+    0x03, 'w', 'w', 'w',
+    0x05, 'o', 't', 'h', 'e', 'r',
+    0x03, 'c', 'o', 'm',
+    0x00,
+  };
+  EXPECT_EQ(data, pkt.data());
+}
+
+TEST(DNSProto, EncodeAddressAnswers) {
+  DNSPacket pkt;
+  pkt.qid_ = 0x1234;
+  pkt.response_ = true;
+  pkt.aa_ = true;
+  pkt.opcode_ = ns_o_query;
+  std::vector<byte> addrv4 = {0x02, 0x03, 0x04, 0x05};
+  pkt.add_answer(new DNSARR("example.com", 0x01020304, addrv4));
+  byte addrv6[16] = {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
+                     0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04};
+  pkt.add_additional(new DNSAaaaRR("www.example.com", 0x01020304, addrv6, 16));
+
+  std::vector<byte> data = {
+    0x12, 0x34,  // qid
+    0x84, // response + query + AA + not-TC + not-RD
+    0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
+    0x00, 0x00,  // num questions
+    0x00, 0x01,  // num answer RRs
+    0x00, 0x00,  // num authority RRs
+    0x00, 0x01,  // num additional RRs
+    // Answer 1
+    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
+    0x03, 'c', 'o', 'm',
+    0x00,
+    0x00, 0x01,  // RR type
+    0x00, 0x01,  // class IN
+    0x01, 0x02, 0x03, 0x04, // TTL
+    0x00, 0x04,  // rdata length
+    0x02, 0x03, 0x04, 0x05,
+    // Additional 1
+    0x03, 'w', 'w', 'w',
+    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
+    0x03, 'c', 'o', 'm',
+    0x00,
+    0x00, 0x1c,  // RR type
+    0x00, 0x01,  // class IN
+    0x01, 0x02, 0x03, 0x04, // TTL
+    0x00, 0x10,  // rdata length
+    0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
+    0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04
+  };
+  EXPECT_EQ(data, pkt.data());
+}
+
+
+}  // namespace test
+}  // namespace ares