1 /*
2  * Copyright (c) 2022 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 
16 #include "bootchart.h"
17 #include "bootstage.h"
18 #include "init_utils.h"
19 #include "param_stub.h"
20 #include "securec.h"
21 
22 using namespace std;
23 using namespace testing::ext;
24 
25 namespace init_ut {
26 extern "C" {
27 long long GetJiffies(void);
28 char *ReadFileToBuffer(const char *fileName, char *buffer, uint32_t bufferSize);
29 void BootchartLogHeader(void);
30 void BootchartLogFile(FILE *log, const char *procfile);
31 void BootchartLogProcessStat(FILE *log, pid_t pid);
32 void bootchartLogProcess(FILE *log);
33 void *BootchartThreadMain(void *data);
34 void BootchartDestory(void);
35 int DoBootchartStart(void);
36 int DoBootchartStop(void);
37 int DoBootchartCmd(int id, const char *name, int argc, const char **argv);
38 int BootchartInit(void);
39 void BootchartExit(void);
40 }
41 
42 class ModulesUnitTest : public testing::Test {
43 public:
SetUpTestCase(void)44     static void SetUpTestCase(void) {};
TearDownTestCase(void)45     static void TearDownTestCase(void) {};
SetUp()46     void SetUp() {};
TearDown()47     void TearDown() {};
48 };
49 
50 HWTEST_F(ModulesUnitTest, TestBootchartInit, TestSize.Level1)
51 {
52     EXPECT_EQ(BootchartInit(), 0);
53     EXPECT_NE(GetJiffies(), -1);
54     EXPECT_NE(DoBootchartStart(), 1);
55     EXPECT_EQ(DoBootchartStop(), 0);
56     BootchartExit();
57 }
58 
59 HWTEST_F(ModulesUnitTest, TestReadFileToBuffer, TestSize.Level1)
60 {
61     const char *fileName = "ModulesTest";
62     char buffer[MAX_BUFFER_LEN] = {0};
63     EXPECT_EQ(ReadFileToBuffer(fileName, buffer, MAX_BUFFER_LEN), nullptr);
64     buffer[1] = 'a';
65     EXPECT_EQ(ReadFileToBuffer(nullptr, buffer, MAX_BUFFER_LEN), nullptr);
66     EXPECT_EQ(ReadFileToBuffer(nullptr, nullptr, MAX_BUFFER_LEN), nullptr);
67 }
68 
69 HWTEST_F(ModulesUnitTest, TestBootchartLogFile, TestSize.Level1)
70 {
71     DoBootchartStart();
72     FILE *log = fopen("/data/init_ut/ModulesTest.log", "w");
73     if (log) {
74         BootchartLogFile(log, "/proc/stat");
75         (void)fflush(log);
76         (void)fclose(log);
77     }
78 }
79 
80 HWTEST_F(ModulesUnitTest, TestBootchartLogProcessStat, TestSize.Level1)
81 {
82     FILE *log = fopen("/data/init_ut/ModulesTest.log", "w");
83     pid_t selfPid = getpid();
84     if (log != nullptr) {
85         BootchartLogProcessStat(log, selfPid);
86         (void)fflush(log);
87         (void)fclose(log);
88     }
89 }
90 
91 HWTEST_F(ModulesUnitTest, TestbootchartLogProcess, TestSize.Level1)
92 {
93     FILE *log = fopen("/data/init_ut/ModulesTest.log", "w");
94     if (log) {
95         bootchartLogProcess(log);
96         (void)fflush(log);
97         (void)fclose(log);
98     }
99 }
100 
101 HWTEST_F(ModulesUnitTest, TestDoBootchartCmd, TestSize.Level1)
102 {
103     const char *argv1[] = { "start" };
104     const char *argv2[] = { "stop" };
105     EXPECT_NE(DoBootchartCmd(0, "bootchart", 1, argv1), 1);
106     EXPECT_NE(DoBootchartCmd(0, "bootchart", 1, argv2), 1);
107 }
108 
109 HWTEST_F(ModulesUnitTest, TestDoBootchartInsall, TestSize.Level1)
110 {
111     TestSetParamCheckResult("ohos.servicectrl.", 0777, 0);
112     SystemWriteParam("persist.init.bootchart.enabled", "1");
113     SystemWriteParam("persist.init.debug.dump.trigger", "1");
114     SystemWriteParam("persist.init.debug.loglevel", "6");
115     SystemWriteParam("ohos.servicectrl.cmd", "setloglevel 10");
116     HookMgrExecute(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, nullptr, nullptr);
117 }
118 } // namespace init_ut
119