param_shell_unittest.cpp 7.6 KB
Newer Older
X
xlei1030 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
M
Mupceet 已提交
15 16
#include <gtest/gtest.h>

X
xlei1030 已提交
17 18 19 20 21
#include "begetctl.h"
#include "securec.h"
#include "shell.h"
#include "shell_utils.h"
#include "shell_bas.h"
L
laiguizhong 已提交
22
#include "init_param.h"
X
xlei1030 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

using namespace std;
using namespace testing::ext;

namespace init_ut {
class ParamShellUnitTest : public testing::Test {
public:
    ParamShellUnitTest() {};
    virtual ~ParamShellUnitTest() {};
    static void SetUpTestCase(void) {};
    static void TearDownTestCase(void) {};
    void SetUp(void) {};
    void TearDown(void) {};
    void TestBody(void) {};
    void TestInitParamShell()
    {
L
laiguizhong 已提交
39
        SystemSetParameter("aaa", "aaa");
X
xlei1030 已提交
40 41 42 43 44 45
        BShellHandle bshd = GetShellHandle();
        if (bshd == nullptr) {
            return;
        }
        const char *args[] = {"paramshell", "\n"};
        const ParamInfo *param = BShellEnvGetReservedParam(bshd, PARAM_REVERESD_NAME_CURR_PARAMETER);
L
laiguizhong 已提交
46
        int ret = BShellEnvSetParam(bshd, PARAM_REVERESD_NAME_CURR_PARAMETER, "..a", PARAM_STRING, (void *)"..a");
X
xlei1030 已提交
47 48
        EXPECT_EQ(ret, 0);
        SetParamShellPrompt(bshd, args[1]);
L
laiguizhong 已提交
49 50 51 52 53 54 55 56
        SetParamShellPrompt(bshd, "..");

        ret = BShellEnvSetParam(bshd, param->name, param->desc, param->type, (void *)"");
        SetParamShellPrompt(bshd, "..");

        SetParamShellPrompt(bshd, ".a");
        SetParamShellPrompt(bshd, ".");
        SetParamShellPrompt(bshd, args[1]);
X
xlei1030 已提交
57 58 59
        BShellParamCmdRegister(bshd, 1);
        BShellEnvStart(bshd);
        ret = BShellEnvOutputPrompt(bshd, "testprompt");
L
laiguizhong 已提交
60
        ret = BShellEnvOutputPrompt(bshd, "testprompt1111111111111111111111111111111111111111111111111111111111");
X
xlei1030 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        BShellEnvOutputByte(bshd, 'o');
        EXPECT_EQ(ret, 0);
    }
    void TestParamShellCmd()
    {
        BShellHandle bshd = GetShellHandle();
        BShellKey *key = BShellEnvGetDefaultKey('\n');
        EXPECT_NE(key, nullptr);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "cd const") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        int ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "cat aaa") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testnotcmd") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
        // test param start with "
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "\"ls") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
        // test argc is 0
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), ",ls") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "$test$") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "exit") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
    }
    void TestParamShellCmd1()
    {
        BShellHandle bshd = GetShellHandle();
        BShellKey *key = BShellEnvGetDefaultKey('\n');
        EXPECT_NE(key, nullptr);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "pwd") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        int ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "help") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "dump") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "dump verbose") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\n');
        EXPECT_EQ(ret, 0);
    }
    void TestParamShellcmdEndkey()
    {
        BShellHandle bshd = GetShellHandle();
L
laiguizhong 已提交
147
        bshd->input(nullptr, 0);
X
xlei1030 已提交
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 173
        BShellKey *key = BShellEnvGetDefaultKey('\b');
        EXPECT_NE(key, nullptr);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testbbackspace") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        bshd->cursor = strlen("testb");
        int ret = key->keyHandle(bshd, '\b');
        EXPECT_EQ(ret, 0);
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testbbackspace") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        bshd->cursor = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\b');
        EXPECT_EQ(ret, 0);

        key = BShellEnvGetDefaultKey('\t');
        if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testtab") != EOK) {
            return;
        }
        bshd->length = strlen(bshd->buffer);
        ret = key->keyHandle(bshd, '\t');
        EXPECT_NE(key, nullptr);
        BShellEnvProcessInput(bshd, (char)3); // 3 is ctrl c
        BShellEnvProcessInput(bshd, '\e');
L
laiguizhong 已提交
174 175 176 177 178
        BShellEnvProcessInput(bshd, '[');
        bshd->length = 1;
        bshd->cursor = 1;
        BShellEnvProcessInput(bshd, 'C');
        BShellEnvProcessInput(bshd, 'D');
X
xlei1030 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    }
};

HWTEST_F(ParamShellUnitTest, TestInitParamShell, TestSize.Level1)
{
    ParamShellUnitTest test;
    test.TestInitParamShell();
    test.TestParamShellCmd();
    test.TestParamShellCmd1();
}
HWTEST_F(ParamShellUnitTest, TestParamShellInput, TestSize.Level1)
{
    BShellHandle bshd = GetShellHandle();
    BShellEnvProcessInput(bshd, '\n');

    BShellEnvProcessInput(bshd, 'l');
    bshd->length = BSH_COMMAND_MAX_LENGTH;

    BShellEnvProcessInput(bshd, 'l');
    bshd->length = sizeof('l');
    bshd->cursor = 0;
    BShellEnvProcessInput(bshd, 's');
    BShellEnvProcessInput(bshd, '\n');
 
    BShellEnvProcessInput(bshd, '\n'); // test bshd buff length is 0

    int ret = BShellEnvRegisterKeyHandle(bshd, 'z', (BShellkeyHandle)(void*)0x409600); // 0x409600 construct address
    EXPECT_EQ(ret, 0);
}
HWTEST_F(ParamShellUnitTest, TestParamShellcmd2, TestSize.Level1)
{
    ParamShellUnitTest test;
    test.TestParamShellcmdEndkey();
L
laiguizhong 已提交
212 213 214 215 216
    GetSystemCommitId();
    BShellEnvLoop(nullptr);
    BShellEnvErrString(GetShellHandle(), 1);
    BShellEnvOutputResult(GetShellHandle(), 1);
    demoExit();
X
xlei1030 已提交
217 218
}
}  // namespace init_ut