1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 
18 #include "netmgr_ext_log_wrapper.h"
19 #include "refbase.h"
20 
21 #include "mdns_client.h"
22 #include "mdns_common.h"
23 #include "mdns_event_stub.h"
24 #include "mdns_packet_parser.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 using namespace testing::ext;
29 
30 static constexpr uint8_t SERVICE_QUERY[] = {
31     // Query ID
32     0x00, 0x00,
33     // Flags
34     0x00, 0x00,
35     // QDCOUNT
36     0x00, 0x01,
37     // ANCOUNT, NSCOUNT, ARCOUNT
38     0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39     // _services._dns-sd._udp.local.
40     0x09, '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', 0x07, '_', 'd', 'n', 's', '-', 's', 'd', 0x04, '_', 'u', 'd',
41     'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00,
42     // PTR QTYPE
43     0x00, static_cast<uint8_t>(DNSProto::RRTYPE_PTR),
44     // QU and class IN
45     0x80, static_cast<uint8_t>(DNSProto::RRCLASS_IN)};
46 
47 static constexpr uint8_t RESPONSE[] =
48     "\x00\x00\x84\x00\x00\x00\x00\x05\x00\x00\x00\x00\x04\x5f\x73\x6d"
49     "\x62\x04\x5f\x74\x63\x70\x05\x6c\x6f\x63\x61\x6c\x00\x00\x0c\x00"
50     "\x01\x00\x00\x11\x94\x00\x06\x03\x4d\x4f\x45\xc0\x0c\xc0\x27\x00"
51     "\x10\x80\x01\x00\x00\x11\x94\x00\x01\x00\xc0\x27\x00\x21\x80\x01"
52     "\x00\x00\x00\x78\x00\x0c\x00\x00\x00\x00\x01\xbd\x03\x6d\x6f\x65"
53     "\xc0\x16\xc0\x4c\x00\x1c\x80\x01\x00\x00\x00\x78\x00\x10\xfe\x80"
54     "\x00\x00\x00\x00\x00\x00\x5d\xde\x75\x10\x90\x99\x2a\xf9\xc0\x4c"
55     "\x00\x01\x80\x01\x00\x00\x00\x78\x00\x04\x0a\x11\x02\xd4";
56 
57 static constexpr uint8_t ATTACK_RESPONSE1[] = "\x0a\x0b\x1b\x20\x20\x20\x20\x0b\x0b\x0b\x0b\x0b\x0b";
58 static constexpr uint8_t ATTACK_RESPONSE2[] = "\x0a\x20\x20\x20\x20\x20\x6f\x20\x20\x01\x20\x20\xfb";
59 static constexpr uint8_t ATTACK_RESPONSE3[] = "\x00\x01\x00\x01\x00\x00\x00\x01\x00\x01\x00\x01\xC0\x0C";
60 static constexpr uint8_t ATTACK_RESPONSE4[] =
61     "\x01\x00\x2b\x00\x00\x00\x00\x00\x01\x10\x00\x00\x00\x00\x0C\xff\xf6"
62     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c\x00\xd4\x03\x00\x00\x2b\xfd\x02"
63     "\x00\x00\x00\xfd\xfd";
64 
65 class MDnsProtocolTest : public testing::Test {
66 public:
67     static void SetUpTestCase();
68     static void TearDownTestCase();
69     void SetUp() override;
70     void TearDown() override;
71 };
72 
SetUpTestCase()73 void MDnsProtocolTest::SetUpTestCase() {}
74 
TearDownTestCase()75 void MDnsProtocolTest::TearDownTestCase() {}
76 
SetUp()77 void MDnsProtocolTest::SetUp() {}
78 
TearDown()79 void MDnsProtocolTest::TearDown() {}
80 
81 /**
82  * @tc.name: PacketParserTest001
83  * @tc.desc: Test MDnsPayloadParser
84  * @tc.type: FUNC
85  */
86 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest001, TestSize.Level1)
87 {
88     MDnsPayloadParser parser;
89     MDnsPayload payload(std::begin(SERVICE_QUERY), std::end(SERVICE_QUERY));
90     auto msg = parser.FromBytes(payload);
91     EXPECT_EQ(parser.ToBytes(msg), payload);
92 }
93 
94 /**
95  * @tc.name: PacketParserTest002
96  * @tc.desc: Test MDnsPayloadParser
97  * @tc.type: FUNC
98  */
99 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest002, TestSize.Level1)
100 {
101     MDnsPayloadParser parser;
102     MDnsPayload payload(std::begin(RESPONSE), std::end(RESPONSE) - 1);
103     auto msg = parser.FromBytes(payload);
104     EXPECT_EQ(parser.ToBytes(msg), payload);
105 }
106 
107 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest003, TestSize.Level1)
108 {
109     MDnsPayloadParser parser;
110     MDnsPayload payload(std::begin(ATTACK_RESPONSE1), std::end(ATTACK_RESPONSE1) - 1);
111     auto msg = parser.FromBytes(payload);
112     EXPECT_NE(parser.GetError(), ERR_OK);
113 }
114 
115 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest004, TestSize.Level1)
116 {
117     MDnsPayloadParser parser;
118     MDnsPayload payload(std::begin(ATTACK_RESPONSE2), std::end(ATTACK_RESPONSE2) - 1);
119     auto msg = parser.FromBytes(payload);
120     EXPECT_NE(parser.GetError(), ERR_OK);
121 }
122 
123 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest005, TestSize.Level1)
124 {
125     MDnsPayloadParser parser;
126     MDnsPayload payload(std::begin(ATTACK_RESPONSE3), std::end(ATTACK_RESPONSE3) - 1);
127     auto msg = parser.FromBytes(payload);
128     EXPECT_NE(parser.GetError(), ERR_OK);
129 }
130 
131 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest006, TestSize.Level1)
132 {
133     MDnsPayloadParser parser;
134     MDnsPayload payload(std::begin(ATTACK_RESPONSE4), std::end(ATTACK_RESPONSE4) - 1);
135     auto msg = parser.FromBytes(payload);
136     EXPECT_NE(parser.GetError(), ERR_OK);
137 }
138 } // namespace NetManagerStandard
139 } // namespace OHOS