1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include "it_test_shm.h"
32
Testcase(VOID)33 static int Testcase(VOID)
34 {
35 int shmid;
36 int ret;
37 void *shm = NULL;
38 struct shmid_ds ds = { 0 };
39 struct shminfo info = { 0 };
40
41 shmid = shmget(IPC_PRIVATE, PAGE_SIZE, 0777 | IPC_CREAT);
42 ICUNIT_ASSERT_NOT_EQUAL(shmid, -1, shmid);
43
44 shm = shmat(shmid, NULL, 0);
45 ICUNIT_GOTO_NOT_EQUAL(shm, INVALID_PTR, shm, ERROR_OUT);
46
47 (void)memset_s(shm, PAGE_SIZE, 0, PAGE_SIZE);
48
49 ret = shmctl(shmid, IPC_STAT, &ds);
50 ICUNIT_GOTO_EQUAL(ret, 0, ret, ERROR_OUT);
51 ICUNIT_GOTO_EQUAL(ds.shm_segsz, PAGE_SIZE, ds.shm_segsz, ERROR_OUT);
52 ICUNIT_GOTO_EQUAL(ds.shm_nattch, 1, ds.shm_nattch, ERROR_OUT);
53 ICUNIT_GOTO_EQUAL(ds.shm_cpid, getpid(), ds.shm_cpid, ERROR_OUT);
54 ICUNIT_GOTO_EQUAL(ds.shm_lpid, getpid(), ds.shm_lpid, ERROR_OUT);
55 ICUNIT_GOTO_EQUAL(ds.shm_perm.uid, getuid(), ds.shm_perm.uid, ERROR_OUT);
56
57 ret = shmctl(shmid, SHM_STAT, &ds);
58 ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, ERROR_OUT);
59 ICUNIT_GOTO_NOT_EQUAL(ret, 0, ret, ERROR_OUT);
60
61 ds.shm_perm.uid = getuid();
62 ds.shm_perm.gid = getgid();
63 ds.shm_perm.mode = 0;
64 ret = shmctl(shmid, IPC_SET, &ds);
65 ICUNIT_GOTO_EQUAL(ret, 0, ret, ERROR_OUT);
66
67 ret = shmctl(shmid, IPC_INFO, (struct shmid_ds *)&info);
68 ICUNIT_GOTO_EQUAL(ret, 192, ret, ERROR_OUT);
69 ICUNIT_GOTO_EQUAL(info.shmmax, 0x1000000, info.shmmax, ERROR_OUT);
70 ICUNIT_GOTO_EQUAL(info.shmmin, 1, info.shmmin, ERROR_OUT);
71 ICUNIT_GOTO_EQUAL(info.shmmni, 192, info.shmmni, ERROR_OUT);
72 ICUNIT_GOTO_EQUAL(info.shmseg, 128, info.shmseg, ERROR_OUT); // 128: expected value of shmseg
73 ICUNIT_GOTO_EQUAL(info.shmall, 0x1000, info.shmall, ERROR_OUT);
74
75 ret = shmdt(shm);
76 ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, ERROR_OUT);
77
78 ret = shmctl(shmid, IPC_RMID, NULL);
79 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
80 return 0;
81
82 ERROR_OUT:
83 ret = shmctl(shmid, IPC_RMID, NULL);
84 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
85 return -1;
86 }
87
ItTestShm004(void)88 void ItTestShm004(void)
89 {
90 TEST_ADD_CASE("IT_MEM_SHM_004", Testcase, TEST_LOS, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
91 }
92