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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 "http://www.google.com/", | 26 "http://www.google.com/", |
27 "http://user:pass@host.com:888/foo;bar?baz#nop", | 27 "http://user:pass@host.com:888/foo;bar?baz#nop", |
28 }; | 28 }; |
29 | 29 |
30 for (size_t i = 0; i < arraysize(serialize_cases); i++) { | 30 for (size_t i = 0; i < arraysize(serialize_cases); i++) { |
31 GURL input(serialize_cases[i]); | 31 GURL input(serialize_cases[i]); |
32 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 32 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
33 IPC::ParamTraits<GURL>::Write(&msg, input); | 33 IPC::ParamTraits<GURL>::Write(&msg, input); |
34 | 34 |
35 GURL output; | 35 GURL output; |
36 PickleIterator iter(msg); | 36 base::PickleIterator iter(msg); |
37 EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); | 37 EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); |
38 | 38 |
39 // We want to test each component individually to make sure its range was | 39 // We want to test each component individually to make sure its range was |
40 // correctly serialized and deserialized, not just the spec. | 40 // correctly serialized and deserialized, not just the spec. |
41 EXPECT_EQ(input.possibly_invalid_spec(), output.possibly_invalid_spec()); | 41 EXPECT_EQ(input.possibly_invalid_spec(), output.possibly_invalid_spec()); |
42 EXPECT_EQ(input.is_valid(), output.is_valid()); | 42 EXPECT_EQ(input.is_valid(), output.is_valid()); |
43 EXPECT_EQ(input.scheme(), output.scheme()); | 43 EXPECT_EQ(input.scheme(), output.scheme()); |
44 EXPECT_EQ(input.username(), output.username()); | 44 EXPECT_EQ(input.username(), output.username()); |
45 EXPECT_EQ(input.password(), output.password()); | 45 EXPECT_EQ(input.password(), output.password()); |
46 EXPECT_EQ(input.host(), output.host()); | 46 EXPECT_EQ(input.host(), output.host()); |
47 EXPECT_EQ(input.port(), output.port()); | 47 EXPECT_EQ(input.port(), output.port()); |
48 EXPECT_EQ(input.path(), output.path()); | 48 EXPECT_EQ(input.path(), output.path()); |
49 EXPECT_EQ(input.query(), output.query()); | 49 EXPECT_EQ(input.query(), output.query()); |
50 EXPECT_EQ(input.ref(), output.ref()); | 50 EXPECT_EQ(input.ref(), output.ref()); |
51 } | 51 } |
52 | 52 |
53 // Test an excessively long GURL. | 53 // Test an excessively long GURL. |
54 { | 54 { |
55 const std::string url = std::string("http://example.org/").append( | 55 const std::string url = std::string("http://example.org/").append( |
56 content::GetMaxURLChars() + 1, 'a'); | 56 content::GetMaxURLChars() + 1, 'a'); |
57 GURL input(url.c_str()); | 57 GURL input(url.c_str()); |
58 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 58 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
59 IPC::ParamTraits<GURL>::Write(&msg, input); | 59 IPC::ParamTraits<GURL>::Write(&msg, input); |
60 | 60 |
61 GURL output; | 61 GURL output; |
62 PickleIterator iter(msg); | 62 base::PickleIterator iter(msg); |
63 EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); | 63 EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); |
64 EXPECT_TRUE(output.is_empty()); | 64 EXPECT_TRUE(output.is_empty()); |
65 } | 65 } |
66 | 66 |
67 // Test an invalid GURL. | 67 // Test an invalid GURL. |
68 { | 68 { |
69 IPC::Message msg; | 69 IPC::Message msg; |
70 msg.WriteString("#inva://idurl/"); | 70 msg.WriteString("#inva://idurl/"); |
71 GURL output; | 71 GURL output; |
72 PickleIterator iter(msg); | 72 base::PickleIterator iter(msg); |
73 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); | 73 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); |
74 } | 74 } |
75 | 75 |
76 // Also test the corrupt case. | 76 // Also test the corrupt case. |
77 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 77 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
78 msg.WriteInt(99); | 78 msg.WriteInt(99); |
79 GURL output; | 79 GURL output; |
80 PickleIterator iter(msg); | 80 base::PickleIterator iter(msg); |
81 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); | 81 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); |
82 } | 82 } |
83 | 83 |
84 // Tests std::pair serialization | 84 // Tests std::pair serialization |
85 TEST(IPCMessageTest, Pair) { | 85 TEST(IPCMessageTest, Pair) { |
86 typedef std::pair<std::string, std::string> TestPair; | 86 typedef std::pair<std::string, std::string> TestPair; |
87 | 87 |
88 TestPair input("foo", "bar"); | 88 TestPair input("foo", "bar"); |
89 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 89 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
90 IPC::ParamTraits<TestPair>::Write(&msg, input); | 90 IPC::ParamTraits<TestPair>::Write(&msg, input); |
91 | 91 |
92 TestPair output; | 92 TestPair output; |
93 PickleIterator iter(msg); | 93 base::PickleIterator iter(msg); |
94 EXPECT_TRUE(IPC::ParamTraits<TestPair>::Read(&msg, &iter, &output)); | 94 EXPECT_TRUE(IPC::ParamTraits<TestPair>::Read(&msg, &iter, &output)); |
95 EXPECT_EQ(output.first, "foo"); | 95 EXPECT_EQ(output.first, "foo"); |
96 EXPECT_EQ(output.second, "bar"); | 96 EXPECT_EQ(output.second, "bar"); |
97 } | 97 } |
98 | 98 |
99 // Tests bitmap serialization. | 99 // Tests bitmap serialization. |
100 TEST(IPCMessageTest, Bitmap) { | 100 TEST(IPCMessageTest, Bitmap) { |
101 SkBitmap bitmap; | 101 SkBitmap bitmap; |
102 | 102 |
103 bitmap.allocN32Pixels(10, 5); | 103 bitmap.allocN32Pixels(10, 5); |
104 memset(bitmap.getPixels(), 'A', bitmap.getSize()); | 104 memset(bitmap.getPixels(), 'A', bitmap.getSize()); |
105 | 105 |
106 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 106 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
107 IPC::ParamTraits<SkBitmap>::Write(&msg, bitmap); | 107 IPC::ParamTraits<SkBitmap>::Write(&msg, bitmap); |
108 | 108 |
109 SkBitmap output; | 109 SkBitmap output; |
110 PickleIterator iter(msg); | 110 base::PickleIterator iter(msg); |
111 EXPECT_TRUE(IPC::ParamTraits<SkBitmap>::Read(&msg, &iter, &output)); | 111 EXPECT_TRUE(IPC::ParamTraits<SkBitmap>::Read(&msg, &iter, &output)); |
112 | 112 |
113 EXPECT_EQ(bitmap.colorType(), output.colorType()); | 113 EXPECT_EQ(bitmap.colorType(), output.colorType()); |
114 EXPECT_EQ(bitmap.width(), output.width()); | 114 EXPECT_EQ(bitmap.width(), output.width()); |
115 EXPECT_EQ(bitmap.height(), output.height()); | 115 EXPECT_EQ(bitmap.height(), output.height()); |
116 EXPECT_EQ(bitmap.rowBytes(), output.rowBytes()); | 116 EXPECT_EQ(bitmap.rowBytes(), output.rowBytes()); |
117 EXPECT_EQ(bitmap.getSize(), output.getSize()); | 117 EXPECT_EQ(bitmap.getSize(), output.getSize()); |
118 EXPECT_EQ(memcmp(bitmap.getPixels(), output.getPixels(), bitmap.getSize()), | 118 EXPECT_EQ(memcmp(bitmap.getPixels(), output.getPixels(), bitmap.getSize()), |
119 0); | 119 0); |
120 | 120 |
121 // Also test the corrupt case. | 121 // Also test the corrupt case. |
122 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 122 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
123 // Copy the first message block over to |bad_msg|. | 123 // Copy the first message block over to |bad_msg|. |
124 const char* fixed_data; | 124 const char* fixed_data; |
125 int fixed_data_size; | 125 int fixed_data_size; |
126 iter = PickleIterator(msg); | 126 iter = base::PickleIterator(msg); |
127 EXPECT_TRUE(iter.ReadData(&fixed_data, &fixed_data_size)); | 127 EXPECT_TRUE(iter.ReadData(&fixed_data, &fixed_data_size)); |
128 bad_msg.WriteData(fixed_data, fixed_data_size); | 128 bad_msg.WriteData(fixed_data, fixed_data_size); |
129 // Add some bogus pixel data. | 129 // Add some bogus pixel data. |
130 const size_t bogus_pixels_size = bitmap.getSize() * 2; | 130 const size_t bogus_pixels_size = bitmap.getSize() * 2; |
131 scoped_ptr<char[]> bogus_pixels(new char[bogus_pixels_size]); | 131 scoped_ptr<char[]> bogus_pixels(new char[bogus_pixels_size]); |
132 memset(bogus_pixels.get(), 'B', bogus_pixels_size); | 132 memset(bogus_pixels.get(), 'B', bogus_pixels_size); |
133 bad_msg.WriteData(bogus_pixels.get(), bogus_pixels_size); | 133 bad_msg.WriteData(bogus_pixels.get(), bogus_pixels_size); |
134 // Make sure we don't read out the bitmap! | 134 // Make sure we don't read out the bitmap! |
135 SkBitmap bad_output; | 135 SkBitmap bad_output; |
136 iter = PickleIterator(bad_msg); | 136 iter = base::PickleIterator(bad_msg); |
137 EXPECT_FALSE(IPC::ParamTraits<SkBitmap>::Read(&bad_msg, &iter, &bad_output)); | 137 EXPECT_FALSE(IPC::ParamTraits<SkBitmap>::Read(&bad_msg, &iter, &bad_output)); |
138 } | 138 } |
139 | 139 |
140 TEST(IPCMessageTest, ListValue) { | 140 TEST(IPCMessageTest, ListValue) { |
141 base::ListValue input; | 141 base::ListValue input; |
142 input.Set(0, new base::FundamentalValue(42.42)); | 142 input.Set(0, new base::FundamentalValue(42.42)); |
143 input.Set(1, new base::StringValue("forty")); | 143 input.Set(1, new base::StringValue("forty")); |
144 input.Set(2, base::Value::CreateNullValue()); | 144 input.Set(2, base::Value::CreateNullValue()); |
145 | 145 |
146 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 146 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
147 IPC::WriteParam(&msg, input); | 147 IPC::WriteParam(&msg, input); |
148 | 148 |
149 base::ListValue output; | 149 base::ListValue output; |
150 PickleIterator iter(msg); | 150 base::PickleIterator iter(msg); |
151 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); | 151 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
152 | 152 |
153 EXPECT_TRUE(input.Equals(&output)); | 153 EXPECT_TRUE(input.Equals(&output)); |
154 | 154 |
155 // Also test the corrupt case. | 155 // Also test the corrupt case. |
156 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 156 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
157 bad_msg.WriteInt(99); | 157 bad_msg.WriteInt(99); |
158 iter = PickleIterator(bad_msg); | 158 iter = base::PickleIterator(bad_msg); |
159 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); | 159 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
160 } | 160 } |
161 | 161 |
162 TEST(IPCMessageTest, DictionaryValue) { | 162 TEST(IPCMessageTest, DictionaryValue) { |
163 base::DictionaryValue input; | 163 base::DictionaryValue input; |
164 input.Set("null", base::Value::CreateNullValue()); | 164 input.Set("null", base::Value::CreateNullValue()); |
165 input.Set("bool", new base::FundamentalValue(true)); | 165 input.Set("bool", new base::FundamentalValue(true)); |
166 input.Set("int", new base::FundamentalValue(42)); | 166 input.Set("int", new base::FundamentalValue(42)); |
167 | 167 |
168 scoped_ptr<base::DictionaryValue> subdict(new base::DictionaryValue()); | 168 scoped_ptr<base::DictionaryValue> subdict(new base::DictionaryValue()); |
169 subdict->Set("str", new base::StringValue("forty two")); | 169 subdict->Set("str", new base::StringValue("forty two")); |
170 subdict->Set("bool", new base::FundamentalValue(false)); | 170 subdict->Set("bool", new base::FundamentalValue(false)); |
171 | 171 |
172 scoped_ptr<base::ListValue> sublist(new base::ListValue()); | 172 scoped_ptr<base::ListValue> sublist(new base::ListValue()); |
173 sublist->Set(0, new base::FundamentalValue(42.42)); | 173 sublist->Set(0, new base::FundamentalValue(42.42)); |
174 sublist->Set(1, new base::StringValue("forty")); | 174 sublist->Set(1, new base::StringValue("forty")); |
175 sublist->Set(2, new base::StringValue("two")); | 175 sublist->Set(2, new base::StringValue("two")); |
176 subdict->Set("list", sublist.release()); | 176 subdict->Set("list", sublist.release()); |
177 | 177 |
178 input.Set("dict", subdict.release()); | 178 input.Set("dict", subdict.release()); |
179 | 179 |
180 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 180 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
181 IPC::WriteParam(&msg, input); | 181 IPC::WriteParam(&msg, input); |
182 | 182 |
183 base::DictionaryValue output; | 183 base::DictionaryValue output; |
184 PickleIterator iter(msg); | 184 base::PickleIterator iter(msg); |
185 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); | 185 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
186 | 186 |
187 EXPECT_TRUE(input.Equals(&output)); | 187 EXPECT_TRUE(input.Equals(&output)); |
188 | 188 |
189 // Also test the corrupt case. | 189 // Also test the corrupt case. |
190 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 190 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
191 bad_msg.WriteInt(99); | 191 bad_msg.WriteInt(99); |
192 iter = PickleIterator(bad_msg); | 192 iter = base::PickleIterator(bad_msg); |
193 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); | 193 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
194 } | 194 } |
195 | 195 |
196 // Tests net::HostPortPair serialization | 196 // Tests net::HostPortPair serialization |
197 TEST(IPCMessageTest, HostPortPair) { | 197 TEST(IPCMessageTest, HostPortPair) { |
198 net::HostPortPair input("host.com", 12345); | 198 net::HostPortPair input("host.com", 12345); |
199 | 199 |
200 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 200 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
201 IPC::ParamTraits<net::HostPortPair>::Write(&msg, input); | 201 IPC::ParamTraits<net::HostPortPair>::Write(&msg, input); |
202 | 202 |
203 net::HostPortPair output; | 203 net::HostPortPair output; |
204 PickleIterator iter(msg); | 204 base::PickleIterator iter(msg); |
205 EXPECT_TRUE(IPC::ParamTraits<net::HostPortPair>::Read(&msg, &iter, &output)); | 205 EXPECT_TRUE(IPC::ParamTraits<net::HostPortPair>::Read(&msg, &iter, &output)); |
206 EXPECT_EQ(input.host(), output.host()); | 206 EXPECT_EQ(input.host(), output.host()); |
207 EXPECT_EQ(input.port(), output.port()); | 207 EXPECT_EQ(input.port(), output.port()); |
208 } | 208 } |
OLD | NEW |