• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <sys/statvfs.h>
17 #include "init_cmds.h"
18 #include "init_reboot.h"
19 #include "init_param.h"
20 #include "param_stub.h"
21 #include "init_utils.h"
22 #include "trigger_manager.h"
23 #include "init_group_manager.h"
24 #include "init_cmdexecutor.h"
25 
26 using namespace testing::ext;
27 using namespace std;
28 
29 namespace init_ut {
30 class InitRebootUnitTest : public testing::Test {
31 public:
SetUpTestCase(void)32     static void SetUpTestCase(void) {};
TearDownTestCase(void)33     static void TearDownTestCase(void) {};
SetUp()34     void SetUp() {};
TearDown()35     void TearDown() {};
36 };
37 
38 #ifndef OHOS_LITE
39 static int g_result = 0;
40 HWTEST_F(InitRebootUnitTest, TestAddRebootCmd, TestSize.Level1)
41 {
__anon72bf07080102(int id, const char *name, int argc, const char **argv) 42     auto rebootCallback = [](int id, const char *name, int argc, const char **argv) -> int {
43         return 0;
44     };
45     int ret = AddRebootCmdExecutor("reboot_cmd1", rebootCallback);
46     EXPECT_EQ(ret, 0);
47     ret = AddRebootCmdExecutor("reboot_cmd2", rebootCallback);
48     EXPECT_EQ(ret, 0);
49     ret = AddRebootCmdExecutor("reboot_cmd3", rebootCallback);
50     EXPECT_EQ(ret, 0);
__anon72bf07080202(int id, const char *name, int argc, const char **argv)51     ret = AddRebootCmdExecutor("reboot_cmd4", [](int id, const char *name, int argc, const char **argv)-> int {
52         g_result = 4; // 4 test index
53         return 0;
54     });
55     EXPECT_EQ(ret, 0);
__anon72bf07080302(int id, const char *name, int argc, const char **argv)56     ret = AddRebootCmdExecutor("reboot_cmd5", [](int id, const char *name, int argc, const char **argv)-> int {
57         g_result = 5; // 5 test index
58         return 0;
59     });
60     EXPECT_EQ(ret, 0);
__anon72bf07080402(int id, const char *name, int argc, const char **argv)61     ret = AddRebootCmdExecutor("reboot_cmd6", [](int id, const char *name, int argc, const char **argv)-> int {
62         g_result = 6; // 6 test index
63         return 0;
64     });
65     EXPECT_EQ(ret, 0);
66     ret = AddRebootCmdExecutor("reboot_cmd7", rebootCallback);
67     EXPECT_EQ(ret, 0);
68     ret = AddRebootCmdExecutor("reboot_cmd7", rebootCallback);
69     EXPECT_NE(ret, 0);
70 
71     TestSetParamCheckResult("ohos.servicectrl.reboot", 0777, 0);
72     // exec
73     SystemWriteParam("ohos.startup.powerctrl", "reboot,reboot_cmd4");
74     EXPECT_EQ(g_result, 4); // 4 test index
75     SystemWriteParam("ohos.startup.powerctrl", "reboot,reboot_cmd5");
76     EXPECT_EQ(g_result, 5); // 5 test index
77     SystemWriteParam("ohos.startup.powerctrl", "reboot,reboot_cmd6");
78     EXPECT_EQ(g_result, 6); // 6 test index
79 
80     // invalid test
__anon72bf07080502(int id, const char *name, int argc, const char **argv)81     ret = AddRebootCmdExecutor(nullptr, [](int id, const char *name, int argc, const char **argv)-> int {
82         printf("reboot_cmd7 %s", name);
83         return 0;
84     });
85     EXPECT_NE(ret, 0);
86     ret = AddRebootCmdExecutor(nullptr, nullptr);
87     EXPECT_NE(ret, 0);
88 }
89 #endif
90 
91 HWTEST_F(InitRebootUnitTest, TestInitReboot, TestSize.Level1)
92 {
93     ExecReboot("reboot");
94     ExecReboot("reboot,shutdown");
95     ExecReboot("reboot,bootloader");
96     ExecReboot("reboot,updater:123");
97     ExecReboot("reboot,flash:123");
98     ExecReboot("reboot,flashd:123");
99     ExecReboot("reboot,suspend:123");
100     const char *option = nullptr;
101     int ret = DoReboot(option);
102     EXPECT_EQ(ret, 0);
103     option = "updater";
104     ret = DoReboot(option);
105     EXPECT_EQ(ret, 0);
106     ret = DoReboot(DEVICE_CMD_SUSPEND);
107     EXPECT_EQ(ret, 0);
108     ret = DoReboot(DEVICE_CMD_FREEZE);
109     EXPECT_EQ(ret, 0);
110     clearMisc();
111 }
112 } // namespace init_ut
113