2 * Copyright (C) 2016 The Android Open Source Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
29 #include <gtest/gtest.h>
34 #include "TemporaryFile.h"
36 TEST(sys_sem, smoke) {
37 if (semctl(-1, 0, IPC_RMID) == -1 && errno == ENOSYS) {
38 GTEST_LOG_(INFO) << "no <sys/sem.h> support in this kernel\n";
42 // Create a semaphore.
44 key_t key = ftok(dir.dirname, 1);
45 int id = semget(key, 1, IPC_CREAT|0666);
48 // Check semaphore info.
50 memset(&ds, 0, sizeof(ds));
51 ASSERT_EQ(0, semctl(id, 0, IPC_STAT, &ds));
52 ASSERT_EQ(1U, ds.sem_nsems);
54 ASSERT_EQ(0, semctl(id, 0, GETVAL));
57 sembuf ops[] = {{ .sem_num = 0, .sem_op = 1, .sem_flg = 0 }};
58 ASSERT_EQ(0, semop(id, ops, 1));
59 ASSERT_EQ(1, semctl(id, 0, GETVAL));
62 timespec ts = { .tv_sec = 0, .tv_nsec = 100 };
63 ops[0] = { .sem_num = 0, .sem_op = 0, .sem_flg = 0 };
65 ASSERT_EQ(-1, semtimedop(id, ops, 1, &ts));
66 ASSERT_EQ(EAGAIN, errno);
67 ASSERT_EQ(1, semctl(id, 0, GETVAL));
70 ops[0] = { .sem_num = 0, .sem_op = -1, .sem_flg = 0 };
71 ASSERT_EQ(0, semop(id, ops, 1));
72 ASSERT_EQ(0, semctl(id, 0, GETVAL));
74 // Destroy the semaphore.
75 ASSERT_EQ(0, semctl(id, 0, IPC_RMID));
78 TEST(sys_sem, semget_failure) {
80 ASSERT_EQ(-1, semget(-1, -1, 0));
81 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
84 TEST(sys_sem, semctl_failure) {
86 ASSERT_EQ(-1, semctl(-1, 0, IPC_RMID));
87 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
90 TEST(sys_sem, semop_failure) {
92 ASSERT_EQ(-1, semop(-1, nullptr, 0));
93 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
96 TEST(sys_sem, semtimedop_failure) {
98 ASSERT_EQ(-1, semtimedop(-1, nullptr, 0, nullptr));
99 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);