• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define private public
19 #include "net_policy_file.h"
20 #undef private
21 #include "net_mgr_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace NetManagerStandard {
25 namespace {
26 using namespace testing::ext;
27 } // namespace
28 
29 class UtNetPolicyFile : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     void SetUp();
34     void TearDown();
35 };
36 
SetUpTestCase()37 void UtNetPolicyFile::SetUpTestCase() {}
38 
TearDownTestCase()39 void UtNetPolicyFile::TearDownTestCase() {}
40 
SetUp()41 void UtNetPolicyFile::SetUp() {}
42 
TearDown()43 void UtNetPolicyFile::TearDown() {}
44 
45 /**
46  * @tc.name: NetPolicyFileTest001
47  * @tc.desc: Test NetPolicyFile NetpolicyFile->
48  * @tc.type: FUNC
49  */
50 HWTEST_F(UtNetPolicyFile, NetPolicyFileTest001, TestSize.Level1)
51 {
52     auto policyFile = DelayedSingleton<NetPolicyFile>::GetInstance();
53     std::string fileName;
54     std::string fileContent;
55     EXPECT_FALSE(policyFile->CreateFile(fileName));
56     EXPECT_FALSE(policyFile->ReadFile(fileName, fileContent));
57     std::remove(POLICY_FILE_NAME);
58     EXPECT_TRUE(policyFile->CreateFile(POLICY_FILE_NAME));
59     EXPECT_TRUE(policyFile->ReadFile(POLICY_FILE_NAME, fileContent));
60     policyFile->GetNetPolicies();
61     std::string content;
62     NetPolicy netPolicy;
63     EXPECT_FALSE(policyFile->Json2Obj(content, netPolicy));
64 }
65 
66 /**
67  * @tc.name: WriteFileTest
68  * @tc.desc: Test NetPolicyFile WriteFile.
69  * @tc.type: FUNC
70  */
71 HWTEST_F(UtNetPolicyFile, WriteFileTest, TestSize.Level1)
72 {
73     auto policyFile = DelayedSingleton<NetPolicyFile>::GetInstance();
74     uint32_t netUidPolicyOpType = NetUidPolicyOpType::NET_POLICY_UID_OP_TYPE_UPDATE;
75     uint32_t uid = 100;
76     uint32_t policy = 1;
77     bool ret = policyFile->WriteFile(netUidPolicyOpType, uid, policy);
78     netUidPolicyOpType = NetUidPolicyOpType::NET_POLICY_UID_OP_TYPE_DELETE;
79     ret = policyFile->WriteFile(netUidPolicyOpType, uid, policy);
80     netUidPolicyOpType = NetUidPolicyOpType::NET_POLICY_UID_OP_TYPE_ADD;
81     ret = policyFile->WriteFile(netUidPolicyOpType, uid, policy);
82     policyFile->netPolicy_.netQuotaPolicies.clear();
83     NetQuotaPolicy quotaPolicy;
84     ret = policyFile->UpdateQuotaPolicyExist(quotaPolicy);
85     EXPECT_FALSE(ret);
86 }
87 
88 /**
89  * @tc.name: ResetPoliciesTest
90  * @tc.desc: Test NetPolicyFile ResetPolicies.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(UtNetPolicyFile, ResetPoliciesTest, TestSize.Level1)
94 {
95     auto policyFile = DelayedSingleton<NetPolicyFile>::GetInstance();
96     std::string iccid;
97     int32_t ret = policyFile->ResetPolicies(iccid);
98     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
99 }
100 } // namespace NetManagerStandard
101 } // namespace OHOS