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 #include <functional> 16 #include <chrono> 17 #include <thread> 18 #include <message_parcel.h> 19 20 #include "gtest/gtest.h" 21 #include "singleton.h" 22 23 #include "allow_type.h" 24 #include "istandby_service.h" 25 #include "resourcce_request.h" 26 #include "standby_ipc_interface_code.h" 27 #include "standby_service_client.h" 28 #include "standby_service_proxy.h" 29 #include "standby_service_subscriber_stub.h" 30 #include "standby_service_subscriber_proxy.h" 31 32 using namespace testing::ext; 33 34 namespace OHOS { 35 namespace DevStandbyMgr { 36 class StandbyServiceClientUnitTest : public testing::Test { 37 public: SetUpTestCase()38 static void SetUpTestCase() {} TearDownTestCase()39 static void TearDownTestCase() {} SetUp()40 void SetUp() override {} TearDown()41 void TearDown() override {} 42 }; 43 44 /** 45 * @tc.name: StandbyServiceClientUnitTest_001 46 * @tc.desc: test SubscribeStandbyCallback. 47 * @tc.type: FUNC 48 * @tc.require: 49 */ 50 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_001, TestSize.Level1) 51 { 52 sptr<IStandbyServiceSubscriber> nullSubscriber = nullptr; 53 EXPECT_NE(StandbyServiceClient::GetInstance().SubscribeStandbyCallback(nullSubscriber), ERR_OK); 54 EXPECT_NE(StandbyServiceClient::GetInstance().SubscribeStandbyCallback(nullSubscriber), ERR_OK); 55 StandbyServiceClient::GetInstance().UnsubscribeStandbyCallback(nullSubscriber); 56 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub(); 57 EXPECT_NE(StandbyServiceClient::GetInstance().SubscribeStandbyCallback(subscriber), ERR_OK); 58 StandbyServiceClient::GetInstance().SubscribeStandbyCallback(subscriber); 59 EXPECT_NE(StandbyServiceClient::GetInstance().UnsubscribeStandbyCallback(subscriber), ERR_OK); 60 EXPECT_NE(StandbyServiceClient::GetInstance().UnsubscribeStandbyCallback(subscriber), ERR_OK); 61 } 62 63 /** 64 * @tc.name: StandbyServiceClientUnitTest_002 65 * @tc.desc: test ApplyAllowResource. 66 * @tc.type: FUNC 67 * @tc.require: 68 */ 69 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_002, TestSize.Level1) 70 { 71 sptr<ResourceRequest> nullRequest = nullptr; 72 EXPECT_NE(StandbyServiceClient::GetInstance().ApplyAllowResource(nullRequest), ERR_OK); 73 EXPECT_NE(StandbyServiceClient::GetInstance().UnapplyAllowResource(nullRequest), ERR_OK); 74 75 sptr<ResourceRequest> resouarceRequest = new (std::nothrow) ResourceRequest(); 76 EXPECT_NE(StandbyServiceClient::GetInstance().ApplyAllowResource(resouarceRequest), ERR_OK); 77 EXPECT_NE(StandbyServiceClient::GetInstance().UnapplyAllowResource(resouarceRequest), ERR_OK); 78 79 sptr<ResourceRequest> validResRequest = new (std::nothrow) ResourceRequest(AllowType::NETWORK, 80 0, "test_process", 100, "test", 1); 81 EXPECT_EQ(StandbyServiceClient::GetInstance().ApplyAllowResource(validResRequest), ERR_OK); 82 EXPECT_EQ(StandbyServiceClient::GetInstance().UnapplyAllowResource(validResRequest), ERR_OK); 83 } 84 85 /** 86 * @tc.name: StandbyServiceClientUnitTest_003 87 * @tc.desc: test GetAllowList. 88 * @tc.type: FUNC 89 * @tc.require: 90 */ 91 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_003, TestSize.Level1) 92 { 93 std::vector<AllowInfo> allowInfoList; 94 sptr<ResourceRequest> nullRequest = nullptr; 95 EXPECT_EQ(StandbyServiceClient::GetInstance().GetAllowList(AllowType::NETWORK, allowInfoList, 0), ERR_OK); 96 EXPECT_NE(StandbyServiceClient::GetInstance().GetAllowList(0, allowInfoList, 0), ERR_OK); 97 EXPECT_EQ(StandbyServiceClient::GetInstance().GetAllowList((1 << 6), allowInfoList, 0), ERR_OK); 98 allowInfoList.emplace_back(AllowInfo {}); 99 StandbyServiceClient::GetInstance().GetAllowList((1 << 6), allowInfoList, 0); 100 } 101 102 /** 103 * @tc.name: StandbyServiceClientUnitTest_004 104 * @tc.desc: test IsDeviceInStandby. 105 * @tc.type: FUNC 106 * @tc.require: 107 */ 108 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_004, TestSize.Level1) 109 { 110 bool isStandby {false}; 111 EXPECT_EQ(StandbyServiceClient::GetInstance().IsDeviceInStandby(isStandby), ERR_OK); 112 } 113 114 /** 115 * @tc.name: StandbyServiceClientUnitTest_005 116 * @tc.desc: test Unmarshalling. 117 * @tc.type: FUNC 118 * @tc.require: 119 */ 120 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_005, TestSize.Level1) 121 { 122 auto allowInfo = std::make_shared<AllowInfo>(); 123 MessageParcel out; 124 EXPECT_TRUE(allowInfo->Marshalling(out)); 125 MessageParcel data; 126 EXPECT_EQ(AllowInfo::Unmarshalling(data), nullptr); 127 EXPECT_EQ(ResourceRequest::Unmarshalling(data), nullptr); 128 } 129 130 /** 131 * @tc.name: StandbyServiceClientUnitTest_006 132 * @tc.desc: test ResetStandbyServiceClient. 133 * @tc.type: FUNC 134 * @tc.require: 135 */ 136 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_006, TestSize.Level1) 137 { 138 StandbyServiceClient::GetInstance().ResetStandbyServiceClient(); 139 StandbyServiceClient::GetInstance().ResetStandbyServiceClient(); 140 StandbyServiceClient::GetInstance().GetStandbyServiceProxy(); 141 StandbyServiceClient::GetInstance().ResetStandbyServiceClient(); 142 EXPECT_EQ(StandbyServiceClient::GetInstance().standbyServiceProxy_, nullptr); 143 } 144 145 /** 146 * @tc.name: StandbyServiceClientUnitTest_007 147 * @tc.desc: test StandbyServiceSubscriberStub. 148 * @tc.type: FUNC 149 * @tc.require: 150 */ 151 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_007, TestSize.Level1) 152 { 153 sptr<StandbyServiceSubscriberStub> subscriber = new (std::nothrow) StandbyServiceSubscriberStub(); 154 MessageParcel data {}; 155 MessageParcel reply {}; 156 MessageOption option {}; 157 subscriber->OnRemoteRequestInner( 158 (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED)), data, reply, option); 159 subscriber->OnRemoteRequestInner( 160 (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED)) + 1, data, reply, option); 161 EXPECT_NE(subscriber->HandleOnDeviceIdleMode(data), ERR_OK); 162 subscriber->HandleOnAllowListChanged(data); 163 data.WriteBool(false); 164 subscriber->HandleOnDeviceIdleMode(data); 165 data.WriteBool(false); 166 subscriber->HandleOnDeviceIdleMode(data); 167 subscriber->HandleOnAllowListChanged(data); 168 MessageParcel allowListData {}; 169 subscriber->HandleOnAllowListChanged(allowListData); 170 allowListData.WriteInt32(0); 171 subscriber->HandleOnAllowListChanged(allowListData); 172 allowListData.WriteInt32(0); 173 subscriber->HandleOnAllowListChanged(allowListData); 174 allowListData.WriteString(""); 175 subscriber->HandleOnAllowListChanged(allowListData); 176 allowListData.WriteUint32(0); 177 subscriber->HandleOnAllowListChanged(allowListData); 178 allowListData.WriteBool(false); 179 subscriber->HandleOnAllowListChanged(allowListData); 180 } 181 182 /** 183 * @tc.name: StandbyServiceClientUnitTest_008 184 * @tc.desc: test StandbyServiceProxy. 185 * @tc.type: FUNC 186 * @tc.require: 187 */ 188 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_008, TestSize.Level1) 189 { 190 sptr<IRemoteObject> impl {}; 191 sptr<StandbyServiceProxy> proxy = new (std::nothrow) StandbyServiceProxy(impl); 192 sptr<IStandbyServiceSubscriber> nullSubscriber = nullptr; 193 EXPECT_NE(proxy->SubscribeStandbyCallback(nullSubscriber), ERR_OK); 194 EXPECT_NE(proxy->SubscribeStandbyCallback(nullSubscriber), ERR_OK); 195 proxy->UnsubscribeStandbyCallback(nullSubscriber); 196 } 197 198 /** 199 * @tc.name: StandbyServiceClientUnitTest_009 200 * @tc.desc: test ReportWorkSchedulerStatus. 201 * @tc.type: FUNC 202 * @tc.require: 203 */ 204 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_009, TestSize.Level1) 205 { 206 EXPECT_EQ(StandbyServiceClient::GetInstance().ReportWorkSchedulerStatus(true, -1, ""), ERR_OK); 207 EXPECT_EQ(StandbyServiceClient::GetInstance().ReportWorkSchedulerStatus(false, -1, ""), ERR_OK); 208 } 209 210 /** 211 * @tc.name: StandbyServiceClientUnitTest_010 212 * @tc.desc: test StandbyServiceSubscriberProxy. 213 * @tc.type: FUNC 214 * @tc.require: 215 */ 216 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_010, TestSize.Level1) 217 { 218 sptr<IRemoteObject> impl {}; 219 sptr<StandbyServiceSubscriberProxy> proxy = new (std::nothrow) StandbyServiceSubscriberProxy(impl); 220 sptr<StandbyServiceSubscriberProxy> nullSubscriber = nullptr; 221 proxy->OnDeviceIdleMode(false, false); 222 proxy->OnAllowListChanged(-1, "", 0, false); 223 EXPECT_NE(proxy, nullptr); 224 } 225 226 /** 227 * @tc.name: StandbyServiceClientUnitTest_011 228 * @tc.desc: test IsStrategyEnabled. 229 * @tc.type: FUNC 230 * @tc.require: 231 */ 232 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_011, TestSize.Level1) 233 { 234 std::string strategyName; 235 bool isEnabled = false; 236 EXPECT_EQ(StandbyServiceClient::GetInstance().IsStrategyEnabled(strategyName, isEnabled), ERR_OK); 237 } 238 239 /** 240 * @tc.name: StandbyServiceClientUnitTest_012 241 * @tc.desc: test IsStrategyEnabled. 242 * @tc.type: FUNC 243 * @tc.require: 244 */ 245 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_012, TestSize.Level1) 246 { 247 DeviceStateType type = DeviceStateType::WIFI_P2P_CHANGE; 248 bool enabled = false; 249 EXPECT_EQ(StandbyServiceClient::GetInstance().ReportDeviceStateChanged(type, enabled), ERR_OK); 250 } 251 252 /** 253 * @tc.name: StandbyServiceClientUnitTest_013 254 * @tc.desc: test IsStrategyEnabled. 255 * @tc.type: FUNC 256 * @tc.require: 257 */ 258 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_013, TestSize.Level1) 259 { 260 uint32_t restrictType = 1; 261 std::vector<AllowInfo> restrictInfoList; 262 uint32_t reasonCode = ReasonCodeEnum::REASON_APP_API; 263 EXPECT_EQ(StandbyServiceClient::GetInstance().GetRestrictList(restrictType, restrictInfoList, reasonCode), ERR_OK); 264 } 265 266 /** 267 * @tc.name: StandbyServiceClientUnitTest_014 268 * @tc.desc: test IsDeviceInStandby. 269 * @tc.type: FUNC 270 * @tc.require: 271 */ 272 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_014, TestSize.Level1) 273 { 274 uint32_t type = 1; 275 bool enable = true; 276 uint32_t interval = 300; 277 EXPECT_EQ(StandbyServiceClient::GetInstance().SetNatInterval(type, enable, interval), ERR_PERMISSION_DENIED); 278 } 279 280 /** 281 * @tc.name: StandbyServiceClientUnitTest_015 282 * @tc.desc: test ReportPowerOverused. 283 * @tc.type: FUNC 284 * @tc.require: 285 */ 286 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_015, TestSize.Level1) 287 { 288 std::string TEST_MODULE_NAME = "TestModule"; 289 uint32_t level = static_cast<uint32_t>(PowerOverusedLevel::NORMAL); 290 EXPECT_EQ(StandbyServiceClient::GetInstance().ReportPowerOverused(TEST_MODULE_NAME, level), ERR_OK); 291 } 292 293 } // namespace DevStandbyMgr 294 } // namespace OHOS 295