1 /* 2 * Copyright (c) 2022-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 <gtest/gtest.h> 17 #include <system_ability_definition.h> 18 19 #include "accesstoken_kit.h" 20 #include "mock_accesstoken_kit.h" 21 #include "permission.h" 22 #include "power_common.h" 23 #include "power_log.h" 24 #include "setting_observer.h" 25 #include "setting_provider.h" 26 #include "sysparam.h" 27 #include "tokenid_kit.h" 28 29 using namespace OHOS::Security::AccessToken; 30 using namespace OHOS::PowerMgr; 31 using namespace testing::ext; 32 using namespace std; 33 34 namespace OHOS { 35 namespace PowerMgr { 36 class PowerMgrUtilTest : public testing::Test {}; 37 } // namespace PowerMgr 38 } // namespace OHOS 39 40 namespace { 41 /** 42 * @tc.name: PermissionIsSystemNative 43 * @tc.desc: The IsSystem and IsPermissionGranted functions are granted by default as TOKEN_NATIVE or TOKEN_SHELL types 44 * @tc.type: FUNC 45 */ 46 HWTEST_F(PowerMgrUtilTest, PermissionIsSystemNative, TestSize.Level0) 47 { 48 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); 49 EXPECT_TRUE(Permission::IsSystem()); 50 EXPECT_TRUE(Permission::IsPermissionGranted("REBOOT")); 51 52 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_SHELL); 53 EXPECT_TRUE(Permission::IsSystem()); 54 EXPECT_TRUE(Permission::IsPermissionGranted("REBOOT")); 55 } 56 57 /** 58 * @tc.name: PermissionIsSystemHap 59 * @tc.desc: The function IsSystem and IsPermissionGranted in the test TOKEN_HAP 60 * @tc.type: FUNC 61 */ 62 HWTEST_F(PowerMgrUtilTest, PermissionIsSystemHap, TestSize.Level0) 63 { 64 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); 65 MockAccesstokenKit::MockSetSystemApp(false); 66 EXPECT_FALSE(Permission::IsSystem()); 67 68 MockAccesstokenKit::MockSetSystemApp(true); 69 EXPECT_TRUE(Permission::IsSystem()); 70 MockAccesstokenKit::MockSetPermissionState(PermissionState::PERMISSION_GRANTED); 71 EXPECT_TRUE(Permission::IsPermissionGranted("REBOOT")); 72 } 73 74 /** 75 * @tc.name: PermissionIsSystemInvalid 76 * @tc.desc: The IsSystem and IsPermissionGranted do not have permissions on TOKEN_INVALID or TOKEN_TYPE_BUTT types 77 * @tc.type: FUNC 78 */ 79 HWTEST_F(PowerMgrUtilTest, PermissionIsSystemInvalid, TestSize.Level0) 80 { 81 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID); 82 EXPECT_FALSE(Permission::IsSystem()); 83 EXPECT_FALSE(Permission::IsPermissionGranted("REBOOT")); 84 85 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_TYPE_BUTT); 86 EXPECT_FALSE(Permission::IsSystem()); 87 EXPECT_FALSE(Permission::IsPermissionGranted("REBOOT")); 88 } 89 90 /** 91 * @tc.name: PermissionIsPermissionGrantedHap 92 * @tc.desc: Test Permission function IsPermissionGranted is TOKEN_HAP 93 * @tc.type: FUNC 94 */ 95 HWTEST_F(PowerMgrUtilTest, PermissionIsPermissionGrantedHap, TestSize.Level0) 96 { 97 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); 98 MockAccesstokenKit::MockSetPermissionState(PermissionState::PERMISSION_DENIED); 99 EXPECT_FALSE(Permission::IsPermissionGranted("REBOOT")); 100 101 MockAccesstokenKit::MockSetPermissionState(PermissionState::PERMISSION_GRANTED); 102 EXPECT_TRUE(Permission::IsPermissionGranted("REBOOT")); 103 } 104 105 /** 106 * @tc.name: PermissionIsPermissionGrantedNative 107 * @tc.desc: Test function IsPermissionGranted is TOKEN_NATIVE or TOKEN_SHELL with permissions by default 108 * @tc.type: FUNC 109 */ 110 HWTEST_F(PowerMgrUtilTest, PermissionIsPermissionGrantedNative, TestSize.Level0) 111 { 112 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); 113 EXPECT_TRUE(Permission::IsPermissionGranted("REBOOT")); 114 115 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_SHELL); 116 EXPECT_TRUE(Permission::IsPermissionGranted("REBOOT")); 117 } 118 119 /** 120 * @tc.name: PermissionIsPermissionGrantedInvalid 121 * @tc.desc: Test Permission function IsSystem is TOKEN_INVALID or TOKEN_TYPE_BUTT without permission 122 * @tc.type: FUNC 123 */ 124 HWTEST_F(PowerMgrUtilTest, PermissionIsPermissionGrantedInvalid, TestSize.Level0) 125 { 126 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID); 127 EXPECT_FALSE(Permission::IsPermissionGranted("REBOOT")); 128 MockAccesstokenKit::MockSetTokenTypeFlag(ATokenTypeEnum::TOKEN_TYPE_BUTT); 129 EXPECT_FALSE(Permission::IsPermissionGranted("REBOOT")); 130 } 131 132 /** 133 * @tc.name: SettingObserverTest001 134 * @tc.desc: test SetKey in proxy 135 * @tc.type: FUNC 136 */ 137 HWTEST_F (PowerMgrUtilTest, SettingObserver001, TestSize.Level0) 138 { 139 POWER_HILOGD(LABEL_TEST, "SettingObserver001::fun is start!"); 140 std::shared_ptr<SettingObserver> settingObserver = std::make_shared<SettingObserver>(); 141 settingObserver->OnChange(); __anon23f011de0202(const std::string&) 142 SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {}; 143 settingObserver->SetUpdateFunc(updateFunc); 144 settingObserver->SetKey("settings.power.wakeup_sources"); 145 std::string key = settingObserver->GetKey(); 146 EXPECT_EQ(key, "settings.power.wakeup_sources"); 147 POWER_HILOGD(LABEL_TEST, "SettingObserver001::fun is end!"); 148 } 149 150 /** 151 * @tc.name: SettingProviderTest001 152 * @tc.desc: test CreateObserver in proxy 153 * @tc.type: FUNC 154 */ 155 HWTEST_F (PowerMgrUtilTest, SettingProvider001, TestSize.Level0) 156 { 157 POWER_HILOGD(LABEL_TEST, "SettingProvider001::fun is start!"); 158 auto& settingProvider = SettingProvider::GetInstance(OHOS::POWER_MANAGER_SERVICE_ID); 159 std::string valueStr; 160 settingProvider.GetStringValue("settings.power.wakeup_sources", valueStr); 161 bool valueBool = false; 162 settingProvider.PutBoolValue("settings.power.suspend_sources", valueBool); 163 settingProvider.GetBoolValue("settings.power.suspend_sources", valueBool); __anon23f011de0302(const std::string&) 164 SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {}; 165 auto observer = settingProvider.CreateObserver("settings.power.suspend_sources", updateFunc); 166 EXPECT_TRUE(observer != nullptr); 167 POWER_HILOGD(LABEL_TEST, "SettingProvider001::fun is end!"); 168 } 169 170 /** 171 * @tc.name: SettingProviderTest002 172 * @tc.desc: test RegisterObserver in proxy 173 * @tc.type: FUNC 174 */ 175 HWTEST_F (PowerMgrUtilTest, SettingProvider002, TestSize.Level0) 176 { 177 POWER_HILOGD(LABEL_TEST, "SettingProvider002::fun is start!"); 178 auto& settingProvider = SettingProvider::GetInstance(OHOS::POWER_MANAGER_SERVICE_ID); 179 int32_t putValue = 10; 180 int32_t getValue; 181 settingProvider.PutIntValue("settings.power.suspend_sources", putValue); 182 settingProvider.GetIntValue("settings.power.suspend_sources", getValue); __anon23f011de0402(const std::string&) 183 SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {}; 184 auto observer = settingProvider.CreateObserver("settings.power.suspend_sources", updateFunc); 185 EXPECT_EQ(OHOS::ERR_OK, settingProvider.RegisterObserver(observer)); 186 POWER_HILOGD(LABEL_TEST, "SettingProvider002::fun is end!"); 187 } 188 189 /** 190 * @tc.name: SettingProviderTest003 191 * @tc.desc: test UnregisterObserver in proxy 192 * @tc.type: FUNC 193 */ 194 HWTEST_F (PowerMgrUtilTest, SettingProvider003, TestSize.Level0) 195 { 196 POWER_HILOGD(LABEL_TEST, "SettingProvider003::fun is start!"); 197 auto& settingProvider = SettingProvider::GetInstance(OHOS::POWER_MANAGER_SERVICE_ID); 198 int64_t value; 199 settingProvider.GetLongValue("settings.display.screen_off_timeout", value); 200 settingProvider.IsValidKey("settings.power.suspend_sources"); __anon23f011de0502(const std::string&) 201 SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {}; 202 auto observer = settingProvider.CreateObserver("settings.power.suspend_sources", updateFunc); 203 OHOS::ErrCode ret = settingProvider.RegisterObserver(observer); 204 ret = settingProvider.UnregisterObserver(observer); 205 EXPECT_EQ(OHOS::ERR_OK, ret); 206 POWER_HILOGD(LABEL_TEST, "SettingProvider003::fun is end!"); 207 } 208 209 /** 210 * @tc.name: SettingProviderTest004 211 * @tc.desc: test PutLongValue func 212 * @tc.type: FUNC 213 */ 214 HWTEST_F(PowerMgrUtilTest, SettingProvider004, TestSize.Level0) 215 { 216 POWER_HILOGD(LABEL_TEST, "SettingProvider004::fun is start!"); 217 static constexpr const char* SETTING_DISPLAY_OFF_TIME_KEY {"settings.display.screen_off_timeout"}; 218 auto& settingProvider = SettingProvider::GetInstance(OHOS::POWER_MANAGER_SERVICE_ID); 219 int64_t value = 0; 220 OHOS::ErrCode ret = settingProvider.PutLongValue(SETTING_DISPLAY_OFF_TIME_KEY, value); 221 EXPECT_EQ(OHOS::ERR_OK, ret); 222 POWER_HILOGD(LABEL_TEST, "SettingProvider004::fun is end!"); 223 } 224 225 /** 226 * @tc.name: SysparamTest001 227 * @tc.desc: test GetIntValue in proxy 228 * @tc.type: FUNC 229 */ 230 HWTEST_F (PowerMgrUtilTest, Sysparam001, TestSize.Level0) 231 { 232 POWER_HILOGD(LABEL_TEST, "Sysparam001::fun is start!"); 233 std::shared_ptr<SysParam> sysParam = std::make_shared<SysParam>(); 234 int32_t def = 0; 235 EXPECT_EQ(OHOS::ERR_OK, sysParam->GetIntValue("settings.power.suspend_sources", def)); 236 POWER_HILOGD(LABEL_TEST, "Sysparam001::fun is end!"); 237 } 238 } // namespace 239