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
3 // found in the LICENSE file.
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "content/public/common/common_param_traits.h"
11 #include "googleurl/src/gurl.h"
12 #include "ipc/ipc_message.h"
13 #include "ipc/ipc_message_utils.h"
14 #include "net/base/host_port_pair.h"
15 #include "printing/backend/print_backend.h"
16 #include "printing/page_range.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/gfx/rect.h"
21 // Tests that serialize/deserialize correctly understand each other
22 TEST(IPCMessageTest
, Serialize
) {
23 const char* serialize_cases
[] = {
24 "http://www.google.com/",
25 "http://user:pass@host.com:888/foo;bar?baz#nop",
29 for (size_t i
= 0; i
< arraysize(serialize_cases
); i
++) {
30 GURL
input(serialize_cases
[i
]);
31 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
32 IPC::ParamTraits
<GURL
>::Write(&msg
, input
);
35 PickleIterator
iter(msg
);
36 EXPECT_TRUE(IPC::ParamTraits
<GURL
>::Read(&msg
, &iter
, &output
));
38 // We want to test each component individually to make sure its range was
39 // correctly serialized and deserialized, not just the spec.
40 EXPECT_EQ(input
.possibly_invalid_spec(), output
.possibly_invalid_spec());
41 EXPECT_EQ(input
.is_valid(), output
.is_valid());
42 EXPECT_EQ(input
.scheme(), output
.scheme());
43 EXPECT_EQ(input
.username(), output
.username());
44 EXPECT_EQ(input
.password(), output
.password());
45 EXPECT_EQ(input
.host(), output
.host());
46 EXPECT_EQ(input
.port(), output
.port());
47 EXPECT_EQ(input
.path(), output
.path());
48 EXPECT_EQ(input
.query(), output
.query());
49 EXPECT_EQ(input
.ref(), output
.ref());
52 // Also test the corrupt case.
53 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
56 PickleIterator
iter(msg
);
57 EXPECT_FALSE(IPC::ParamTraits
<GURL
>::Read(&msg
, &iter
, &output
));
60 // Tests std::pair serialization
61 TEST(IPCMessageTest
, Pair
) {
62 typedef std::pair
<std::string
, std::string
> TestPair
;
64 TestPair
input("foo", "bar");
65 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
66 IPC::ParamTraits
<TestPair
>::Write(&msg
, input
);
69 PickleIterator
iter(msg
);
70 EXPECT_TRUE(IPC::ParamTraits
<TestPair
>::Read(&msg
, &iter
, &output
));
71 EXPECT_EQ(output
.first
, "foo");
72 EXPECT_EQ(output
.second
, "bar");
76 // Tests bitmap serialization.
77 TEST(IPCMessageTest
, Bitmap
) {
80 bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, 10, 5);
82 memset(bitmap
.getPixels(), 'A', bitmap
.getSize());
84 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
85 IPC::ParamTraits
<SkBitmap
>::Write(&msg
, bitmap
);
88 PickleIterator
iter(msg
);
89 EXPECT_TRUE(IPC::ParamTraits
<SkBitmap
>::Read(&msg
, &iter
, &output
));
91 EXPECT_EQ(bitmap
.config(), output
.config());
92 EXPECT_EQ(bitmap
.width(), output
.width());
93 EXPECT_EQ(bitmap
.height(), output
.height());
94 EXPECT_EQ(bitmap
.rowBytes(), output
.rowBytes());
95 EXPECT_EQ(bitmap
.getSize(), output
.getSize());
96 EXPECT_EQ(memcmp(bitmap
.getPixels(), output
.getPixels(), bitmap
.getSize()),
99 // Also test the corrupt case.
100 IPC::Message
bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
101 // Copy the first message block over to |bad_msg|.
102 const char* fixed_data
;
104 iter
= PickleIterator(msg
);
105 msg
.ReadData(&iter
, &fixed_data
, &fixed_data_size
);
106 bad_msg
.WriteData(fixed_data
, fixed_data_size
);
107 // Add some bogus pixel data.
108 const size_t bogus_pixels_size
= bitmap
.getSize() * 2;
109 scoped_array
<char> bogus_pixels(new char[bogus_pixels_size
]);
110 memset(bogus_pixels
.get(), 'B', bogus_pixels_size
);
111 bad_msg
.WriteData(bogus_pixels
.get(), bogus_pixels_size
);
112 // Make sure we don't read out the bitmap!
114 iter
= PickleIterator(bad_msg
);
115 EXPECT_FALSE(IPC::ParamTraits
<SkBitmap
>::Read(&bad_msg
, &iter
, &bad_output
));
118 TEST(IPCMessageTest
, ListValue
) {
120 input
.Set(0, Value::CreateDoubleValue(42.42));
121 input
.Set(1, Value::CreateStringValue("forty"));
122 input
.Set(2, Value::CreateNullValue());
124 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
125 IPC::WriteParam(&msg
, input
);
128 PickleIterator
iter(msg
);
129 EXPECT_TRUE(IPC::ReadParam(&msg
, &iter
, &output
));
131 EXPECT_TRUE(input
.Equals(&output
));
133 // Also test the corrupt case.
134 IPC::Message
bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
135 bad_msg
.WriteInt(99);
136 iter
= PickleIterator(bad_msg
);
137 EXPECT_FALSE(IPC::ReadParam(&bad_msg
, &iter
, &output
));
140 TEST(IPCMessageTest
, DictionaryValue
) {
141 DictionaryValue input
;
142 input
.Set("null", Value::CreateNullValue());
143 input
.Set("bool", Value::CreateBooleanValue(true));
144 input
.Set("int", Value::CreateIntegerValue(42));
146 scoped_ptr
<DictionaryValue
> subdict(new DictionaryValue());
147 subdict
->Set("str", Value::CreateStringValue("forty two"));
148 subdict
->Set("bool", Value::CreateBooleanValue(false));
150 scoped_ptr
<ListValue
> sublist(new ListValue());
151 sublist
->Set(0, Value::CreateDoubleValue(42.42));
152 sublist
->Set(1, Value::CreateStringValue("forty"));
153 sublist
->Set(2, Value::CreateStringValue("two"));
154 subdict
->Set("list", sublist
.release());
156 input
.Set("dict", subdict
.release());
158 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
159 IPC::WriteParam(&msg
, input
);
161 DictionaryValue output
;
162 PickleIterator
iter(msg
);
163 EXPECT_TRUE(IPC::ReadParam(&msg
, &iter
, &output
));
165 EXPECT_TRUE(input
.Equals(&output
));
167 // Also test the corrupt case.
168 IPC::Message
bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
169 bad_msg
.WriteInt(99);
170 iter
= PickleIterator(bad_msg
);
171 EXPECT_FALSE(IPC::ReadParam(&bad_msg
, &iter
, &output
));
174 // Tests net::HostPortPair serialization
175 TEST(IPCMessageTest
, HostPortPair
) {
176 net::HostPortPair
input("host.com", 12345);
178 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
179 IPC::ParamTraits
<net::HostPortPair
>::Write(&msg
, input
);
181 net::HostPortPair output
;
182 PickleIterator
iter(msg
);
183 EXPECT_TRUE(IPC::ParamTraits
<net::HostPortPair
>::Read(&msg
, &iter
, &output
));
184 EXPECT_EQ(input
.host(), output
.host());
185 EXPECT_EQ(input
.port(), output
.port());