blob: 5acc792ef71d0f6b049dd7bbfd4ef67096ebb8c4 [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/sem.h>
33
34#include "TemporaryFile.h"
35
36TEST(sys_sem, smoke) {
37 // Create a semaphore.
38 TemporaryDir dir;
39 key_t key = ftok(dir.dirname, 1);
40 int id = semget(key, 1, IPC_CREAT|0666);
41 ASSERT_NE(id, -1);
42
43 // Check semaphore info.
44 semid_ds ds;
45 memset(&ds, 0, sizeof(ds));
46 ASSERT_EQ(0, semctl(id, 0, IPC_STAT, &ds));
47 ASSERT_EQ(1U, ds.sem_nsems);
48
49 ASSERT_EQ(0, semctl(id, 0, GETVAL));
50
51 // Increment.
52 sembuf ops[] = {{ .sem_num = 0, .sem_op = 1, .sem_flg = 0 }};
53 ASSERT_EQ(0, semop(id, ops, 1));
54 ASSERT_EQ(1, semctl(id, 0, GETVAL));
55
56 // Test timeouts.
57 timespec ts = { .tv_sec = 0, .tv_nsec = 100 };
58 ops[0] = { .sem_num = 0, .sem_op = 0, .sem_flg = 0 };
59 errno = 0;
60 ASSERT_EQ(-1, semtimedop(id, ops, 1, &ts));
61 ASSERT_EQ(EAGAIN, errno);
62 ASSERT_EQ(1, semctl(id, 0, GETVAL));
63
64 // Decrement.
65 ops[0] = { .sem_num = 0, .sem_op = -1, .sem_flg = 0 };
66 ASSERT_EQ(0, semop(id, ops, 1));
67 ASSERT_EQ(0, semctl(id, 0, GETVAL));
68
69 // Destroy the semaphore.
70 ASSERT_EQ(0, semctl(id, 0, IPC_RMID));
71}
72
73TEST(sys_sem, semget_failure) {
74 errno = 0;
75 ASSERT_EQ(-1, semget(-1, -1, 0));
76 ASSERT_EQ(EINVAL, errno);
77}
78
79TEST(sys_sem, semctl_failure) {
80 errno = 0;
81 ASSERT_EQ(-1, semctl(-1, 0, IPC_RMID));
82 ASSERT_EQ(EINVAL, errno);
83}
84
85TEST(sys_sem, semop_failure) {
86 errno = 0;
87 ASSERT_EQ(-1, semop(-1, nullptr, 0));
88 ASSERT_EQ(EINVAL, errno);
89}
90
91TEST(sys_sem, semtimedop_failure) {
92 errno = 0;
93 ASSERT_EQ(-1, semtimedop(-1, nullptr, 0, nullptr));
94 ASSERT_EQ(EINVAL, errno);
95}