ParameterTest.cpp 12.0 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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.
 */

#include <cstdio>
#include "gtest/gtest.h"
#include "parameter.h"
using namespace std;
using namespace testing::ext;

namespace StartUpLite {
    static const int GET_DEF_PARA_FUN_MAX = 24;
    static const int MAX_LEN = 128;
    static const int WRONG_LEN = 2;
J
jiyong 已提交
26

W
wenjun 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
class ParameterTest : public testing::Test {
protected:
    static void SetUpTestCase(void) 
    {
        mkdir("/data", S_IRUSR | S_IWUSR);
        mkdir("/data/system", S_IRUSR | S_IWUSR);
        mkdir("/data/system/param", S_IRUSR | S_IWUSR);
    }
    static void TearDownTestCase(void) {}
    virtual void SetUp() {}
    virtual void TearDown() {}
    string defSysParam = "data of sys param ***...";
    using GetRdonlyPara = char* (*)();
    using GetDefParaNode = struct TagGetDefParaNode {
        char const *funName;
        GetRdonlyPara fun;
    };
    GetDefParaNode getDefPara[StartUpLite::GET_DEF_PARA_FUN_MAX] = {
        {"GetProductType", GetProductType},
        {"GetManufacture", GetManufacture},
        {"GetBrand", GetBrand},
        {"GetMarketName", GetMarketName},
        {"GetProductSeries", GetProductSeries},
        {"GetProductModel", GetProductModel},
        {"GetSoftwareModel", GetSoftwareModel},
        {"GetHardwareModel", GetHardwareModel},
        {"GetHardwareProfile", GetHardwareProfile},
        {"GetOsName", GetOsName},
        {"GetDisplayVersion", GetDisplayVersion},
        {"GetBootloaderVersion", GetBootloaderVersion},
        {"GetSecurityPatchTag", GetSecurityPatchTag},
        {"GetAbiList", GetAbiList},
        {"GetFirstApiLevel", GetFirstApiLevel},
        {"GetSdkApiLevel", GetSdkApiLevel},
        {"GetIncrementalVersion", GetIncrementalVersion},
        {"GetVersionId", GetVersionId},
        {"GetBuildType", GetBuildType},
        {"GetBuildUser", GetBuildUser},
        {"GetBuildHost", GetBuildHost},
        {"GetBuildTime", GetBuildTime},
        {"GetBuildRootHash", GetBuildRootHash},
        {"GetSerial", GetSerial},
    };
};

/**
 * @tc.number    : SUB_START_Para_Setting_Legal_0010 
 * @tc.name      : SetParameter legal test with Lowercase alphanumeric, underscore, dot
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
77
HWTEST_F(ParameterTest, SUB_START_Para_Setting_Legal_0010, Function | MediumTest | Level0)
W
wenjun 已提交
78 79 80 81
{
    int ret;

    char key[] = "rw.sys.version";
M
mamingshuai 已提交
82
    char value[] = "OEM-10.1.0";
W
wenjun 已提交
83 84 85 86 87 88 89 90 91
    ret = SetParameter(key, value);
    EXPECT_EQ(ret, 0);
}

/**
 * @tc.number    : SUB_START_Para_Setting_Legal_0020 
 * @tc.name      : SetParameter legal test with key 31 bytes, value 127 bytes
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
92
HWTEST_F(ParameterTest, SUB_START_Para_Setting_Legal_0020, Function | MediumTest | Level0)
W
wenjun 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
{
    int ret;

    char key1[] = "rw.sys.version.version.version.";
    char value1[] = "set with key = 31";
    ret = SetParameter(key1, value1);
    EXPECT_EQ(ret, 0);

    char key2[] = "rw.sys.version.version";
    char value2[] = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890\
abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrs";
    ret = SetParameter(key2, value2);
    EXPECT_EQ(ret, 0);
}

/**
 * @tc.number    : SUB_START_Para_Setting_ilLegal_0010 
 * @tc.name      : SetParameter legal test with key is nullptr, value is nullptr
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
113
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_0010, Function | MediumTest | Level2)
W
wenjun 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
{
    int ret;

    char value[] = "test with null";
    ret = SetParameter(nullptr, value);
    EXPECT_EQ(ret, -9);

    char key[] = "rw.sys.version";
    ret = SetParameter(key, nullptr);
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Setting_ilLegal_0020 
 * @tc.name      : SetParameter legal test with key is NULL, value is NULL
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
131
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_0020, Function | MediumTest | Level2)
W
wenjun 已提交
132 133 134 135 136 137 138 139 140 141
{
    int ret = SetParameter("\0", "\0");
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Setting_ilLegal_key_0010 
 * @tc.name      : SetParameter legal test with key 32 or more than 32 bytes
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
142
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_key_0010, Function | MediumTest | Level2)
W
wenjun 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
{
    int ret;

    char key1[] = "rw.sys.version.version.version.v";
    char value1[] = "set with key = 32";
    ret = SetParameter(key1, value1);
    EXPECT_EQ(ret, -9);

    char key2[] = "rw.sys.version.version.version.version";
    char value2[] = "set with key > 32";
    ret = SetParameter(key2, value2);
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Setting_ilLegal_key_0030 
 * @tc.name      : SetParameter legal test with illegal characters
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
162
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_key_0030, Function | MediumTest | Level2)
W
wenjun 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176
{
    int ret;

    char key[] = "rw.sys.version*%version";
    char value[] = "set value with illegal key";
    ret = SetParameter(key, value);
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Setting_ilLegal_value_0010 
 * @tc.name      : SetParameter legal test with value is 128 or more than 128 bytes
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
177
HWTEST_F(ParameterTest, SUB_START_Para_Setting_ilLegal_value_0010, Function | MediumTest | Level2)
W
wenjun 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
{
    int ret;

    char key1[] = "rw.sys.version.version";
    char value1[] = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890\
abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrst";
    ret = SetParameter(key1, value1);
    EXPECT_EQ(ret, -9);

    char key2[] = "rw.sys.version.version";
    char value2[] = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890\
abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890";
    ret = SetParameter(key2, value2);
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Getting_Legal_0010 
 * @tc.name      : GetParameter legal test with Lowercase alphanumeric, underscore, dot
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
199
HWTEST_F(ParameterTest, SUB_START_Para_Getting_Legal_0010, Function | MediumTest | Level0)
W
wenjun 已提交
200 201 202 203
{
    int ret;

    char key[] = "rw.sys.version";
M
mamingshuai 已提交
204
    char rightVal[] = "OEM-10.1.0";
W
wenjun 已提交
205 206
    char value[StartUpLite::MAX_LEN] = {0};
    ret = GetParameter(key, defSysParam.c_str(), value, StartUpLite::MAX_LEN);
M
mamingshuai 已提交
207 208
    EXPECT_EQ(ret, (int)strlen(rightVal));
    value[MAX_LEN - 1] = '\0';
W
wenjun 已提交
209 210 211 212 213 214 215 216
    EXPECT_STREQ(value, rightVal);
}

/**
 * @tc.number    : SUB_START_Para_Getting_Legal_0020 
 * @tc.name      : GetParameter legal test with defaut value point is nullptr
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
217
HWTEST_F(ParameterTest, SUB_START_Para_Getting_Legal_0020, Function | MediumTest | Level0)
W
wenjun 已提交
218 219 220 221
{
    int ret;

    char key[] = "rw.sys.version";
M
mamingshuai 已提交
222
    char rightVal[] = "OEM-10.1.0";
W
wenjun 已提交
223 224
    char value[StartUpLite::MAX_LEN] = {0};
    ret = GetParameter(key, nullptr, value, StartUpLite::MAX_LEN);
M
mamingshuai 已提交
225 226
    EXPECT_EQ(ret, (int)strlen(rightVal));
    value[MAX_LEN - 1] = '\0';
W
wenjun 已提交
227 228 229 230 231 232 233 234
    EXPECT_STREQ(value, rightVal);
}

/**
 * @tc.number    : SUB_START_Para_Getting_Legal_0030 
 * @tc.name      : GetParameter legal test with length is 31 bytes, value is 127 bytes
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
235
HWTEST_F(ParameterTest, SUB_START_Para_Getting_Legal_0030, Function | MediumTest | Level0)
W
wenjun 已提交
236 237 238 239 240 241 242
{
    int ret;

    char key1[] = "rw.sys.version.version.version.";
    char rightVal1[] = "set with key = 31";
    char value1[StartUpLite::MAX_LEN] = {0};
    ret = GetParameter(key1, defSysParam.c_str(), value1, StartUpLite::MAX_LEN);
M
mamingshuai 已提交
243 244
    EXPECT_EQ(ret, (int)strlen(rightVal1));
    value1[MAX_LEN - 1] = '\0';
W
wenjun 已提交
245 246 247 248 249 250 251
    EXPECT_STREQ(value1, rightVal1);

    char key2[] = "rw.sys.version.version";
    char rightVal2[] = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890\
abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrs";
    char value2[StartUpLite::MAX_LEN] = {0};
    ret = GetParameter(key2, defSysParam.c_str(), value2, StartUpLite::MAX_LEN);
M
mamingshuai 已提交
252 253
    value2[MAX_LEN - 1] = '\0';
    EXPECT_EQ(ret, (int)strlen(rightVal2));
W
wenjun 已提交
254 255 256 257 258 259 260 261
    EXPECT_STREQ(value2, rightVal2);
}

/**
 * @tc.number    : SUB_START_Para_Getting_ilLegal_0010 
 * @tc.name      : GetParameter legal test with value length is too short
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
262
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0010, Function | MediumTest | Level2)
W
wenjun 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276
{
    int ret;

    char key[] = "rw.sys.version";
    char value[StartUpLite::WRONG_LEN] = {0};
    ret = GetParameter(key, defSysParam.c_str(), value, StartUpLite::WRONG_LEN);
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Getting_ilLegal_0020 
 * @tc.name      : GetParameter legal test with value point is nullptr
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
277
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0020, Function | MediumTest | Level2)
W
wenjun 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290
{
    int ret;

    char key[] = "rw.sys.version";
    ret = GetParameter(key, defSysParam.c_str(), nullptr, StartUpLite::MAX_LEN);
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Getting_ilLegal_0030 
 * @tc.name      : GetParameter legal test with key is not exist and vlan len is too short
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
291
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0030, Function | MediumTest | Level2)
W
wenjun 已提交
292 293 294 295 296 297
{
    int ret;

    char key1[] = "rw.product.not.exist";
    char value1[StartUpLite::MAX_LEN] = {0};
    ret = GetParameter(key1, defSysParam.c_str(), value1, StartUpLite::MAX_LEN);
M
mamingshuai 已提交
298 299
    EXPECT_EQ(ret, (int)strlen(defSysParam.c_str()));
    value1[MAX_LEN - 1] = '\0';
W
wenjun 已提交
300 301 302 303 304 305 306 307 308 309 310 311
    EXPECT_STREQ(value1, defSysParam.c_str());

    char value2[StartUpLite::WRONG_LEN] = {0};
    ret = GetParameter(key1, defSysParam.c_str(), value2, StartUpLite::WRONG_LEN);
    EXPECT_EQ(ret, -1);
}

/**
 * @tc.number    : SUB_START_Para_Getting_ilLegal_0040 
 * @tc.name      : GetParameter legal test with key is 32 bytes
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
312
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0040, Function | MediumTest | Level2)
W
wenjun 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326
{
    int ret;

    char key[] = "rw.sys.version.version.version.v";
    char value[StartUpLite::MAX_LEN] = {0};
    ret = GetParameter(key, defSysParam.c_str(), value, StartUpLite::MAX_LEN);
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Getting_ilLegal_0050 
 * @tc.name      : GetParameter legal test with key is nullptr
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
327
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0050, Function | MediumTest | Level2)
W
wenjun 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340
{
    int ret;

    char value[StartUpLite::MAX_LEN] = {0};
    ret = GetParameter(nullptr, defSysParam.c_str(), value, StartUpLite::MAX_LEN);
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Getting_ilLegal_0060 
 * @tc.name      : GetParameter legal test with key is illegal with Special characters
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
341
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ilLegal_0060, Function | MediumTest | Level2)
W
wenjun 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355
{
    int ret;

    char key[] = "rw.sys.version*%version";
    char value[StartUpLite::MAX_LEN] = {0};
    ret = GetParameter(key, defSysParam.c_str(), value, StartUpLite::MAX_LEN);
    EXPECT_EQ(ret, -9);
}

/**
 * @tc.number    : SUB_START_Para_Getting_ReadOnly_0010 
 * @tc.name      : GetParameter read only parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
356
HWTEST_F(ParameterTest, SUB_START_Para_Getting_ReadOnly_0010, Function | MediumTest | Level0)
W
wenjun 已提交
357 358 359 360 361 362 363 364 365 366 367 368
{
    char *value = nullptr;

    for (int loop = 0; loop < StartUpLite::GET_DEF_PARA_FUN_MAX; loop++) {
        value = getDefPara[loop].fun();
        EXPECT_STRNE(value, (char *)nullptr);
        if (value != nullptr) {
            free(value);
        }
    }
}
}