• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include <string.h>
6 #include <utility>
7 
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "content/public/common/common_param_traits.h"
11 #include "ipc/ipc_message.h"
12 #include "ipc/ipc_message_utils.h"
13 #include "net/base/host_port_pair.h"
14 #include "printing/backend/print_backend.h"
15 #include "printing/page_range.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/gfx/rect.h"
19 #include "url/gurl.h"
20 
21 // Tests that serialize/deserialize correctly understand each other
TEST(IPCMessageTest,Serialize)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",
26   };
27 
28   for (size_t i = 0; i < arraysize(serialize_cases); i++) {
29     GURL input(serialize_cases[i]);
30     IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
31     IPC::ParamTraits<GURL>::Write(&msg, input);
32 
33     GURL output;
34     PickleIterator iter(msg);
35     EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
36 
37     // We want to test each component individually to make sure its range was
38     // correctly serialized and deserialized, not just the spec.
39     EXPECT_EQ(input.possibly_invalid_spec(), output.possibly_invalid_spec());
40     EXPECT_EQ(input.is_valid(), output.is_valid());
41     EXPECT_EQ(input.scheme(), output.scheme());
42     EXPECT_EQ(input.username(), output.username());
43     EXPECT_EQ(input.password(), output.password());
44     EXPECT_EQ(input.host(), output.host());
45     EXPECT_EQ(input.port(), output.port());
46     EXPECT_EQ(input.path(), output.path());
47     EXPECT_EQ(input.query(), output.query());
48     EXPECT_EQ(input.ref(), output.ref());
49   }
50 
51   // Test an invalid GURL.
52   {
53     IPC::Message msg;
54     msg.WriteString("#inva://idurl/");
55     GURL output;
56     PickleIterator iter(msg);
57     EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
58   }
59 
60   // Also test the corrupt case.
61   IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
62   msg.WriteInt(99);
63   GURL output;
64   PickleIterator iter(msg);
65   EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output));
66 }
67 
68 // Tests std::pair serialization
TEST(IPCMessageTest,Pair)69 TEST(IPCMessageTest, Pair) {
70   typedef std::pair<std::string, std::string> TestPair;
71 
72   TestPair input("foo", "bar");
73   IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
74   IPC::ParamTraits<TestPair>::Write(&msg, input);
75 
76   TestPair output;
77   PickleIterator iter(msg);
78   EXPECT_TRUE(IPC::ParamTraits<TestPair>::Read(&msg, &iter, &output));
79   EXPECT_EQ(output.first, "foo");
80   EXPECT_EQ(output.second, "bar");
81 }
82 
83 // Tests bitmap serialization.
TEST(IPCMessageTest,Bitmap)84 TEST(IPCMessageTest, Bitmap) {
85   SkBitmap bitmap;
86 
87   bitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 5);
88   bitmap.allocPixels();
89   memset(bitmap.getPixels(), 'A', bitmap.getSize());
90 
91   IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
92   IPC::ParamTraits<SkBitmap>::Write(&msg, bitmap);
93 
94   SkBitmap output;
95   PickleIterator iter(msg);
96   EXPECT_TRUE(IPC::ParamTraits<SkBitmap>::Read(&msg, &iter, &output));
97 
98   EXPECT_EQ(bitmap.config(), output.config());
99   EXPECT_EQ(bitmap.width(), output.width());
100   EXPECT_EQ(bitmap.height(), output.height());
101   EXPECT_EQ(bitmap.rowBytes(), output.rowBytes());
102   EXPECT_EQ(bitmap.getSize(), output.getSize());
103   EXPECT_EQ(memcmp(bitmap.getPixels(), output.getPixels(), bitmap.getSize()),
104             0);
105 
106   // Also test the corrupt case.
107   IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
108   // Copy the first message block over to |bad_msg|.
109   const char* fixed_data;
110   int fixed_data_size;
111   iter = PickleIterator(msg);
112   EXPECT_TRUE(msg.ReadData(&iter, &fixed_data, &fixed_data_size));
113   bad_msg.WriteData(fixed_data, fixed_data_size);
114   // Add some bogus pixel data.
115   const size_t bogus_pixels_size = bitmap.getSize() * 2;
116   scoped_ptr<char[]> bogus_pixels(new char[bogus_pixels_size]);
117   memset(bogus_pixels.get(), 'B', bogus_pixels_size);
118   bad_msg.WriteData(bogus_pixels.get(), bogus_pixels_size);
119   // Make sure we don't read out the bitmap!
120   SkBitmap bad_output;
121   iter = PickleIterator(bad_msg);
122   EXPECT_FALSE(IPC::ParamTraits<SkBitmap>::Read(&bad_msg, &iter, &bad_output));
123 }
124 
TEST(IPCMessageTest,ListValue)125 TEST(IPCMessageTest, ListValue) {
126   base::ListValue input;
127   input.Set(0, new base::FundamentalValue(42.42));
128   input.Set(1, new base::StringValue("forty"));
129   input.Set(2, base::Value::CreateNullValue());
130 
131   IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
132   IPC::WriteParam(&msg, input);
133 
134   base::ListValue output;
135   PickleIterator iter(msg);
136   EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
137 
138   EXPECT_TRUE(input.Equals(&output));
139 
140   // Also test the corrupt case.
141   IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
142   bad_msg.WriteInt(99);
143   iter = PickleIterator(bad_msg);
144   EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
145 }
146 
TEST(IPCMessageTest,DictionaryValue)147 TEST(IPCMessageTest, DictionaryValue) {
148   base::DictionaryValue input;
149   input.Set("null", base::Value::CreateNullValue());
150   input.Set("bool", new base::FundamentalValue(true));
151   input.Set("int", new base::FundamentalValue(42));
152 
153   scoped_ptr<base::DictionaryValue> subdict(new base::DictionaryValue());
154   subdict->Set("str", new base::StringValue("forty two"));
155   subdict->Set("bool", new base::FundamentalValue(false));
156 
157   scoped_ptr<base::ListValue> sublist(new base::ListValue());
158   sublist->Set(0, new base::FundamentalValue(42.42));
159   sublist->Set(1, new base::StringValue("forty"));
160   sublist->Set(2, new base::StringValue("two"));
161   subdict->Set("list", sublist.release());
162 
163   input.Set("dict", subdict.release());
164 
165   IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
166   IPC::WriteParam(&msg, input);
167 
168   base::DictionaryValue output;
169   PickleIterator iter(msg);
170   EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
171 
172   EXPECT_TRUE(input.Equals(&output));
173 
174   // Also test the corrupt case.
175   IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
176   bad_msg.WriteInt(99);
177   iter = PickleIterator(bad_msg);
178   EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
179 }
180 
181 // Tests net::HostPortPair serialization
TEST(IPCMessageTest,HostPortPair)182 TEST(IPCMessageTest, HostPortPair) {
183   net::HostPortPair input("host.com", 12345);
184 
185   IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
186   IPC::ParamTraits<net::HostPortPair>::Write(&msg, input);
187 
188   net::HostPortPair output;
189   PickleIterator iter(msg);
190   EXPECT_TRUE(IPC::ParamTraits<net::HostPortPair>::Read(&msg, &iter, &output));
191   EXPECT_EQ(input.host(), output.host());
192   EXPECT_EQ(input.port(), output.port());
193 }
194