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
BootchartLogFileTest(void)50 static int BootchartLogFileTest(void)
51 {
52 DoBootchartStart();
53 FILE *log = fopen("/data/init_ut/ModulesTest.log", "w");
54 if (log) {
55 BootchartLogFile(log, "/proc/stat");
56 (void)fflush(log);
57 (void)fclose(log);
58 }
59 return 0;
60 }
61
BootchartLogProcessStatTest(void)62 static int BootchartLogProcessStatTest(void)
63 {
64 FILE *log = fopen("/data/init_ut/ModulesTest.log", "w");
65 pid_t selfPid = getpid();
66 if (log != nullptr) {
67 BootchartLogProcessStat(log, selfPid);
68 (void)fflush(log);
69 (void)fclose(log);
70 }
71 return 0;
72 }
73
BootchartLogProcessTest(void)74 static int BootchartLogProcessTest(void)
75 {
76 FILE *log = fopen("/data/init_ut/ModulesTest.log", "w");
77 if (log) {
78 bootchartLogProcess(log);
79 (void)fflush(log);
80 (void)fclose(log);
81 }
82 return 0;
83 }
84
85 HWTEST_F(ModulesUnitTest, TestBootchartInit, TestSize.Level1)
86 {
87 EXPECT_EQ(BootchartInit(), 0);
88 EXPECT_NE(GetJiffies(), -1);
89 EXPECT_NE(DoBootchartStart(), 1);
90 EXPECT_EQ(DoBootchartStop(), 0);
91 BootchartExit();
92 }
93
94 HWTEST_F(ModulesUnitTest, TestReadFileToBuffer, TestSize.Level1)
95 {
96 const char *fileName = "ModulesTest";
97 char buffer[MAX_BUFFER_LEN] = {0};
98 EXPECT_EQ(ReadFileToBuffer(fileName, buffer, MAX_BUFFER_LEN), nullptr);
99 buffer[1] = 'a';
100 EXPECT_EQ(ReadFileToBuffer(nullptr, buffer, MAX_BUFFER_LEN), nullptr);
101 EXPECT_EQ(ReadFileToBuffer(nullptr, nullptr, MAX_BUFFER_LEN), nullptr);
102 }
103
104 HWTEST_F(ModulesUnitTest, TestBootchartLogFile, TestSize.Level1)
105 {
106 EXPECT_EQ(BootchartLogFileTest(), 0);
107 }
108
109 HWTEST_F(ModulesUnitTest, TestBootchartLogProcessStat, TestSize.Level1)
110 {
111 EXPECT_EQ(BootchartLogProcessStatTest(), 0);
112 }
113
114 HWTEST_F(ModulesUnitTest, TestbootchartLogProcess, TestSize.Level1)
115 {
116 EXPECT_EQ(BootchartLogProcessTest(), 0);
117 }
118
119 HWTEST_F(ModulesUnitTest, TestDoBootchartCmd, TestSize.Level1)
120 {
121 const char *argv1[] = { "start" };
122 const char *argv2[] = { "stop" };
123 EXPECT_NE(DoBootchartCmd(0, "bootchart", 1, argv1), 1);
124 EXPECT_NE(DoBootchartCmd(0, "bootchart", 1, argv2), 1);
125 }
126
127 HWTEST_F(ModulesUnitTest, TestDoBootchartInsall, TestSize.Level1)
128 {
129 TestSetParamCheckResult("ohos.servicectrl.", 0777, 0);
130 int ret = SystemWriteParam("persist.init.bootchart.enabled", "1");
131 EXPECT_EQ(ret, 0);
132 ret = SystemWriteParam("persist.init.debug.dump.trigger", "1");
133 EXPECT_EQ(ret, 0);
134 ret = SystemWriteParam("persist.init.debug.loglevel", "6");
135 EXPECT_EQ(ret, 0);
136 ret = SystemWriteParam("ohos.servicectrl.cmd", "setloglevel 10");
137 EXPECT_EQ(ret, 0);
138 HookMgrExecute(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, nullptr, nullptr);
139 }
140 } // namespace init_ut
141