• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <thread>
17 
18 #include <gtest/gtest.h>
19 
20 #include "accesstoken_kit.h"
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23 
24 #include "net_mgr_log_wrapper.h"
25 #include "net_policy_callback_test.h"
26 #include "net_policy_client.h"
27 #include "net_policy_constants.h"
28 #include "net_policy_inner_define.h"
29 
30 namespace OHOS {
31 namespace NetManagerStandard {
32 namespace {
33 using namespace Security::AccessToken;
34 using Security::AccessToken::AccessTokenID;
35 HapInfoParams testInfoParms1 = {.userID = 1,
36                                 .bundleName = "net_policy_manager_test",
37                                 .instIndex = 0,
38                                 .appIDDesc = "test",
39                                 .isSystemApp = true};
40 
41 PermissionDef testPermDef1 = {.permissionName = "ohos.permission.MANAGE_NET_STRATEGY",
42                               .bundleName = "net_policy_manager_test",
43                               .grantMode = 1,
44                               .availableLevel = APL_SYSTEM_BASIC,
45                               .label = "label",
46                               .labelId = 1,
47                               .description = "Test net policy connectivity internal",
48                               .descriptionId = 1};
49 
50 PermissionStateFull testState1 = {.permissionName = "ohos.permission.MANAGE_NET_STRATEGY",
51                                   .isGeneral = true,
52                                   .resDeviceID = {"local"},
53                                   .grantStatus = {PermissionState::PERMISSION_GRANTED},
54                                   .grantFlags = {2}};
55 
56 HapPolicyParams testPolicyPrams1 = {.apl = APL_SYSTEM_BASIC,
57                                     .domain = "test.domain",
58                                     .permList = {testPermDef1},
59                                     .permStateList = {testState1}};
60 } // namespace
61 
62 
63 class AccessToken {
64 public:
AccessToken(HapInfoParams & testInfoParms,HapPolicyParams & testPolicyPrams)65     AccessToken(HapInfoParams &testInfoParms, HapPolicyParams &testPolicyPrams) : currentID_(GetSelfTokenID())
66     {
67         AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
68         accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
69         SetSelfTokenID(tokenIdEx.tokenIDEx);
70     }
~AccessToken()71     ~AccessToken()
72     {
73         AccessTokenKit::DeleteToken(accessID_);
74         SetSelfTokenID(currentID_);
75     }
76 
77 private:
78     AccessTokenID currentID_;
79     AccessTokenID accessID_ = 0;
80 };
81 
82 using namespace testing::ext;
83 class NetPolicySettingTest : public testing::Test {
84 public:
85     static void SetUpTestCase();
86     static void TearDownTestCase();
87     void SetUp();
88     void TearDown();
89 
90     sptr<NetPolicyCallbackTest> GetINetPolicyCallbackSample() const;
91 };
92 
SetUpTestCase()93 void NetPolicySettingTest::SetUpTestCase()
94 {
95 }
96 
TearDownTestCase()97 void NetPolicySettingTest::TearDownTestCase()
98 {
99 }
SetUp()100 void NetPolicySettingTest::SetUp() {}
101 
TearDown()102 void NetPolicySettingTest::TearDown() {}
103 
104 HWTEST_F(NetPolicySettingTest, OpenPowerSave, TestSize.Level1)
105 {
106     AccessToken token(testInfoParms1, testPolicyPrams1);
107     int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetPowerSavePolicy(true);
108     EXPECT_EQ(result, NETMANAGER_SUCCESS);
109 }
110 
111 HWTEST_F(NetPolicySettingTest, ClosePowerSave, TestSize.Level1)
112 {
113     AccessToken token(testInfoParms1, testPolicyPrams1);
114     int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetPowerSavePolicy(false);
115     EXPECT_EQ(result, NETMANAGER_SUCCESS);
116 }
117 
118 HWTEST_F(NetPolicySettingTest, OpenDeviceIdle, TestSize.Level1)
119 {
120     AccessToken token(testInfoParms1, testPolicyPrams1);
121     int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetDeviceIdlePolicy(true);
122     EXPECT_EQ(result, NETMANAGER_SUCCESS);
123 }
124 
125 HWTEST_F(NetPolicySettingTest, CloseDeviceIdle, TestSize.Level1)
126 {
127     AccessToken token(testInfoParms1, testPolicyPrams1);
128     int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetDeviceIdlePolicy(false);
129     EXPECT_EQ(result, NETMANAGER_SUCCESS);
130 }
131 } // namespace NetManagerStandard
132 } // namespace OHOS
133