blob: e71597931efdd50bb722dc313190366f7fab98d2 [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/msg.h>
33
34#include "TemporaryFile.h"
35
36TEST(sys_msg, smoke) {
37 // Create a queue.
38 TemporaryDir dir;
39 key_t key = ftok(dir.dirname, 1);
40 int id = msgget(key, IPC_CREAT|0666);
41 ASSERT_NE(id, -1);
42
43 // Queue should be empty.
44 msqid_ds ds;
45 memset(&ds, 0, sizeof(ds));
46 ASSERT_EQ(0, msgctl(id, IPC_STAT, &ds));
47 ASSERT_EQ(0U, ds.msg_qnum);
48 ASSERT_EQ(0U, ds.msg_cbytes);
49
50 // Send a message.
51 struct {
52 long type;
53 char data[32];
54 } msg = { 1, "hello world" };
55 ASSERT_EQ(0, msgsnd(id, &msg, sizeof(msg), 0));
56
57 // Queue should be non-empty.
58 ASSERT_EQ(0, msgctl(id, IPC_STAT, &ds));
59 ASSERT_EQ(1U, ds.msg_qnum);
60 ASSERT_EQ(sizeof(msg), ds.msg_cbytes);
61
62 // Read the message.
63 memset(&msg, 0, sizeof(msg));
64 ASSERT_EQ(static_cast<ssize_t>(sizeof(msg)), msgrcv(id, &msg, sizeof(msg), 0, 0));
65 ASSERT_EQ(1, msg.type);
66 ASSERT_STREQ("hello world", msg.data);
67
68 // Destroy the queue.
69 ASSERT_EQ(0, msgctl(id, IPC_RMID, 0));
70}
71
72TEST(sys_msg, msgctl_failure) {
73 errno = 0;
74 ASSERT_EQ(-1, msgctl(-1, IPC_STAT, nullptr));
75 ASSERT_EQ(EINVAL, errno);
76}
77
78TEST(sys_msg, msgget_failure) {
79 errno = 0;
80 ASSERT_EQ(-1, msgget(-1, 0));
81 ASSERT_EQ(ENOENT, errno);
82}
83
84TEST(sys_msg, msgrcv_failure) {
85 errno = 0;
86 ASSERT_EQ(-1, msgrcv(-1, nullptr, 0, 0, 0));
87 ASSERT_EQ(EINVAL, errno);
88}
89
90TEST(sys_msg, msgsnd_failure) {
91 errno = 0;
92 ASSERT_EQ(-1, msgsnd(-1, "", 0, 0));
93 ASSERT_EQ(EINVAL, errno);
94}