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