| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 29 | #include <errno.h> | 
|  | 30 | #include <sys/shm.h> | 
|  | 31 |  | 
| Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 32 | #include <android-base/file.h> | 
|  | 33 | #include <gtest/gtest.h> | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | TEST(sys_shm, smoke) { | 
| Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 36 | if (shmctl(-1, IPC_STAT, nullptr) == -1 && errno == ENOSYS) { | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 37 | GTEST_SKIP() << "no <sys/shm.h> support in this kernel"; | 
| Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 40 | // Create a segment. | 
|  | 41 | TemporaryDir dir; | 
| Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 42 | key_t key = ftok(dir.path, 1); | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 43 | int id = shmget(key, 1234, IPC_CREAT|0666); | 
|  | 44 | ASSERT_NE(id, -1); | 
|  | 45 |  | 
|  | 46 | // Check segment info. | 
|  | 47 | shmid_ds ds; | 
|  | 48 | memset(&ds, 0, sizeof(ds)); | 
|  | 49 | ASSERT_EQ(0, shmctl(id, IPC_STAT, &ds)); | 
|  | 50 | ASSERT_EQ(1234U, ds.shm_segsz); | 
|  | 51 |  | 
|  | 52 | // Attach. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 53 | void* p = shmat(id, nullptr, SHM_RDONLY); | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 54 | ASSERT_NE(p, nullptr); | 
|  | 55 |  | 
|  | 56 | // Detach. | 
|  | 57 | ASSERT_EQ(0, shmdt(p)); | 
|  | 58 |  | 
|  | 59 | // Destroy the segment. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 60 | ASSERT_EQ(0, shmctl(id, IPC_RMID, nullptr)); | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
|  | 63 | TEST(sys_shm, shmat_failure) { | 
|  | 64 | errno = 0; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 65 | ASSERT_EQ(reinterpret_cast<void*>(-1), shmat(-1, nullptr, SHM_RDONLY)); | 
| Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 66 | ASSERT_TRUE(errno == EINVAL || errno == ENOSYS); | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
|  | 69 | TEST(sys_shm, shmctl_failure) { | 
|  | 70 | errno = 0; | 
|  | 71 | ASSERT_EQ(-1, shmctl(-1, IPC_STAT, nullptr)); | 
| Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(errno == EINVAL || errno == ENOSYS); | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
|  | 75 | TEST(sys_shm, shmdt_failure) { | 
|  | 76 | errno = 0; | 
|  | 77 | ASSERT_EQ(-1, shmdt(nullptr)); | 
| Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(errno == EINVAL || errno == ENOSYS); | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
|  | 81 | TEST(sys_shm, shmget_failure) { | 
|  | 82 | errno = 0; | 
|  | 83 | ASSERT_EQ(-1, shmget(-1, 1234, 0)); | 
| Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 84 | ASSERT_TRUE(errno == ENOENT || errno == ENOSYS); | 
| Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 85 | } |