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 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <sys/msg.h> |
| 33 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 34 | #include <android-base/file.h> |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 35 | |
| 36 | TEST(sys_msg, smoke) { |
Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 37 | if (msgctl(-1, IPC_STAT, nullptr) == -1 && errno == ENOSYS) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 38 | GTEST_SKIP() << "no <sys/msg.h> support in this kernel"; |
Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 41 | // Create a queue. |
| 42 | TemporaryDir dir; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 43 | key_t key = ftok(dir.path, 1); |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 44 | int id = msgget(key, IPC_CREAT|0666); |
| 45 | ASSERT_NE(id, -1); |
| 46 | |
| 47 | // Queue should be empty. |
| 48 | msqid_ds ds; |
| 49 | memset(&ds, 0, sizeof(ds)); |
| 50 | ASSERT_EQ(0, msgctl(id, IPC_STAT, &ds)); |
| 51 | ASSERT_EQ(0U, ds.msg_qnum); |
| 52 | ASSERT_EQ(0U, ds.msg_cbytes); |
| 53 | |
| 54 | // Send a message. |
| 55 | struct { |
| 56 | long type; |
| 57 | char data[32]; |
| 58 | } msg = { 1, "hello world" }; |
Kevin Brodsky | 3cc6ae0 | 2023-10-30 09:35:09 +0000 | [diff] [blame] | 59 | ASSERT_EQ(0, msgsnd(id, &msg, sizeof(msg.data), 0)); |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 60 | |
| 61 | // Queue should be non-empty. |
| 62 | ASSERT_EQ(0, msgctl(id, IPC_STAT, &ds)); |
| 63 | ASSERT_EQ(1U, ds.msg_qnum); |
Kevin Brodsky | 3cc6ae0 | 2023-10-30 09:35:09 +0000 | [diff] [blame] | 64 | ASSERT_EQ(sizeof(msg.data), ds.msg_cbytes); |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 65 | |
| 66 | // Read the message. |
| 67 | memset(&msg, 0, sizeof(msg)); |
Kevin Brodsky | 3cc6ae0 | 2023-10-30 09:35:09 +0000 | [diff] [blame] | 68 | ASSERT_EQ(static_cast<ssize_t>(sizeof(msg.data)), |
| 69 | msgrcv(id, &msg, sizeof(msg.data), 0, 0)); |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 70 | ASSERT_EQ(1, msg.type); |
| 71 | ASSERT_STREQ("hello world", msg.data); |
| 72 | |
| 73 | // Destroy the queue. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 74 | ASSERT_EQ(0, msgctl(id, IPC_RMID, nullptr)); |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST(sys_msg, msgctl_failure) { |
zijunzhao | c76899f | 2023-04-15 01:13:26 +0000 | [diff] [blame] | 78 | #pragma clang diagnostic push |
| 79 | #pragma clang diagnostic ignored "-Wnonnull" |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 80 | errno = 0; |
| 81 | ASSERT_EQ(-1, msgctl(-1, IPC_STAT, nullptr)); |
Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 82 | ASSERT_TRUE(errno == EINVAL || errno == ENOSYS); |
zijunzhao | c76899f | 2023-04-15 01:13:26 +0000 | [diff] [blame] | 83 | #pragma clang diagnostic pop |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST(sys_msg, msgget_failure) { |
| 87 | errno = 0; |
| 88 | ASSERT_EQ(-1, msgget(-1, 0)); |
Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 89 | ASSERT_TRUE(errno == ENOENT || errno == ENOSYS); |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST(sys_msg, msgrcv_failure) { |
zijunzhao | c76899f | 2023-04-15 01:13:26 +0000 | [diff] [blame] | 93 | #pragma clang diagnostic push |
| 94 | #pragma clang diagnostic ignored "-Wnonnull" |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 95 | errno = 0; |
| 96 | ASSERT_EQ(-1, msgrcv(-1, nullptr, 0, 0, 0)); |
Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 97 | ASSERT_TRUE(errno == EINVAL || errno == ENOSYS); |
zijunzhao | c76899f | 2023-04-15 01:13:26 +0000 | [diff] [blame] | 98 | #pragma clang diagnostic pop |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | TEST(sys_msg, msgsnd_failure) { |
Kevin Brodsky | 3cc6ae0 | 2023-10-30 09:35:09 +0000 | [diff] [blame] | 102 | struct { |
| 103 | long type; |
| 104 | char data[1]; |
| 105 | } msg = { 1, "" }; |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 106 | errno = 0; |
Kevin Brodsky | 3cc6ae0 | 2023-10-30 09:35:09 +0000 | [diff] [blame] | 107 | ASSERT_EQ(-1, msgsnd(-1, &msg, sizeof(msg.data), 0)); |
Elliott Hughes | 40bae4f | 2016-08-26 18:33:19 -0700 | [diff] [blame] | 108 | ASSERT_TRUE(errno == EINVAL || errno == ENOSYS); |
Elliott Hughes | 7c59f3f | 2016-08-16 18:14:26 -0700 | [diff] [blame] | 109 | } |