OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 "net/spdy/spdy_alt_svc_wire_format.h" | 5 #include "net/spdy/spdy_alt_svc_wire_format.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/platform_test.h" | 9 #include "testing/platform_test.h" |
10 | 10 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 if (i & 1 << 10) { | 110 if (i & 1 << 10) { |
111 header_field_value.append(" "); | 111 header_field_value.append(" "); |
112 } | 112 } |
113 if (i & 1 << 11) { | 113 if (i & 1 << 11) { |
114 header_field_value.append(","); | 114 header_field_value.append(","); |
115 } | 115 } |
116 if (i & 1 << 12) { | 116 if (i & 1 << 12) { |
117 header_field_value.append(" "); | 117 header_field_value.append(" "); |
118 } | 118 } |
119 | 119 |
120 std::string protocol_id; | 120 SpdyAltSvcWireFormat::AlternativeService altsvc; |
121 std::string host; | 121 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue(header_field_value, |
122 uint16 port = 0; | 122 &altsvc)); |
123 uint32 max_age = 0; | 123 EXPECT_EQ("a=b%c", altsvc.protocol_id); |
124 double p = 0.0; | 124 EXPECT_EQ(expected_host, altsvc.host); |
125 | 125 EXPECT_EQ(42, altsvc.port); |
126 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 126 EXPECT_EQ(expected_max_age, altsvc.max_age); |
127 header_field_value, &protocol_id, &host, &port, &max_age, &p)); | 127 EXPECT_DOUBLE_EQ(expected_p, altsvc.p); |
128 EXPECT_EQ("a=b%c", protocol_id); | |
129 EXPECT_EQ(expected_host, host); | |
130 EXPECT_EQ(42, port); | |
131 EXPECT_EQ(expected_max_age, max_age); | |
132 EXPECT_DOUBLE_EQ(expected_p, p); | |
133 } | 128 } |
134 } | 129 } |
135 | 130 |
136 // Test SerializeHeaderFieldValue() with and without hostname and each | 131 // Test SerializeHeaderFieldValue() with and without hostname and each |
137 // parameter. | 132 // parameter. |
138 TEST(SpdyAltSvcWireFormatTest, SerializeHeaderFieldValue) { | 133 TEST(SpdyAltSvcWireFormatTest, SerializeHeaderFieldValue) { |
139 for (int i = 0; i < 1 << 3; ++i) { | 134 for (int i = 0; i < 1 << 3; ++i) { |
| 135 SpdyAltSvcWireFormat::AlternativeService altsvc; |
| 136 altsvc.protocol_id = "a=b%c"; |
| 137 altsvc.port = 42; |
140 std::string expected_header_field_value = "a%3Db%25c=\""; | 138 std::string expected_header_field_value = "a%3Db%25c=\""; |
141 std::string host; | |
142 if (i & 1 << 0) { | 139 if (i & 1 << 0) { |
143 host = "foo\"bar\\baz"; | 140 altsvc.host = "foo\"bar\\baz"; |
144 expected_header_field_value.append("foo\\\"bar\\\\baz"); | 141 expected_header_field_value.append("foo\\\"bar\\\\baz"); |
145 } | 142 } |
146 expected_header_field_value.append(":42\""); | 143 expected_header_field_value.append(":42\""); |
147 int max_age = 86400; | 144 altsvc.max_age = 86400; |
148 if (i & 1 << 1) { | 145 if (i & 1 << 1) { |
149 max_age = 1111; | 146 altsvc.max_age = 1111; |
150 expected_header_field_value.append("; ma=1111"); | 147 expected_header_field_value.append("; ma=1111"); |
151 } | 148 } |
152 double p = 1.0; | 149 altsvc.p = 1.0; |
153 if (i & 1 << 2) { | 150 if (i & 1 << 2) { |
154 p = 0.33; | 151 altsvc.p = 0.33; |
155 expected_header_field_value.append("; p=0.33"); | 152 expected_header_field_value.append("; p=0.33"); |
156 } | 153 } |
157 EXPECT_EQ(expected_header_field_value, | 154 EXPECT_EQ(expected_header_field_value, |
158 SpdyAltSvcWireFormat::SerializeHeaderFieldValue("a=b%c", host, 42, | 155 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc)); |
159 max_age, p)); | |
160 } | 156 } |
161 } | 157 } |
162 | 158 |
163 // ParseHeaderFieldValue() should return false on malformed field values: | 159 // ParseHeaderFieldValue() should return false on malformed field values: |
164 // invalid percent encoding, unmatched quotation mark, empty port, non-numeric | 160 // invalid percent encoding, unmatched quotation mark, empty port, non-numeric |
165 // characters in numeric fields, negative or larger than 1.0 probability. | 161 // characters in numeric fields, negative or larger than 1.0 probability. |
166 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValueInvalid) { | 162 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValueInvalid) { |
167 std::string protocol_id; | 163 SpdyAltSvcWireFormat::AlternativeService altsvc; |
168 std::string host; | |
169 uint16 port; | |
170 uint32 max_age; | |
171 double p; | |
172 const char* invalid_field_value_array[] = {"", "a%", "a%x", "a%b", "a%9z", | 164 const char* invalid_field_value_array[] = {"", "a%", "a%x", "a%b", "a%9z", |
173 "a=", "a=\"", "a=\"b\"", "a=\":\"", "a=\"c:\"", "a=\"c:foo\"", | 165 "a=", "a=\"", "a=\"b\"", "a=\":\"", "a=\"c:\"", "a=\"c:foo\"", |
174 "a=\"c:42foo\"", "a=\"b:42\"bar", "a=\"b:42\" ; m", | 166 "a=\"c:42foo\"", "a=\"b:42\"bar", "a=\"b:42\" ; m", |
175 "a=\"b:42\" ; min-age", "a=\"b:42\" ; ma", "a=\"b:42\" ; ma=", | 167 "a=\"b:42\" ; min-age", "a=\"b:42\" ; ma", "a=\"b:42\" ; ma=", |
176 "a=\"b:42\" ; ma=ma", "a=\"b:42\" ; ma=123bar", "a=\"b:42\" ; p=-2", | 168 "a=\"b:42\" ; ma=ma", "a=\"b:42\" ; ma=123bar", "a=\"b:42\" ; p=-2", |
177 "a=\"b:42\" ; p=..", "a=\"b:42\" ; p=1.05"}; | 169 "a=\"b:42\" ; p=..", "a=\"b:42\" ; p=1.05"}; |
178 for (const char* invalid_field_value : invalid_field_value_array) { | 170 for (const char* invalid_field_value : invalid_field_value_array) { |
179 EXPECT_FALSE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 171 EXPECT_FALSE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
180 invalid_field_value, &protocol_id, &host, &port, &max_age, &p)) | 172 invalid_field_value, &altsvc)) |
181 << invalid_field_value; | 173 << invalid_field_value; |
182 } | 174 } |
183 } | 175 } |
184 | 176 |
185 // ParseHeaderFieldValue() should return false on a field values truncated | 177 // ParseHeaderFieldValue() should return false on a field values truncated |
186 // before closing quotation mark, without trying to access memory beyond the end | 178 // before closing quotation mark, without trying to access memory beyond the end |
187 // of the input. | 179 // of the input. |
188 TEST(SpdyAltSvcWireFormatTest, ParseTruncatedHeaderFieldValue) { | 180 TEST(SpdyAltSvcWireFormatTest, ParseTruncatedHeaderFieldValue) { |
189 std::string protocol_id; | 181 SpdyAltSvcWireFormat::AlternativeService altsvc; |
190 std::string host; | |
191 uint16 port; | |
192 uint32 max_age; | |
193 double p; | |
194 const char* field_value_array[] = { | 182 const char* field_value_array[] = { |
195 "p=\":137\"", "p=\"foo:137\"", "p%25=\"foo\\\"bar\\\\baz:137\""}; | 183 "p=\":137\"", "p=\"foo:137\"", "p%25=\"foo\\\"bar\\\\baz:137\""}; |
196 for (std::string field_value : field_value_array) { | 184 for (std::string field_value : field_value_array) { |
197 for (size_t len = 1; len < field_value.size(); ++len) { | 185 for (size_t len = 1; len < field_value.size(); ++len) { |
198 EXPECT_FALSE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 186 EXPECT_FALSE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
199 field_value.substr(0, len), &protocol_id, &host, &port, &max_age, &p)) | 187 field_value.substr(0, len), &altsvc)) |
200 << len; | 188 << len; |
201 } | 189 } |
202 } | 190 } |
203 } | 191 } |
204 | 192 |
205 // Tests of private methods. | 193 // Tests of private methods. |
206 | 194 |
207 // Test SkipWhiteSpace(). | 195 // Test SkipWhiteSpace(). |
208 TEST(SpdyAltSvcWireFormatTest, SkipWhiteSpace) { | 196 TEST(SpdyAltSvcWireFormatTest, SkipWhiteSpace) { |
209 StringPiece input("a \tb "); | 197 StringPiece input("a \tb "); |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 StringPiece input(invalid_input); | 399 StringPiece input(invalid_input); |
412 double probability; | 400 double probability; |
413 EXPECT_FALSE(test::SpdyAltSvcWireFormatPeer::ParseProbability( | 401 EXPECT_FALSE(test::SpdyAltSvcWireFormatPeer::ParseProbability( |
414 input.begin(), input.end(), &probability)); | 402 input.begin(), input.end(), &probability)); |
415 } | 403 } |
416 } | 404 } |
417 | 405 |
418 } // namespace | 406 } // namespace |
419 | 407 |
420 } // namespace net | 408 } // namespace net |
OLD | NEW |