• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "discovery_callback_stub.h"
24 #include "registration_callback_stub.h"
25 #include "resolve_callback_stub.h"
26 #include "mdns_packet_parser.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 using namespace testing::ext;
31 
32 static constexpr uint8_t SERVICE_QUERY[] = {
33     // Query ID
34     0x00, 0x00,
35     // Flags
36     0x00, 0x00,
37     // QDCOUNT
38     0x00, 0x01,
39     // ANCOUNT, NSCOUNT, ARCOUNT
40     0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41     // _services._dns-sd._udp.local.
42     0x09, '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', 0x07, '_', 'd', 'n', 's', '-', 's', 'd', 0x04, '_', 'u', 'd',
43     'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00,
44     // PTR QTYPE
45     0x00, static_cast<uint8_t>(DNSProto::RRTYPE_PTR),
46     // QU and class IN
47     0x80, static_cast<uint8_t>(DNSProto::RRCLASS_IN)};
48 
49 static constexpr uint8_t RESPONSE[] =
50     "\x00\x00\x84\x00\x00\x00\x00\x05\x00\x00\x00\x00\x04\x5f\x73\x6d"
51     "\x62\x04\x5f\x74\x63\x70\x05\x6c\x6f\x63\x61\x6c\x00\x00\x0c\x00"
52     "\x01\x00\x00\x11\x94\x00\x06\x03\x4d\x4f\x45\xc0\x0c\xc0\x27\x00"
53     "\x10\x80\x01\x00\x00\x11\x94\x00\x01\x00\xc0\x27\x00\x21\x80\x01"
54     "\x00\x00\x00\x78\x00\x0c\x00\x00\x00\x00\x01\xbd\x03\x6d\x6f\x65"
55     "\xc0\x16\xc0\x4c\x00\x1c\x80\x01\x00\x00\x00\x78\x00\x10\xfe\x80"
56     "\x00\x00\x00\x00\x00\x00\x5d\xde\x75\x10\x90\x99\x2a\xf9\xc0\x4c"
57     "\x00\x01\x80\x01\x00\x00\x00\x78\x00\x04\x0a\x11\x02\xd4";
58 
59 static constexpr uint8_t ATTACK_RESPONSE1[] = "\x0a\x0b\x1b\x20\x20\x20\x20\x0b\x0b\x0b\x0b\x0b\x0b";
60 static constexpr uint8_t ATTACK_RESPONSE2[] = "\x0a\x20\x20\x20\x20\x20\x6f\x20\x20\x01\x20\x20\xfb";
61 static constexpr uint8_t ATTACK_RESPONSE3[] = "\x00\x01\x00\x01\x00\x00\x00\x01\x00\x01\x00\x01\xC0\x0C";
62 static constexpr uint8_t ATTACK_RESPONSE4[] =
63     "\x01\x00\x2b\x00\x00\x00\x00\x00\x01\x10\x00\x00\x00\x00\x0C\xff\xf6"
64     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c\x00\xd4\x03\x00\x00\x2b\xfd\x02"
65     "\x00\x00\x00\xfd\xfd";
66 
67 class MDnsProtocolTest : public testing::Test {
68 public:
69     static void SetUpTestCase();
70     static void TearDownTestCase();
71     void SetUp() override;
72     void TearDown() override;
73 };
74 
SetUpTestCase()75 void MDnsProtocolTest::SetUpTestCase() {}
76 
TearDownTestCase()77 void MDnsProtocolTest::TearDownTestCase() {}
78 
SetUp()79 void MDnsProtocolTest::SetUp() {}
80 
TearDown()81 void MDnsProtocolTest::TearDown() {}
82 
83 /**
84  * @tc.name: PacketParserTest001
85  * @tc.desc: Test MDnsPayloadParser
86  * @tc.type: FUNC
87  */
88 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest001, TestSize.Level1)
89 {
90     MDnsPayloadParser parser;
91     MDnsPayload payload(std::begin(SERVICE_QUERY), std::end(SERVICE_QUERY));
92     auto msg = parser.FromBytes(payload);
93     EXPECT_EQ(parser.ToBytes(msg), payload);
94 }
95 
96 /**
97  * @tc.name: PacketParserTest002
98  * @tc.desc: Test MDnsPayloadParser
99  * @tc.type: FUNC
100  */
101 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest002, TestSize.Level1)
102 {
103     MDnsPayloadParser parser;
104     MDnsPayload payload(std::begin(RESPONSE), std::end(RESPONSE) - 1);
105     auto msg = parser.FromBytes(payload);
106     EXPECT_EQ(parser.ToBytes(msg), payload);
107 }
108 
109 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest003, TestSize.Level1)
110 {
111     MDnsPayloadParser parser;
112     MDnsPayload payload(std::begin(ATTACK_RESPONSE1), std::end(ATTACK_RESPONSE1) - 1);
113     auto msg = parser.FromBytes(payload);
114     EXPECT_NE(parser.GetError(), ERR_OK);
115 }
116 
117 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest004, TestSize.Level1)
118 {
119     MDnsPayloadParser parser;
120     MDnsPayload payload(std::begin(ATTACK_RESPONSE2), std::end(ATTACK_RESPONSE2) - 1);
121     auto msg = parser.FromBytes(payload);
122     EXPECT_NE(parser.GetError(), ERR_OK);
123 }
124 
125 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest005, TestSize.Level1)
126 {
127     MDnsPayloadParser parser;
128     MDnsPayload payload(std::begin(ATTACK_RESPONSE3), std::end(ATTACK_RESPONSE3) - 1);
129     auto msg = parser.FromBytes(payload);
130     EXPECT_NE(parser.GetError(), ERR_OK);
131 }
132 
133 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest006, TestSize.Level1)
134 {
135     MDnsPayloadParser parser;
136     MDnsPayload payload(std::begin(ATTACK_RESPONSE4), std::end(ATTACK_RESPONSE4) - 1);
137     auto msg = parser.FromBytes(payload);
138     EXPECT_NE(parser.GetError(), ERR_OK);
139 }
140 } // namespace NetManagerStandard
141 } // namespace OHOS