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