• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 using namespace std;
29 using namespace testing::ext;
30 extern "C" {
31 extern void ReportBootEventComplete(ListNode *events);
32 }
33 
AddBootEvent(ListNode * events,const char * name,int32_t type)34 static 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 
BootEventDestroyProc(ListNode * node)52 static 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 
TestReportBootEventComplete(ListNode * events)64 static int TestReportBootEventComplete(ListNode *events)
65 {
66     ReportBootEventComplete(events);
67     return 0;
68 }
69 
TestReportSysEvent(const StartupEvent * event)70 static int TestReportSysEvent(const StartupEvent *event)
71 {
72     ReportSysEvent(event);
73     return 0;
74 }
75 
76 namespace init_ut {
77 class SysEventUnitTest : public testing::Test {
78 public:
SetUpTestCase(void)79     static void SetUpTestCase(void) {};
TearDownTestCase(void)80     static void TearDownTestCase(void) {};
SetUp()81     void SetUp() {};
TearDown()82     void TearDown() {};
83 };
84 
85 HWTEST_F(SysEventUnitTest, SysEventTest_001, TestSize.Level1)
86 {
87     EXPECT_EQ(TestReportBootEventComplete(nullptr), 0);
88 }
89 
90 HWTEST_F(SysEventUnitTest, SysEventTest_002, TestSize.Level1)
91 {
92     ListNode events = { &events, &events };
93     // empty event
94     EXPECT_EQ(TestReportBootEventComplete(&events), 0);
95 }
96 
97 HWTEST_F(SysEventUnitTest, SysEventTest_003, TestSize.Level1)
98 {
99     ListNode events = { &events, &events };
100     // create event and report
101     AddBootEvent(&events, "bootevent.11111.xxxx", BOOTEVENT_TYPE_JOB);
102     AddBootEvent(&events, "bootevent.22222222.xxxx", BOOTEVENT_TYPE_SERVICE);
103     AddBootEvent(&events, "bootevent.33333333333.xxxx", BOOTEVENT_TYPE_SERVICE);
104     AddBootEvent(&events, "bootevent.44444444444444", BOOTEVENT_TYPE_SERVICE);
105     AddBootEvent(&events, "bootevent.44444444444444.6666666666.777777", BOOTEVENT_TYPE_SERVICE);
106     SystemWriteParam("ohos.boot.bootreason", "-1");
107     EXPECT_EQ(TestReportBootEventComplete(&events), 0);
108     OH_ListRemoveAll(&events, BootEventDestroyProc);
109 }
110 
111 HWTEST_F(SysEventUnitTest, SysEventTest_004, TestSize.Level1)
112 {
113     struct timespec curr = {0};
114     if (clock_gettime(CLOCK_MONOTONIC, &curr) != 0) {
115         return;
116     }
117     StartupTimeEvent startupTime = {};
118     startupTime.event.type = STARTUP_TIME;
119     startupTime.totalTime = curr.tv_sec;
120     startupTime.totalTime = startupTime.totalTime * MSECTONSEC;
121     startupTime.totalTime += curr.tv_nsec / USTONSEC;
122     startupTime.detailTime = const_cast<char *>("buffer");
123     startupTime.reason = const_cast<char *>("");
124     startupTime.firstStart = 1;
125     EXPECT_EQ(TestReportSysEvent(&startupTime.event), 0);
126 }
127 
128 HWTEST_F(SysEventUnitTest, SysEventTest_005, TestSize.Level1)
129 {
130     struct timespec curr = {0};
131     if (clock_gettime(CLOCK_MONOTONIC, &curr) != 0) {
132         return;
133     }
134     StartupTimeEvent startupTime = {};
135     startupTime.event.type = STARTUP_EVENT_MAX;
136     startupTime.totalTime = curr.tv_sec;
137     startupTime.totalTime = startupTime.totalTime * MSECTONSEC;
138     startupTime.totalTime += curr.tv_nsec / USTONSEC;
139     startupTime.detailTime = const_cast<char *>("buffer");
140     startupTime.reason = const_cast<char *>("");
141     startupTime.firstStart = 1;
142     EXPECT_EQ(TestReportSysEvent(&startupTime.event), 0);
143 }
144 
145 HWTEST_F(SysEventUnitTest, SysEventTest_006, TestSize.Level1)
146 {
147     int ret = TestReportSysEvent(nullptr);
148     EXPECT_EQ(ret, 0);
149 }
150 
151 HWTEST_F(SysEventUnitTest, SysEventTest_007, TestSize.Level1)
152 {
153     const char *appfwkReady[] = {"bootevent.appfwk.ready"};
154     int ret = PluginExecCmd("unset_bootevent", 1, appfwkReady);
155     EXPECT_EQ(ret, 0);
156     printf("SysEventTest_007:%d\n", ret);
157 }
158 } // namespace init_ut
159