1 /*
2 * Copyright (c) 2022-2024 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 #ifdef GTEST_API_
19 #define private public
20 #endif
21
22 #include "data_receiver.h"
23 #include "netlink_define.h"
24
25 namespace OHOS {
26 namespace nmd {
27 namespace {
28 constexpr int32_t TEST_SOCKET = 112;
29 constexpr int32_t TEST_FORMAT = NetlinkDefine::NETLINK_FORMAT_BINARY_UNICAST;
30 using namespace testing::ext;
31 } // namespace
32
33 class DataReceiverTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 static inline std::shared_ptr<DataReceiver> instance_ = std::make_shared<DataReceiver>(TEST_SOCKET, TEST_FORMAT);
40 };
41
SetUpTestCase()42 void DataReceiverTest::SetUpTestCase() {}
43
TearDownTestCase()44 void DataReceiverTest::TearDownTestCase() {}
45
SetUp()46 void DataReceiverTest::SetUp() {}
47
TearDown()48 void DataReceiverTest::TearDown() {}
49
50 HWTEST_F(DataReceiverTest, SocketErrorTest001, TestSize.Level1)
51 {
52 int32_t testSocket = -1;
53 std::unique_ptr<DataReceiver> receiver = std::make_unique<DataReceiver>(testSocket, TEST_FORMAT);
54 ASSERT_NE(receiver, nullptr);
55 }
56
57 HWTEST_F(DataReceiverTest, FormatErrorTest002, TestSize.Level1)
58 {
59 int32_t testFormat = 6;
60 std::unique_ptr<DataReceiver> receiver = std::make_unique<DataReceiver>(TEST_SOCKET, testFormat);
61 ASSERT_NE(receiver, nullptr);
62 }
63
64 HWTEST_F(DataReceiverTest, StartTest001, TestSize.Level1)
65 {
__anon8da7be9f0202(std::shared_ptr<NetsysEventMessage> msg) 66 DataReceiver::EventCallback callback = [](std::shared_ptr<NetsysEventMessage> msg) { (void)msg; };
67 instance_->RegisterCallback(callback);
68
69 int32_t ret = instance_->Start();
70 EXPECT_EQ(ret, NetlinkResult::OK);
71 }
72
73 HWTEST_F(DataReceiverTest, StopTest001, TestSize.Level1)
74 {
75 int32_t ret = instance_->Stop();
76 EXPECT_EQ(ret, NetlinkResult::OK);
77 }
78
79 HWTEST_F(DataReceiverTest, DataReceiverBranchTest001, TestSize.Level1)
80 {
81 int32_t socket = 0;
82 instance_->StartReceive(socket);
83 uid_t uid = 0;
84 ssize_t count = instance_->ReceiveMessage(true, uid);
85 EXPECT_LE(count, 0);
86 }
87 } // namespace nmd
88 } // namespace OHOS