1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2023 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 <cinttypes> 16d9f0492fSopenharmony_ci#include <ctime> 17d9f0492fSopenharmony_ci#include <sys/time.h> 18d9f0492fSopenharmony_ci 19d9f0492fSopenharmony_ci#include "bootevent.h" 20d9f0492fSopenharmony_ci#include "init_param.h" 21d9f0492fSopenharmony_ci#include "init_utils.h" 22d9f0492fSopenharmony_ci#include "list.h" 23d9f0492fSopenharmony_ci#include "param_stub.h" 24d9f0492fSopenharmony_ci#include "securec.h" 25d9f0492fSopenharmony_ci#include "sys_event.h" 26d9f0492fSopenharmony_ci#include "init_cmdexecutor.h" 27d9f0492fSopenharmony_ci 28d9f0492fSopenharmony_ciusing namespace std; 29d9f0492fSopenharmony_ciusing namespace testing::ext; 30d9f0492fSopenharmony_ciextern "C" { 31d9f0492fSopenharmony_ciextern void ReportBootEventComplete(ListNode *events); 32d9f0492fSopenharmony_ci} 33d9f0492fSopenharmony_ci 34d9f0492fSopenharmony_cistatic void AddBootEvent(ListNode *events, const char *name, int32_t type) 35d9f0492fSopenharmony_ci{ 36d9f0492fSopenharmony_ci BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)calloc(1, sizeof(BOOT_EVENT_PARAM_ITEM)); 37d9f0492fSopenharmony_ci if (item == nullptr) { 38d9f0492fSopenharmony_ci return; 39d9f0492fSopenharmony_ci } 40d9f0492fSopenharmony_ci OH_ListInit(&item->node); 41d9f0492fSopenharmony_ci item->paramName = strdup(name); 42d9f0492fSopenharmony_ci if (item->paramName == nullptr) { 43d9f0492fSopenharmony_ci free(item); 44d9f0492fSopenharmony_ci return; 45d9f0492fSopenharmony_ci } 46d9f0492fSopenharmony_ci (void)clock_gettime(CLOCK_MONOTONIC, &(item->timestamp[BOOTEVENT_FORK])); 47d9f0492fSopenharmony_ci (void)clock_gettime(CLOCK_MONOTONIC, &(item->timestamp[BOOTEVENT_READY])); 48d9f0492fSopenharmony_ci item->flags = type; 49d9f0492fSopenharmony_ci OH_ListAddTail(events, (ListNode *)&item->node); 50d9f0492fSopenharmony_ci} 51d9f0492fSopenharmony_ci 52d9f0492fSopenharmony_cistatic void BootEventDestroyProc(ListNode *node) 53d9f0492fSopenharmony_ci{ 54d9f0492fSopenharmony_ci if (node == nullptr) { 55d9f0492fSopenharmony_ci return; 56d9f0492fSopenharmony_ci } 57d9f0492fSopenharmony_ci BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)node; 58d9f0492fSopenharmony_ci OH_ListRemove(node); 59d9f0492fSopenharmony_ci OH_ListInit(node); 60d9f0492fSopenharmony_ci free(item->paramName); 61d9f0492fSopenharmony_ci free(item); 62d9f0492fSopenharmony_ci} 63d9f0492fSopenharmony_ci 64d9f0492fSopenharmony_cinamespace init_ut { 65d9f0492fSopenharmony_ciclass SysEventUnitTest : public testing::Test { 66d9f0492fSopenharmony_cipublic: 67d9f0492fSopenharmony_ci static void SetUpTestCase(void) {}; 68d9f0492fSopenharmony_ci static void TearDownTestCase(void) {}; 69d9f0492fSopenharmony_ci void SetUp() {}; 70d9f0492fSopenharmony_ci void TearDown() {}; 71d9f0492fSopenharmony_ci}; 72d9f0492fSopenharmony_ci 73d9f0492fSopenharmony_ciHWTEST_F(SysEventUnitTest, SysEventTest_001, TestSize.Level1) 74d9f0492fSopenharmony_ci{ 75d9f0492fSopenharmony_ci ReportBootEventComplete(nullptr); 76d9f0492fSopenharmony_ci} 77d9f0492fSopenharmony_ci 78d9f0492fSopenharmony_ciHWTEST_F(SysEventUnitTest, SysEventTest_002, TestSize.Level1) 79d9f0492fSopenharmony_ci{ 80d9f0492fSopenharmony_ci ListNode events = { &events, &events }; 81d9f0492fSopenharmony_ci // empty event 82d9f0492fSopenharmony_ci ReportBootEventComplete(&events); 83d9f0492fSopenharmony_ci} 84d9f0492fSopenharmony_ci 85d9f0492fSopenharmony_ciHWTEST_F(SysEventUnitTest, SysEventTest_003, TestSize.Level1) 86d9f0492fSopenharmony_ci{ 87d9f0492fSopenharmony_ci ListNode events = { &events, &events }; 88d9f0492fSopenharmony_ci // create event and report 89d9f0492fSopenharmony_ci AddBootEvent(&events, "bootevent.11111.xxxx", BOOTEVENT_TYPE_JOB); 90d9f0492fSopenharmony_ci AddBootEvent(&events, "bootevent.22222222.xxxx", BOOTEVENT_TYPE_SERVICE); 91d9f0492fSopenharmony_ci AddBootEvent(&events, "bootevent.33333333333.xxxx", BOOTEVENT_TYPE_SERVICE); 92d9f0492fSopenharmony_ci AddBootEvent(&events, "bootevent.44444444444444", BOOTEVENT_TYPE_SERVICE); 93d9f0492fSopenharmony_ci AddBootEvent(&events, "bootevent.44444444444444.6666666666.777777", BOOTEVENT_TYPE_SERVICE); 94d9f0492fSopenharmony_ci SystemWriteParam("ohos.boot.bootreason", "-1"); 95d9f0492fSopenharmony_ci ReportBootEventComplete(&events); 96d9f0492fSopenharmony_ci OH_ListRemoveAll(&events, BootEventDestroyProc); 97d9f0492fSopenharmony_ci} 98d9f0492fSopenharmony_ci 99d9f0492fSopenharmony_ciHWTEST_F(SysEventUnitTest, SysEventTest_004, TestSize.Level1) 100d9f0492fSopenharmony_ci{ 101d9f0492fSopenharmony_ci struct timespec curr = {0}; 102d9f0492fSopenharmony_ci if (clock_gettime(CLOCK_MONOTONIC, &curr) != 0) { 103d9f0492fSopenharmony_ci return; 104d9f0492fSopenharmony_ci } 105d9f0492fSopenharmony_ci StartupTimeEvent startupTime = {}; 106d9f0492fSopenharmony_ci startupTime.event.type = STARTUP_TIME; 107d9f0492fSopenharmony_ci startupTime.totalTime = curr.tv_sec; 108d9f0492fSopenharmony_ci startupTime.totalTime = startupTime.totalTime * MSECTONSEC; 109d9f0492fSopenharmony_ci startupTime.totalTime += curr.tv_nsec / USTONSEC; 110d9f0492fSopenharmony_ci startupTime.detailTime = const_cast<char *>("buffer"); 111d9f0492fSopenharmony_ci startupTime.reason = const_cast<char *>(""); 112d9f0492fSopenharmony_ci startupTime.firstStart = 1; 113d9f0492fSopenharmony_ci ReportSysEvent(&startupTime.event); 114d9f0492fSopenharmony_ci} 115d9f0492fSopenharmony_ci 116d9f0492fSopenharmony_ciHWTEST_F(SysEventUnitTest, SysEventTest_005, TestSize.Level1) 117d9f0492fSopenharmony_ci{ 118d9f0492fSopenharmony_ci struct timespec curr = {0}; 119d9f0492fSopenharmony_ci if (clock_gettime(CLOCK_MONOTONIC, &curr) != 0) { 120d9f0492fSopenharmony_ci return; 121d9f0492fSopenharmony_ci } 122d9f0492fSopenharmony_ci StartupTimeEvent startupTime = {}; 123d9f0492fSopenharmony_ci startupTime.event.type = STARTUP_EVENT_MAX; 124d9f0492fSopenharmony_ci startupTime.totalTime = curr.tv_sec; 125d9f0492fSopenharmony_ci startupTime.totalTime = startupTime.totalTime * MSECTONSEC; 126d9f0492fSopenharmony_ci startupTime.totalTime += curr.tv_nsec / USTONSEC; 127d9f0492fSopenharmony_ci startupTime.detailTime = const_cast<char *>("buffer"); 128d9f0492fSopenharmony_ci startupTime.reason = const_cast<char *>(""); 129d9f0492fSopenharmony_ci startupTime.firstStart = 1; 130d9f0492fSopenharmony_ci ReportSysEvent(&startupTime.event); 131d9f0492fSopenharmony_ci} 132d9f0492fSopenharmony_ci 133d9f0492fSopenharmony_ciHWTEST_F(SysEventUnitTest, SysEventTest_006, TestSize.Level1) 134d9f0492fSopenharmony_ci{ 135d9f0492fSopenharmony_ci ReportSysEvent(nullptr); 136d9f0492fSopenharmony_ci} 137d9f0492fSopenharmony_ci 138d9f0492fSopenharmony_ciHWTEST_F(SysEventUnitTest, SysEventTest_007, TestSize.Level1) 139d9f0492fSopenharmony_ci{ 140d9f0492fSopenharmony_ci const char *appfwkReady[] = {"bootevent.appfwk.ready"}; 141d9f0492fSopenharmony_ci int ret = PluginExecCmd("unset_bootevent", 1, appfwkReady); 142d9f0492fSopenharmony_ci printf("SysEventTest_007:%d\n", ret); 143d9f0492fSopenharmony_ci} 144d9f0492fSopenharmony_ci} // namespace init_ut 145