Home | History | Annotate | Download | only in tests
      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 
     29 #include <gtest/gtest.h>
     30 
     31 #include <errno.h>
     32 #include <sys/shm.h>
     33 
     34 #include "TemporaryFile.h"
     35 
     36 TEST(sys_shm, smoke) {
     37   if (shmctl(-1, IPC_STAT, nullptr) == -1 && errno == ENOSYS) {
     38     GTEST_LOG_(INFO) << "no <sys/shm.h> support in this kernel\n";
     39     return;
     40   }
     41 
     42   // Create a segment.
     43   TemporaryDir dir;
     44   key_t key = ftok(dir.dirname, 1);
     45   int id = shmget(key, 1234, IPC_CREAT|0666);
     46   ASSERT_NE(id, -1);
     47 
     48   // Check segment info.
     49   shmid_ds ds;
     50   memset(&ds, 0, sizeof(ds));
     51   ASSERT_EQ(0, shmctl(id, IPC_STAT, &ds));
     52   ASSERT_EQ(1234U, ds.shm_segsz);
     53 
     54   // Attach.
     55   void* p = shmat(id, 0, SHM_RDONLY);
     56   ASSERT_NE(p, nullptr);
     57 
     58   // Detach.
     59   ASSERT_EQ(0, shmdt(p));
     60 
     61   // Destroy the segment.
     62   ASSERT_EQ(0, shmctl(id, IPC_RMID, 0));
     63 }
     64 
     65 TEST(sys_shm, shmat_failure) {
     66   errno = 0;
     67   ASSERT_EQ(reinterpret_cast<void*>(-1), shmat(-1, 0, SHM_RDONLY));
     68   ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
     69 }
     70 
     71 TEST(sys_shm, shmctl_failure) {
     72   errno = 0;
     73   ASSERT_EQ(-1, shmctl(-1, IPC_STAT, nullptr));
     74   ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
     75 }
     76 
     77 TEST(sys_shm, shmdt_failure) {
     78   errno = 0;
     79   ASSERT_EQ(-1, shmdt(nullptr));
     80   ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
     81 }
     82 
     83 TEST(sys_shm, shmget_failure) {
     84   errno = 0;
     85   ASSERT_EQ(-1, shmget(-1, 1234, 0));
     86   ASSERT_TRUE(errno == ENOENT || errno == ENOSYS);
     87 }
     88