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