1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2022 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 16d9f0492fSopenharmony_ci#include <cerrno> 17d9f0492fSopenharmony_ci#include <cstdio> 18d9f0492fSopenharmony_ci#include <ctime> 19d9f0492fSopenharmony_ci#include <sys/types.h> 20d9f0492fSopenharmony_ci#include <sys/mount.h> 21d9f0492fSopenharmony_ci#include <sys/stat.h> 22d9f0492fSopenharmony_ci 23d9f0492fSopenharmony_ci#include "init.h" 24d9f0492fSopenharmony_ci#include "device.h" 25d9f0492fSopenharmony_ci#include "init_cmds.h" 26d9f0492fSopenharmony_ci#include "init_log.h" 27d9f0492fSopenharmony_ci#include "init_service.h" 28d9f0492fSopenharmony_ci#include "init_unittest.h" 29d9f0492fSopenharmony_ci#include "init_adapter.h" 30d9f0492fSopenharmony_ci#include "init_utils.h" 31d9f0492fSopenharmony_ci#include "loop_event.h" 32d9f0492fSopenharmony_ci#include "param_stub.h" 33d9f0492fSopenharmony_ci#include "fs_manager/fs_manager.h" 34d9f0492fSopenharmony_ci#include "fd_holder.h" 35d9f0492fSopenharmony_ci#include "fd_holder_service.h" 36d9f0492fSopenharmony_ci#include "bootstage.h" 37d9f0492fSopenharmony_ci#include "parameter.h" 38d9f0492fSopenharmony_ci 39d9f0492fSopenharmony_ciusing namespace testing::ext; 40d9f0492fSopenharmony_ciusing namespace std; 41d9f0492fSopenharmony_ci 42d9f0492fSopenharmony_ciextern "C" { 43d9f0492fSopenharmony_ciINIT_STATIC void ProcessSignal(const struct signalfd_siginfo *siginfo); 44d9f0492fSopenharmony_ci} 45d9f0492fSopenharmony_ci 46d9f0492fSopenharmony_cinamespace init_ut { 47d9f0492fSopenharmony_ciclass InitUnitTest : public testing::Test { 48d9f0492fSopenharmony_cipublic: 49d9f0492fSopenharmony_ci static void SetUpTestCase(void) {}; 50d9f0492fSopenharmony_ci static void TearDownTestCase(void) {}; 51d9f0492fSopenharmony_ci void SetUp() {}; 52d9f0492fSopenharmony_ci void TearDown() {}; 53d9f0492fSopenharmony_ci}; 54d9f0492fSopenharmony_ci 55d9f0492fSopenharmony_ciHWTEST_F(InitUnitTest, TestSignalHandle, TestSize.Level1) 56d9f0492fSopenharmony_ci{ 57d9f0492fSopenharmony_ci struct signalfd_siginfo siginfo; 58d9f0492fSopenharmony_ci siginfo.ssi_signo = SIGCHLD; 59d9f0492fSopenharmony_ci ProcessSignal(&siginfo); 60d9f0492fSopenharmony_ci siginfo.ssi_signo = SIGTERM; 61d9f0492fSopenharmony_ci ProcessSignal(&siginfo); 62d9f0492fSopenharmony_ci siginfo.ssi_signo = SIGUSR1; 63d9f0492fSopenharmony_ci ProcessSignal(&siginfo); 64d9f0492fSopenharmony_ci SUCCEED(); 65d9f0492fSopenharmony_ci} 66d9f0492fSopenharmony_ci 67d9f0492fSopenharmony_ciHWTEST_F(InitUnitTest, TestSystemPrepare, TestSize.Level1) 68d9f0492fSopenharmony_ci{ 69d9f0492fSopenharmony_ci SetStubResult(STUB_MOUNT, -1); 70d9f0492fSopenharmony_ci SetStubResult(STUB_MKNODE, -1); 71d9f0492fSopenharmony_ci CreateFsAndDeviceNode(); 72d9f0492fSopenharmony_ci 73d9f0492fSopenharmony_ci SetStubResult(STUB_MOUNT, 0); 74d9f0492fSopenharmony_ci SetStubResult(STUB_MKNODE, 0); 75d9f0492fSopenharmony_ci CreateFsAndDeviceNode(); 76d9f0492fSopenharmony_ci} 77d9f0492fSopenharmony_ci 78d9f0492fSopenharmony_ciHWTEST_F(InitUnitTest, TestSystemExecRcs, TestSize.Level1) 79d9f0492fSopenharmony_ci{ 80d9f0492fSopenharmony_ci SystemExecuteRcs(); 81d9f0492fSopenharmony_ci Service *service = GetServiceByName("param_watcher"); 82d9f0492fSopenharmony_ci int ret = KeepCapability(service); 83d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 84d9f0492fSopenharmony_ci ret = SetAmbientCapability(34); // CAP_SYSLOG 85d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 86d9f0492fSopenharmony_ci} 87d9f0492fSopenharmony_ci 88d9f0492fSopenharmony_cistatic void TestProcessTimer(const TimerHandle taskHandle, void *context) 89d9f0492fSopenharmony_ci{ 90d9f0492fSopenharmony_ci static int count = 0; 91d9f0492fSopenharmony_ci printf("ProcessTimer %d\n", count); 92d9f0492fSopenharmony_ci if (count == 0) { // 2 stop 93d9f0492fSopenharmony_ci // set service pid for test 94d9f0492fSopenharmony_ci Service *service = GetServiceByName("param_watcher"); 95d9f0492fSopenharmony_ci if (service != nullptr) { 96d9f0492fSopenharmony_ci service->pid = getpid(); 97d9f0492fSopenharmony_ci } 98d9f0492fSopenharmony_ci int fds1[] = {1, 0}; 99d9f0492fSopenharmony_ci ServiceSaveFd("param_watcher", fds1, ARRAY_LENGTH(fds1)); 100d9f0492fSopenharmony_ci ServiceSaveFdWithPoll("param_watcher", fds1, 0); 101d9f0492fSopenharmony_ci ServiceSaveFdWithPoll("param_watcher", fds1, ARRAY_LENGTH(fds1)); 102d9f0492fSopenharmony_ci EXPECT_EQ(setenv("OHOS_FD_HOLD_param_watcher", "1 0", 0), 0); 103d9f0492fSopenharmony_ci 104d9f0492fSopenharmony_ci size_t fdCount = 0; 105d9f0492fSopenharmony_ci int *fds = ServiceGetFd("param_watcher", &fdCount); 106d9f0492fSopenharmony_ci EXPECT_TRUE(fds != nullptr); 107d9f0492fSopenharmony_ci free(fds); 108d9f0492fSopenharmony_ci 109d9f0492fSopenharmony_ci ServiceSaveFd("testservice", fds1, ARRAY_LENGTH(fds1)); 110d9f0492fSopenharmony_ci ServiceSaveFd("deviceinfoservice", fds1, ARRAY_LENGTH(fds1)); 111d9f0492fSopenharmony_ci } 112d9f0492fSopenharmony_ci if (count == 1) { 113d9f0492fSopenharmony_ci LE_StopTimer(LE_GetDefaultLoop(), taskHandle); 114d9f0492fSopenharmony_ci LE_StopLoop(LE_GetDefaultLoop()); 115d9f0492fSopenharmony_ci } 116d9f0492fSopenharmony_ci count++; 117d9f0492fSopenharmony_ci} 118d9f0492fSopenharmony_ci 119d9f0492fSopenharmony_ciHWTEST_F(InitUnitTest, TestFdHoldService, TestSize.Level1) 120d9f0492fSopenharmony_ci{ 121d9f0492fSopenharmony_ci RegisterFdHoldWatcher(-1); 122d9f0492fSopenharmony_ci TimerHandle timer = nullptr; 123d9f0492fSopenharmony_ci int ret = LE_CreateTimer(LE_GetDefaultLoop(), &timer, TestProcessTimer, nullptr); 124d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 125d9f0492fSopenharmony_ci ret = LE_StartTimer(LE_GetDefaultLoop(), timer, 500, 4); 126d9f0492fSopenharmony_ci EXPECT_EQ(ret, 0); 127d9f0492fSopenharmony_ci SystemRun(); 128d9f0492fSopenharmony_ci} 129d9f0492fSopenharmony_ci 130d9f0492fSopenharmony_ciHWTEST_F(InitUnitTest, TestInitLog, TestSize.Level1) 131d9f0492fSopenharmony_ci{ 132d9f0492fSopenharmony_ci // test log 133d9f0492fSopenharmony_ci CheckAndCreateDir(INIT_LOG_PATH); 134d9f0492fSopenharmony_ci SetInitLogLevel(INIT_DEBUG); 135d9f0492fSopenharmony_ci INIT_LOGI("TestInitLog"); 136d9f0492fSopenharmony_ci INIT_LOGV("TestInitLog"); 137d9f0492fSopenharmony_ci INIT_LOGE("TestInitLog"); 138d9f0492fSopenharmony_ci INIT_LOGW("TestInitLog"); 139d9f0492fSopenharmony_ci INIT_LOGF("TestInitLog"); 140d9f0492fSopenharmony_ci // restore log level 141d9f0492fSopenharmony_ci int32_t loglevel = GetIntParameter("persist.init.debug.loglevel", INIT_ERROR); 142d9f0492fSopenharmony_ci SetInitLogLevel((InitLogLevel)loglevel); 143d9f0492fSopenharmony_ci} 144d9f0492fSopenharmony_ci} 145