OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "dbus/message.h" | 5 #include "dbus/message.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 TEST(MessageTest, AppendAndPopFileDescriptor) { | 117 TEST(MessageTest, AppendAndPopFileDescriptor) { |
118 if (!IsDBusTypeUnixFdSupported()) { | 118 if (!IsDBusTypeUnixFdSupported()) { |
119 LOG(WARNING) << "FD passing is not supported"; | 119 LOG(WARNING) << "FD passing is not supported"; |
120 return; | 120 return; |
121 } | 121 } |
122 | 122 |
123 std::unique_ptr<Response> message(Response::CreateEmpty()); | 123 std::unique_ptr<Response> message(Response::CreateEmpty()); |
124 MessageWriter writer(message.get()); | 124 MessageWriter writer(message.get()); |
125 | 125 |
126 // Append stdout. | 126 // Append stdout. |
127 FileDescriptor temp(1); | 127 const int fd_in = 1; |
128 // Descriptor should not be valid until checked. | 128 writer.AppendFileDescriptor(fd_in); |
129 ASSERT_FALSE(temp.is_valid()); | |
130 // NB: thread IO requirements not relevant for unit tests. | |
131 temp.CheckValidity(); | |
132 ASSERT_TRUE(temp.is_valid()); | |
133 writer.AppendFileDescriptor(temp); | |
134 | 129 |
135 FileDescriptor fd_value; | 130 base::ScopedFD fd_out; |
136 | 131 |
137 MessageReader reader(message.get()); | 132 MessageReader reader(message.get()); |
138 ASSERT_TRUE(reader.HasMoreData()); | 133 ASSERT_TRUE(reader.HasMoreData()); |
139 ASSERT_EQ(Message::UNIX_FD, reader.GetDataType()); | 134 ASSERT_EQ(Message::UNIX_FD, reader.GetDataType()); |
140 ASSERT_EQ("h", reader.GetDataSignature()); | 135 ASSERT_EQ("h", reader.GetDataSignature()); |
141 ASSERT_TRUE(reader.PopFileDescriptor(&fd_value)); | 136 ASSERT_TRUE(reader.PopFileDescriptor(&fd_out)); |
142 ASSERT_FALSE(reader.HasMoreData()); | 137 ASSERT_FALSE(reader.HasMoreData()); |
143 // Descriptor is not valid until explicitly checked. | |
144 ASSERT_FALSE(fd_value.is_valid()); | |
145 fd_value.CheckValidity(); | |
146 ASSERT_TRUE(fd_value.is_valid()); | |
147 | 138 |
148 // Stdout should be returned but we cannot check the descriptor | 139 // Stdout should be returned but we cannot check the descriptor |
149 // value because stdout will be dup'd. Instead check st_rdev | 140 // value because stdout will be dup'd. Instead check st_rdev |
150 // which should be identical. | 141 // which should be identical. |
151 struct stat sb_stdout; | 142 struct stat sb_stdout; |
152 int status_stdout = HANDLE_EINTR(fstat(1, &sb_stdout)); | 143 int status_stdout = HANDLE_EINTR(fstat(fd_in, &sb_stdout)); |
153 ASSERT_GE(status_stdout, 0); | 144 ASSERT_GE(status_stdout, 0); |
154 struct stat sb_fd; | 145 struct stat sb_fd; |
155 int status_fd = HANDLE_EINTR(fstat(fd_value.value(), &sb_fd)); | 146 int status_fd = HANDLE_EINTR(fstat(fd_out.get(), &sb_fd)); |
156 ASSERT_GE(status_fd, 0); | 147 ASSERT_GE(status_fd, 0); |
157 EXPECT_EQ(sb_stdout.st_rdev, sb_fd.st_rdev); | 148 EXPECT_EQ(sb_stdout.st_rdev, sb_fd.st_rdev); |
158 } | 149 } |
159 | 150 |
160 // Check all variant types can be properly written and read. | 151 // Check all variant types can be properly written and read. |
161 TEST(MessageTest, AppendAndPopVariantDataTypes) { | 152 TEST(MessageTest, AppendAndPopVariantDataTypes) { |
162 std::unique_ptr<Response> message(Response::CreateEmpty()); | 153 std::unique_ptr<Response> message(Response::CreateEmpty()); |
163 MessageWriter writer(message.get()); | 154 MessageWriter writer(message.get()); |
164 | 155 |
165 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path". | 156 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path". |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 | 674 |
684 ASSERT_EQ("message_type: MESSAGE_METHOD_RETURN\n" | 675 ASSERT_EQ("message_type: MESSAGE_METHOD_RETURN\n" |
685 "signature: s\n\n" | 676 "signature: s\n\n" |
686 "string \"oooooooooooooooooooooooooooooooooooooooooooooooo" | 677 "string \"oooooooooooooooooooooooooooooooooooooooooooooooo" |
687 "oooooooooooooooooooooooooooooooooooooooooooooooooooo... " | 678 "oooooooooooooooooooooooooooooooooooooooooooooooooooo... " |
688 "(1000 bytes in total)\"\n", | 679 "(1000 bytes in total)\"\n", |
689 message->ToString()); | 680 message->ToString()); |
690 } | 681 } |
691 | 682 |
692 } // namespace dbus | 683 } // namespace dbus |
OLD | NEW |