• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
65 
66 HWTEST_F(ModulesUnitTest, TestBootchartLogFile, TestSize.Level1)
67 {
68     DoBootchartStart();
69     FILE *log = fopen("/data/init_ut/ModulesTest.log", "w");
70     BootchartLogFile(log, "/proc/stat");
71     if (log != NULL) {
72         (void)fflush(log);
73         (void)fclose(log);
74     }
75 }
76 
77 HWTEST_F(ModulesUnitTest, TestBootchartLogProcessStat, TestSize.Level1)
78 {
79     FILE *log = fopen("/data/init_ut/ModulesTest.log", "w");
80     pid_t selfPid = getpid();
81     BootchartLogProcessStat(log, selfPid);
82     if (log != NULL) {
83         (void)fflush(log);
84         (void)fclose(log);
85     }
86 }
87 
88 HWTEST_F(ModulesUnitTest, TestbootchartLogProcess, TestSize.Level1)
89 {
90     FILE *log = fopen("/data/init_ut/ModulesTest.log", "w");
91     bootchartLogProcess(log);
92     if (log != NULL) {
93         (void)fflush(log);
94         (void)fclose(log);
95     }
96 }
97 
98 HWTEST_F(ModulesUnitTest, TestDoBootchartCmd, TestSize.Level1)
99 {
100     const char *argv1[] = { "start" };
101     const char *argv2[] = { "stop" };
102     EXPECT_NE(DoBootchartCmd(0, "bootchart", 1, argv1), 1);
103     EXPECT_NE(DoBootchartCmd(0, "bootchart", 1, argv2), 1);
104 }
105 
106 HWTEST_F(ModulesUnitTest, TestDoBootchartInsall, TestSize.Level1)
107 {
108     TestSetParamCheckResult("ohos.servicectrl.", 0777, 0);
109     SystemWriteParam("persist.init.bootchart.enabled", "1");
110     SystemWriteParam("persist.init.debug.dump.trigger", "1");
111     SystemWriteParam("persist.init.debug.loglevel", "6");
112     SystemWriteParam("ohos.servicectrl.cmd", "setloglevel 10");
113     HookMgrExecute(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, NULL, NULL);
114 }
115 } // namespace init_ut
116