Update V8 to version 4.7.42.
[chromium-blink-merge.git] / content / common / common_param_traits_unittest.cc
blobaefab1c63a61097637f72ae7bc854d73535ebffc
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.
5 #include <string.h>
6 #include <utility>
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "content/public/common/common_param_traits.h"
11 #include "content/public/common/url_utils.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/geometry/rect.h"
20 #include "ui/gfx/ipc/gfx_param_traits.h"
21 #include "url/gurl.h"
23 // Tests that serialize/deserialize correctly understand each other
24 TEST(IPCMessageTest, Serialize) {
25 const char* serialize_cases[] = {
26 "http://www.google.com/",
27 "http://user:pass@host.com:888/foo;bar?baz#nop",
30 for (size_t i = 0; i < arraysize(serialize_cases); i++) {
31 GURL input(serialize_cases[i]);
32 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
33 IPC::ParamTraits<GURL>::Write(&msg, input);
35 GURL output;
36 base::PickleIterator iter(msg);
37 EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
39 // We want to test each component individually to make sure its range was
40 // correctly serialized and deserialized, not just the spec.
41 EXPECT_EQ(input.possibly_invalid_spec(), output.possibly_invalid_spec());
42 EXPECT_EQ(input.is_valid(), output.is_valid());
43 EXPECT_EQ(input.scheme(), output.scheme());
44 EXPECT_EQ(input.username(), output.username());
45 EXPECT_EQ(input.password(), output.password());
46 EXPECT_EQ(input.host(), output.host());
47 EXPECT_EQ(input.port(), output.port());
48 EXPECT_EQ(input.path(), output.path());
49 EXPECT_EQ(input.query(), output.query());
50 EXPECT_EQ(input.ref(), output.ref());
53 // Test an excessively long GURL.
55 const std::string url = std::string("http://example.org/").append(
56 content::GetMaxURLChars() + 1, 'a');
57 GURL input(url.c_str());
58 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
59 IPC::ParamTraits<GURL>::Write(&msg, input);
61 GURL output;
62 base::PickleIterator iter(msg);
63 EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
64 EXPECT_TRUE(output.is_empty());
67 // Test an invalid GURL.
69 IPC::Message msg;
70 msg.WriteString("#inva://idurl/");
71 GURL output;
72 base::PickleIterator iter(msg);
73 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
76 // Also test the corrupt case.
77 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
78 msg.WriteInt(99);
79 GURL output;
80 base::PickleIterator iter(msg);
81 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
84 // Tests std::pair serialization
85 TEST(IPCMessageTest, Pair) {
86 typedef std::pair<std::string, std::string> TestPair;
88 TestPair input("foo", "bar");
89 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
90 IPC::ParamTraits<TestPair>::Write(&msg, input);
92 TestPair output;
93 base::PickleIterator iter(msg);
94 EXPECT_TRUE(IPC::ParamTraits<TestPair>::Read(&msg, &iter, &output));
95 EXPECT_EQ(output.first, "foo");
96 EXPECT_EQ(output.second, "bar");
99 // Tests bitmap serialization.
100 TEST(IPCMessageTest, Bitmap) {
101 SkBitmap bitmap;
103 bitmap.allocN32Pixels(10, 5);
104 memset(bitmap.getPixels(), 'A', bitmap.getSize());
106 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
107 IPC::ParamTraits<SkBitmap>::Write(&msg, bitmap);
109 SkBitmap output;
110 base::PickleIterator iter(msg);
111 EXPECT_TRUE(IPC::ParamTraits<SkBitmap>::Read(&msg, &iter, &output));
113 EXPECT_EQ(bitmap.colorType(), output.colorType());
114 EXPECT_EQ(bitmap.width(), output.width());
115 EXPECT_EQ(bitmap.height(), output.height());
116 EXPECT_EQ(bitmap.rowBytes(), output.rowBytes());
117 EXPECT_EQ(bitmap.getSize(), output.getSize());
118 EXPECT_EQ(memcmp(bitmap.getPixels(), output.getPixels(), bitmap.getSize()),
121 // Also test the corrupt case.
122 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
123 // Copy the first message block over to |bad_msg|.
124 const char* fixed_data;
125 int fixed_data_size;
126 iter = base::PickleIterator(msg);
127 EXPECT_TRUE(iter.ReadData(&fixed_data, &fixed_data_size));
128 bad_msg.WriteData(fixed_data, fixed_data_size);
129 // Add some bogus pixel data.
130 const size_t bogus_pixels_size = bitmap.getSize() * 2;
131 scoped_ptr<char[]> bogus_pixels(new char[bogus_pixels_size]);
132 memset(bogus_pixels.get(), 'B', bogus_pixels_size);
133 bad_msg.WriteData(bogus_pixels.get(), bogus_pixels_size);
134 // Make sure we don't read out the bitmap!
135 SkBitmap bad_output;
136 iter = base::PickleIterator(bad_msg);
137 EXPECT_FALSE(IPC::ParamTraits<SkBitmap>::Read(&bad_msg, &iter, &bad_output));
140 TEST(IPCMessageTest, ListValue) {
141 base::ListValue input;
142 input.Set(0, new base::FundamentalValue(42.42));
143 input.Set(1, new base::StringValue("forty"));
144 input.Set(2, base::Value::CreateNullValue());
146 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
147 IPC::WriteParam(&msg, input);
149 base::ListValue output;
150 base::PickleIterator iter(msg);
151 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
153 EXPECT_TRUE(input.Equals(&output));
155 // Also test the corrupt case.
156 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
157 bad_msg.WriteInt(99);
158 iter = base::PickleIterator(bad_msg);
159 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
162 TEST(IPCMessageTest, DictionaryValue) {
163 base::DictionaryValue input;
164 input.Set("null", base::Value::CreateNullValue());
165 input.Set("bool", new base::FundamentalValue(true));
166 input.Set("int", new base::FundamentalValue(42));
168 scoped_ptr<base::DictionaryValue> subdict(new base::DictionaryValue());
169 subdict->Set("str", new base::StringValue("forty two"));
170 subdict->Set("bool", new base::FundamentalValue(false));
172 scoped_ptr<base::ListValue> sublist(new base::ListValue());
173 sublist->Set(0, new base::FundamentalValue(42.42));
174 sublist->Set(1, new base::StringValue("forty"));
175 sublist->Set(2, new base::StringValue("two"));
176 subdict->Set("list", sublist.release());
178 input.Set("dict", subdict.release());
180 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
181 IPC::WriteParam(&msg, input);
183 base::DictionaryValue output;
184 base::PickleIterator iter(msg);
185 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
187 EXPECT_TRUE(input.Equals(&output));
189 // Also test the corrupt case.
190 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
191 bad_msg.WriteInt(99);
192 iter = base::PickleIterator(bad_msg);
193 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
196 // Tests net::HostPortPair serialization
197 TEST(IPCMessageTest, HostPortPair) {
198 net::HostPortPair input("host.com", 12345);
200 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
201 IPC::ParamTraits<net::HostPortPair>::Write(&msg, input);
203 net::HostPortPair output;
204 base::PickleIterator iter(msg);
205 EXPECT_TRUE(IPC::ParamTraits<net::HostPortPair>::Read(&msg, &iter, &output));
206 EXPECT_EQ(input.host(), output.host());
207 EXPECT_EQ(input.port(), output.port());