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 { __anon8e31d35f0102(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); __anon8e31d35f0202(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); __anon8e31d35f0302(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); __anon8e31d35f0402(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 __anon8e31d35f0502(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 SystemWriteParam("ohos.startup.powerctrl", "reboot"); 94 SystemWriteParam("ohos.startup.powerctrl", "reboot,shutdown"); 95 SystemWriteParam("ohos.startup.powerctrl", "reboot,suspend"); 96 SystemWriteParam("ohos.startup.powerctrl", "reboot,charge"); 97 SystemWriteParam("ohos.startup.powerctrl", "reboot,updater"); 98 SystemWriteParam("ohos.startup.powerctrl", "reboot,updater:2222222"); 99 SystemWriteParam("ohos.startup.powerctrl", "reboot,flashd"); 100 SystemWriteParam("ohos.startup.powerctrl", "reboot,flashd:1000000"); 101 } 102 103 HWTEST_F(InitRebootUnitTest, TestRebootCmdExec, TestSize.Level1) 104 { 105 PARAM_LOGE("TestRebootCmdExec"); 106 PluginExecCmdByName("reboot", "reboot"); 107 PluginExecCmdByName("reboot.shutdown", "reboot,shutdown"); 108 PluginExecCmdByName("reboot.shutdown", "reboot,shutdown:333333333333"); 109 PluginExecCmdByName("reboot.suspend", "reboot,suspend"); 110 PluginExecCmdByName("reboot.charge", "reboot,charge"); 111 PluginExecCmdByName("reboot.updater", "reboot,updater"); 112 PluginExecCmdByName("reboot.updater", "reboot,updater:2222222"); 113 PluginExecCmdByName("reboot.flashd", "reboot,flashd"); 114 PluginExecCmdByName("reboot.flashd", "reboot,flashd:1000000"); 115 PluginExecCmdByName("reboot.panic", "reboot,panic"); 116 PluginExecCmdByName("reboot.stop", "reboot,stop"); 117 PluginExecCmdByName("reboot.other", "reboot,other"); 118 PARAM_LOGE("TestRebootCmdExec end"); 119 int ret = UpdateMiscMessage("charge:wwwwwwwwwww", "charge", "charge:", "boot_charge"); 120 if (ret == 0) { 121 ret = GetBootModeFromMisc(); 122 EXPECT_EQ(ret, GROUP_CHARGE); 123 clearMisc(); 124 } 125 } 126 #endif 127 128 HWTEST_F(InitRebootUnitTest, TestInitReboot, TestSize.Level1) 129 { 130 ExecReboot("reboot"); 131 ExecReboot("reboot,shutdown"); 132 ExecReboot("reboot,bootloader"); 133 ExecReboot("reboot,updater:123"); 134 ExecReboot("reboot,flash:123"); 135 ExecReboot("reboot,flashd:123"); 136 ExecReboot("reboot,suspend:123"); 137 const char *option = nullptr; 138 int ret = DoReboot(option); 139 EXPECT_EQ(ret, 0); 140 option = "updater"; 141 ret = DoReboot(option); 142 EXPECT_EQ(ret, 0); 143 ret = DoReboot(DEVICE_CMD_SUSPEND); 144 EXPECT_EQ(ret, 0); 145 ret = DoReboot(DEVICE_CMD_FREEZE); 146 EXPECT_EQ(ret, 0); 147 148 ret = DoRebootExt("shutdown", DEVICE_CMD_FREEZE); 149 EXPECT_EQ(ret, 0); 150 } 151 152 HWTEST_F(InitRebootUnitTest, TestAbnormalReboot, TestSize.Level1) 153 { 154 int ret = DoRoot_(nullptr, 0); 155 EXPECT_EQ(ret, 0); 156 157 ret = DoRebootShutdown(0, nullptr, 0, nullptr); 158 EXPECT_EQ(ret, -1); 159 160 ret = DoRebootFlashed(0, nullptr, 0, nullptr); 161 EXPECT_EQ(ret, -1); 162 163 ret = DoRebootOther(0, nullptr, 0, nullptr); 164 EXPECT_EQ(ret, -1); 165 } 166 } // namespace init_ut 167