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 <ctime>
17 #include <thread>
18 #include <vector>
19
20 #include <gtest/gtest.h>
21
22 #ifdef GTEST_API_
23 #define private public
24 #define protected public
25 #endif
26
27 #include "net_manager_center.h"
28 #include "net_stats_callback_test.h"
29 #include "net_stats_constants.h"
30 #include "net_stats_service.h"
31
32 namespace OHOS {
33 namespace NetManagerStandard {
34 namespace {
35 #define DTEST_LOG std::cout << __func__ << ":" << __LINE__ << ":"
36 } // namespace
37
38 using namespace testing::ext;
39 class NetStatsServiceExceptionTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp();
44 void TearDown();
45 static inline std::shared_ptr<NetStatsService> instance_ = nullptr;
46 };
47
SetUpTestCase()48 void NetStatsServiceExceptionTest::SetUpTestCase()
49 {
50 instance_ = std::make_shared<NetStatsService>();
51 }
52
TearDownTestCase()53 void NetStatsServiceExceptionTest::TearDownTestCase()
54 {
55 instance_ = nullptr;
56 }
57
SetUp()58 void NetStatsServiceExceptionTest::SetUp() {}
59
TearDown()60 void NetStatsServiceExceptionTest::TearDown() {}
61
62 HWTEST_F(NetStatsServiceExceptionTest, DumpTest001, TestSize.Level1)
63 {
64 int32_t testFd = 1001;
65 std::vector<std::u16string> mockArgs;
66
67 mockArgs.push_back(std::u16string(u"mockArg1"));
68 mockArgs.push_back(std::u16string(u"mockArg2"));
69 mockArgs.push_back(std::u16string(u"mockArg3"));
70 mockArgs.push_back(std::u16string(u"mockArg4"));
71 auto result = instance_->Dump(testFd, mockArgs);
72 DTEST_LOG << "Dump result: " << result << std::endl;
73 EXPECT_GE(result, -1);
74 }
75
76 HWTEST_F(NetStatsServiceExceptionTest, GetDumpMessageTest001, TestSize.Level1)
77 {
78 std::string mockMessage;
79 instance_->GetDumpMessage(mockMessage);
80 EXPECT_FALSE(mockMessage.empty());
81 }
82
83 HWTEST_F(NetStatsServiceExceptionTest, InitTest001, TestSize.Level1)
84 {
85 auto result = instance_->Init();
86 EXPECT_FALSE(result);
87 }
88 } // namespace NetManagerStandard
89 } // namespace OHOS
90