client_unittest.cpp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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.
 */

#include "init_unittest.h"
#include "init_utils.h"
#include "param_request.h"
#include "param_stub.h"
#include "sys_param.h"

using namespace std;

4
411148299@qq.com 已提交
24
static int g_testPermissionResult = DAC_RESULT_PERMISSION;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
static void ClientCheckParamValue(const char *name, const char *expectValue)
{
    char tmp[PARAM_BUFFER_SIZE] = { 0 };
    u_int32_t len = sizeof(tmp);
    int ret = SystemGetParameter(name, tmp, &len);
    printf("ClientCheckParamValue name %s value: \'%s\' expectValue:\'%s\' \n", name, tmp, expectValue);
    if (ret == 0 && len > 0) {
        EXPECT_NE((int)strlen(tmp), 0);
        if (expectValue != nullptr) {
            EXPECT_EQ(strcmp(tmp, expectValue), 0);
        }
    } else {
        EXPECT_NE(0, 0);
    }
}

// 多线程测试
static void *TestSendParamSetMsg(void *args)
{
X
xionglei6 已提交
44 45 46
    if (args == nullptr) {
        return nullptr;
    }
47 48 49 50 51 52 53 54 55
    std::string name = (char *)args;
    printf("TestSendParamSetMsg name :\'%s\' \n", name.c_str());
    SystemSetParameter(name.c_str(), name.c_str());
    ClientCheckParamValue(name.c_str(), name.c_str());
    return nullptr;
}

static void *TestSendParamWaitMsg(void *args)
{
X
xionglei6 已提交
56 57 58
    if (args == nullptr) {
        return nullptr;
    }
59 60 61 62 63 64 65 66 67
    std::string name = "Wati.";
    name = name + (char *)args;
    printf("TestSendParamWaitMsg name :\'%s\' \n", name.c_str());
    SystemWaitParameter(name.c_str(), name.c_str(), 1);
    return nullptr;
}

static void TestForMultiThread()
{
4
411148299@qq.com 已提交
68
    static const int threadMaxNumer = 2;
69
    printf("TestForMultiThread \n");
4
411148299@qq.com 已提交
70
    pthread_t tids[threadMaxNumer + threadMaxNumer];
71 72 73 74 75 76 77
    const char *names[] = {
        "thread.1111.2222.3333.4444.5555",
        "thread.2222.1111.2222.3333.4444",
        "thread.3333.1111.2222.4444.5555",
        "thread.4444.5555.1111.2222.3333",
        "thread.5555.1111.2222.3333.4444"
    };
4
411148299@qq.com 已提交
78
    for (size_t i = 0; i < threadMaxNumer; i++) {
79 80
        pthread_create(&tids[i], nullptr, TestSendParamSetMsg, (void *)names[i % ARRAY_LENGTH(names)]);
    }
4
411148299@qq.com 已提交
81
    for (size_t i = threadMaxNumer; i < threadMaxNumer + threadMaxNumer; i++) {
82 83
        pthread_create(&tids[i], nullptr, TestSendParamWaitMsg, (void *)names[i % ARRAY_LENGTH(names)]);
    }
4
411148299@qq.com 已提交
84
    for (size_t i = 0; i < threadMaxNumer + threadMaxNumer; i++) {
85 86 87 88 89 90
        pthread_join(tids[i], nullptr);
    }
}

static void TestParamTraversal()
{
X
add ut  
xionglei6 已提交
91
    SystemTraversalParameter([](ParamHandle handle, void *cookie) {
92 93 94 95 96 97 98 99
            char value[PARAM_BUFFER_SIZE + PARAM_BUFFER_SIZE] = { 0 };
            uint32_t commitId = 0;
            int ret = SystemGetParameterCommitId(handle, &commitId);
            EXPECT_EQ(ret, 0);
            SystemGetParameterName(handle, value, PARAM_BUFFER_SIZE);
            u_int32_t len = PARAM_BUFFER_SIZE;
            SystemGetParameterValue(handle, ((char *)value) + PARAM_BUFFER_SIZE, &len);
            printf("$$$$$$$$Param %s=%s \n", (char *)value, ((char *)value) + PARAM_BUFFER_SIZE);
X
xionglei6 已提交
100 101
        },
        nullptr);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
}

static void TestPersistParam()
{
    SystemSetParameter("persist.111.ffff.bbbb.cccc.dddd.eeee", "1101");
    SystemSetParameter("persist.111.aaaa.bbbb.cccc.dddd.eeee", "1102");
    SystemSetParameter("persist.111.bbbb.cccc.dddd.eeee", "1103");
    ClientCheckParamValue("persist.111.bbbb.cccc.dddd.eeee", "1103");
    SystemSetParameter("persist.111.cccc.bbbb.cccc.dddd.eeee", "1104");
    SystemSetParameter("persist.111.eeee.bbbb.cccc.dddd.eeee", "1105");
    ClientCheckParamValue("persist.111.ffff.bbbb.cccc.dddd.eeee", "1101");
    SystemSetParameter("persist.111.ffff.bbbb.cccc.dddd.eeee", "1106");
    ClientCheckParamValue("persist.111.ffff.bbbb.cccc.dddd.eeee", "1106");
}

static void TestPermission()
{
    const char *testName = "persist.111.ffff.bbbb.cccc.dddd.eeee.55555";
    char tmp[PARAM_BUFFER_SIZE] = { 0 };
X
xionglei6 已提交
121
    int ret;
122 123 124 125
    // 允许本地校验
    ParamSecurityOps *paramSecurityOps = &GetClientParamWorkSpace()->paramSecurityOps;
    paramSecurityOps->securityCheckParamPermission = TestCheckParamPermission;
    g_testPermissionResult = DAC_RESULT_FORBIDED;
X
xionglei6 已提交
126 127
    if ((GetClientParamWorkSpace() != nullptr) && (GetClientParamWorkSpace()->securityLabel != nullptr)) {
        GetClientParamWorkSpace()->securityLabel->flags = LABEL_CHECK_FOR_ALL_PROCESS;
X
xionglei6 已提交
128
        ret = SystemSetParameter(testName, "22202");
X
xionglei6 已提交
129 130
        EXPECT_EQ(ret, DAC_RESULT_FORBIDED);
    }
131 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
    paramSecurityOps->securityEncodeLabel = TestEncodeSecurityLabel;
    paramSecurityOps->securityDecodeLabel = TestDecodeSecurityLabel;
    paramSecurityOps->securityFreeLabel = TestFreeLocalSecurityLabel;
    paramSecurityOps->securityCheckParamPermission = TestCheckParamPermission;
    g_testPermissionResult = 0;
    ret = SystemSetParameter(testName, "22202");
    EXPECT_EQ(ret, 0);
    ClientCheckParamValue(testName, "22202");

    const int testResult = 201;
    g_testPermissionResult = testResult;
    // 禁止写/读
    ret = SystemSetParameter(testName, "3333");
    EXPECT_EQ(ret, testResult);
    u_int32_t len = sizeof(tmp);
    ret = SystemGetParameter(testName, tmp, &len);
    EXPECT_EQ(ret, testResult);
    RegisterSecurityOps(paramSecurityOps, 0);
}

static void TestCmd()
{
    // set
    const char *argForSet[] = { "param", "set", "aaaa", "2222" };
    RunParamCommand(ARRAY_LENGTH(argForSet), const_cast<char **>(argForSet));

S
sun_fan 已提交
157 158 159 160
    // set fail
    const char *argForSet1[] = { "param", "set", "aaaa&&&&&&&&&####", "2222" };
    RunParamCommand(ARRAY_LENGTH(argForSet1), const_cast<char **>(argForSet1));

161 162 163 164
    // get
    const char *argForGet[] = { "param", "get", "aaaa" };
    RunParamCommand(ARRAY_LENGTH(argForGet), const_cast<char **>(argForGet));

S
sun_fan 已提交
165 166 167 168
    // get not exit
    const char *argForGet1[] = { "param", "get", "aaaaaaaa" };
    RunParamCommand(ARRAY_LENGTH(argForGet1), const_cast<char **>(argForGet1));

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    // get all
    const char *argForGet2[] = { "param", "get" };
    RunParamCommand(ARRAY_LENGTH(argForGet2), const_cast<char **>(argForGet2));

    // set 失败
    const char *argForSet2[] = { "param", "set", "aaaa" };
    RunParamCommand(ARRAY_LENGTH(argForSet2), const_cast<char **>(argForSet2));

    const char *argForSet3[] = { "paramset", "aaaa", "aaaa" };
    RunParamCommand(ARRAY_LENGTH(argForSet3), const_cast<char **>(argForSet3));

    const char *argForSet4[] = { "paramset", "aaaa" };
    RunParamCommand(ARRAY_LENGTH(argForSet4), const_cast<char **>(argForSet4));

    const char *argForSet5[] = { "param" };
    RunParamCommand(ARRAY_LENGTH(argForSet5), const_cast<char **>(argForSet5));

    const char *argForSet6[] = { "param", "44444" };
    RunParamCommand(ARRAY_LENGTH(argForSet6), const_cast<char **>(argForSet6));

    const char *argForSet7[] = { "paramget", "aaaa" };
    RunParamCommand(ARRAY_LENGTH(argForSet7), const_cast<char **>(argForSet7));

S
sun_fan 已提交
192
    const char *argForSet8[] = { "param", "dump", "verbose" };
193 194
    RunParamCommand(ARRAY_LENGTH(argForSet8), const_cast<char **>(argForSet8));

S
sun_fan 已提交
195
    const char *argForSet9[] = { "param", "wait", "test.wait.001", "*", "1" };
196 197 198
    RunParamCommand(ARRAY_LENGTH(argForSet9), const_cast<char **>(argForSet9));
}

4
411148299@qq.com 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
void TestClientApi(char testBuffer[], uint32_t size, const char *name, const char *value)
{
    ParamHandle handle;
    int ret = SystemFindParameter(name, &handle);
    SystemSetParameter(name, value);
    ret = SystemFindParameter(name, &handle);
    EXPECT_EQ(ret, 0);
    uint32_t commitId = 0;
    ret = SystemGetParameterCommitId(handle, &commitId);
    EXPECT_EQ(ret, 0);
    ret = SystemGetParameterName(handle, testBuffer, size);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(strcmp(testBuffer, name), 0);
    ret = SystemGetParameterValue(handle, testBuffer, &size);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(strcmp(testBuffer, value), 0);
}

217 218 219 220 221 222 223 224 225 226
void TestClient(int index)
{
    char testBuffer[PARAM_BUFFER_SIZE] = { 0 };
    const std::string value = "test.add.client.value.001";
    std::string name = "test.add.client.001";
    name += std::to_string(index);
    switch (index) {
        case 0: {
            ParamWorkSpace *space = GetClientParamWorkSpace();
            if (space != nullptr && space->securityLabel != nullptr) {
4
411148299@qq.com 已提交
227 228
                space->securityLabel->cred.uid = 1000; // 1000 test uid
                space->securityLabel->cred.gid = 1000; // 1000 test gid
229 230
            }
        }
4
411148299@qq.com 已提交
231
        case 1: { // 1 set test
232 233 234 235 236 237 238 239 240
            SystemSetParameter(name.c_str(), value.c_str());
            ClientCheckParamValue(name.c_str(), value.c_str());
            SystemWaitParameter(name.c_str(), value.c_str(), 1);
            TestPersistParam();
            // wait
            SystemWaitParameter(name.c_str(), value.c_str(), 1);
            SystemWaitParameter(name.c_str(), nullptr, 0);
            break;
        }
4
411148299@qq.com 已提交
241
        case 2: { // 2 api test
4
411148299@qq.com 已提交
242
            TestClientApi(testBuffer, PARAM_BUFFER_SIZE, name.c_str(), value.c_str());
243 244
            break;
        }
4
411148299@qq.com 已提交
245
        case 3: // 3 Traversal test
246 247 248
            TestParamTraversal();
            SystemDumpParameters(1);
            break;
4
411148299@qq.com 已提交
249
        case 4: { // 4 watcher test
250 251 252 253 254 255 256 257 258 259
            int ret = WatchParamCheck(name.c_str());
            EXPECT_EQ(ret, 0);
            ret = WatchParamCheck("&&&&&.test.tttt");
            EXPECT_NE(ret, 0);
            // test permission
            TestPermission();
            // test cmd
            TestCmd();
            break;
        }
4
411148299@qq.com 已提交
260
        case 5: // 5 multi thread test
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
            TestForMultiThread();
            break;
        default:
            break;
    }
}

int TestEncodeSecurityLabel(const ParamSecurityLabel *srcLabel, char *buffer, uint32_t *bufferSize)
{
    PARAM_CHECK(bufferSize != nullptr, return -1, "Invalid param");
    if (buffer == nullptr) {
        *bufferSize = sizeof(ParamSecurityLabel);
        return 0;
    }
    PARAM_CHECK(*bufferSize >= sizeof(ParamSecurityLabel), return -1, "Invalid buffersize %u", *bufferSize);
    *bufferSize = sizeof(ParamSecurityLabel);
    return memcpy_s(buffer, *bufferSize, srcLabel, sizeof(ParamSecurityLabel));
}

int TestDecodeSecurityLabel(ParamSecurityLabel **srcLabel, const char *buffer, uint32_t bufferSize)
{
    PARAM_CHECK(bufferSize >= sizeof(ParamSecurityLabel), return -1, "Invalid buffersize %u", bufferSize);
    PARAM_CHECK(srcLabel != nullptr && buffer != nullptr, return -1, "Invalid param");
    *srcLabel = (ParamSecurityLabel *)buffer;
    return 0;
}

int TestCheckParamPermission(const ParamSecurityLabel *srcLabel, const ParamAuditData *auditData, uint32_t mode)
{
    // DAC_RESULT_FORBIDED
    return g_testPermissionResult;
}

int TestFreeLocalSecurityLabel(ParamSecurityLabel *srcLabel)
{
    return 0;
X
xionglei6 已提交
297
}