ShmTest.cpp 14.0 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <gtest/gtest.h>
#include "utils.h"
#include "log.h"

using namespace testing::ext;

class ShmTest : public::testing::Test {
public:
    static const char SHM_TEST_STR[];
    static const int SHM_TEST_STR_LEN;
};

const char ShmTest::SHM_TEST_STR[] = "hello shmem";
const int ShmTest::SHM_TEST_STR_LEN = sizeof(SHM_TEST_STR);

/**
 * @tc.number   SUB_KERNEL_IPC_SHM_ALL_0100
 * @tc.name     basic function test : hello world
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
43
HWTEST_F(ShmTest, testShmBasic, Function | MediumTest | Level0)
W
wenjun 已提交
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 77 78 79 80 81
{
    const int memSize = 1024;
    int shmid;
    char *shared = nullptr;

    shmid = shmget(static_cast<key_t> IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    ASSERT_NE(shmid, -1) << "> parent: shmid errno = " << errno;

    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {
        int exitCode = 0;
        Msleep(40);
        shared = static_cast<char*>(shmat(shmid, nullptr, 0));
        if (shared == reinterpret_cast<char*>(-1)) {
            LOG("> child: shmat errno = %d", errno);
            exit(1);
        }
        if (strncmp(shared, SHM_TEST_STR, SHM_TEST_STR_LEN) != 0) {
            LOG("> child: error strncmp() shared = %s", shared);
            exitCode = 1;
        }
        if ((shmdt(shared)) == -1) {
            LOG("> child: shmdt errno = %d", errno);
            exit(1);
        }
        if (shmctl(shmid, IPC_RMID, nullptr) == -1) {
            LOG("> child: shmctl errno = %d", errno);
            exit(1);
        }
        exit(exitCode);
    }
    // parent
    shared = static_cast<char*>(shmat(shmid, nullptr, 0));
    ASSERT_NE(shared, (void*)-1) << "> parent : error: shmat";
    strncpy(shared, SHM_TEST_STR, SHM_TEST_STR_LEN);
    ASSERT_NE(shmdt(shared), -1) << "> parent: error : shmdt";

M
mamingshuai 已提交
82 83
    Msleep(50);
    WaitProcExitedOK(pid);
W
wenjun 已提交
84 85 86 87 88 89 90
}

/**
 * @tc.number   SUB_KERNEL_IPC_SHM_ALL_0300
 * @tc.name     Single-process test
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
91
HWTEST_F(ShmTest, testShmSingleProcess, Function | MediumTest | Level1)
W
wenjun 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
{
    const int memSize = 1024;
    char *shared = nullptr;

    int shmid = shmget(static_cast<key_t> IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    ASSERT_NE(shmid, -1) << "> shmget errno = " << errno;

    shared = static_cast<char*>(shmat(shmid, nullptr, 0));
    ASSERT_NE(shared, (void*)-1) << "> shmat errno = " << errno;
    strncpy(shared, SHM_TEST_STR, SHM_TEST_STR_LEN);

    int re = shmdt(shared);
    ASSERT_NE(re, -1) << "> shmdt errno = " << errno;

    re = shmctl(shmid, IPC_RMID, nullptr);
    ASSERT_NE(re, -1) << "> shmctl errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_IPC_SHM_SHMGET_0100
 * @tc.name     shmat basic function test
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
115
HWTEST_F(ShmTest, testShmget, Function | MediumTest | Level0)
W
wenjun 已提交
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
    const int memSize = 1024;
    int tmp;
    int shmid;
    int shmidTmp;
    int tmpKey = GetRandom(2147483647);
    LOG("> tmpKey = %d", tmpKey);

    shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    ASSERT_NE(shmid, -1) << "> shmget : errno = " << errno;
    tmp = shmctl(shmid, IPC_RMID, nullptr);
    ASSERT_NE(tmp, -1) << "shmctl : error : IPC_RMID ";

    // make sure tmpKey free
    shmid = shmget(key_t(tmpKey), memSize, 0666 | IPC_CREAT);
    ASSERT_NE(shmid, -1) << "> shmget : errno = " << errno;
    tmp = shmctl(shmid, IPC_RMID, nullptr);
    ASSERT_NE(tmp, -1) << "shmctl : error : IPC_RMID ";

    shmid = shmget(key_t(tmpKey), memSize, IPC_EXCL | IPC_CREAT);
    ASSERT_NE(shmid, -1) << "> shmget : errno = " << errno;

    shmidTmp = shmid;
    shmidTmp = shmget(key_t(tmpKey), memSize, IPC_EXCL | IPC_CREAT);
    ASSERT_EQ(shmidTmp, -1) << "> shmget : errno = " << errno;

    tmp = shmctl(shmid, IPC_RMID, nullptr);
    ASSERT_NE(tmp, -1) << "> shmctl : error : IPC_RMID ";

    shmid = shmget(key_t(tmpKey), memSize, 0);
    ASSERT_EQ(shmid, -1) << "> shmget : errno = " << errno;

    shmid = shmget(IPC_PRIVATE, 0, 0666 | IPC_CREAT);
    ASSERT_EQ(shmid, -1) << "> shmget : errno = " << errno;

    shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    ASSERT_NE(shmid, -1) << "> shmget : errno = " << errno;

    tmp = shmctl(shmid, IPC_RMID, nullptr);
    ASSERT_NE(tmp, -1) << "> shmctl : error : IPC_RMID";
}

/**
 * @tc.number   SUB_KERNEL_IPC_SHM_SHMAT_0100
 * @tc.name     shmat: SHM_REMAP function test
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
163
HWTEST_F(ShmTest, testShmatSHM_REMAP, Function | MediumTest | Level1)
W
wenjun 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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
{
    const int memSize = 1024;
    int shmid;
    char *shared = nullptr;

    shmid = shmget(static_cast<key_t> IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    ASSERT_NE(shmid, -1) << "> parent: error : shmget";

    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: error : fork";
    if (pid == 0) {
        Msleep(10);
        int exitCode = 0;
        shared = static_cast<char*>(shmat(shmid, nullptr, 0));
        if (shared == reinterpret_cast<char*>(-1)) {
            LOG("> child: shmat 1 : errno = %d", errno);
            exit(1);
        }
        if (shmdt(shared) == -1) {
            LOG("> child: shmdt: errno = %d", errno);
            exit(1);
        }
        shared = static_cast<char*>(shmat(shmid, shared, SHM_REMAP));
        if (shared == reinterpret_cast<char*>(-1)) {
            LOG("> child: shmat 2 : errno = %d", errno);
            exit(1);
        }
        if (strncmp(shared, SHM_TEST_STR, SHM_TEST_STR_LEN) != 0) {
            LOG("> child: error strncmp() shared = %s", shared);
            exitCode = 1;
        }
        if ((shmdt(shared)) == -1) {
            LOG("> child: shmdt : errno = %d", errno);
            exit(1);
        }
        exit(exitCode);
    }
    // parent
    shared = static_cast<char*>(shmat(shmid, nullptr, 0));
    ASSERT_NE(shared, (void*)-1) << "> shmat 1 : errno = " << errno;
    ASSERT_NE(shmdt(shared), -1) << "> shmdt 1 : errno = " << errno;
    shared = static_cast<char*>(shmat(shmid, shared, SHM_REMAP));
    ASSERT_NE(shared, (void*)-1) << "> parent: shmat 2 : errno = " << errno;
    strncpy(shared, SHM_TEST_STR, SHM_TEST_STR_LEN);
    ASSERT_NE(shmdt(shared), -1) << "> parent: shmdt 2 : errno = " << errno;
209
    Msleep(50);
W
wenjun 已提交
210 211 212
    ASSERT_NE(shmctl(shmid, IPC_RMID, nullptr), -1) << "> parent: shmctl : errno = " << errno;

    Msleep(50);
M
mamingshuai 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    WaitProcExitedOK(pid);
}

/**
 * @tc.number   SUB_KERNEL_IPC_SHM_SHMAT_0200
 * @tc.name     shmat: SHM_RDONLY function test
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ShmTest, testShmatSHM_RDONLY, Function | MediumTest | Level0)
{
    const int memSize = 1024;
    int shmid;
    char *shared = nullptr;

    shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    EXPECT_NE(shmid, -1) << "> parent: shmget errno = " << errno;
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {
        int exitCode = 0;
        Msleep(10);
        shared = static_cast<char*>(shmat(shmid, nullptr, SHM_RDONLY));
        if (shared == reinterpret_cast<char*>(-1)){
            LOG("> child: shmat : errno = %d", errno);
            exitCode = 1;
        }
        if (strncmp(shared, SHM_TEST_STR, SHM_TEST_STR_LEN) != 0) {
            LOG("> child: error strncmp()");
            exitCode = 1;
        }
        shared[0] = '1';  // segment error
        if (shmdt(shared) == -1) {
            LOG("> child: shmdt : errno = %d", errno);
            exitCode = 1;
        }
        exit(exitCode);
    }
    shared = static_cast<char*>(shmat(shmid, nullptr, 0));
    EXPECT_NE(shared, (char*)-1) << "shmat : errno = " << errno;
    strncpy(shared, SHM_TEST_STR, SHM_TEST_STR_LEN);

    int exitCode;
    int procStat = CheckProcStatus(pid, &exitCode, 0);
    EXPECT_EQ(procStat, 2);
    EXPECT_EQ(exitCode, 12) << "target process should exited 255.";

    EXPECT_NE(shmdt(shared), -1) << "> shmdt : errno = " << errno;
    EXPECT_NE(shmctl(shmid, IPC_RMID, nullptr), -1) << "> shmctl : IPC_RMID errno = " << errno;
W
wenjun 已提交
261 262 263 264 265 266 267
}

/**
 * @tc.number   SUB_KERNEL_IPC_SHM_SHMAT_0300
 * @tc.name     shmat: SHM_RND function test
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
268
HWTEST_F(ShmTest, testShmatSHM_RND, Function | MediumTest | Level0)
W
wenjun 已提交
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 297 298 299
{
    const int memSize = 1024;
    int tmp;
    int shmid;
    void *shared = nullptr;

    Msleep(10);
    shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    EXPECT_NE(shmid, -1) << "shmget : errno = " << errno;

    shared = shmat(shmid, nullptr, 0);
    EXPECT_NE(shared, (void*)-1) << "> shmat : errno = " << errno;
    int re = shmdt(shared);
    EXPECT_NE(re, -1) << "> shmdt : errno = " << errno;

    void *sharedTmp = shared;
    sharedTmp = shmat(shmid, sharedTmp, SHM_RND);
    EXPECT_NE(sharedTmp, (void*)-1) << "> shmat : error : sharedTmp = " << sharedTmp << "shared = " << shared;
    strncpy(static_cast<char*>(sharedTmp), SHM_TEST_STR, SHM_TEST_STR_LEN);
    re = shmdt(sharedTmp);
    EXPECT_NE(re, -1) << "> shmdt : errno = " << errno;

    tmp = shmctl(shmid, IPC_RMID, nullptr);
    EXPECT_NE(tmp, -1) << "> shmctl : IPC_RMID errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_IPC_SHM_SHMCTL_0100
 * @tc.name     shmctl: IPC_STAT and IPC_SET function test
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
300
HWTEST_F(ShmTest, testShmctl, Function | MediumTest | Level1)
W
wenjun 已提交
301 302
{
    const int memSize = 4096;
M
mamingshuai 已提交
303
    struct shmid_ds shmPerm = {0};
W
wenjun 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    int tmp;
    int shmid;

    LOG("> getuid = %d", getuid());
    LOG("> getgid = %d", getgid());
    LOG("> getpid = %d", getpid());

    Msleep(10);
    shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    ASSERT_NE(shmid, -1) << "> shmget : errno = " << errno;

    tmp = shmctl(shmid, IPC_STAT, &shmPerm);
    EXPECT_NE(tmp, -1) << "> shmctl : IPC_STAT1 :erron = " << errno;
    EXPECT_EQ(shmPerm.shm_lpid, 0);                         // PID of last shmat/shmdt
    EXPECT_EQ(shmPerm.shm_nattch, 0);                       // No. of current attaches

    char *shared = static_cast<char*>(shmat(shmid, nullptr, 0));
    EXPECT_NE(shared, (void*)-1) << "> shmat : errno = " << errno;

    tmp = shmctl(shmid, IPC_STAT, &shmPerm);
    ASSERT_NE(tmp, -1) << "> shmctl : IPC_STAT2 :erron = " << errno;
    EXPECT_EQ(shmPerm.shm_perm.__key, 0);                   // Key supplied to shmget
    EXPECT_EQ(shmPerm.shm_perm.cuid, geteuid());            // Effective UID of creator
    EXPECT_EQ(shmPerm.shm_perm.uid, geteuid());             // Effective UID of owne
    EXPECT_EQ(shmPerm.shm_perm.cgid, getegid());            // Effective GID of creator
    EXPECT_EQ(shmPerm.shm_perm.gid, getegid());             // Effective GID of owner
    EXPECT_EQ(shmPerm.shm_perm.mode, 0666);                 // Permissions + SHM_DEST and SHM_LOCKED flags
    LOG("> last shmat() time %lld", shmPerm.shm_atime);     // Last attach time
    LOG("> last shmdt() time %lld", shmPerm.shm_dtime);     //  Last detach time
    EXPECT_EQ(shmPerm.shm_segsz, memSize);                  // Size of segment (bytes)
    EXPECT_EQ(shmPerm.shm_cpid, getpid());                  // PID of creator
    EXPECT_EQ(shmPerm.shm_lpid, getpid());                  // PID of last shmat/shmdt
    EXPECT_EQ(shmPerm.shm_nattch, 1);                       // No. of current attaches

    tmp = shmctl(shmid, IPC_SET, &shmPerm);
    EXPECT_NE(shared, (void*)-1) << "> shmat : errno = " << errno;

    tmp = shmdt(shared);
    ASSERT_NE(tmp, -1) << "> shmdt : errno = " << errno;

    tmp = shmctl(shmid, IPC_RMID, nullptr);
    ASSERT_NE(tmp, -1) << "> shmctl : IPC_RMID : errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_IPC_SHM_SHMCTL_0200
 * @tc.name     shmctl: IPC_INFO and SHM_INFO function test
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
353
HWTEST_F(ShmTest, testShmctlINFO, Function | MediumTest | Level0)
W
wenjun 已提交
354 355
{
    int tmp;
M
mamingshuai 已提交
356
    struct shminfo buf = {0};
W
wenjun 已提交
357 358 359 360 361 362 363 364 365
    tmp = shmctl(0, IPC_INFO, (struct shmid_ds *) &buf);
    EXPECT_NE(tmp, -1) << "shmctl : IPC_INFO errno = " << errno;
    LOG("> shminfo.shmmax = %lu", buf.shmmax);  // Maximum segment size 42428800
    EXPECT_EQ(buf.shmmin, 1) << "> shminfo.shmmin = " << buf.shmmin;    // Minimum segment size 1
    LOG("> shminfo.shmmni = %lu", buf.shmmni);  // Maximum number of segments 192
    LOG("> shminfo.shmseg = %lu", buf.shmseg);  // Maximum number of segments that a process can attach 128
    LOG("> shminfo.shmall = %lu", buf.shmall);  // Maximum number of pages of shared memory, system-wide 12800

    // Describes the system resource information for Shared memory
M
mamingshuai 已提交
366
    struct shm_info info = {0};
W
wenjun 已提交
367 368 369 370 371 372 373 374 375
    tmp = shmctl(0, SHM_INFO, (struct shmid_ds *) &info);
    EXPECT_NE(tmp, -1) << "shmctl : SHM_INFO errno = " << errno;
    LOG("> shm_info.used_ids         = %d", info.used_ids);             // Currently existing segments
    LOG("> shm_info.shm_tot          = %lu", info.shm_tot);             // Total number of shared memory pages
    LOG("> shm_info.shm_rss          = %lu", info.shm_rss);             // Resident shared memory pages
    LOG("> shm_info.shm_swp          = %lu", info.shm_swp);             // Swapped shared memory pages
    LOG("> shm_info.__swap_attempts  = %ld", info.__swap_attempts);     // Number of exchange attempts
    LOG("> shm_info.__swap_successes = %ld", info.__swap_successes);    // Number of successful exchanges
}