• 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 "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 class MDnsProtocolTest : public testing::Test {
58 public:
59     static void SetUpTestCase();
60     static void TearDownTestCase();
61     void SetUp() override;
62     void TearDown() override;
63 };
64 
SetUpTestCase()65 void MDnsProtocolTest::SetUpTestCase() {}
66 
TearDownTestCase()67 void MDnsProtocolTest::TearDownTestCase() {}
68 
SetUp()69 void MDnsProtocolTest::SetUp() {}
70 
TearDown()71 void MDnsProtocolTest::TearDown() {}
72 
73 /**
74  * @tc.name: PacketParserTest001
75  * @tc.desc: Test MDnsPayloadParser
76  * @tc.type: FUNC
77  */
78 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest001, TestSize.Level1)
79 {
80     MDnsPayloadParser parser;
81     MDnsPayload payload(std::begin(SERVICE_QUERY), std::end(SERVICE_QUERY));
82     auto msg = parser.FromBytes(payload);
83     EXPECT_EQ(parser.ToBytes(msg), payload);
84 }
85 
86 /**
87  * @tc.name: PacketParserTest002
88  * @tc.desc: Test MDnsPayloadParser
89  * @tc.type: FUNC
90  */
91 HWTEST_F(MDnsProtocolTest, MDnsPayloadParserTest002, TestSize.Level1)
92 {
93     MDnsPayloadParser parser;
94     MDnsPayload payload(std::begin(RESPONSE), std::end(RESPONSE) - 1);
95     auto msg = parser.FromBytes(payload);
96     EXPECT_EQ(parser.ToBytes(msg), payload);
97 }
98 
99 } // namespace NetManagerStandard
100 } // namespace OHOS