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_msg, smoke) {
37 if (msgctl(-1, IPC_STAT, nullptr) == -1 && errno == ENOSYS) {
38 GTEST_LOG_(INFO) << "no <sys/msg.h> support in this kernel\n";
44 key_t key = ftok(dir.dirname, 1);
45 int id = msgget(key, IPC_CREAT|0666);
48 // Queue should be empty.
50 memset(&ds, 0, sizeof(ds));
51 ASSERT_EQ(0, msgctl(id, IPC_STAT, &ds));
52 ASSERT_EQ(0U, ds.msg_qnum);
53 ASSERT_EQ(0U, ds.msg_cbytes);
59 } msg = { 1, "hello world" };
60 ASSERT_EQ(0, msgsnd(id, &msg, sizeof(msg), 0));
62 // Queue should be non-empty.
63 ASSERT_EQ(0, msgctl(id, IPC_STAT, &ds));
64 ASSERT_EQ(1U, ds.msg_qnum);
65 ASSERT_EQ(sizeof(msg), ds.msg_cbytes);
68 memset(&msg, 0, sizeof(msg));
69 ASSERT_EQ(static_cast<ssize_t>(sizeof(msg)), msgrcv(id, &msg, sizeof(msg), 0, 0));
70 ASSERT_EQ(1, msg.type);
71 ASSERT_STREQ("hello world", msg.data);
74 ASSERT_EQ(0, msgctl(id, IPC_RMID, 0));
77 TEST(sys_msg, msgctl_failure) {
79 ASSERT_EQ(-1, msgctl(-1, IPC_STAT, nullptr));
80 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
83 TEST(sys_msg, msgget_failure) {
85 ASSERT_EQ(-1, msgget(-1, 0));
86 ASSERT_TRUE(errno == ENOENT || errno == ENOSYS);
89 TEST(sys_msg, msgrcv_failure) {
91 ASSERT_EQ(-1, msgrcv(-1, nullptr, 0, 0, 0));
92 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);
95 TEST(sys_msg, msgsnd_failure) {
97 ASSERT_EQ(-1, msgsnd(-1, "", 0, 0));
98 ASSERT_TRUE(errno == EINVAL || errno == ENOSYS);