cmds_unittest.cpp 8.0 KB
Newer Older
X
add ut  
xionglei6 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sys/statvfs.h>
#include "init_cmds.h"
#include "init_param.h"
#include "init_unittest.h"
#include "init_utils.h"
#include "param_libuvadp.h"
#include "trigger_manager.h"

using namespace testing::ext;
using namespace std;

namespace init_ut {
class CmdsUnitTest : public testing::Test {
public:
    static void SetUpTestCase(void)
    {
        InitParamService();
X
init ut  
xionglei6 已提交
33
        mkdir("/data/init_ut", S_IRWXU | S_IRWXG | S_IRWXO);
X
add ut  
xionglei6 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    };
    static void TearDownTestCase(void) {};
    void SetUp() {};
    void TearDown() {};
};

HWTEST_F(CmdsUnitTest, TestCmdExecByName, TestSize.Level1)
{
    SetInitLogLevel(INIT_INFO);
    DoCmdByName("load_param ", "     /system/etc/param            onlyadd");
    DoCmdByName("symlink ", "/proc/self/fd/0 /dev/stdin");
    DoCmdByName("insmod ",
        "libdemo.z.so anony=1 mmz_allocator=hisi mmz=anonymous,0,0xA8000000,384M || report_error");
    DoCmdByName("insmod ", "/vendor/modules/hi_irq.ko");

    DoCmdByName("setparam ", "sys.usb.config ${persist.sys.usb.config}");

    DoCmdByName("load_persist_params ", "");
    DoCmdByName("trigger ", "");
    DoCmdByName("domainname ", "localdomain");
    DoCmdByName("hostname ", "localhost");
    DoCmdByName("sleep ", "1");
    DoCmdByName("setrlimit ", "RLIMIT_NICE 40 40");
    DoCmdByName("setrlimit ", "RLIMIT_NICE2 40 40");
    DoCmdByName("start ", "init_ut");
    DoCmdByName("stop ", "init_ut");
    DoCmdByName("reset ", "init_ut");
    DoCmdByName("reboot ", "");
    DoCmdByName("ifup ", "lo");
    DoCmdByName("mknode ", "/dev/null b 0666 1 3");
    DoCmdByName("makedev ", "999 999");
    DoCmdByName("mount_fstab ", "");
    DoCmdByName("umount_fstab ", "");
}

HWTEST_F(CmdsUnitTest, TestCommonMkdir, TestSize.Level1)
{
    auto checkMkdirCmd = [=](const char *mkdirFile, const char *cmdLine) {
        DoCmdByName("mkdir ", cmdLine);
        return access(mkdirFile, F_OK);
    };
    EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir0", "/data/init_ut/test_dir0"), 0);
    EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir1", "/data/init_ut/test_dir1 0755"), 0);
    EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir2", "/data/init_ut/test_dir2 0755 system system"), 0);

    // abnormal
    EXPECT_NE(checkMkdirCmd("/data/init_ut/test_dir3", ""), 0);
    EXPECT_NE(checkMkdirCmd("/data/init_ut/test_dir4", "/data/init_ut/test_dir4 0755 system"), 0);
    EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir5", "/data/init_ut/test_dir5 0755 error error"), 0);
}

HWTEST_F(CmdsUnitTest, TestCommonChown, TestSize.Level1)
{
    const char *testFile = "/data/init_ut/test_dir0";
    DoCmdByName("chown ", "system system /data/init_ut/test_dir0");
    struct stat info;
    stat(testFile, &info);
    const unsigned int systemUidGid = 1000;
    EXPECT_EQ(info.st_uid, systemUidGid);
    EXPECT_EQ(info.st_gid, systemUidGid);

    // abnormal
    DoCmdByName("chown ", "error error /data/init_ut/test_dir0");
    stat(testFile, &info);
    EXPECT_EQ(info.st_uid, systemUidGid);
    EXPECT_EQ(info.st_gid, systemUidGid);
}

HWTEST_F(CmdsUnitTest, TestCommonChmod, TestSize.Level1)
{
    const char *testFile = "/data/init_ut/test_dir0/test_file0";
    const mode_t testMode = S_IRWXU | S_IRWXG | S_IRWXO;
    int fd = open(testFile, O_CREAT | O_WRONLY);
    ASSERT_GE(fd, 0);
    DoCmdByName("chmod ", "777 /data/init_ut/test_dir0/test_file0");
    struct stat info;
    stat(testFile, &info);
    EXPECT_EQ(testMode, testMode & info.st_mode);

    // abnormal
    DoCmdByName("chmod ", "999 /data/init_ut/test_dir0/test_file0");
    stat(testFile, &info);
    EXPECT_EQ(testMode, testMode & info.st_mode);
    DoCmdByName("chmod ", "777 /data/init_ut/test_dir0/test_file001");

    close(fd);
X
xionglei6 已提交
120
    fd = -1;
X
add ut  
xionglei6 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

HWTEST_F(CmdsUnitTest, TestCommonCopy, TestSize.Level1)
{
    const char *testFile1 = "/data/init_ut/test_dir0/test_file_copy1";
    DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file0 /data/init_ut/test_dir0/test_file_copy1");
    int fd = open(testFile1, O_RDWR);
    ASSERT_GE(fd, 0);
    write(fd, "aaa", strlen("aaa"));

    const char *testFile2 = "/data/init_ut/test_dir0/test_file_copy2";
    DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy1 /data/init_ut/test_dir0/test_file_copy2");
    int ret = access(testFile2, F_OK);
    EXPECT_EQ(ret, 0);
    close(fd);
X
xionglei6 已提交
136
    fd = -1;
X
add ut  
xionglei6 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    // abnormal
    DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy1 /data/init_ut/test_dir0/test_file_copy1");
    DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy11 /data/init_ut/test_dir0/test_file_copy1");
    DoCmdByName("copy ", "a");
}

HWTEST_F(CmdsUnitTest, TestCommonWrite, TestSize.Level1)
{
    const char *testFile1 = "/data/init_ut/test_dir0/test_file_write1";
    int fd = open(testFile1, O_RDWR | O_CREAT);
    ASSERT_GE(fd, 0);

    DoCmdByName("write ", "/data/init_ut/test_dir0/test_file_write1 aaa");
    const int bufLen = 50;
    char buffer[bufLen];
X
xionglei6 已提交
152
    int length = read(fd, buffer, bufLen - 1);
X
xionglei6 已提交
153
    EXPECT_EQ(length, strlen("aaa"));
X
add ut  
xionglei6 已提交
154
    close(fd);
X
xionglei6 已提交
155
    fd = -1;
X
add ut  
xionglei6 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    // abnormal
    DoCmdByName("write ", "/data/init_ut/test_dir0/test_file_write2 aaa");
}

HWTEST_F(CmdsUnitTest, TestCommonRm, TestSize.Level1)
{
    const char *testFile1 = "/data/init_ut/test_dir0/test_file_write1";
    DoCmdByName("rm ", testFile1);
    int ret = access(testFile1, F_OK);
    EXPECT_NE(ret, 0);

    testFile1 = "/data/init_ut/test_dir1";
    DoCmdByName("rmdir ", testFile1);
    ret = access(testFile1, F_OK);
    EXPECT_NE(ret, 0);

    // abnormal
    DoCmdByName("rmdir ", testFile1);
}

HWTEST_F(CmdsUnitTest, TestCommonExport, TestSize.Level1)
{
    DoCmdByName("export ", "TEST_INIT 1");
    EXPECT_STREQ("1", getenv("TEST_INIT"));
    unsetenv("TEST_INIT");
    EXPECT_STRNE("1", getenv("TEST_INIT"));
}

HWTEST_F(CmdsUnitTest, TestCommonMount, TestSize.Level1)
{
    DoCmdByName("mount ", "ext4 /dev/block/platform/soc/10100000.himci.eMMC/by-name/vendor "
        "/vendor wait rdonly barrier=1");
    struct statvfs64 vfs {};
    int ret = statvfs64("/vendor", &vfs);
    EXPECT_GE(ret, 0);
    EXPECT_GT(vfs.f_bsize, 0);
}

HWTEST_F(CmdsUnitTest, TestGetCmdKey, TestSize.Level1)
{
    const char *cmd1 = GetCmdKey(0);
    EXPECT_STREQ(cmd1, "start ");
    const int execPos = 17;
    cmd1 = GetCmdKey(execPos);
    EXPECT_STREQ(cmd1, "exec ");
}

HWTEST_F(CmdsUnitTest, TestDoCmdByIndex, TestSize.Level1)
{
    DoCmdByIndex(1, "/data/init_ut/test_cmd_dir0");
    int ret = access("/data/init_ut/test_cmd_dir0", F_OK);
    EXPECT_EQ(ret, 0);

    const int execPos = 17;
    DoCmdByIndex(execPos, "sleep 1");
}

HWTEST_F(CmdsUnitTest, TestGetCmdLinesFromJson, TestSize.Level1)
{
    const char *jsonStr = "{\"jobs\":[{\"name\":\"init\",\"cmds\":[\"sleep 1\"]}]}";
    cJSON* jobItem = cJSON_Parse(jsonStr);
    ASSERT_NE(nullptr, jobItem);
    cJSON *cmdsItem = cJSON_GetObjectItem(jobItem, "jobs");
    ASSERT_NE(nullptr, cmdsItem);
    ASSERT_TRUE(cJSON_IsArray(cmdsItem));

    cJSON *cmdsItem1 = cJSON_GetArrayItem(cmdsItem, 0);
    ASSERT_NE(nullptr, cmdsItem1);
    CmdLines **cmdLines = (CmdLines **)calloc(1, 1);
X
xionglei6 已提交
225
    ASSERT_NE(nullptr, cmdLines);
X
add ut  
xionglei6 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    int ret = GetCmdLinesFromJson(cmdsItem1, cmdLines);
    EXPECT_EQ(ret, -1);
    cJSON *cmdsItem2 = cJSON_GetObjectItem(cmdsItem1, "cmds");
    ASSERT_NE(nullptr, cmdsItem2);
    ret = GetCmdLinesFromJson(cmdsItem2, cmdLines);
    EXPECT_EQ(ret, 0);

    cJSON_Delete(jobItem);
    if (cmdLines[0] != nullptr) {
        free(cmdLines[0]);
        cmdLines[0] = nullptr;
    }
    free(cmdLines);
    cmdLines = nullptr;
}
} // namespace init_ut