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