cmd_func_test.cpp 31.1 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
Mupceet 已提交
24
#include "beget_ext.h"
M
mamingshuai 已提交
25
#include "cJSON.h"
26
#include "init_cmds.h"
Z
zhong_ning 已提交
27
#include "init_jobs_internal.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()
    {
C
cheng_jinsong 已提交
127
        EnableInitLog();
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;
M
mamingshuai 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    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));
}

/*
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;
M
mamingshuai 已提交
210 211 212 213 214 215 216 217 218 219
    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));
    }
}

/*
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;
M
mamingshuai 已提交
227 228 229 230 231 232 233
    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();
234
        char *curCmd = (char *)malloc(curCmdLen + MAX_CMD_CONTENT_LEN + 10);
M
mamingshuai 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        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 已提交
258 259 260 261
 * @tc.name: cmdFuncParseCmdTest_005
 * @tc.desc: parse function, parse success test
 * @tc.type: FUNC
 */
262
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0)
M
mamingshuai 已提交
263
{
264
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    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));
};

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

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

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

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

    std::string cmdStr = "mkdir ";
    std::string cmdContentStr = "/DirNotExist/DirNotExist/DirNotExist";
Z
zhong_ning 已提交
330 331
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
332 333 334 335 336
    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
337
    DIR *dirTmp = opendir(cmdContentStr.c_str());
M
mamingshuai 已提交
338 339 340 341 342 343 344
    EXPECT_TRUE(dirTmp == nullptr);
    EXPECT_TRUE(errno == ENOENT);
    if (dirTmp != nullptr) {    // just in case
        closedir(dirTmp);
        dirTmp = nullptr;
    }

Z
zhong_ning 已提交
345 346
    // error argument count, bad format
    cmdContentStr = "  /storage/data/cmdFuncDoCmdTest003 0755 system";
Z
zhong_ning 已提交
347 348
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    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 已提交
364 365 366 367
 * @tc.name: cmdFuncDoCmdTest_004
 * @tc.desc: do cmd function, do chmod fail test
 * @tc.type: FUNC
 */
368
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0)
M
mamingshuai 已提交
369
{
370
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
371 372 373 374
    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 已提交
375 376
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
377 378 379 380 381
    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 已提交
382 383
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command .c_str(), &curCmdLine);
M
mamingshuai 已提交
384 385 386 387 388
    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 已提交
389 390
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
391 392 393 394 395
    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 已提交
396 397
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
398 399 400 401 402
    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 已提交
403 404
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    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 已提交
420 421 422 423
 * @tc.name: cmdFuncDoCmdTest_005
 * @tc.desc: do cmd function, do chown fail test
 * @tc.type: FUNC
 */
424
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0)
M
mamingshuai 已提交
425
{
426
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
427 428 429 430
    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 已提交
431 432
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
433 434 435 436 437
    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 已提交
438 439
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    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 已提交
456 457 458 459
 * @tc.name: cmdFuncDoCmdTest_006
 * @tc.desc: do cmd function, do success test
 * @tc.type: FUNC
 */
460
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0)
M
mamingshuai 已提交
461
{
462
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
463 464 465 466

    // mkdir success
    std::string cmdStr = "mkdir ";
    std::string cmdContentStr = TEST_DRI + "/cmdFuncDoCmdTest006";
Z
zhong_ning 已提交
467 468
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481
    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 已提交
482
        EXPECT_EQ(0, 0);
M
mamingshuai 已提交
483 484 485 486 487
    }

    // chmod success
    cmdStr = "chmod ";
    cmdContentStr = "0440 " + TEST_FILE;
Z
zhong_ning 已提交
488 489
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    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 已提交
506 507
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521
    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 已提交
522 523 524 525
 * @tc.name: cfgCheckStat_001
 * @tc.desc: init.cfg file state check
 * @tc.type: FUNC
 */
526
HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level0)
M
mamingshuai 已提交
527 528 529 530 531 532 533 534 535 536 537 538
{
    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()
{
539 540
    char *buffer = nullptr;
    FILE *fd = nullptr;
M
mamingshuai 已提交
541 542 543 544 545 546 547 548
    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 已提交
549
        buffer = static_cast<char*>(malloc(static_cast<size_t>(fileStat.st_size) + 1));
M
mamingshuai 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
        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;
}

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

579
static int IsForbidden(const char *fieldStr)
M
mamingshuai 已提交
580 581 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
{
    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;
    }

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

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

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

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

631
    cJSON *filedJ = cJSON_GetObjectItem(curItem, "uid");
M
mamingshuai 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    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) {
650
        cJSON *capJ = cJSON_GetArrayItem(filedJ, i);
M
mamingshuai 已提交
651 652 653 654 655
        EXPECT_TRUE(cJSON_IsNumber(capJ));
        EXPECT_TRUE(cJSON_GetNumberValue(capJ) >= 0.0);
    }
}

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

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

670
static void CheckCmd(const TestCmdLine *resCmd)
M
mamingshuai 已提交
671 672 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
{
    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 已提交
700 701
            if (resCmd->cmdContent[i] != ' ') {
                continue;
M
mamingshuai 已提交
702
            }
M
Mupceet 已提交
703 704 705 706 707
            ++spaceCnt;
            if (spacePos != 0) {
                EXPECT_NE(spacePos + 1, i);    // consecutive spaces should not appear
            }
            spacePos = i;
M
mamingshuai 已提交
708 709 710 711 712 713
        }
        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 已提交
714 715
    } else if (strcmp("export ", resCmd->name) == 0) {
        EXPECT_NE(' ', resCmd->cmdContent[0]);   // should not start with space
M
mamingshuai 已提交
716 717 718 719 720
    } else {    // unknown cmd
        EXPECT_TRUE(false);
    }
}

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

727
    cJSON *cmdsItem = cJSON_GetObjectItem(jobItem, CMDS_ARR_NAME_IN_JSON.c_str());
M
mamingshuai 已提交
728 729 730 731 732 733 734
    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) {
735
        char *cmdLineStr = cJSON_GetStringValue(cJSON_GetArrayItem(cmdsItem, i));
M
mamingshuai 已提交
736 737 738
        EXPECT_TRUE(cmdLineStr != nullptr);
        EXPECT_TRUE(strlen(cmdLineStr) > 0);

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

static void CheckJobs(const cJSON* fileRoot)
{
    int jobArrSize = 0;
749
    cJSON *jobArr = GetArrItem(fileRoot, jobArrSize, JOBS_ARR_NAME_IN_JSON);
M
mamingshuai 已提交
750 751 752 753 754 755 756
    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) {
757
        cJSON *jobItem = cJSON_GetArrayItem(jobArr, i);
M
mamingshuai 已提交
758
        EXPECT_TRUE(jobItem != nullptr);
759
        char *jobNameStr = cJSON_GetStringValue(cJSON_GetObjectItem(jobItem, "name"));
M
mamingshuai 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
        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 已提交
779 780 781 782
 * @tc.name: cfgCheckContent_001
 * @tc.desc: init.cfg file content check
 * @tc.type: FUNC
 */
783
HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level0)
M
mamingshuai 已提交
784
{
785
    char *fileBuf = ReadFileToBuf();
M
mamingshuai 已提交
786 787 788 789 790
    if (fileBuf == nullptr) {
        EXPECT_TRUE(fileBuf != nullptr);
        return;
    }

791
    cJSON *fileRoot = cJSON_Parse(fileBuf);
M
mamingshuai 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
    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()
{
810
    FILE *testCfgFile = fopen(TEST_CFG_ILLEGAL.c_str(), "w+");
M
mamingshuai 已提交
811 812 813 814 815 816
    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 已提交
817
        (void)fclose(testCfgFile);
M
mamingshuai 已提交
818 819 820
        return;
    }

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

/*
 * @tc.name: cmdFuncDoLoadCfgTest_001
 * @tc.desc: parse function, parse success test
 * @tc.type: FUNC
 */
829
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0)
M
mamingshuai 已提交
830
{
831
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
832 833 834 835 836 837 838 839 840 841 842 843
    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
 */
844
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0)
M
mamingshuai 已提交
845
{
846
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
847 848 849 850 851
    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 已提交
852 853
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
854 855 856 857 858 859 860 861 862
    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 已提交
863 864
    command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
865 866 867 868 869 870 871 872
    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 已提交
873
        EXPECT_EQ(0, 0);
M
mamingshuai 已提交
874 875 876 877 878 879 880 881
    }
}

/*
 * @tc.name: cmdFuncDoLoadCfgTest_003
 * @tc.desc: fstab.cfg file success test
 * @tc.type: FUNC
 */
882
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0)
M
mamingshuai 已提交
883
{
884
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
885 886 887 888
    std::string cmdStr = "loadcfg ";
    std::string cmdContentStr = "/patch/fstab.cfg";
    char buf[CAT_BUF_SIZE] = {0};
    struct stat testCfgStat = {0};
889
    FILE *fd = nullptr;
M
mamingshuai 已提交
890 891
    size_t size;
    bool hasZpfs = false;
Z
zhong_ning 已提交
892 893
    std::string command = cmdStr + cmdContentStr;
    ParseCmdLine(command.c_str(), &curCmdLine);
M
mamingshuai 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
    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 已提交
909
            size = fread(buf, 1, CAT_BUF_SIZE - 1, fd);
M
mamingshuai 已提交
910 911 912 913
            if (size < 0) {
                EXPECT_TRUE(size >= 0);
                break;
            }
S
sun_fan 已提交
914
            buf[CAT_BUF_SIZE - 1] = 0;
M
mamingshuai 已提交
915 916 917 918 919
            if (strstr(buf, "zpfs") != nullptr) {
                hasZpfs = true;
                break;
            }
        } while (size > 0);
X
xionglei6 已提交
920
        EXPECT_TRUE(hasZpfs);
M
mamingshuai 已提交
921 922 923 924 925 926 927 928 929
        fclose(fd);
    }
}

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

/*
 * @tc.name: cmdJobTest_002
 * @tc.desc: job functions test
 * @tc.type: FUNC
 */
949
HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level0)
M
mamingshuai 已提交
950 951 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
{
    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