blob: 4929b8f9d915119b13bee5bb6494d10c8471cf04 [file] [log] [blame]
Elliott Hughes7c59f3f2016-08-16 18:14:26 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <gtest/gtest.h>
30
31#include <errno.h>
32#include <sys/shm.h>
33
34#include "TemporaryFile.h"
35
36TEST(sys_shm, smoke) {
37 // Create a segment.
38 TemporaryDir dir;
39 key_t key = ftok(dir.dirname, 1);
40 int id = shmget(key, 1234, IPC_CREAT|0666);
41 ASSERT_NE(id, -1);
42
43 // Check segment info.
44 shmid_ds ds;
45 memset(&ds, 0, sizeof(ds));
46 ASSERT_EQ(0, shmctl(id, IPC_STAT, &ds));
47 ASSERT_EQ(1234U, ds.shm_segsz);
48
49 // Attach.
50 void* p = shmat(id, 0, SHM_RDONLY);
51 ASSERT_NE(p, nullptr);
52
53 // Detach.
54 ASSERT_EQ(0, shmdt(p));
55
56 // Destroy the segment.
57 ASSERT_EQ(0, shmctl(id, IPC_RMID, 0));
58}
59
60TEST(sys_shm, shmat_failure) {
61 errno = 0;
62 ASSERT_EQ(reinterpret_cast<void*>(-1), shmat(-1, 0, SHM_RDONLY));
63 ASSERT_EQ(EINVAL, errno);
64}
65
66TEST(sys_shm, shmctl_failure) {
67 errno = 0;
68 ASSERT_EQ(-1, shmctl(-1, IPC_STAT, nullptr));
69 ASSERT_EQ(EINVAL, errno);
70}
71
72TEST(sys_shm, shmdt_failure) {
73 errno = 0;
74 ASSERT_EQ(-1, shmdt(nullptr));
75 ASSERT_EQ(EINVAL, errno);
76}
77
78TEST(sys_shm, shmget_failure) {
79 errno = 0;
80 ASSERT_EQ(-1, shmget(-1, 1234, 0));
81 ASSERT_EQ(ENOENT, errno);
82}