1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci * You may obtain a copy of the License at 6d9f0492fSopenharmony_ci * 7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci * 9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci * limitations under the License. 14d9f0492fSopenharmony_ci */ 15d9f0492fSopenharmony_ci#include <gtest/gtest.h> 16d9f0492fSopenharmony_ci 17d9f0492fSopenharmony_ci#include "if_system_ability_manager.h" 18d9f0492fSopenharmony_ci#include "init_param.h" 19d9f0492fSopenharmony_ci#include "iservice_registry.h" 20d9f0492fSopenharmony_ci#include "iwatcher.h" 21d9f0492fSopenharmony_ci#include "iwatcher_manager.h" 22d9f0492fSopenharmony_ci#include "message_parcel.h" 23d9f0492fSopenharmony_ci#include "parameter.h" 24d9f0492fSopenharmony_ci#include "param_manager.h" 25d9f0492fSopenharmony_ci#include "param_stub.h" 26d9f0492fSopenharmony_ci#include "param_utils.h" 27d9f0492fSopenharmony_ci#include "system_ability_definition.h" 28d9f0492fSopenharmony_ci#include "watcher.h" 29d9f0492fSopenharmony_ci#include "watcher_manager_kits.h" 30d9f0492fSopenharmony_ci#include "watcher_manager_proxy.h" 31d9f0492fSopenharmony_ci#include "service_watcher.h" 32d9f0492fSopenharmony_ci 33d9f0492fSopenharmony_ciusing namespace testing::ext; 34d9f0492fSopenharmony_ciusing namespace std; 35d9f0492fSopenharmony_ciusing namespace OHOS; 36d9f0492fSopenharmony_ciusing namespace OHOS::init_param; 37d9f0492fSopenharmony_ci 38d9f0492fSopenharmony_ciint g_callbackCount = 0; 39d9f0492fSopenharmony_cistatic void TestParameterChange(const char *key, const char *value, void *context) 40d9f0492fSopenharmony_ci{ 41d9f0492fSopenharmony_ci printf("TestParameterChange key:%s %s \n", key, value); 42d9f0492fSopenharmony_ci g_callbackCount++; 43d9f0492fSopenharmony_ci} 44d9f0492fSopenharmony_ci 45d9f0492fSopenharmony_cistatic void TestWatcherCallBack(const char *key, const ServiceInfo *status) 46d9f0492fSopenharmony_ci{ 47d9f0492fSopenharmony_ci printf("TestWatcherCallBack key:%s %d", key, status->status); 48d9f0492fSopenharmony_ci} 49d9f0492fSopenharmony_ci 50d9f0492fSopenharmony_ciclass WatcherAgentUnitTest : public ::testing::Test { 51d9f0492fSopenharmony_cipublic: 52d9f0492fSopenharmony_ci WatcherAgentUnitTest() {} 53d9f0492fSopenharmony_ci virtual ~WatcherAgentUnitTest() {} 54d9f0492fSopenharmony_ci 55d9f0492fSopenharmony_ci void SetUp() 56d9f0492fSopenharmony_ci { 57d9f0492fSopenharmony_ci if (GetParamSecurityLabel() != nullptr) { 58d9f0492fSopenharmony_ci GetParamSecurityLabel()->cred.uid = 1000; // 1000 test uid 59d9f0492fSopenharmony_ci GetParamSecurityLabel()->cred.gid = 1000; // 1000 test gid 60d9f0492fSopenharmony_ci } 61d9f0492fSopenharmony_ci SetTestPermissionResult(0); 62d9f0492fSopenharmony_ci } 63d9f0492fSopenharmony_ci void TearDown() {} 64d9f0492fSopenharmony_ci void TestBody() {} 65d9f0492fSopenharmony_ci 66d9f0492fSopenharmony_ci int TestAddWatcher0(size_t index) 67d9f0492fSopenharmony_ci { 68d9f0492fSopenharmony_ci int ret = 0; 69d9f0492fSopenharmony_ci // has beed deleted 70d9f0492fSopenharmony_ci ret = RemoveParameterWatcher("test.permission.watcher.test1", 71d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 72d9f0492fSopenharmony_ci EXPECT_NE(ret, 0); 73d9f0492fSopenharmony_ci 74d9f0492fSopenharmony_ci // delete all 75d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test1", 76d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 77d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 78d9f0492fSopenharmony_ci index++; 79d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test1", 80d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 81d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 82d9f0492fSopenharmony_ci ret = RemoveParameterWatcher("test.permission.watcher.test1", nullptr, nullptr); 83d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 84d9f0492fSopenharmony_ci // 非法 85d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.tes^^^^t1*", TestParameterChange, nullptr); 86d9f0492fSopenharmony_ci EXPECT_EQ(ret, PARAM_CODE_INVALID_NAME); 87d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.read.test1*", TestParameterChange, nullptr); 88d9f0492fSopenharmony_ci EXPECT_EQ(ret, DAC_RESULT_FORBIDED); 89d9f0492fSopenharmony_ci return ret; 90d9f0492fSopenharmony_ci } 91d9f0492fSopenharmony_ci int TestAddWatcher() 92d9f0492fSopenharmony_ci { 93d9f0492fSopenharmony_ci size_t index = 1; 94d9f0492fSopenharmony_ci int ret = SystemWatchParameter("test.permission.watcher.test1", 95d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 96d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 97d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test1*", 98d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 99d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 100d9f0492fSopenharmony_ci // repeat add, return fail 101d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test1", 102d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 103d9f0492fSopenharmony_ci EXPECT_NE(ret, 0); 104d9f0492fSopenharmony_ci index++; 105d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test1", 106d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 107d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 108d9f0492fSopenharmony_ci index++; 109d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test1", 110d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 111d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 112d9f0492fSopenharmony_ci 113d9f0492fSopenharmony_ci // delete 114d9f0492fSopenharmony_ci ret = RemoveParameterWatcher("test.permission.watcher.test1", 115d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 116d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 117d9f0492fSopenharmony_ci ret = RemoveParameterWatcher("test.permission.watcher.test1", 118d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 119d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 120d9f0492fSopenharmony_ci index--; 121d9f0492fSopenharmony_ci ret = RemoveParameterWatcher("test.permission.watcher.test1", 122d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 123d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 124d9f0492fSopenharmony_ci index--; 125d9f0492fSopenharmony_ci ret = RemoveParameterWatcher("test.permission.watcher.test1", 126d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 127d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 128d9f0492fSopenharmony_ci return TestAddWatcher0(index); 129d9f0492fSopenharmony_ci } 130d9f0492fSopenharmony_ci 131d9f0492fSopenharmony_ci int TestDelWatcher() 132d9f0492fSopenharmony_ci { 133d9f0492fSopenharmony_ci size_t index = 1; 134d9f0492fSopenharmony_ci int ret = SystemWatchParameter("test.permission.watcher.test3.1", 135d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 136d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 137d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test3.1*", 138d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 139d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 140d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test3.2", 141d9f0492fSopenharmony_ci TestParameterChange, reinterpret_cast<void *>(index)); 142d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 143d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test3.2", TestParameterChange, 144d9f0492fSopenharmony_ci reinterpret_cast<void *>(index)); 145d9f0492fSopenharmony_ci EXPECT_EQ(ret, PARAM_WATCHER_CALLBACK_EXIST); 146d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test3.1", nullptr, nullptr); 147d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 148d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test3.1*", nullptr, nullptr); 149d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 150d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.test3.2", nullptr, nullptr); 151d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 152d9f0492fSopenharmony_ci 153d9f0492fSopenharmony_ci // 非法 154d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.watcher.tes^^^^t1*", nullptr, nullptr); 155d9f0492fSopenharmony_ci EXPECT_EQ(ret, PARAM_CODE_INVALID_NAME); 156d9f0492fSopenharmony_ci ret = SystemWatchParameter("test.permission.read.test1*", nullptr, nullptr); 157d9f0492fSopenharmony_ci EXPECT_EQ(ret, DAC_RESULT_FORBIDED); 158d9f0492fSopenharmony_ci return 0; 159d9f0492fSopenharmony_ci } 160d9f0492fSopenharmony_ci 161d9f0492fSopenharmony_ci int TestRecvMessage(const std::string &name) 162d9f0492fSopenharmony_ci { 163d9f0492fSopenharmony_ci MessageParcel data; 164d9f0492fSopenharmony_ci MessageParcel reply; 165d9f0492fSopenharmony_ci MessageOption option; 166d9f0492fSopenharmony_ci data.WriteInterfaceToken(IWatcher::GetDescriptor()); 167d9f0492fSopenharmony_ci data.WriteString(name); 168d9f0492fSopenharmony_ci data.WriteString(name); 169d9f0492fSopenharmony_ci data.WriteString("watcherId"); 170d9f0492fSopenharmony_ci g_callbackCount = 0; 171d9f0492fSopenharmony_ci int ret = SystemWatchParameter(name.c_str(), TestParameterChange, nullptr); 172d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 173d9f0492fSopenharmony_ci WatcherManagerKits &instance = OHOS::init_param::WatcherManagerKits::GetInstance(); 174d9f0492fSopenharmony_ci if (instance.remoteWatcher_ != nullptr) { 175d9f0492fSopenharmony_ci instance.remoteWatcher_->OnRemoteRequest(IWatcher::PARAM_CHANGE, data, reply, option); 176d9f0492fSopenharmony_ci instance.remoteWatcher_->OnRemoteRequest(IWatcher::PARAM_CHANGE + 1, data, reply, option); 177d9f0492fSopenharmony_ci instance.remoteWatcher_->OnParameterChange(name.c_str(), "testname", "testvalue"); 178d9f0492fSopenharmony_ci EXPECT_EQ(g_callbackCount, 2); // 2 is callback Count 179d9f0492fSopenharmony_ci instance.remoteWatcher_->OnParameterChange(name.c_str(), "testname.2", "testvalue"); 180d9f0492fSopenharmony_ci EXPECT_EQ(g_callbackCount, 3); // 3 is callback Count 181d9f0492fSopenharmony_ci instance.remoteWatcher_->OnParameterChange(name.c_str(), "testname.2", "testvalue"); 182d9f0492fSopenharmony_ci EXPECT_EQ(g_callbackCount, 3); // 3 is callback Count 183d9f0492fSopenharmony_ci 184d9f0492fSopenharmony_ci // prefix not exit 185d9f0492fSopenharmony_ci instance.remoteWatcher_->OnParameterChange("44444444444444444444", "testname.2", "testvalue"); 186d9f0492fSopenharmony_ci } 187d9f0492fSopenharmony_ci EXPECT_EQ(g_callbackCount, 3); // 3 is callback Count 188d9f0492fSopenharmony_ci return 0; 189d9f0492fSopenharmony_ci } 190d9f0492fSopenharmony_ci 191d9f0492fSopenharmony_ci int TestResetService() 192d9f0492fSopenharmony_ci { 193d9f0492fSopenharmony_ci sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 194d9f0492fSopenharmony_ci WATCHER_CHECK(samgr != nullptr, return -1, "Get samgr failed"); 195d9f0492fSopenharmony_ci sptr<IRemoteObject> object = samgr->GetSystemAbility(PARAM_WATCHER_DISTRIBUTED_SERVICE_ID); 196d9f0492fSopenharmony_ci WATCHER_CHECK(object != nullptr, return -1, "Get watcher manager object from samgr failed"); 197d9f0492fSopenharmony_ci OHOS::init_param::WatcherManagerKits &instance = OHOS::init_param::WatcherManagerKits::GetInstance(); 198d9f0492fSopenharmony_ci if (instance.GetDeathRecipient() != nullptr) { 199d9f0492fSopenharmony_ci instance.GetDeathRecipient()->OnRemoteDied(object); 200d9f0492fSopenharmony_ci } 201d9f0492fSopenharmony_ci return 0; 202d9f0492fSopenharmony_ci } 203d9f0492fSopenharmony_ci 204d9f0492fSopenharmony_ci void TestWatcherProxy() 205d9f0492fSopenharmony_ci { 206d9f0492fSopenharmony_ci sptr<WatcherManagerProxy> watcherManager = new(std::nothrow) WatcherManagerProxy(nullptr); 207d9f0492fSopenharmony_ci ASSERT_NE(watcherManager, nullptr); 208d9f0492fSopenharmony_ci 209d9f0492fSopenharmony_ci WatcherManagerKits &instance = OHOS::init_param::WatcherManagerKits::GetInstance(); 210d9f0492fSopenharmony_ci sptr<Watcher> remoteWatcher = new OHOS::init_param::WatcherManagerKits::RemoteWatcher(&instance); 211d9f0492fSopenharmony_ci ASSERT_NE(remoteWatcher, nullptr); 212d9f0492fSopenharmony_ci 213d9f0492fSopenharmony_ci MessageParcel data; 214d9f0492fSopenharmony_ci MessageParcel reply; 215d9f0492fSopenharmony_ci MessageOption option; 216d9f0492fSopenharmony_ci data.WriteInterfaceToken(IWatcher::GetDescriptor()); 217d9f0492fSopenharmony_ci data.WriteString("name"); 218d9f0492fSopenharmony_ci data.WriteString("name"); 219d9f0492fSopenharmony_ci data.WriteString("watcherId"); 220d9f0492fSopenharmony_ci remoteWatcher->OnRemoteRequest(IWatcher::PARAM_CHANGE, data, reply, option); 221d9f0492fSopenharmony_ci 222d9f0492fSopenharmony_ci // invalid parameter 223d9f0492fSopenharmony_ci data.WriteInterfaceToken(IWatcher::GetDescriptor()); 224d9f0492fSopenharmony_ci data.WriteString("name"); 225d9f0492fSopenharmony_ci data.WriteString("watcherId"); 226d9f0492fSopenharmony_ci remoteWatcher->OnRemoteRequest(IWatcher::PARAM_CHANGE, data, reply, option); 227d9f0492fSopenharmony_ci 228d9f0492fSopenharmony_ci data.WriteInterfaceToken(IWatcher::GetDescriptor()); 229d9f0492fSopenharmony_ci data.WriteString("name"); 230d9f0492fSopenharmony_ci remoteWatcher->OnRemoteRequest(IWatcher::PARAM_CHANGE, data, reply, option); 231d9f0492fSopenharmony_ci 232d9f0492fSopenharmony_ci data.WriteInterfaceToken(IWatcher::GetDescriptor()); 233d9f0492fSopenharmony_ci remoteWatcher->OnRemoteRequest(IWatcher::PARAM_CHANGE, data, reply, option); 234d9f0492fSopenharmony_ci 235d9f0492fSopenharmony_ci data.WriteInterfaceToken(IWatcher::GetDescriptor()); 236d9f0492fSopenharmony_ci data.WriteString("name"); 237d9f0492fSopenharmony_ci data.WriteString("name"); 238d9f0492fSopenharmony_ci data.WriteString("watcherId"); 239d9f0492fSopenharmony_ci remoteWatcher->OnRemoteRequest(IWatcher::PARAM_CHANGE + 1, data, reply, option); 240d9f0492fSopenharmony_ci remoteWatcher->OnRemoteRequest(IWatcher::PARAM_CHANGE + 1, data, reply, option); 241d9f0492fSopenharmony_ci 242d9f0492fSopenharmony_ci uint32_t watcherId = watcherManager->AddRemoteWatcher(1000, remoteWatcher); 243d9f0492fSopenharmony_ci // add watcher 244d9f0492fSopenharmony_ci int ret = watcherManager->AddWatcher("test.watcher.proxy", watcherId); 245d9f0492fSopenharmony_ci ASSERT_EQ(ret, 0); 246d9f0492fSopenharmony_ci ret = watcherManager->DelWatcher("test.watcher.proxy", watcherId); 247d9f0492fSopenharmony_ci ASSERT_EQ(ret, 0); 248d9f0492fSopenharmony_ci ret = watcherManager->RefreshWatcher("test.watcher.proxy", watcherId); 249d9f0492fSopenharmony_ci ASSERT_EQ(ret, 0); 250d9f0492fSopenharmony_ci watcherManager->DelRemoteWatcher(watcherId); 251d9f0492fSopenharmony_ci } 252d9f0492fSopenharmony_ci}; 253d9f0492fSopenharmony_ci 254d9f0492fSopenharmony_ciHWTEST_F(WatcherAgentUnitTest, Init_TestAddWatcher_001, TestSize.Level0) 255d9f0492fSopenharmony_ci{ 256d9f0492fSopenharmony_ci WatcherAgentUnitTest test; 257d9f0492fSopenharmony_ci test.TestAddWatcher(); 258d9f0492fSopenharmony_ci} 259d9f0492fSopenharmony_ci 260d9f0492fSopenharmony_ciHWTEST_F(WatcherAgentUnitTest, Init_TestRecvMessage_001, TestSize.Level0) 261d9f0492fSopenharmony_ci{ 262d9f0492fSopenharmony_ci WatcherAgentUnitTest test; 263d9f0492fSopenharmony_ci test.TestRecvMessage("test.permission.watcher.agent.test1"); 264d9f0492fSopenharmony_ci} 265d9f0492fSopenharmony_ci 266d9f0492fSopenharmony_ciHWTEST_F(WatcherAgentUnitTest, Init_TestDelWatcher_001, TestSize.Level0) 267d9f0492fSopenharmony_ci{ 268d9f0492fSopenharmony_ci WatcherAgentUnitTest test; 269d9f0492fSopenharmony_ci test.TestDelWatcher(); 270d9f0492fSopenharmony_ci} 271d9f0492fSopenharmony_ci 272d9f0492fSopenharmony_ciHWTEST_F(WatcherAgentUnitTest, Init_TestResetService_001, TestSize.Level0) 273d9f0492fSopenharmony_ci{ 274d9f0492fSopenharmony_ci WatcherAgentUnitTest test; 275d9f0492fSopenharmony_ci test.TestResetService(); 276d9f0492fSopenharmony_ci} 277d9f0492fSopenharmony_ci 278d9f0492fSopenharmony_ciHWTEST_F(WatcherAgentUnitTest, Init_TestWatcherService_001, TestSize.Level0) 279d9f0492fSopenharmony_ci{ 280d9f0492fSopenharmony_ci const char *errstr = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111"; 281d9f0492fSopenharmony_ci ServiceWatchForStatus("param_watcher", TestWatcherCallBack); 282d9f0492fSopenharmony_ci ServiceWaitForStatus("param_watcher", SERVICE_STARTED, 1); 283d9f0492fSopenharmony_ci EXPECT_NE(ServiceWatchForStatus(errstr, TestWatcherCallBack), 0); 284d9f0492fSopenharmony_ci EXPECT_NE(ServiceWatchForStatus(nullptr, TestWatcherCallBack), 0); 285d9f0492fSopenharmony_ci WatchParameter("testParam", nullptr, nullptr); 286d9f0492fSopenharmony_ci WatchParameter(nullptr, nullptr, nullptr); 287d9f0492fSopenharmony_ci} 288d9f0492fSopenharmony_ci 289d9f0492fSopenharmony_ciHWTEST_F(WatcherAgentUnitTest, Init_TestInvalidWatcher_001, TestSize.Level0) 290d9f0492fSopenharmony_ci{ 291d9f0492fSopenharmony_ci int ret = SystemWatchParameter(nullptr, TestParameterChange, nullptr); 292d9f0492fSopenharmony_ci ASSERT_NE(ret, 0); 293d9f0492fSopenharmony_ci ret = RemoveParameterWatcher(nullptr, nullptr, nullptr); 294d9f0492fSopenharmony_ci ASSERT_NE(ret, 0); 295d9f0492fSopenharmony_ci} 296d9f0492fSopenharmony_ci 297d9f0492fSopenharmony_ciHWTEST_F(WatcherAgentUnitTest, Init_TestWatcherProxy_001, TestSize.Level0) 298d9f0492fSopenharmony_ci{ 299d9f0492fSopenharmony_ci WatcherAgentUnitTest test; 300d9f0492fSopenharmony_ci test.TestWatcherProxy(); 301d9f0492fSopenharmony_ci} 302