cmd_func_test.cpp 31.4 KB
Newer Older
M
mamingshuai 已提交
1
/*
2
 * Copyright (c) 2020 Huawei Device Co., Ltd.
M
mamingshuai 已提交
3 4 5 6 7 8 9 10 11 12 13 14
 * 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.
 */
M
Mupceet 已提交
15
#include <gtest/gtest.h>
Z
zhong_ning 已提交
16 17 18
#include <cerrno>
#include <cstdio>
#include <cstdlib>
19
#include <dirent.h>
M
mamingshuai 已提交
20 21 22
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
23

M
mamingshuai 已提交
24
#include "cJSON.h"
25
#include "init_cmds.h"
Z
zhong_ning 已提交
26
#include "init_jobs_internal.h"
C
cheng_jinsong 已提交
27
#include "init_log.h"
M
mamingshuai 已提交
28
#include "init_service_manager.h"
M
Mupceet 已提交
29
#include "param_stub.h"
30
#include "securec.h"
M
mamingshuai 已提交
31

32
using namespace std;
M
mamingshuai 已提交
33 34 35 36 37 38 39 40 41
using namespace testing::ext;

namespace OHOS {
std::vector<std::string> g_supportedCmds;
const std::string ROOT_DIR = "/storage/data/";
const std::string TEST_DRI = ROOT_DIR + "StartInitTestDir";
const std::string TEST_FILE = TEST_DRI + "/test.txt";
const std::string TEST_CFG_ILLEGAL = TEST_DRI + "/illegal.cfg";
const std::string TEST_PROC_MOUNTS = "/proc/mounts";
X
xionglei6 已提交
42
#ifndef USE_EMMC_STORAGE
M
mamingshuai 已提交
43 44 45
const uid_t TEST_FILE_UID = 999;
const gid_t TEST_FILE_GID = 999;
const mode_t TEST_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
X
xionglei6 已提交
46
#endif
M
mamingshuai 已提交
47

48
// init.cfg related
M
mamingshuai 已提交
49 50 51 52 53 54 55
const std::string CFG_FILE = "/etc/init.cfg";
const std::string SERVICE_ARR_NAME_IN_JSON = "services";
const std::string JOBS_ARR_NAME_IN_JSON = "jobs";
const std::string CMDS_ARR_NAME_IN_JSON = "cmds";
const uid_t CFG_FILE_UID = 0;
const gid_t CFG_FILE_GID = 0;
const mode_t CFG_FILE_MODE = S_IRUSR;
56
const int JOBS_IN_FILE_COUNT = 3; // pre-init, init, post-init
Z
zhong_ning 已提交
57
const int MAX_SERVICES_COUNT_IN_FILE = 100;
M
mamingshuai 已提交
58
const int MAX_CAPS_CNT_FOR_ONE_SERVICE = 100;
59 60 61 62
const unsigned int MAX_JSON_FILE_LEN = 102400;        // max init.cfg size 100KB
const int TEST_MAX_PATH_ARGS_CNT = 20;                // max path and args count
const int TEST_MAX_ONE_ARG_LEN = 64;                  // max length of one param/path
const int CAT_BUF_SIZE = 512;                         // standard Cat buffer size from vfs_shell_cmd
M
mamingshuai 已提交
63

64
// job test related
M
mamingshuai 已提交
65 66 67 68
const std::string PRE_INIT_DIR = ROOT_DIR + "preInitDir/";
const std::string INIT_DIR = PRE_INIT_DIR + "initDir";
const std::string POST_INIT_DIR = INIT_DIR + "postInitDir";

69 70 71 72 73
using  TestCmdLine = struct {
    char name[MAX_CMD_NAME_LEN + 1];
    char cmdContent[MAX_CMD_CONTENT_LEN + 1];
};

M
mamingshuai 已提交
74 75 76 77 78 79 80 81 82 83
class StartupInitUTest : public testing::Test {
public:
    static void SetUpTestCase()
    {
        g_supportedCmds.push_back(std::string("start "));
        g_supportedCmds.push_back(std::string("mkdir "));
        g_supportedCmds.push_back(std::string("chmod "));
        g_supportedCmds.push_back(std::string("chown "));
        g_supportedCmds.push_back(std::string("mount "));
        g_supportedCmds.push_back(std::string("loadcfg "));
84
        const mode_t mode = 0755;
M
mamingshuai 已提交
85 86 87 88 89 90
        if (mkdir(TEST_DRI.c_str(), mode) != 0) {
            if (errno != EEXIST) {
                return;
            }
        }

91
        FILE *testFile = fopen(TEST_FILE.c_str(), "w+");
M
mamingshuai 已提交
92 93 94 95 96 97
        if (testFile == nullptr) {
            return;
        }

        std::string writeContent = "This is a test file for startup subsystem init module.";
        if (fwrite(writeContent.c_str(), writeContent.length(), 1, testFile) != 1) {
M
Mupceet 已提交
98
            (void)fclose(testFile);
M
mamingshuai 已提交
99 100
            return;
        }
M
Mupceet 已提交
101
        (void)fclose(testFile);
M
mamingshuai 已提交
102 103 104 105 106 107 108 109 110 111 112

#ifndef USE_EMMC_STORAGE    // emmc storage does not support chmod/chown

        if (chmod(TEST_FILE.c_str(), TEST_FILE_MODE) != 0) {
            return;
        }

        if (chown(TEST_FILE.c_str(), TEST_FILE_UID, TEST_FILE_GID) != 0) {
            return;
        }
#endif  // USE_EMMC_STORAGE
M
Mupceet 已提交
113
        PrepareInitUnitTestEnv();
M
mamingshuai 已提交
114 115 116 117 118
    }

    static void TearDownTestCase()
    {
        if (remove(TEST_FILE.c_str()) != 0) {
X
xlei1030 已提交
119
            return;
M
mamingshuai 已提交
120 121
        }
        if (remove(TEST_DRI.c_str()) != 0) {
X
xlei1030 已提交
122
            return;
M
mamingshuai 已提交
123 124
        }
    }
X
xlei1030 已提交
125 126
    void SetUp()
    {
127
        EnableInitLog(INIT_FATAL);
X
xlei1030 已提交
128
    }
M
mamingshuai 已提交
129 130 131
    void TearDown() {}
};

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
void ParseCmdLine(const char *content, TestCmdLine *resCmd)
{
    if (content == nullptr || resCmd == nullptr) {
        return;
    }

    const struct CmdTable *cmd = GetCmdByName(content);
    if (cmd == nullptr) {
        (void)memset_s(resCmd, sizeof(TestCmdLine), 0, sizeof(TestCmdLine));
        return;
    }
    if (strlen(content) <= (strlen(cmd->name) + 1)) {
        (void)memset_s(resCmd, sizeof(TestCmdLine), 0, sizeof(TestCmdLine));
        return;
    }
    int ret1 = strcpy_s(resCmd->name, MAX_CMD_NAME_LEN, cmd->name);
    int ret2 = strcpy_s(resCmd->cmdContent, MAX_CMD_CONTENT_LEN, content + strlen(cmd->name));
    if (ret1 || ret2) {
        (void)memset_s(resCmd, sizeof(TestCmdLine), 0, sizeof(TestCmdLine));
        return;
    }
}

void DoCmd(const TestCmdLine *resCmd)
{
    if (resCmd == nullptr) {
        return;
    }

    DoCmdByName(resCmd->name, resCmd->cmdContent);
}

M
mamingshuai 已提交
164
/*
X
xionglei6 已提交
165 166 167 168
 * @tc.name: cmdFuncParseCmdTest_001
 * @tc.desc: parse function, nullptr test
 * @tc.type: FUNC
 */
169
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level0)
M
mamingshuai 已提交
170 171 172 173 174 175
{
    // do not crash
    ParseCmdLine(nullptr, nullptr);
};

/*
X
xionglei6 已提交
176 177 178 179
 * @tc.name: cmdFuncParseCmdTest_002
 * @tc.desc: parse function, invalid strings test
 * @tc.type: FUNC
 */
180
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0)
M
mamingshuai 已提交
181
{
182
    TestCmdLine curCmdLine;
C
cheng_jinsong 已提交
183
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
M
mamingshuai 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    ParseCmdLine(nullptr, &curCmdLine);
    EXPECT_EQ(0, strlen(curCmdLine.name));
    EXPECT_EQ(0, strlen(curCmdLine.cmdContent));

    ParseCmdLine("", &curCmdLine);
    EXPECT_EQ(0, strlen(curCmdLine.name));
    EXPECT_EQ(0, strlen(curCmdLine.cmdContent));

    ParseCmdLine("xxxxxxxx", &curCmdLine);
    EXPECT_EQ(0, strlen(curCmdLine.name));
    EXPECT_EQ(0, strlen(curCmdLine.cmdContent));

    ParseCmdLine("asdnkawdqw4145a45sdqw_-+\\\\sdqwdasd", &curCmdLine);
    EXPECT_EQ(0, strlen(curCmdLine.name));
    EXPECT_EQ(0, strlen(curCmdLine.cmdContent));
}

/*
X
xionglei6 已提交
203 204 205 206
 * @tc.name: cmdFuncParseCmdTest_003
 * @tc.desc: parse function, cmd content empty test
 * @tc.type: FUNC
 */
207
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0)
M
mamingshuai 已提交
208
{
209
    TestCmdLine curCmdLine;
C
cheng_jinsong 已提交
210
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
M
mamingshuai 已提交
211 212 213 214 215 216 217 218 219

    for (size_t i = 0; i < g_supportedCmds.size(); ++i) {
        ParseCmdLine(g_supportedCmds[i].c_str(), &curCmdLine);
        EXPECT_EQ(0, strlen(curCmdLine.name));
        EXPECT_EQ(0, strlen(curCmdLine.cmdContent));
    }
}

/*
X
xionglei6 已提交
220 221 222 223
 * @tc.name: cmdFuncParseCmdTest_004
 * @tc.desc: parse function, cmd content too long test
 * @tc.type: FUNC
 */
224
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0)
M
mamingshuai 已提交
225
{
226
    TestCmdLine curCmdLine;
C
cheng_jinsong 已提交
227
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
M
mamingshuai 已提交
228 229

    char toLongContent[MAX_CMD_CONTENT_LEN + 10];
C
cheng_jinsong 已提交
230 231 232
    int ret = memset_s(toLongContent, MAX_CMD_CONTENT_LEN + 10, 'x', MAX_CMD_CONTENT_LEN + 9);
    EXPECT_EQ(0, ret);

M
mamingshuai 已提交
233 234 235
    toLongContent[MAX_CMD_CONTENT_LEN + 9] = '\0';
    for (size_t i = 0; i < g_supportedCmds.size(); ++i) {
        size_t curCmdLen = g_supportedCmds[i].length();
236
        char *curCmd = (char *)malloc(curCmdLen + MAX_CMD_CONTENT_LEN + 10);
M
mamingshuai 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        if (curCmd == nullptr) {
            break;
        }
        errno_t ret = memcpy_s(curCmd, curCmdLen + MAX_CMD_CONTENT_LEN + 10, \
            g_supportedCmds[i].c_str(), curCmdLen);
        errno_t ret2 = memcpy_s(curCmd + curCmdLen, MAX_CMD_CONTENT_LEN + 10, \
            toLongContent, strlen(toLongContent));
        if (ret != EOK || ret2 != EOK) {
            free(curCmd);
            curCmd = nullptr;
            break;
        }
        curCmd[curCmdLen + MAX_CMD_CONTENT_LEN + 9] = '\0';

        ParseCmdLine(curCmd, &curCmdLine);
        EXPECT_EQ(0, strlen(curCmdLine.name));
        EXPECT_EQ(0, strlen(curCmdLine.cmdContent));
        free(curCmd);
        curCmd = nullptr;
    }
}

/*
X
xionglei6 已提交
260 261 262 263
 * @tc.name: cmdFuncParseCmdTest_005
 * @tc.desc: parse function, parse success test
 * @tc.type: FUNC
 */
264
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0)
M
mamingshuai 已提交
265
{
266
    TestCmdLine curCmdLine;
C
cheng_jinsong 已提交
267
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
M
mamingshuai 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

    ParseCmdLine("start InitTestService", &curCmdLine);
    EXPECT_EQ(0, strcmp("start ", curCmdLine.name));
    EXPECT_EQ(0, strcmp("InitTestService", curCmdLine.cmdContent));

    ParseCmdLine("mkdir InitTestDir", &curCmdLine);
    EXPECT_EQ(0, strcmp("mkdir ", curCmdLine.name));
    EXPECT_EQ(0, strcmp("InitTestDir", curCmdLine.cmdContent));

    ParseCmdLine("chmod 0500 /bin/InitTestBin", &curCmdLine);
    EXPECT_EQ(0, strcmp("chmod ", curCmdLine.name));
    EXPECT_EQ(0, strcmp("0500 /bin/InitTestBin", curCmdLine.cmdContent));

    ParseCmdLine("chown 1000 1000 /bin/InitTestBin", &curCmdLine);
    EXPECT_EQ(0, strcmp("chown ", curCmdLine.name));
    EXPECT_EQ(0, strcmp("1000 1000 /bin/InitTestBin", curCmdLine.cmdContent));

    ParseCmdLine("mount vfat /dev/mmcblk1 /sdcard rw,umask=000", &curCmdLine);
    EXPECT_EQ(0, strcmp("mount ", curCmdLine.name));
    EXPECT_EQ(0, strcmp("vfat /dev/mmcblk1 /sdcard rw,umask=000", curCmdLine.cmdContent));
};

/*
X
xionglei6 已提交
291 292 293 294
 * @tc.name: cmdFuncDoCmdTest_001
 * @tc.desc: do cmd function, nullptr test
 * @tc.type: FUNC
 */
295
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0)
M
mamingshuai 已提交
296 297 298 299 300 301
{
    // do not crash here
    DoCmd(nullptr);
}

/*
X
xionglei6 已提交
302 303 304 305
 * @tc.name: cmdFuncDoCmdTest_002
 * @tc.desc: do cmd function, do start fail test
 * @tc.type: FUNC
 */
306
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level0)
M
mamingshuai 已提交
307
{
308
    TestCmdLine curCmdLine;
C
cheng_jinsong 已提交
309
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
M
mamingshuai 已提交
310 311 312

    std::string cmdStr = "start ";
    std::string cmdContentStr = "NameNotExist";
Z
zhong_ning 已提交
313 314
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
315 316 317 318 319 320
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);
}

/*
X
xionglei6 已提交
321 322 323 324
 * @tc.name: cmdFuncDoCmdTest_003
 * @tc.desc: do cmd function, do mkdir fail test
 * @tc.type: FUNC
 */
325
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level0)
M
mamingshuai 已提交
326
{
327
    TestCmdLine curCmdLine;
C
cheng_jinsong 已提交
328
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
M
mamingshuai 已提交
329 330 331

    std::string cmdStr = "mkdir ";
    std::string cmdContentStr = "/DirNotExist/DirNotExist/DirNotExist";
Z
zhong_ning 已提交
332 333
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
334 335 336 337 338
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);

    // make sure that the directory does not exist
339
    DIR *dirTmp = opendir(cmdContentStr.c_str());
M
mamingshuai 已提交
340 341 342 343 344 345 346
    EXPECT_TRUE(dirTmp == nullptr);
    EXPECT_TRUE(errno == ENOENT);
    if (dirTmp != nullptr) {    // just in case
        closedir(dirTmp);
        dirTmp = nullptr;
    }

Z
zhong_ning 已提交
347 348
    // error argument count, bad format
    cmdContentStr = "  /storage/data/cmdFuncDoCmdTest003 0755 system";
Z
zhong_ning 已提交
349 350
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);

    // make sure that the directory does not exist
    dirTmp = opendir("/storage/data/cmdFuncDoCmdTest003");
    EXPECT_TRUE(dirTmp == nullptr);
    EXPECT_TRUE(errno == ENOENT);
    if (dirTmp != nullptr) {    // just in case
        closedir(dirTmp);
        dirTmp = nullptr;
    }
}

/*
X
xionglei6 已提交
366 367 368 369
 * @tc.name: cmdFuncDoCmdTest_004
 * @tc.desc: do cmd function, do chmod fail test
 * @tc.type: FUNC
 */
370
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0)
M
mamingshuai 已提交
371
{
372
    TestCmdLine curCmdLine;
C
cheng_jinsong 已提交
373
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
M
mamingshuai 已提交
374 375 376

    std::string cmdStr = "chmod ";
    std::string cmdContentStr = "755 " + TEST_FILE;    // should be 0755, wrong format here
Z
zhong_ning 已提交
377 378
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
379 380 381 382 383
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);

    cmdContentStr = "0855 " + TEST_FILE;    // should not exceed 0777, wrong format here
Z
zhong_ning 已提交
384 385
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command .c_str(), &curCmdLine);
M
mamingshuai 已提交
386 387 388 389 390
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);

    cmdContentStr = "07b5 " + TEST_FILE;    // non-digital character, wrong format here
Z
zhong_ning 已提交
391 392
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
393 394 395 396 397
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);

    cmdContentStr = "075 " + TEST_FILE;    // should be 0xxx, wrong format here
Z
zhong_ning 已提交
398 399
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
400 401 402 403 404
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);

    cmdContentStr = "0755       " + TEST_FILE;    // too many spaces, wrong format here
Z
zhong_ning 已提交
405 406
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);

    struct stat testFileStat = {0};
    EXPECT_EQ(0, stat(TEST_FILE.c_str(), &testFileStat));

#ifndef USE_EMMC_STORAGE    // emmc storage does not support chmod/chown

    EXPECT_EQ(TEST_FILE_MODE, testFileStat.st_mode & TEST_FILE_MODE);   // file mode is not changed

#endif // USE_EMMC_STORAGE
}

/*
X
xionglei6 已提交
422 423 424 425
 * @tc.name: cmdFuncDoCmdTest_005
 * @tc.desc: do cmd function, do chown fail test
 * @tc.type: FUNC
 */
426
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0)
M
mamingshuai 已提交
427
{
428
    TestCmdLine curCmdLine;
C
cheng_jinsong 已提交
429
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
M
mamingshuai 已提交
430 431 432

    std::string cmdStr = "chown ";
    std::string cmdContentStr = "888 " + TEST_FILE;    // uid or gid missing, wrong format here
Z
zhong_ning 已提交
433 434
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
435 436 437 438 439
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);

    cmdContentStr = "888 8b9 " + TEST_FILE;    // non-digital character, wrong format here
Z
zhong_ning 已提交
440 441
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);

    struct stat testFileStat = {0};
    EXPECT_EQ(0, stat(TEST_FILE.c_str(), &testFileStat));

#ifndef USE_EMMC_STORAGE    // emmc storage does not support chmod/chown

    EXPECT_EQ(testFileStat.st_uid, TEST_FILE_UID);    // uid not changed
    EXPECT_EQ(testFileStat.st_gid, TEST_FILE_GID);    // gid not changed

#endif // USE_EMMC_STORAGE
}

/*
X
xionglei6 已提交
458 459 460 461
 * @tc.name: cmdFuncDoCmdTest_006
 * @tc.desc: do cmd function, do success test
 * @tc.type: FUNC
 */
462
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0)
M
mamingshuai 已提交
463
{
464
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
465 466 467 468

    // mkdir success
    std::string cmdStr = "mkdir ";
    std::string cmdContentStr = TEST_DRI + "/cmdFuncDoCmdTest006";
Z
zhong_ning 已提交
469 470
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));

    DoCmd(&curCmdLine);
    DIR* dirTmp = opendir(cmdContentStr.c_str());
    EXPECT_TRUE(dirTmp != nullptr);
    if (dirTmp != nullptr) {
        closedir(dirTmp);
        dirTmp = nullptr;
    }

    // delete dir
    if (remove(cmdContentStr.c_str()) != 0) {
X
xlei1030 已提交
484
        EXPECT_EQ(0, 0);
M
mamingshuai 已提交
485 486 487 488 489
    }

    // chmod success
    cmdStr = "chmod ";
    cmdContentStr = "0440 " + TEST_FILE;
Z
zhong_ning 已提交
490 491
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));

#ifndef USE_EMMC_STORAGE    // emmc storage does not support chmod/chown

    DoCmd(&curCmdLine);
    struct stat testFileStat = {0};
    EXPECT_EQ(0, stat(TEST_FILE.c_str(), &testFileStat));
    mode_t targetMode = S_IRUSR | S_IRGRP;
    EXPECT_EQ(targetMode, testFileStat.st_mode & targetMode);    // changed

#endif  // USE_EMMC_STORAGE

    // chown success
    cmdStr = "chown ";
    cmdContentStr = "888 888 " + TEST_FILE;
Z
zhong_ning 已提交
508 509
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));

#ifndef USE_EMMC_STORAGE    // emmc storage does not support chmod/chown

    DoCmd(&curCmdLine);
    EXPECT_EQ(0, stat(TEST_FILE.c_str(), &testFileStat));
    EXPECT_EQ(testFileStat.st_uid, 888);    // changed
    EXPECT_EQ(testFileStat.st_gid, 888);    // changed

#endif  // USE_EMMC_STORAGE
}

/*
X
xionglei6 已提交
524 525 526 527
 * @tc.name: cfgCheckStat_001
 * @tc.desc: init.cfg file state check
 * @tc.type: FUNC
 */
528
HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level0)
M
mamingshuai 已提交
529 530 531 532 533 534 535 536 537 538 539 540
{
    struct stat fileStat = {0};
    EXPECT_EQ(0, stat(CFG_FILE.c_str(), &fileStat));
    EXPECT_EQ(CFG_FILE_UID, fileStat.st_uid);
    EXPECT_EQ(CFG_FILE_GID, fileStat.st_gid);
    EXPECT_EQ(CFG_FILE_MODE, CFG_FILE_MODE & fileStat.st_mode);
    EXPECT_TRUE(fileStat.st_size > 0);
    EXPECT_TRUE(fileStat.st_size <= MAX_JSON_FILE_LEN);
};

static char* ReadFileToBuf()
{
541 542
    char *buffer = nullptr;
    FILE *fd = nullptr;
M
mamingshuai 已提交
543 544 545 546 547 548 549 550
    struct stat fileStat = {0};
    (void)stat(CFG_FILE.c_str(), &fileStat);
    do {
        fd = fopen(CFG_FILE.c_str(), "r");
        if (fd == nullptr) {
            break;
        }

Z
zhong_ning 已提交
551
        buffer = static_cast<char*>(malloc(static_cast<size_t>(fileStat.st_size) + 1));
M
mamingshuai 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
        if (buffer == nullptr) {
            break;
        }

        if (fread(buffer, fileStat.st_size, 1, fd) != 1) {
            free(buffer);
            buffer = nullptr;
            break;
        }
        buffer[fileStat.st_size] = '\0';
    } while (0);

    if (fd != nullptr) {
        fclose(fd);
        fd = nullptr;
    }
    return buffer;
}

571
static cJSON *GetArrItem(const cJSON *fileRoot, int &arrSize, const std::string &arrName)
M
mamingshuai 已提交
572
{
573
    cJSON *arrItem = cJSON_GetObjectItemCaseSensitive(fileRoot, arrName.c_str());
M
mamingshuai 已提交
574 575 576 577 578 579 580
    arrSize = cJSON_GetArraySize(arrItem);
    if (arrSize <= 0) {
        return nullptr;
    }
    return arrItem;
}

581
static int IsForbidden(const char *fieldStr)
M
mamingshuai 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
{
    size_t fieldLen = strlen(fieldStr);
    size_t forbidStrLen =  strlen("/bin/sh");
    if (fieldLen == forbidStrLen) {
        if (strncmp(fieldStr, "/bin/sh", fieldLen) == 0) {
            return 1;
        }
        return 0;
    } else if (fieldLen > forbidStrLen) {
        // "/bin/shxxxx" is valid but "/bin/sh xxxx" is invalid
        if (strncmp(fieldStr, "/bin/sh", forbidStrLen) == 0) {
            if (fieldStr[forbidStrLen] == ' ') {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
}

static void CheckService(const cJSON* curItem)
{
    if (curItem == nullptr) {
        return;
    }

609
    char *nameStr = cJSON_GetStringValue(cJSON_GetObjectItem(curItem, "name"));
M
mamingshuai 已提交
610 611 612 613 614 615
    if (nameStr == nullptr) {
        EXPECT_TRUE(nameStr != nullptr);
    } else {
        EXPECT_TRUE(strlen(nameStr) > 0);
    }

616
    cJSON *pathArgsItem = cJSON_GetObjectItem(curItem, "path");
M
mamingshuai 已提交
617 618 619 620
    EXPECT_TRUE(cJSON_IsArray(pathArgsItem));

    int pathArgsCnt = cJSON_GetArraySize(pathArgsItem);
    EXPECT_TRUE(pathArgsCnt > 0);
621
    EXPECT_TRUE(pathArgsCnt <= TEST_MAX_PATH_ARGS_CNT);
M
mamingshuai 已提交
622 623

    for (int i = 0; i < pathArgsCnt; ++i) {
624 625
        char *curParam = cJSON_GetStringValue(cJSON_GetArrayItem(pathArgsItem, i));
        EXPECT_TRUE(curParam != nullptr);
M
mamingshuai 已提交
626
        EXPECT_TRUE(strlen(curParam) > 0);
627
        EXPECT_TRUE(strlen(curParam) <= TEST_MAX_ONE_ARG_LEN);
M
mamingshuai 已提交
628 629 630 631 632
        if (i == 0) {
            EXPECT_TRUE(IsForbidden(curParam) == 0);
        }
    }

633
    cJSON *filedJ = cJSON_GetObjectItem(curItem, "uid");
634 635
    EXPECT_TRUE(cJSON_IsNumber(filedJ) || cJSON_IsString(filedJ));
    EXPECT_TRUE(cJSON_GetNumberValue(filedJ) >= 0.0 || cJSON_GetStringValue(filedJ));
M
mamingshuai 已提交
636 637

    filedJ = cJSON_GetObjectItem(curItem, "gid");
638 639
    EXPECT_TRUE(cJSON_IsNumber(filedJ) || cJSON_IsArray(filedJ));
    EXPECT_TRUE(cJSON_GetNumberValue(filedJ) >= 0.0 || cJSON_GetArraySize(filedJ) >= 0);
M
mamingshuai 已提交
640 641 642 643 644 645 646 647 648 649 650 651

    filedJ = cJSON_GetObjectItem(curItem, "once");
    EXPECT_TRUE(cJSON_IsNumber(filedJ));

    filedJ = cJSON_GetObjectItem(curItem, "importance");
    EXPECT_TRUE(cJSON_IsNumber(filedJ));

    filedJ = cJSON_GetObjectItem(curItem, "caps");
    EXPECT_TRUE(cJSON_IsArray(filedJ));
    int capsCnt = cJSON_GetArraySize(filedJ);
    EXPECT_TRUE(capsCnt <= MAX_CAPS_CNT_FOR_ONE_SERVICE);
    for (int i = 0; i < capsCnt; ++i) {
652
        cJSON *capJ = cJSON_GetArrayItem(filedJ, i);
653 654
        EXPECT_TRUE(cJSON_IsNumber(capJ) || cJSON_GetStringValue(capJ));
        EXPECT_TRUE(cJSON_GetNumberValue(capJ) >= 0.0 || cJSON_GetStringValue(capJ));
M
mamingshuai 已提交
655 656 657
    }
}

658
static void CheckServices(const cJSON *fileRoot)
M
mamingshuai 已提交
659 660
{
    int servArrSize = 0;
661
    cJSON *serviceArr = GetArrItem(fileRoot, servArrSize, SERVICE_ARR_NAME_IN_JSON);
M
mamingshuai 已提交
662
    EXPECT_TRUE(serviceArr != nullptr);
Z
zhong_ning 已提交
663
    EXPECT_TRUE(servArrSize <= MAX_SERVICES_COUNT_IN_FILE);
M
mamingshuai 已提交
664 665

    for (int i = 0; i < servArrSize; ++i) {
666
        cJSON *curItem = cJSON_GetArrayItem(serviceArr, i);
M
mamingshuai 已提交
667 668 669 670 671
        EXPECT_TRUE(curItem != nullptr);
        CheckService(curItem);
    }
}

672
static void CheckCmd(const TestCmdLine *resCmd)
M
mamingshuai 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
{
    EXPECT_TRUE(strlen(resCmd->name) > 0);
    EXPECT_TRUE(strlen(resCmd->cmdContent) > 0);

    if (strcmp("start ", resCmd->name) == 0) {
        for (size_t i = 0; i < strlen(resCmd->cmdContent); ++i) {
            EXPECT_NE(' ', resCmd->cmdContent[i]);    // no spaces in service name
        }
    } else if (strcmp("mkdir ", resCmd->name) == 0) {
        for (size_t i = 0; i < strlen(resCmd->cmdContent); ++i) {
            EXPECT_NE('.', resCmd->cmdContent[i]);    // no dots in path string
        }
    } else if (strcmp("chmod ", resCmd->name) == 0) {
        EXPECT_TRUE(strlen(resCmd->cmdContent) >= 6);    // 0xxx x    at least 6 characters
        EXPECT_EQ('0', resCmd->cmdContent[0]);
        EXPECT_EQ(' ', resCmd->cmdContent[4]);    // 4 bytes, after 0xxx must be space
        for (int i = 1; i < 4; ++i) {    // 4 bytes, 0xxx, xxx must be digits
            EXPECT_TRUE(resCmd->cmdContent[i] >= '0' && resCmd->cmdContent[i] <= '7');
        }
        for (size_t i = 5; i < strlen(resCmd->cmdContent); ++i) {    // target starts from index 5
            EXPECT_NE(' ', resCmd->cmdContent[i]);    // no spaces allowed
        }
    } else if (strcmp("chown ", resCmd->name) == 0) {
        EXPECT_TRUE(strlen(resCmd->cmdContent) >= 5);    // x y z   at least 5 characters
        EXPECT_NE(' ', resCmd->cmdContent[0]);           // should not start with space
        EXPECT_NE(' ', resCmd->cmdContent[strlen(resCmd->cmdContent) - 1]);  // should not end with space
        size_t spacePos = 0;
        size_t spaceCnt = 0;
        for (size_t i = 1; i < strlen(resCmd->cmdContent); ++i) {
M
Mupceet 已提交
702 703
            if (resCmd->cmdContent[i] != ' ') {
                continue;
M
mamingshuai 已提交
704
            }
M
Mupceet 已提交
705 706 707 708 709
            ++spaceCnt;
            if (spacePos != 0) {
                EXPECT_NE(spacePos + 1, i);    // consecutive spaces should not appear
            }
            spacePos = i;
M
mamingshuai 已提交
710 711 712 713 714 715
        }
        EXPECT_EQ(spaceCnt, 2);    // 2 spaces allowed in cmd content
    } else if (strcmp("mount ", resCmd->name) == 0) {
        EXPECT_NE(' ', resCmd->cmdContent[0]);    // should not start with space
    } else if (strcmp("loadcfg ", resCmd->name) == 0) {
        EXPECT_NE(' ', resCmd->cmdContent[0]);   // should not start with space
X
xionglei6 已提交
716 717
    } else if (strcmp("export ", resCmd->name) == 0) {
        EXPECT_NE(' ', resCmd->cmdContent[0]);   // should not start with space
M
mamingshuai 已提交
718 719 720 721 722
    } else {    // unknown cmd
        EXPECT_TRUE(false);
    }
}

723
static void CheckJob(const cJSON *jobItem)
M
mamingshuai 已提交
724 725 726 727 728
{
    if (jobItem == nullptr) {
        return;
    }

729
    cJSON *cmdsItem = cJSON_GetObjectItem(jobItem, CMDS_ARR_NAME_IN_JSON.c_str());
M
mamingshuai 已提交
730 731 732 733 734 735 736
    EXPECT_TRUE(cmdsItem != nullptr);
    EXPECT_TRUE(cJSON_IsArray(cmdsItem));

    int cmdLinesCnt = cJSON_GetArraySize(cmdsItem);
    EXPECT_TRUE(cmdLinesCnt <= MAX_CMD_CNT_IN_ONE_JOB);

    for (int i = 0; i < cmdLinesCnt; ++i) {
737
        char *cmdLineStr = cJSON_GetStringValue(cJSON_GetArrayItem(cmdsItem, i));
M
mamingshuai 已提交
738 739 740
        EXPECT_TRUE(cmdLineStr != nullptr);
        EXPECT_TRUE(strlen(cmdLineStr) > 0);

741
        TestCmdLine resCmd;
M
mamingshuai 已提交
742 743 744 745 746 747 748 749 750
        (void)memset_s(&resCmd, sizeof(resCmd), 0, sizeof(resCmd));
        ParseCmdLine(cmdLineStr, &resCmd);
        CheckCmd(&resCmd);
    }
}

static void CheckJobs(const cJSON* fileRoot)
{
    int jobArrSize = 0;
751
    cJSON *jobArr = GetArrItem(fileRoot, jobArrSize, JOBS_ARR_NAME_IN_JSON);
M
mamingshuai 已提交
752 753 754 755 756 757 758
    EXPECT_TRUE(jobArr != nullptr);
    EXPECT_TRUE(jobArrSize == JOBS_IN_FILE_COUNT);

    bool findPreInit = false;
    bool findInit = false;
    bool findPostInit = false;
    for (int i = 0; i < jobArrSize; ++i) {
759
        cJSON *jobItem = cJSON_GetArrayItem(jobArr, i);
M
mamingshuai 已提交
760
        EXPECT_TRUE(jobItem != nullptr);
761
        char *jobNameStr = cJSON_GetStringValue(cJSON_GetObjectItem(jobItem, "name"));
M
mamingshuai 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
        EXPECT_TRUE(jobNameStr != nullptr);
        if (strcmp(jobNameStr, "pre-init") == 0) {
            findPreInit = true;
        } else if (strcmp(jobNameStr, "init") == 0) {
            findInit = true;
        }  else if (strcmp(jobNameStr, "post-init") == 0) {
            findPostInit = true;
        } else {
            EXPECT_TRUE(false);    // unknown job name
            continue;
        }

        CheckJob(jobItem);
    }

    EXPECT_TRUE(findPreInit && findInit && findPostInit);
}

/*
X
xionglei6 已提交
781 782 783 784
 * @tc.name: cfgCheckContent_001
 * @tc.desc: init.cfg file content check
 * @tc.type: FUNC
 */
785
HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level0)
M
mamingshuai 已提交
786
{
787
    char *fileBuf = ReadFileToBuf();
M
mamingshuai 已提交
788 789 790 791 792
    if (fileBuf == nullptr) {
        EXPECT_TRUE(fileBuf != nullptr);
        return;
    }

793
    cJSON *fileRoot = cJSON_Parse(fileBuf);
M
mamingshuai 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
    free(fileBuf);
    fileBuf = nullptr;

    EXPECT_TRUE(fileRoot != nullptr);

    CheckServices(fileRoot);
    CheckJobs(fileRoot);
    cJSON_Delete(fileRoot);
    fileRoot = nullptr;
}

/*
 * @tc.name: CreateIllegalCfg
 * @tc.desc: Create illegal Config file for testing
 * @tc.type: FUNC
 */
static void CreateIllegalCfg()
{
812
    FILE *testCfgFile = fopen(TEST_CFG_ILLEGAL.c_str(), "w+");
M
mamingshuai 已提交
813 814 815 816 817 818
    if (testCfgFile == nullptr) {
        return;
    }

    std::string writeContent = "mount zpfs /patch/etc:/etc /etc";
    if (fwrite(writeContent.c_str(), writeContent.length(), 1, testCfgFile) != 1) {
M
Mupceet 已提交
819
        (void)fclose(testCfgFile);
M
mamingshuai 已提交
820 821 822
        return;
    }

M
Mupceet 已提交
823
    (void)fclose(testCfgFile);
M
mamingshuai 已提交
824 825 826 827 828 829 830
}

/*
 * @tc.name: cmdFuncDoLoadCfgTest_001
 * @tc.desc: parse function, parse success test
 * @tc.type: FUNC
 */
831
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0)
M
mamingshuai 已提交
832
{
833
    TestCmdLine curCmdLine;
C
cheng_jinsong 已提交
834
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
M
mamingshuai 已提交
835 836 837 838 839 840 841 842 843 844 845

    ParseCmdLine("loadcfg /patch/fstab.cfg", &curCmdLine);
    EXPECT_EQ(0, strcmp("loadcfg ", curCmdLine.name));
    EXPECT_EQ(0, strcmp("/patch/fstab.cfg", curCmdLine.cmdContent));
};

/*
 * @tc.name: cmdFuncDoLoadCfgTest_002
 * @tc.desc: fstab.cfg file fail test
 * @tc.type: FUNC
 */
846
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0)
M
mamingshuai 已提交
847
{
848
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
849 850 851 852
    std::string cmdStr = "loadcfg ";
    std::string cmdContentStr = "/patch/file_not_exist.cfg";
    struct stat testCfgStat = {0};

C
cheng_jinsong 已提交
853
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
Z
zhong_ning 已提交
854 855
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
856 857 858 859 860 861 862 863
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    stat(cmdContentStr.c_str(), &testCfgStat);
    EXPECT_TRUE(testCfgStat.st_size == 0);
    DoCmd(&curCmdLine);

    cmdContentStr = TEST_CFG_ILLEGAL;
    CreateIllegalCfg();
C
cheng_jinsong 已提交
864
    (void)memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
Z
zhong_ning 已提交
865 866
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
867 868 869 870 871 872 873 874
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    EXPECT_EQ(0, stat(cmdContentStr.c_str(), &testCfgStat));
    EXPECT_TRUE(testCfgStat.st_size > 0);
    DoCmd(&curCmdLine);

    // remove tmp file
    if (remove(TEST_CFG_ILLEGAL.c_str()) != 0) {
X
xlei1030 已提交
875
        EXPECT_EQ(0, 0);
M
mamingshuai 已提交
876 877 878 879 880 881 882 883
    }
}

/*
 * @tc.name: cmdFuncDoLoadCfgTest_003
 * @tc.desc: fstab.cfg file success test
 * @tc.type: FUNC
 */
884
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0)
M
mamingshuai 已提交
885
{
886
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
887 888 889 890
    std::string cmdStr = "loadcfg ";
    std::string cmdContentStr = "/patch/fstab.cfg";
    char buf[CAT_BUF_SIZE] = {0};
    struct stat testCfgStat = {0};
891
    FILE *fd = nullptr;
M
mamingshuai 已提交
892 893
    size_t size;
    bool hasZpfs = false;
Z
zhong_ning 已提交
894 895
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));

    DoCmd(&curCmdLine);

    stat(cmdContentStr.c_str(), &testCfgStat);
    if (testCfgStat.st_size > 0) {
        fd = fopen(TEST_PROC_MOUNTS.c_str(), "r");

        if (fd == nullptr) {
            EXPECT_TRUE(fd != nullptr);
            return;
        }

        do {
S
sun_fan 已提交
911
            size = fread(buf, 1, CAT_BUF_SIZE - 1, fd);
M
mamingshuai 已提交
912 913 914 915
            if (size < 0) {
                EXPECT_TRUE(size >= 0);
                break;
            }
S
sun_fan 已提交
916
            buf[CAT_BUF_SIZE - 1] = 0;
M
mamingshuai 已提交
917 918 919 920 921
            if (strstr(buf, "zpfs") != nullptr) {
                hasZpfs = true;
                break;
            }
        } while (size > 0);
X
xionglei6 已提交
922
        EXPECT_TRUE(hasZpfs);
M
mamingshuai 已提交
923 924 925 926 927 928 929 930 931
        fclose(fd);
    }
}

/*
 * @tc.name: cmdJobTest_001
 * @tc.desc: job functions test
 * @tc.type: FUNC
 */
932
HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0)
M
mamingshuai 已提交
933 934 935 936 937 938
{
    // functions do not crash
    ParseAllJobs(nullptr);
    DoJob(nullptr);
    DoJob("job name does not exist");
    ReleaseAllJobs();
X
xionglei6 已提交
939
    StartServiceByName("service name does not exist");
X
xionglei6 已提交
940
    StopAllServices(0, nullptr, 0, nullptr);
M
mamingshuai 已提交
941 942 943 944 945 946 947 948 949 950
    ServiceReap(nullptr);
    EXPECT_NE(0, ServiceStart(nullptr));
    EXPECT_NE(0, ServiceStop(nullptr));
}

/*
 * @tc.name: cmdJobTest_002
 * @tc.desc: job functions test
 * @tc.type: FUNC
 */
951
HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level0)
M
mamingshuai 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
{
    std::string cfgJson = "{\"jobs\":[{\"name\":\"pre-init\",\"cmds\":[\"mkdir " +
        PRE_INIT_DIR + "\"]},{\"name\":\"init\",\"cmds\":[\"mkdir " + INIT_DIR +
        "\"]},{\"name\":\"post-init\",\"cmds\":[\"mkdir " + POST_INIT_DIR + "\"]}]}";
    cJSON* jobItem = cJSON_Parse(cfgJson.c_str());
    EXPECT_NE(nullptr, jobItem);
    if (jobItem == nullptr) {
        return;
    }
    ParseAllJobs(jobItem);
    DoJob("pre-init");
    DoJob("init");
    DoJob("post-init");

    // check if dir exists
    struct stat postDirStat = {0};
    EXPECT_EQ(0, stat(POST_INIT_DIR.c_str(), &postDirStat));

    // release resource
    cJSON_Delete(jobItem);
    if (remove(POST_INIT_DIR.c_str()) != 0 ||
        remove(INIT_DIR.c_str()) != 0 ||
        remove(PRE_INIT_DIR.c_str()) != 0) {
    }
    ReleaseAllJobs();
}
}  // namespace OHOS