cmd_func_test.cpp 31.2 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 49 50 51 52 53 54 55

// 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;
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 63
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 已提交
64 65 66 67 68 69

// job test releated
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";

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

M
mamingshuai 已提交
75 76 77 78 79 80 81 82 83 84
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 "));
85
        const mode_t mode = 0755;
M
mamingshuai 已提交
86 87 88 89 90 91
        if (mkdir(TEST_DRI.c_str(), mode) != 0) {
            if (errno != EEXIST) {
                return;
            }
        }

92
        FILE *testFile = fopen(TEST_FILE.c_str(), "w+");
M
mamingshuai 已提交
93 94 95 96 97 98
        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 已提交
99
            (void)fclose(testFile);
M
mamingshuai 已提交
100 101
            return;
        }
M
Mupceet 已提交
102
        (void)fclose(testFile);
M
mamingshuai 已提交
103 104 105 106 107 108 109 110 111 112 113

#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 已提交
114
        PrepareInitUnitTestEnv();
M
mamingshuai 已提交
115 116 117 118 119
    }

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

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

/*
X
xionglei6 已提交
177 178 179 180
 * @tc.name: cmdFuncParseCmdTest_002
 * @tc.desc: parse function, invalid strings test
 * @tc.type: FUNC
 */
181
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0)
M
mamingshuai 已提交
182
{
183
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    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 已提交
204 205 206 207
 * @tc.name: cmdFuncParseCmdTest_003
 * @tc.desc: parse function, cmd content empty test
 * @tc.type: FUNC
 */
208
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0)
M
mamingshuai 已提交
209
{
210
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
211 212 213 214 215 216 217 218 219 220
    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 已提交
221 222 223 224
 * @tc.name: cmdFuncParseCmdTest_004
 * @tc.desc: parse function, cmd content too long test
 * @tc.type: FUNC
 */
225
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0)
M
mamingshuai 已提交
226
{
227
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
228 229 230 231 232 233 234
    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();
235
        char *curCmd = (char *)malloc(curCmdLen + MAX_CMD_CONTENT_LEN + 10);
M
mamingshuai 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        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 已提交
259 260 261 262
 * @tc.name: cmdFuncParseCmdTest_005
 * @tc.desc: parse function, parse success test
 * @tc.type: FUNC
 */
263
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0)
M
mamingshuai 已提交
264
{
265
    TestCmdLine curCmdLine;
M
mamingshuai 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    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 已提交
290 291 292 293
 * @tc.name: cmdFuncDoCmdTest_001
 * @tc.desc: do cmd function, nullptr test
 * @tc.type: FUNC
 */
294
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0)
M
mamingshuai 已提交
295 296 297 298 299 300
{
    // do not crash here
    DoCmd(nullptr);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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