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_param.h" 19 #include "init_unittest.h" 20 #include "init_utils.h" 21 #include "trigger_manager.h" 22 23 using namespace testing::ext; 24 using namespace std; 25 26 namespace init_ut { 27 class CmdsUnitTest : public testing::Test { 28 public: SetUpTestCase(void)29 static void SetUpTestCase(void) 30 { 31 InitParamService(); 32 mkdir("/data/init_ut", S_IRWXU | S_IRWXG | S_IRWXO); 33 }; TearDownTestCase(void)34 static void TearDownTestCase(void) {}; SetUp()35 void SetUp() {}; TearDown()36 void TearDown() {}; 37 }; 38 39 HWTEST_F(CmdsUnitTest, TestCmdExecByName, TestSize.Level1) 40 { 41 SetInitLogLevel(INIT_INFO); 42 DoCmdByName("load_param ", " /system/etc/param onlyadd"); 43 DoCmdByName("symlink ", "/proc/self/fd/0 /dev/stdin"); 44 DoCmdByName("insmod ", 45 "libdemo.z.so anony=1 mmz_allocator=hisi mmz=anonymous,0,0xA8000000,384M || report_error"); 46 DoCmdByName("insmod ", "/vendor/modules/hi_irq.ko"); 47 48 DoCmdByName("setparam ", "sys.usb.config ${persist.sys.usb.config}"); 49 50 DoCmdByName("load_persist_params ", ""); 51 DoCmdByName("trigger ", ""); 52 DoCmdByName("domainname ", "localdomain"); 53 DoCmdByName("hostname ", "localhost"); 54 DoCmdByName("sleep ", "1"); 55 DoCmdByName("setrlimit ", "RLIMIT_NICE 40 40"); 56 DoCmdByName("setrlimit ", "RLIMIT_NICE2 40 40"); 57 DoCmdByName("start ", "init_ut"); 58 DoCmdByName("stop ", "init_ut"); 59 DoCmdByName("reset ", "init_ut"); 60 DoCmdByName("reboot ", ""); 61 DoCmdByName("ifup ", "lo"); 62 DoCmdByName("mknode ", "/dev/null b 0666 1 3"); 63 DoCmdByName("makedev ", "999 999"); 64 DoCmdByName("mount_fstab ", ""); 65 DoCmdByName("umount_fstab ", ""); 66 } 67 68 HWTEST_F(CmdsUnitTest, TestCommonMkdir, TestSize.Level1) 69 { __anoncfa060420102(const char *mkdirFile, const char *cmdLine) 70 auto checkMkdirCmd = [=](const char *mkdirFile, const char *cmdLine) { 71 DoCmdByName("mkdir ", cmdLine); 72 return access(mkdirFile, F_OK); 73 }; 74 EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir0", "/data/init_ut/test_dir0"), 0); 75 EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir1", "/data/init_ut/test_dir1 0755"), 0); 76 EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir2", "/data/init_ut/test_dir2 0755 system system"), 0); 77 78 // abnormal 79 EXPECT_NE(checkMkdirCmd("/data/init_ut/test_dir3", ""), 0); 80 EXPECT_NE(checkMkdirCmd("/data/init_ut/test_dir4", "/data/init_ut/test_dir4 0755 system"), 0); 81 EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir5", "/data/init_ut/test_dir5 0755 error error"), 0); 82 } 83 84 HWTEST_F(CmdsUnitTest, TestCommonChown, TestSize.Level1) 85 { 86 const char *testFile = "/data/init_ut/test_dir0"; 87 DoCmdByName("chown ", "system system /data/init_ut/test_dir0"); 88 struct stat info; 89 stat(testFile, &info); 90 const unsigned int systemUidGid = 1000; 91 EXPECT_EQ(info.st_uid, systemUidGid); 92 EXPECT_EQ(info.st_gid, systemUidGid); 93 94 // abnormal 95 DoCmdByName("chown ", "error error /data/init_ut/test_dir0"); 96 stat(testFile, &info); 97 EXPECT_EQ(info.st_uid, systemUidGid); 98 EXPECT_EQ(info.st_gid, systemUidGid); 99 } 100 101 HWTEST_F(CmdsUnitTest, TestCommonChmod, TestSize.Level1) 102 { 103 const char *testFile = "/data/init_ut/test_dir0/test_file0"; 104 const mode_t testMode = S_IRWXU | S_IRWXG | S_IRWXO; 105 int fd = open(testFile, O_CREAT | O_WRONLY, testMode); 106 ASSERT_GE(fd, 0); 107 DoCmdByName("chmod ", "777 /data/init_ut/test_dir0/test_file0"); 108 struct stat info; 109 stat(testFile, &info); 110 EXPECT_EQ(testMode, testMode & info.st_mode); 111 112 // abnormal 113 DoCmdByName("chmod ", "999 /data/init_ut/test_dir0/test_file0"); 114 stat(testFile, &info); 115 EXPECT_EQ(testMode, testMode & info.st_mode); 116 DoCmdByName("chmod ", "777 /data/init_ut/test_dir0/test_file001"); 117 118 close(fd); 119 fd = -1; 120 } 121 122 HWTEST_F(CmdsUnitTest, TestCommonCopy, TestSize.Level1) 123 { 124 const char *testFile1 = "/data/init_ut/test_dir0/test_file_copy1"; 125 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file0 /data/init_ut/test_dir0/test_file_copy1"); 126 int fd = open(testFile1, O_RDWR); 127 ASSERT_GE(fd, 0); 128 write(fd, "aaa", strlen("aaa")); 129 130 const char *testFile2 = "/data/init_ut/test_dir0/test_file_copy2"; 131 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy1 /data/init_ut/test_dir0/test_file_copy2"); 132 int ret = access(testFile2, F_OK); 133 EXPECT_EQ(ret, 0); 134 close(fd); 135 fd = -1; 136 // abnormal 137 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy1 /data/init_ut/test_dir0/test_file_copy1"); 138 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy11 /data/init_ut/test_dir0/test_file_copy1"); 139 DoCmdByName("copy ", "a"); 140 } 141 142 HWTEST_F(CmdsUnitTest, TestCommonWrite, TestSize.Level1) 143 { 144 const char *testFile1 = "/data/init_ut/test_dir0/test_file_write1"; 145 int fd = open(testFile1, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); 146 ASSERT_GE(fd, 0); 147 148 DoCmdByName("write ", "/data/init_ut/test_dir0/test_file_write1 aaa"); 149 const int bufLen = 50; 150 char buffer[bufLen]; 151 int length = read(fd, buffer, bufLen - 1); 152 EXPECT_EQ(length, strlen("aaa")); 153 close(fd); 154 fd = -1; 155 // abnormal 156 DoCmdByName("write ", "/data/init_ut/test_dir0/test_file_write2 aaa"); 157 } 158 159 HWTEST_F(CmdsUnitTest, TestCommonRm, TestSize.Level1) 160 { 161 const char *testFile1 = "/data/init_ut/test_dir0/test_file_write1"; 162 DoCmdByName("rm ", testFile1); 163 int ret = access(testFile1, F_OK); 164 EXPECT_NE(ret, 0); 165 166 testFile1 = "/data/init_ut/test_dir1"; 167 DoCmdByName("rmdir ", testFile1); 168 ret = access(testFile1, F_OK); 169 EXPECT_NE(ret, 0); 170 171 // abnormal 172 DoCmdByName("rmdir ", testFile1); 173 } 174 175 HWTEST_F(CmdsUnitTest, TestCommonExport, TestSize.Level1) 176 { 177 DoCmdByName("export ", "TEST_INIT 1"); 178 EXPECT_STREQ("1", getenv("TEST_INIT")); 179 unsetenv("TEST_INIT"); 180 EXPECT_STRNE("1", getenv("TEST_INIT")); 181 } 182 183 HWTEST_F(CmdsUnitTest, TestCommonMount, TestSize.Level1) 184 { 185 DoCmdByName("mount ", "ext4 /dev/block/platform/soc/10100000.himci.eMMC/by-name/vendor " 186 "/vendor wait rdonly barrier=1"); 187 struct statvfs64 vfs {}; 188 int ret = statvfs64("/vendor", &vfs); 189 EXPECT_GE(ret, 0); 190 EXPECT_GT(vfs.f_bsize, 0); 191 } 192 193 HWTEST_F(CmdsUnitTest, TestGetCmdKey, TestSize.Level1) 194 { 195 const char *cmd1 = GetCmdKey(0); 196 EXPECT_STREQ(cmd1, "start "); 197 } 198 199 HWTEST_F(CmdsUnitTest, TestDoCmdByIndex, TestSize.Level1) 200 { 201 DoCmdByIndex(1, "/data/init_ut/test_cmd_dir0"); 202 int ret = access("/data/init_ut/test_cmd_dir0", F_OK); 203 EXPECT_EQ(ret, 0); 204 205 const int execPos = 17; 206 DoCmdByIndex(execPos, "sleep 1"); 207 } 208 209 HWTEST_F(CmdsUnitTest, TestGetCmdLinesFromJson, TestSize.Level1) 210 { 211 const char *jsonStr = "{\"jobs\":[{\"name\":\"init\",\"cmds\":[\"sleep 1\"]}]}"; 212 cJSON* jobItem = cJSON_Parse(jsonStr); 213 ASSERT_NE(nullptr, jobItem); 214 cJSON *cmdsItem = cJSON_GetObjectItem(jobItem, "jobs"); 215 ASSERT_NE(nullptr, cmdsItem); 216 ASSERT_TRUE(cJSON_IsArray(cmdsItem)); 217 218 cJSON *cmdsItem1 = cJSON_GetArrayItem(cmdsItem, 0); 219 ASSERT_NE(nullptr, cmdsItem1); 220 CmdLines **cmdLines = (CmdLines **)calloc(1, sizeof(CmdLines *)); 221 ASSERT_NE(nullptr, cmdLines); 222 int ret = GetCmdLinesFromJson(cmdsItem1, cmdLines); 223 EXPECT_EQ(ret, -1); 224 cJSON *cmdsItem2 = cJSON_GetObjectItem(cmdsItem1, "cmds"); 225 ASSERT_NE(nullptr, cmdsItem2); 226 ret = GetCmdLinesFromJson(cmdsItem2, cmdLines); 227 EXPECT_EQ(ret, 0); 228 229 cJSON_Delete(jobItem); 230 if (cmdLines[0] != nullptr) { 231 free(cmdLines[0]); 232 cmdLines[0] = nullptr; 233 } 234 free(cmdLines); 235 cmdLines = nullptr; 236 } 237 } // namespace init_ut 238