1 /*
2  * Copyright (c) 2022 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 "net_manager_constants.h"
19 #include "net_stats_callback_test.h"
20 #include "net_stats_callback.h"
21 
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 using namespace testing::ext;
26 constexpr const char *TEST_IFACE = "TEST_IFACE";
27 constexpr uint32_t TEST_UID = 4454;
28 } // namespace
29 
30 class NetStatsCallbackIpcTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp();
35     void TearDown();
36     static inline std::shared_ptr<NetStatsCallback> instance_ = std::make_shared<NetStatsCallback>();
37     static inline sptr<INetStatsCallback> callback_ = nullptr;
38 };
39 
SetUpTestCase()40 void NetStatsCallbackIpcTest::SetUpTestCase()
41 {
42     callback_ = new (std::nothrow) NetStatsCallbackTest();
43 }
44 
TearDownTestCase()45 void NetStatsCallbackIpcTest::TearDownTestCase() {}
46 
SetUp()47 void NetStatsCallbackIpcTest::SetUp() {}
48 
TearDown()49 void NetStatsCallbackIpcTest::TearDown() {}
50 
51 HWTEST_F(NetStatsCallbackIpcTest, NotifyNetIfaceStatsChangedTest001, TestSize.Level1)
52 {
53     instance_->RegisterNetStatsCallback(callback_);
54     instance_->RegisterNetStatsCallback(callback_);
55     instance_->RegisterNetStatsCallback(nullptr);
56     instance_->UnregisterNetStatsCallback(callback_);
57     instance_->UnregisterNetStatsCallback(callback_);
58     instance_->UnregisterNetStatsCallback(nullptr);
59     for (int16_t i = 0; i < LIMIT_STATS_CALLBACK_NUM; i++) {
60         sptr<INetStatsCallback> callback = new (std::nothrow) NetStatsCallbackTest();
61         instance_->RegisterNetStatsCallback(callback);
62     }
63     instance_->RegisterNetStatsCallback(callback_);
64 
65     int32_t ret = instance_->NotifyNetIfaceStatsChanged(TEST_IFACE);
66     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
67 }
68 
69 HWTEST_F(NetStatsCallbackIpcTest, NotifyNetUidStatsChangedTest001, TestSize.Level1)
70 {
71     int32_t ret = instance_->NotifyNetUidStatsChanged(TEST_IFACE, TEST_UID);
72     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
73 }
74 } // namespace NetManagerStandard
75 } // namespace OHOS