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

M
mamingshuai 已提交
23
#include "cJSON.h"
24
#include "init_cmds.h"
Z
zhong_ning 已提交
25
#include "init_jobs_internal.h"
M
mamingshuai 已提交
26
#include "init_service_manager.h"
27 28
#include "securec.h"
#include "gtest/gtest.h"
M
mamingshuai 已提交
29

30
using namespace std;
M
mamingshuai 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
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";
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;

// init.cfg releated
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;
52
const int JOBS_IN_FILE_COUNT = 3; // pre-init, init, post-init
Z
zhong_ning 已提交
53
const int MAX_SERVICES_COUNT_IN_FILE = 100;
M
mamingshuai 已提交
54
const int MAX_CAPS_CNT_FOR_ONE_SERVICE = 100;
55 56 57 58 59
const unsigned int MAX_CAPABILITY_VALUE = 4294967295; // 0xFFFFFFFF
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 已提交
60 61 62 63 64 65 66

// job test releated
const pid_t INVALID_PID = -1;
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";

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

M
mamingshuai 已提交
72 73 74 75 76 77 78 79 80 81
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 "));
82
        const mode_t mode = 0755;
M
mamingshuai 已提交
83 84 85 86 87 88 89 90
        if (mkdir(TEST_DRI.c_str(), mode) != 0) {
            if (errno != EEXIST) {
                printf("[----------] StartupInitUTest, mkdir for %s failed, error %d.\n",\
                    TEST_DRI.c_str(), errno);
                return;
            }
        }

91
        FILE *testFile = fopen(TEST_FILE.c_str(), "w+");
M
mamingshuai 已提交
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        if (testFile == nullptr) {
            printf("[----------] StartupInitUTest, open file %s failed, error %d.\n",\
                TEST_FILE.c_str(), errno);
            return;
        }

        std::string writeContent = "This is a test file for startup subsystem init module.";
        if (fwrite(writeContent.c_str(), writeContent.length(), 1, testFile) != 1) {
            printf("[----------] StartupInitUTest, open file %s failed, error %d.\n", TEST_FILE.c_str(), errno);
            fclose(testFile);
            return;
        }
        fclose(testFile);

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

        if (chmod(TEST_FILE.c_str(), TEST_FILE_MODE) != 0) {
            printf("[----------] StartupInitUTest, chmod for file %s failed, error %d.\n", TEST_FILE.c_str(), errno);
            return;
        }

        if (chown(TEST_FILE.c_str(), TEST_FILE_UID, TEST_FILE_GID) != 0) {
            printf("[----------] StartupInitUTest, chown for file %s failed, error %d.\n", TEST_FILE.c_str(), errno);
            return;
        }

#endif  // USE_EMMC_STORAGE

        printf("[----------] StartupInitUTest, cmd func test setup.\n");
    }

    static void TearDownTestCase()
    {
        if (remove(TEST_FILE.c_str()) != 0) {
            printf("[----------] StartupInitUTest, remove %s failed, error %d.\n", TEST_FILE.c_str(), errno);
        }

        if (remove(TEST_DRI.c_str()) != 0) {
            printf("[----------] StartupInitUTest, remove %s failed, error %d.\n", TEST_DRI.c_str(), errno);
        }
        printf("[----------] StartupInitUTest, cmd func test teardown.\n");
    }
    void SetUp() {}
    void TearDown() {}
};

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 164 165 166 167 168 169 170 171 172
void ParseCmdLine(const char *content, TestCmdLine *resCmd)
{
    if (content == nullptr || resCmd == nullptr) {
        return;
    }

    const struct CmdTable *cmd = GetCmdByName(content);
    if (cmd == nullptr) {
        printf("Cannot support command: %s \n", content);
        (void)memset_s(resCmd, sizeof(TestCmdLine), 0, sizeof(TestCmdLine));
        return;
    }
    if (strlen(content) <= (strlen(cmd->name) + 1)) {
        printf("Error content for command: %s \n", content);
        (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) {
        printf("Error copy command: %s \n", content);
        (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 已提交
173 174 175 176
/*
 ** @tc.name: cmdFuncParseCmdTest_001
 ** @tc.desc: parse function, nullptr test
 ** @tc.type: FUNC
177
 ** @tc.require:
M
mamingshuai 已提交
178
 **/
179
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level0)
M
mamingshuai 已提交
180 181 182 183 184 185 186 187 188
{
    // do not crash
    ParseCmdLine(nullptr, nullptr);
};

/*
 ** @tc.name: cmdFuncParseCmdTest_002
 ** @tc.desc: parse function, invalid strings test
 ** @tc.type: FUNC
189
 ** @tc.require:
M
mamingshuai 已提交
190
 **/
191
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0)
M
mamingshuai 已提交
192
{
193
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));

    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));
}

/*
 ** @tc.name: cmdFuncParseCmdTest_003
 ** @tc.desc: parse function, cmd content empty test
 ** @tc.type: FUNC
217
 ** @tc.require:
M
mamingshuai 已提交
218
 **/
219
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0)
M
mamingshuai 已提交
220
{
221
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));

    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));
    }
}

/*
 ** @tc.name: cmdFuncParseCmdTest_004
 ** @tc.desc: parse function, cmd content too long test
 ** @tc.type: FUNC
235
 ** @tc.require:
M
mamingshuai 已提交
236
 **/
237
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0)
M
mamingshuai 已提交
238
{
239
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
240 241 242 243 244 245 246
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));

    char toLongContent[MAX_CMD_CONTENT_LEN + 10];
    memset_s(toLongContent, MAX_CMD_CONTENT_LEN + 10, 'x', MAX_CMD_CONTENT_LEN + 9);
    toLongContent[MAX_CMD_CONTENT_LEN + 9] = '\0';
    for (size_t i = 0; i < g_supportedCmds.size(); ++i) {
        size_t curCmdLen = g_supportedCmds[i].length();
247
        char *curCmd = (char *)malloc(curCmdLen + MAX_CMD_CONTENT_LEN + 10);
M
mamingshuai 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        if (curCmd == nullptr) {
            printf("[----------] StartupInitUTest, cmdFuncParseCmdTest004, malloc failed.\n");
            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) {
            printf("[----------] StartupInitUTest, cmdFuncParseCmdTest004, memcpy_s failed.\n");
            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;
    }
}

/*
 ** @tc.name: cmdFuncParseCmdTest_005
 ** @tc.desc: parse function, parse success test
 ** @tc.type: FUNC
276
 ** @tc.require:
M
mamingshuai 已提交
277
 **/
278
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0)
M
mamingshuai 已提交
279
{
280
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));

    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));
};

/*
 ** @tc.name: cmdFuncDoCmdTest_001
 ** @tc.desc: do cmd function, nullptr test
 ** @tc.type: FUNC
308
 ** @tc.require:
M
mamingshuai 已提交
309
 **/
310
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0)
M
mamingshuai 已提交
311 312 313 314 315 316 317 318 319
{
    // do not crash here
    DoCmd(nullptr);
}

/*
 ** @tc.name: cmdFuncDoCmdTest_002
 ** @tc.desc: do cmd function, do start fail test
 ** @tc.type: FUNC
320
 ** @tc.require:
M
mamingshuai 已提交
321
 **/
322
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level0)
M
mamingshuai 已提交
323
{
324
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
325 326 327 328
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));

    std::string cmdStr = "start ";
    std::string cmdContentStr = "NameNotExist";
Z
zhong_ning 已提交
329 330
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
331 332 333 334 335 336 337 338 339
    EXPECT_EQ(0, strcmp(cmdStr.c_str(), curCmdLine.name));
    EXPECT_EQ(0, strcmp(cmdContentStr.c_str(), curCmdLine.cmdContent));
    DoCmd(&curCmdLine);
}

/*
 ** @tc.name: cmdFuncDoCmdTest_003
 ** @tc.desc: do cmd function, do mkdir fail test
 ** @tc.type: FUNC
340
 ** @tc.require:
M
mamingshuai 已提交
341
 **/
342
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level0)
M
mamingshuai 已提交
343
{
344
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
345 346 347 348
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));

    std::string cmdStr = "mkdir ";
    std::string cmdContentStr = "/DirNotExist/DirNotExist/DirNotExist";
Z
zhong_ning 已提交
349 350
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
351 352 353 354 355
    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
356
    DIR *dirTmp = opendir(cmdContentStr.c_str());
M
mamingshuai 已提交
357 358 359 360 361 362 363
    EXPECT_TRUE(dirTmp == nullptr);
    EXPECT_TRUE(errno == ENOENT);
    if (dirTmp != nullptr) {    // just in case
        closedir(dirTmp);
        dirTmp = nullptr;
    }

Z
zhong_ning 已提交
364 365
    // error argument count, bad format
    cmdContentStr = "  /storage/data/cmdFuncDoCmdTest003 0755 system";
Z
zhong_ning 已提交
366 367
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    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;
    }
}

/*
 ** @tc.name: cmdFuncDoCmdTest_004
 ** @tc.desc: do cmd function, do chmod fail test
 ** @tc.type: FUNC
386
 ** @tc.require:
M
mamingshuai 已提交
387
 **/
388
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0)
M
mamingshuai 已提交
389
{
390
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
391 392 393 394
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));

    std::string cmdStr = "chmod ";
    std::string cmdContentStr = "755 " + TEST_FILE;    // should be 0755, wrong format here
Z
zhong_ning 已提交
395 396
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
397 398 399 400 401
    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 已提交
402 403
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command .c_str(), &curCmdLine);
M
mamingshuai 已提交
404 405 406 407 408
    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 已提交
409 410
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
411 412 413 414 415
    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 已提交
416 417
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
418 419 420 421 422
    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 已提交
423 424
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    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
}

/*
 ** @tc.name: cmdFuncDoCmdTest_005
 ** @tc.desc: do cmd function, do chown fail test
 ** @tc.type: FUNC
443
 ** @tc.require:
M
mamingshuai 已提交
444
 **/
445
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0)
M
mamingshuai 已提交
446
{
447
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
448 449 450 451
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));

    std::string cmdStr = "chown ";
    std::string cmdContentStr = "888 " + TEST_FILE;    // uid or gid missing, wrong format here
Z
zhong_ning 已提交
452 453
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
454 455 456 457 458
    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 已提交
459 460
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    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
}

/*
 ** @tc.name: cmdFuncDoCmdTest_006
 ** @tc.desc: do cmd function, do success test
 ** @tc.type: FUNC
480
 ** @tc.require:
M
mamingshuai 已提交
481
 **/
482
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0)
M
mamingshuai 已提交
483
{
484
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
485 486 487 488

    // mkdir success
    std::string cmdStr = "mkdir ";
    std::string cmdContentStr = TEST_DRI + "/cmdFuncDoCmdTest006";
Z
zhong_ning 已提交
489 490
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    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) {
        printf("[----------] StartupInitUTest, cmdFuncDoCmdTest006 remove %s failed, error %d.\n",\
            TEST_DRI.c_str(), errno);
    }

    // chmod success
    cmdStr = "chmod ";
    cmdContentStr = "0440 " + TEST_FILE;
Z
zhong_ning 已提交
511 512
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
    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 已提交
529 530
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    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
}

/*
 ** @tc.name: cfgCheckStat_001
 ** @tc.desc: init.cfg file state check
 ** @tc.type: FUNC
548
 ** @tc.require:
M
mamingshuai 已提交
549
 **/
550
HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level0)
M
mamingshuai 已提交
551 552 553 554 555 556 557 558 559 560 561 562
{
    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()
{
563 564
    char *buffer = nullptr;
    FILE *fd = nullptr;
M
mamingshuai 已提交
565 566 567 568 569 570 571 572
    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 已提交
573
        buffer = static_cast<char*>(malloc(static_cast<size_t>(fileStat.st_size) + 1));
M
mamingshuai 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
        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;
}

593
static cJSON *GetArrItem(const cJSON *fileRoot, int &arrSize, const std::string &arrName)
M
mamingshuai 已提交
594
{
595
    cJSON *arrItem = cJSON_GetObjectItemCaseSensitive(fileRoot, arrName.c_str());
M
mamingshuai 已提交
596 597 598 599 600 601 602
    arrSize = cJSON_GetArraySize(arrItem);
    if (arrSize <= 0) {
        return nullptr;
    }
    return arrItem;
}

603
static int IsForbidden(const char *fieldStr)
M
mamingshuai 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
{
    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;
    }

631
    char *nameStr = cJSON_GetStringValue(cJSON_GetObjectItem(curItem, "name"));
M
mamingshuai 已提交
632 633 634 635 636 637
    if (nameStr == nullptr) {
        EXPECT_TRUE(nameStr != nullptr);
    } else {
        EXPECT_TRUE(strlen(nameStr) > 0);
    }

638
    cJSON *pathArgsItem = cJSON_GetObjectItem(curItem, "path");
M
mamingshuai 已提交
639 640 641 642
    EXPECT_TRUE(cJSON_IsArray(pathArgsItem));

    int pathArgsCnt = cJSON_GetArraySize(pathArgsItem);
    EXPECT_TRUE(pathArgsCnt > 0);
643
    EXPECT_TRUE(pathArgsCnt <= TEST_MAX_PATH_ARGS_CNT);
M
mamingshuai 已提交
644 645

    for (int i = 0; i < pathArgsCnt; ++i) {
646 647
        char *curParam = cJSON_GetStringValue(cJSON_GetArrayItem(pathArgsItem, i));
        EXPECT_TRUE(curParam != nullptr);
M
mamingshuai 已提交
648
        EXPECT_TRUE(strlen(curParam) > 0);
649
        EXPECT_TRUE(strlen(curParam) <= TEST_MAX_ONE_ARG_LEN);
M
mamingshuai 已提交
650 651 652 653 654
        if (i == 0) {
            EXPECT_TRUE(IsForbidden(curParam) == 0);
        }
    }

655
    cJSON *filedJ = cJSON_GetObjectItem(curItem, "uid");
M
mamingshuai 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    EXPECT_TRUE(cJSON_IsNumber(filedJ));
    EXPECT_TRUE(cJSON_GetNumberValue(filedJ) >= 0.0);

    filedJ = cJSON_GetObjectItem(curItem, "gid");
    EXPECT_TRUE(cJSON_IsNumber(filedJ));
    EXPECT_TRUE(cJSON_GetNumberValue(filedJ) >= 0.0);

    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) {
674
        cJSON *capJ = cJSON_GetArrayItem(filedJ, i);
M
mamingshuai 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687
        EXPECT_TRUE(cJSON_IsNumber(capJ));
        EXPECT_TRUE(cJSON_GetNumberValue(capJ) >= 0.0);

        // only shell can have all capabilities
        if ((unsigned int)cJSON_GetNumberValue(capJ) == MAX_CAPABILITY_VALUE) {
            if (nameStr != nullptr) {
                EXPECT_EQ(0, strcmp(nameStr, "shell"));
            }
            EXPECT_EQ(1, capsCnt);
        }
    }
}

688
static void CheckServices(const cJSON *fileRoot)
M
mamingshuai 已提交
689 690
{
    int servArrSize = 0;
691
    cJSON *serviceArr = GetArrItem(fileRoot, servArrSize, SERVICE_ARR_NAME_IN_JSON);
M
mamingshuai 已提交
692
    EXPECT_TRUE(serviceArr != nullptr);
Z
zhong_ning 已提交
693
    EXPECT_TRUE(servArrSize <= MAX_SERVICES_COUNT_IN_FILE);
M
mamingshuai 已提交
694 695

    for (int i = 0; i < servArrSize; ++i) {
696
        cJSON *curItem = cJSON_GetArrayItem(serviceArr, i);
M
mamingshuai 已提交
697 698 699 700 701
        EXPECT_TRUE(curItem != nullptr);
        CheckService(curItem);
    }
}

702
static void CheckCmd(const TestCmdLine *resCmd)
M
mamingshuai 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
{
    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 spaces in path string
            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
            EXPECT_NE('.', resCmd->cmdContent[i]);    // no dots 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) {
            if (resCmd->cmdContent[i] == ' ') {
                ++spaceCnt;
                if (spacePos != 0) {
                    EXPECT_NE(spacePos + 1, i);    // consecutive spaces should not appear
                }
                spacePos = i;
            }
        }
        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
    } else {    // unknown cmd
        EXPECT_TRUE(false);
    }
}

752
static void CheckJob(const cJSON *jobItem)
M
mamingshuai 已提交
753 754 755 756 757
{
    if (jobItem == nullptr) {
        return;
    }

758
    cJSON *cmdsItem = cJSON_GetObjectItem(jobItem, CMDS_ARR_NAME_IN_JSON.c_str());
M
mamingshuai 已提交
759 760 761 762 763 764 765
    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) {
766
        char *cmdLineStr = cJSON_GetStringValue(cJSON_GetArrayItem(cmdsItem, i));
M
mamingshuai 已提交
767 768 769
        EXPECT_TRUE(cmdLineStr != nullptr);
        EXPECT_TRUE(strlen(cmdLineStr) > 0);

770
        TestCmdLine resCmd;
M
mamingshuai 已提交
771 772 773 774 775 776 777 778 779
        (void)memset_s(&resCmd, sizeof(resCmd), 0, sizeof(resCmd));
        ParseCmdLine(cmdLineStr, &resCmd);
        CheckCmd(&resCmd);
    }
}

static void CheckJobs(const cJSON* fileRoot)
{
    int jobArrSize = 0;
780
    cJSON *jobArr = GetArrItem(fileRoot, jobArrSize, JOBS_ARR_NAME_IN_JSON);
M
mamingshuai 已提交
781 782 783 784 785 786 787
    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) {
788
        cJSON *jobItem = cJSON_GetArrayItem(jobArr, i);
M
mamingshuai 已提交
789
        EXPECT_TRUE(jobItem != nullptr);
790
        char *jobNameStr = cJSON_GetStringValue(cJSON_GetObjectItem(jobItem, "name"));
M
mamingshuai 已提交
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
        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);
}

/*
 ** @tc.name: cfgCheckContent_001
 ** @tc.desc: init.cfg file content check
 ** @tc.type: FUNC
813
 ** @tc.require:
M
mamingshuai 已提交
814
 **/
815
HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level0)
M
mamingshuai 已提交
816
{
817
    char *fileBuf = ReadFileToBuf();
M
mamingshuai 已提交
818 819 820 821 822
    if (fileBuf == nullptr) {
        EXPECT_TRUE(fileBuf != nullptr);
        return;
    }

823
    cJSON *fileRoot = cJSON_Parse(fileBuf);
M
mamingshuai 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    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
839
 * @tc.require:
M
mamingshuai 已提交
840 841 842
 */
static void CreateIllegalCfg()
{
843
    FILE *testCfgFile = fopen(TEST_CFG_ILLEGAL.c_str(), "w+");
M
mamingshuai 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    if (testCfgFile == nullptr) {
        printf("[----------] StartupInitUTest, open file %s failed, error %d.\n", TEST_CFG_ILLEGAL.c_str(), errno);
        return;
    }

    std::string writeContent = "mount zpfs /patch/etc:/etc /etc";
    if (fwrite(writeContent.c_str(), writeContent.length(), 1, testCfgFile) != 1) {
        printf("[----------] StartupInitUTest, open file %s failed, error %d.\n", TEST_CFG_ILLEGAL.c_str(), errno);
        fclose(testCfgFile);
        return;
    }

    fclose(testCfgFile);
}

/*
 * @tc.name: cmdFuncDoLoadCfgTest_001
 * @tc.desc: parse function, parse success test
 * @tc.type: FUNC
863
 * @tc.require:
M
mamingshuai 已提交
864
 */
865
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0)
M
mamingshuai 已提交
866
{
867
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
868 869 870 871 872 873 874 875 876 877 878
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));

    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
879
 * @tc.require:
M
mamingshuai 已提交
880
 */
881
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0)
M
mamingshuai 已提交
882
{
883
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
884 885 886 887 888
    std::string cmdStr = "loadcfg ";
    std::string cmdContentStr = "/patch/file_not_exist.cfg";
    struct stat testCfgStat = {0};

    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
Z
zhong_ning 已提交
889 890
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
891 892 893 894 895 896 897 898 899
    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();
    memset_s(&curCmdLine, sizeof(curCmdLine), 0, sizeof(curCmdLine));
Z
zhong_ning 已提交
900 901
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    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) {
        printf("[----------] StartupInitUTest, remove %s failed, error %d.\n",\
            TEST_CFG_ILLEGAL.c_str(), errno);
    }
}

/*
 * @tc.name: cmdFuncDoLoadCfgTest_003
 * @tc.desc: fstab.cfg file success test
 * @tc.type: FUNC
919
 * @tc.require:
M
mamingshuai 已提交
920
 */
921
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0)
M
mamingshuai 已提交
922
{
923
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
924 925 926 927
    std::string cmdStr = "loadcfg ";
    std::string cmdContentStr = "/patch/fstab.cfg";
    char buf[CAT_BUF_SIZE] = {0};
    struct stat testCfgStat = {0};
928
    FILE *fd = nullptr;
M
mamingshuai 已提交
929 930
    size_t size;
    bool hasZpfs = false;
Z
zhong_ning 已提交
931 932
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
    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 已提交
948
            size = fread(buf, 1, CAT_BUF_SIZE - 1, fd);
M
mamingshuai 已提交
949 950 951 952
            if (size < 0) {
                EXPECT_TRUE(size >= 0);
                break;
            }
S
sun_fan 已提交
953
            buf[CAT_BUF_SIZE - 1] = 0;
M
mamingshuai 已提交
954 955 956 957 958 959 960 961 962 963 964 965 966 967
            if (strstr(buf, "zpfs") != nullptr) {
                hasZpfs = true;
                break;
            }
        } while (size > 0);
        EXPECT_TRUE(hasZpfs == true);
        fclose(fd);
    }
}

/*
 * @tc.name: cmdJobTest_001
 * @tc.desc: job functions test
 * @tc.type: FUNC
968
 * @tc.require:
M
mamingshuai 已提交
969
 */
970
HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0)
M
mamingshuai 已提交
971 972 973 974 975 976
{
    // functions do not crash
    ParseAllJobs(nullptr);
    DoJob(nullptr);
    DoJob("job name does not exist");
    ReleaseAllJobs();
S
sun_fan 已提交
977
    StartServiceByName("service name does not exist", false);
978
    StopAllServices(0);
M
mamingshuai 已提交
979 980 981 982 983 984 985 986 987
    ServiceReap(nullptr);
    EXPECT_NE(0, ServiceStart(nullptr));
    EXPECT_NE(0, ServiceStop(nullptr));
}

/*
 * @tc.name: cmdJobTest_002
 * @tc.desc: job functions test
 * @tc.type: FUNC
988
 * @tc.require:
M
mamingshuai 已提交
989
 */
990
HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level0)
M
mamingshuai 已提交
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
{
    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) {
        printf("[----------] StartupInitUTest, job test, parse %s failed.\n", cfgJson.c_str());
        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) {
        printf("[----------] StartupInitUTest, job test, remove failed, error %d.\n", errno);
    }
    ReleaseAllJobs();
}
}  // namespace OHOS