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 #include "reboot_adp.h" 26 27 using namespace testing::ext; 28 using namespace std; 29 30 namespace init_ut { 31 class InitRebootUnitTest : public testing::Test { 32 public: SetUpTestCase(void)33 static void SetUpTestCase(void) {}; TearDownTestCase(void)34 static void TearDownTestCase(void) {}; SetUp()35 void SetUp() {}; TearDown()36 void TearDown() {}; 37 }; 38 39 #ifndef OHOS_LITE 40 static int g_result = 0; 41 HWTEST_F(InitRebootUnitTest, TestAddRebootCmdExt, TestSize.Level1) 42 { __anon80cb68890102(int id, const char *name, int argc, const char **argv) 43 auto rebootCallback = [](int id, const char *name, int argc, const char **argv) -> int { 44 return 0; 45 }; 46 int ret = AddRebootCmdExecutor("reboot_cmd1", rebootCallback); 47 EXPECT_EQ(ret, 0); 48 ret = AddRebootCmdExecutor("reboot_cmd2", rebootCallback); 49 EXPECT_EQ(ret, 0); 50 ret = AddRebootCmdExecutor("reboot_cmd3", rebootCallback); 51 EXPECT_EQ(ret, 0); __anon80cb68890202(int id, const char *name, int argc, const char **argv)52 ret = AddRebootCmdExecutor("reboot_cmd4", [](int id, const char *name, int argc, const char **argv)-> int { 53 g_result = 4; // 4 test index 54 return 0; 55 }); 56 EXPECT_EQ(ret, 0); __anon80cb68890302(int id, const char *name, int argc, const char **argv)57 ret = AddRebootCmdExecutor("reboot_cmd5", [](int id, const char *name, int argc, const char **argv)-> int { 58 g_result = 5; // 5 test index 59 return 0; 60 }); 61 EXPECT_EQ(ret, 0); __anon80cb68890402(int id, const char *name, int argc, const char **argv)62 ret = AddRebootCmdExecutor("reboot_cmd6", [](int id, const char *name, int argc, const char **argv)-> int { 63 g_result = 6; // 6 test index 64 return 0; 65 }); 66 EXPECT_EQ(ret, 0); 67 ret = AddRebootCmdExecutor("reboot_cmd7", rebootCallback); 68 EXPECT_EQ(ret, 0); 69 ret = AddRebootCmdExecutor("reboot_cmd7", rebootCallback); 70 EXPECT_NE(ret, 0); 71 72 TestSetParamCheckResult("ohos.servicectrl.reboot", 0777, 0); 73 // exec 74 SystemWriteParam("ohos.startup.powerctrl", "reboot,reboot_cmd4"); 75 EXPECT_EQ(g_result, 4); // 4 test index 76 SystemWriteParam("ohos.startup.powerctrl", "reboot,reboot_cmd5"); 77 EXPECT_EQ(g_result, 5); // 5 test index 78 SystemWriteParam("ohos.startup.powerctrl", "reboot,reboot_cmd6"); 79 EXPECT_EQ(g_result, 6); // 6 test index 80 81 // invalid test __anon80cb68890502(int id, const char *name, int argc, const char **argv)82 ret = AddRebootCmdExecutor(nullptr, [](int id, const char *name, int argc, const char **argv)-> int { 83 printf("reboot_cmd7 %s", name); 84 return 0; 85 }); 86 EXPECT_NE(ret, 0); 87 ret = AddRebootCmdExecutor(nullptr, nullptr); 88 EXPECT_NE(ret, 0); 89 } 90 91 HWTEST_F(InitRebootUnitTest, TestAddRebootCmdNormal, TestSize.Level1) 92 { 93 int ret = SystemWriteParam("ohos.startup.powerctrl", "reboot"); 94 EXPECT_EQ(ret, 0); 95 ret = SystemWriteParam("ohos.startup.powerctrl", "reboot,shutdown"); 96 EXPECT_EQ(ret, 0); 97 ret = SystemWriteParam("ohos.startup.powerctrl", "reboot,suspend"); 98 EXPECT_EQ(ret, 0); 99 ret = SystemWriteParam("ohos.startup.powerctrl", "reboot,charge"); 100 EXPECT_EQ(ret, 0); 101 ret = SystemWriteParam("ohos.startup.powerctrl", "reboot,updater"); 102 EXPECT_EQ(ret, 0); 103 ret = SystemWriteParam("ohos.startup.powerctrl", "reboot,updater:2222222"); 104 EXPECT_EQ(ret, 0); 105 ret = SystemWriteParam("ohos.startup.powerctrl", "reboot,flashd"); 106 EXPECT_EQ(ret, 0); 107 ret = SystemWriteParam("ohos.startup.powerctrl", "reboot,flashd:1000000"); 108 EXPECT_EQ(ret, 0); 109 } 110 111 HWTEST_F(InitRebootUnitTest, TestRebootCmdExec, TestSize.Level1) 112 { 113 PARAM_LOGE("TestRebootCmdExec"); 114 PluginExecCmdByName("reboot", "reboot"); 115 PluginExecCmdByName("reboot.shutdown", "reboot,shutdown"); 116 PluginExecCmdByName("reboot.shutdown", "reboot,shutdown:333333333333"); 117 PluginExecCmdByName("reboot.suspend", "reboot,suspend"); 118 PluginExecCmdByName("reboot.charge", "reboot,charge"); 119 PluginExecCmdByName("reboot.updater", "reboot,updater"); 120 PluginExecCmdByName("reboot.updater", "reboot,updater:2222222"); 121 PluginExecCmdByName("reboot.flashd", "reboot,flashd"); 122 PluginExecCmdByName("reboot.flashd", "reboot,flashd:1000000"); 123 PluginExecCmdByName("reboot.panic", "reboot,panic"); 124 PluginExecCmdByName("reboot.stop", "reboot,stop"); 125 PluginExecCmdByName("reboot.other", "reboot,other"); 126 PARAM_LOGE("TestRebootCmdExec end"); 127 int ret = UpdateMiscMessage("charge:wwwwwwwwwww", "charge", "charge:", "boot_charge"); 128 if (ret == 0) { 129 ret = GetBootModeFromMisc(); 130 EXPECT_EQ(ret, GROUP_CHARGE); 131 clearMisc(); 132 } 133 } 134 #endif 135 136 HWTEST_F(InitRebootUnitTest, TestInitReboot, TestSize.Level1) 137 { 138 ExecReboot("reboot"); 139 ExecReboot("reboot,shutdown"); 140 ExecReboot("reboot,bootloader"); 141 ExecReboot("reboot,updater:123"); 142 ExecReboot("reboot,flash:123"); 143 ExecReboot("reboot,flashd:123"); 144 ExecReboot("reboot,suspend:123"); 145 const char *option = nullptr; 146 int ret = DoReboot(option); 147 EXPECT_EQ(ret, 0); 148 option = "updater"; 149 ret = DoReboot(option); 150 EXPECT_EQ(ret, 0); 151 ret = DoReboot(DEVICE_CMD_SUSPEND); 152 EXPECT_EQ(ret, 0); 153 ret = DoReboot(DEVICE_CMD_FREEZE); 154 EXPECT_EQ(ret, 0); 155 156 ret = DoRebootExt("shutdown", DEVICE_CMD_FREEZE); 157 EXPECT_EQ(ret, 0); 158 } 159 160 HWTEST_F(InitRebootUnitTest, TestAbnormalReboot, TestSize.Level1) 161 { 162 int ret = DoRoot_(nullptr, 0); 163 EXPECT_EQ(ret, 0); 164 165 ret = DoRebootShutdown(0, nullptr, 0, nullptr); 166 EXPECT_EQ(ret, -1); 167 168 ret = DoRebootFlashed(0, nullptr, 0, nullptr); 169 EXPECT_EQ(ret, -1); 170 171 ret = DoRebootOther(0, nullptr, 0, nullptr); 172 EXPECT_EQ(ret, -1); 173 } 174 } // namespace init_ut 175