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 "ipc/ipc_message.h" | 5 #include "ipc/ipc_message.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <limits> | |
10 | |
9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
11 #include "base/values.h" | 13 #include "base/values.h" |
12 #include "ipc/ipc_message_utils.h" | 14 #include "ipc/ipc_message_utils.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 // IPC messages for testing ---------------------------------------------------- | 17 // IPC messages for testing ---------------------------------------------------- |
16 | 18 |
17 #define IPC_MESSAGE_IMPL | 19 #define IPC_MESSAGE_IMPL |
18 #include "ipc/ipc_message_macros.h" | 20 #include "ipc/ipc_message_macros.h" |
19 | 21 |
20 #define IPC_MESSAGE_START TestMsgStart | 22 #define IPC_MESSAGE_START TestMsgStart |
21 | 23 |
22 IPC_MESSAGE_CONTROL0(TestMsgClassEmpty) | 24 IPC_MESSAGE_CONTROL0(TestMsgClassEmpty) |
23 | 25 |
24 IPC_MESSAGE_CONTROL1(TestMsgClassI, int) | 26 IPC_MESSAGE_CONTROL1(TestMsgClassI, int) |
25 | 27 |
26 IPC_SYNC_MESSAGE_CONTROL1_1(TestMsgClassIS, int, std::string) | 28 IPC_SYNC_MESSAGE_CONTROL1_1(TestMsgClassIS, int, std::string) |
27 | 29 |
28 namespace { | 30 namespace IPC { |
29 | 31 |
30 TEST(IPCMessageTest, BasicMessageTest) { | 32 TEST(IPCMessageTest, BasicMessageTest) { |
31 int v1 = 10; | 33 int v1 = 10; |
32 std::string v2("foobar"); | 34 std::string v2("foobar"); |
33 base::string16 v3(base::ASCIIToUTF16("hello world")); | 35 base::string16 v3(base::ASCIIToUTF16("hello world")); |
34 | 36 |
35 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); | 37 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
36 EXPECT_TRUE(m.WriteInt(v1)); | 38 EXPECT_TRUE(m.WriteInt(v1)); |
37 EXPECT_TRUE(m.WriteString(v2)); | 39 EXPECT_TRUE(m.WriteString(v2)); |
38 EXPECT_TRUE(m.WriteString16(v3)); | 40 EXPECT_TRUE(m.WriteString16(v3)); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 | 110 |
109 EXPECT_TRUE(input.Equals(&output)); | 111 EXPECT_TRUE(input.Equals(&output)); |
110 | 112 |
111 // Also test the corrupt case. | 113 // Also test the corrupt case. |
112 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 114 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
113 bad_msg.WriteInt(99); | 115 bad_msg.WriteInt(99); |
114 iter = base::PickleIterator(bad_msg); | 116 iter = base::PickleIterator(bad_msg); |
115 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); | 117 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
116 } | 118 } |
117 | 119 |
120 TEST(IPCMessageTest, FindNext) { | |
121 IPC::Message message; | |
122 EXPECT_TRUE(message.WriteString("Goooooooogle")); | |
123 EXPECT_TRUE(message.WriteInt(111)); | |
124 | |
125 std::vector<char> message_data(message.size() + 7); | |
126 memcpy(message_data.data(), message.data(), message.size()); | |
127 | |
128 const char* data_start = message_data.data(); | |
129 const char* data_end = data_start + message.size(); | |
130 | |
131 IPC::Message::NextMessageInfo next; | |
132 | |
133 // Data range contains the entire message plus some extra bytes | |
134 IPC::Message::FindNext(data_start, data_end + 1, &next); | |
135 EXPECT_TRUE(next.message_found); | |
136 EXPECT_EQ(next.message_size, message.size()); | |
137 EXPECT_EQ(next.pickle_end, data_end); | |
138 EXPECT_EQ(next.message_end, data_end); | |
139 | |
140 // Data range exactly contains the entire message | |
141 IPC::Message::FindNext(data_start, data_end, &next); | |
142 EXPECT_TRUE(next.message_found); | |
143 EXPECT_EQ(next.message_size, message.size()); | |
144 EXPECT_EQ(next.pickle_end, data_end); | |
145 EXPECT_EQ(next.message_end, data_end); | |
146 | |
147 // Data range doesn't contain the entire message | |
148 // (but contains the message header) | |
149 IPC::Message::FindNext(data_start, data_end - 1, &next); | |
150 EXPECT_FALSE(next.message_found); | |
151 #if USE_ATTACHMENT_BROKER | |
152 EXPECT_EQ(next.message_size, 0u); | |
153 #else | |
154 EXPECT_EQ(next.message_size, message.size()); | |
155 #endif | |
156 | |
157 // Data range doesn't contain the message header | |
158 // (but contains the pickle header) | |
159 IPC::Message::FindNext(data_start, | |
160 data_start + sizeof(IPC::Message::Header) - 1, | |
161 &next); | |
162 EXPECT_FALSE(next.message_found); | |
163 EXPECT_EQ(next.message_size, 0u); | |
164 | |
165 // Data range doesn't contain the pickle header | |
166 IPC::Message::FindNext(data_start, | |
167 data_start + sizeof(base::Pickle::Header) - 1, | |
168 &next); | |
169 EXPECT_FALSE(next.message_found); | |
170 EXPECT_EQ(next.message_size, 0u); | |
171 } | |
172 | |
173 TEST(IPCMessageTest, FindNextOverflow) { | |
174 IPC::Message message; | |
175 EXPECT_TRUE(message.WriteString("Data")); | |
176 EXPECT_TRUE(message.WriteInt(777)); | |
177 | |
178 const char* data_start = reinterpret_cast<const char*>(message.data()); | |
179 const char* data_end = data_start + message.size(); | |
180 | |
181 IPC::Message::NextMessageInfo next; | |
182 | |
183 // Payload size is negative (defeats 'start + size > end' check) | |
184 message.header()->payload_size = static_cast<uint32_t>(-1); | |
185 IPC::Message::FindNext(data_start, data_end, &next); | |
186 EXPECT_FALSE(next.message_found); | |
187 #if USE_ATTACHMENT_BROKER | |
188 EXPECT_EQ(next.message_size, 0u); | |
189 #else | |
190 if (sizeof(size_t) > sizeof(uint32_t)) { | |
191 // No overflow, just insane message size | |
192 EXPECT_EQ(next.message_size, | |
193 message.header()->payload_size + sizeof(IPC::Message::Header)); | |
194 } else { | |
195 // Actual overflow, reported as max size_t | |
196 EXPECT_EQ(next.message_size, std::numeric_limits<size_t>::max()); | |
197 } | |
198 #endif | |
199 | |
200 // Payload size is max positive integer (defeats size < 0 check, while | |
201 // still potentially causing overflow down the road). | |
202 message.header()->payload_size = std::numeric_limits<int32_t>::max(); | |
203 IPC::Message::FindNext(data_start, data_end, &next); | |
204 EXPECT_FALSE(next.message_found); | |
205 #if USE_ATTACHMENT_BROKER | |
206 EXPECT_EQ(next.message_size, 0u); | |
207 #else | |
208 EXPECT_EQ(next.message_size, | |
209 message.header()->payload_size + sizeof(IPC::Message::Header)); | |
210 #endif | |
211 } | |
212 | |
213 namespace { | |
214 | |
118 class IPCMessageParameterTest : public testing::Test { | 215 class IPCMessageParameterTest : public testing::Test { |
119 public: | 216 public: |
120 IPCMessageParameterTest() : extra_param_("extra_param"), called_(false) {} | 217 IPCMessageParameterTest() : extra_param_("extra_param"), called_(false) {} |
121 | 218 |
122 bool OnMessageReceived(const IPC::Message& message) { | 219 bool OnMessageReceived(const IPC::Message& message) { |
123 bool handled = true; | 220 bool handled = true; |
124 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(IPCMessageParameterTest, message, | 221 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(IPCMessageParameterTest, message, |
125 &extra_param_) | 222 &extra_param_) |
126 IPC_MESSAGE_HANDLER(TestMsgClassEmpty, OnEmpty) | 223 IPC_MESSAGE_HANDLER(TestMsgClassEmpty, OnEmpty) |
127 IPC_MESSAGE_HANDLER(TestMsgClassI, OnInt) | 224 IPC_MESSAGE_HANDLER(TestMsgClassI, OnInt) |
(...skipping 25 matching lines...) Expand all Loading... | |
153 | 250 |
154 bool Send(IPC::Message* reply) { | 251 bool Send(IPC::Message* reply) { |
155 delete reply; | 252 delete reply; |
156 return true; | 253 return true; |
157 }*/ | 254 }*/ |
158 | 255 |
159 std::string extra_param_; | 256 std::string extra_param_; |
160 bool called_; | 257 bool called_; |
161 }; | 258 }; |
162 | 259 |
260 } // namespace | |
Maria
2015/09/25 22:23:00
if we are putting this into an anonymous namespace
Dmitry Skiba
2015/09/28 17:37:18
I think the convention is that helpers are in unna
| |
261 | |
163 TEST_F(IPCMessageParameterTest, EmptyDispatcherWithParam) { | 262 TEST_F(IPCMessageParameterTest, EmptyDispatcherWithParam) { |
164 TestMsgClassEmpty message; | 263 TestMsgClassEmpty message; |
165 EXPECT_TRUE(OnMessageReceived(message)); | 264 EXPECT_TRUE(OnMessageReceived(message)); |
166 EXPECT_TRUE(called_); | 265 EXPECT_TRUE(called_); |
167 } | 266 } |
168 | 267 |
169 #if defined(OS_ANDROID) | 268 #if defined(OS_ANDROID) |
170 #define MAYBE_OneIntegerWithParam DISABLED_OneIntegerWithParam | 269 #define MAYBE_OneIntegerWithParam DISABLED_OneIntegerWithParam |
171 #else | 270 #else |
172 #define MAYBE_OneIntegerWithParam OneIntegerWithParam | 271 #define MAYBE_OneIntegerWithParam OneIntegerWithParam |
173 #endif | 272 #endif |
174 TEST_F(IPCMessageParameterTest, MAYBE_OneIntegerWithParam) { | 273 TEST_F(IPCMessageParameterTest, MAYBE_OneIntegerWithParam) { |
175 TestMsgClassI message(42); | 274 TestMsgClassI message(42); |
176 EXPECT_TRUE(OnMessageReceived(message)); | 275 EXPECT_TRUE(OnMessageReceived(message)); |
177 EXPECT_TRUE(called_); | 276 EXPECT_TRUE(called_); |
178 } | 277 } |
179 | 278 |
180 /* TODO: handle sync IPCs | 279 /* TODO: handle sync IPCs |
181 TEST_F(IPCMessageParameterTest, Sync) { | 280 TEST_F(IPCMessageParameterTest, Sync) { |
182 std::string output; | 281 std::string output; |
183 TestMsgClassIS message(42, &output); | 282 TestMsgClassIS message(42, &output); |
184 EXPECT_TRUE(OnMessageReceived(message)); | 283 EXPECT_TRUE(OnMessageReceived(message)); |
185 EXPECT_TRUE(called_); | 284 EXPECT_TRUE(called_); |
186 EXPECT_EQ(output, std::string("out")); | 285 EXPECT_EQ(output, std::string("out")); |
187 }*/ | 286 }*/ |
188 | 287 |
189 } // namespace | 288 } // namespace IPC |
OLD | NEW |