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 <algorithm> | 5 #include <algorithm> |
6 #include <iostream> | 6 #include <iostream> |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 LOG(ERROR) << "Expected header named '" << it->first | 561 LOG(ERROR) << "Expected header named '" << it->first |
562 << "' to have a value of '" << it->second | 562 << "' to have a value of '" << it->second |
563 << "'. The actual value received was '" << it2->second | 563 << "'. The actual value received was '" << it2->second |
564 << "'."; | 564 << "'."; |
565 return false; | 565 return false; |
566 } | 566 } |
567 } | 567 } |
568 return true; | 568 return true; |
569 } | 569 } |
570 | 570 |
571 void AddSpdySettingFromWireFormat(SettingsMap* settings, | |
572 uint32 key, | |
573 uint32 value) { | |
574 SettingsFlagsAndId flags_and_id = | |
575 SettingsFlagsAndId::FromWireFormat(spdy_version_, key); | |
576 SpdySettingsIds id = static_cast<SpdySettingsIds>(flags_and_id.id()); | |
577 SpdySettingsFlags flags = | |
578 static_cast<SpdySettingsFlags>(flags_and_id.flags()); | |
579 CHECK(settings->find(id) == settings->end()); | |
580 settings->insert(std::make_pair(id, SettingsFlagsAndValue(flags, value))); | |
581 } | |
582 | |
583 bool IsSpdy2() { return spdy_version_ == SPDY2; } | 571 bool IsSpdy2() { return spdy_version_ == SPDY2; } |
584 bool IsSpdy3() { return spdy_version_ == SPDY3; } | 572 bool IsSpdy3() { return spdy_version_ == SPDY3; } |
585 bool IsSpdy4() { return spdy_version_ == SPDY4; } | 573 bool IsSpdy4() { return spdy_version_ == SPDY4; } |
586 | 574 |
587 // Version of SPDY protocol to be used. | 575 // Version of SPDY protocol to be used. |
588 SpdyMajorVersion spdy_version_; | 576 SpdyMajorVersion spdy_version_; |
589 unsigned char spdy_version_ch_; | 577 unsigned char spdy_version_ch_; |
590 }; | 578 }; |
591 | 579 |
592 // All tests are run with 3 different SPDY versions: SPDY/2, SPDY/3, SPDY/4. | 580 // All tests are run with 3 different SPDY versions: SPDY/2, SPDY/3, SPDY/4. |
(...skipping 1616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2209 TEST_P(SpdyFramerTest, CreateSettings) { | 2197 TEST_P(SpdyFramerTest, CreateSettings) { |
2210 SpdyFramer framer(spdy_version_); | 2198 SpdyFramer framer(spdy_version_); |
2211 | 2199 |
2212 { | 2200 { |
2213 const char kDescription[] = "Network byte order SETTINGS frame"; | 2201 const char kDescription[] = "Network byte order SETTINGS frame"; |
2214 | 2202 |
2215 const unsigned char kV2FrameData[] = { | 2203 const unsigned char kV2FrameData[] = { |
2216 0x80, spdy_version_ch_, 0x00, 0x04, | 2204 0x80, spdy_version_ch_, 0x00, 0x04, |
2217 0x00, 0x00, 0x00, 0x0c, | 2205 0x00, 0x00, 0x00, 0x0c, |
2218 0x00, 0x00, 0x00, 0x01, | 2206 0x00, 0x00, 0x00, 0x01, |
2219 0x04, 0x03, 0x02, 0x01, | 2207 0x07, 0x00, 0x00, 0x01, |
2220 0x0a, 0x0b, 0x0c, 0x0d, | 2208 0x0a, 0x0b, 0x0c, 0x0d, |
2221 }; | 2209 }; |
2222 const unsigned char kV3FrameData[] = { | 2210 const unsigned char kV3FrameData[] = { |
2223 0x80, spdy_version_ch_, 0x00, 0x04, | 2211 0x80, spdy_version_ch_, 0x00, 0x04, |
2224 0x00, 0x00, 0x00, 0x0c, | 2212 0x00, 0x00, 0x00, 0x0c, |
2225 0x00, 0x00, 0x00, 0x01, | 2213 0x00, 0x00, 0x00, 0x01, |
2226 0x01, 0x02, 0x03, 0x04, | 2214 0x01, 0x00, 0x00, 0x07, |
2227 0x0a, 0x0b, 0x0c, 0x0d, | 2215 0x0a, 0x0b, 0x0c, 0x0d, |
2228 }; | 2216 }; |
2229 const unsigned char kV4FrameData[] = { | 2217 const unsigned char kV4FrameData[] = { |
2230 0x00, 0x05, 0x04, 0x00, | 2218 0x00, 0x05, 0x04, 0x00, |
2231 0x00, 0x00, 0x00, 0x00, | 2219 0x00, 0x00, 0x00, 0x00, |
2232 0x01, 0x0a, 0x0b, 0x0c, | 2220 0x04, 0x0a, 0x0b, 0x0c, |
2233 0x0d, | 2221 0x0d, |
2234 }; | 2222 }; |
2235 | 2223 |
2236 uint32 kValue = 0x0a0b0c0d; | 2224 uint32 kValue = 0x0a0b0c0d; |
2237 SpdySettingsIR settings_ir; | 2225 SpdySettingsIR settings_ir; |
2238 | 2226 |
2239 SpdySettingsFlags kFlags = static_cast<SpdySettingsFlags>(0x01); | 2227 SpdySettingsFlags kFlags = static_cast<SpdySettingsFlags>(0x01); |
2240 SpdySettingsIds kId = static_cast<SpdySettingsIds>(0x020304); | 2228 SpdySettingsIds kId = SETTINGS_INITIAL_WINDOW_SIZE; |
2241 if (IsSpdy4()) { | |
2242 kId = static_cast<SpdySettingsIds>(0x01); | |
2243 } | |
2244 SettingsMap settings; | 2229 SettingsMap settings; |
2245 settings[kId] = SettingsFlagsAndValue(kFlags, kValue); | 2230 settings[kId] = SettingsFlagsAndValue(kFlags, kValue); |
2246 EXPECT_EQ(kFlags, settings[kId].first); | 2231 EXPECT_EQ(kFlags, settings[kId].first); |
2247 EXPECT_EQ(kValue, settings[kId].second); | 2232 EXPECT_EQ(kValue, settings[kId].second); |
2248 settings_ir.AddSetting(kId, | 2233 settings_ir.AddSetting(kId, |
2249 kFlags & SETTINGS_FLAG_PLEASE_PERSIST, | 2234 kFlags & SETTINGS_FLAG_PLEASE_PERSIST, |
2250 kFlags & SETTINGS_FLAG_PERSISTED, | 2235 kFlags & SETTINGS_FLAG_PERSISTED, |
2251 kValue); | 2236 kValue); |
2252 | 2237 |
2253 scoped_ptr<SpdyFrame> frame(framer.SerializeSettings(settings_ir)); | 2238 scoped_ptr<SpdyFrame> frame(framer.SerializeSettings(settings_ir)); |
2254 if (IsSpdy2()) { | 2239 if (IsSpdy2()) { |
2255 CompareFrame(kDescription, *frame, kV2FrameData, arraysize(kV2FrameData)); | 2240 CompareFrame(kDescription, *frame, kV2FrameData, arraysize(kV2FrameData)); |
2256 } else if (IsSpdy3()) { | 2241 } else if (IsSpdy3()) { |
2257 CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData)); | 2242 CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData)); |
2258 } else { | 2243 } else { |
2259 CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData)); | 2244 CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData)); |
2260 } | 2245 } |
2261 } | 2246 } |
2262 | 2247 |
2263 { | 2248 { |
2264 const char kDescription[] = "Basic SETTINGS frame"; | 2249 const char kDescription[] = "Basic SETTINGS frame"; |
2265 | 2250 |
2266 SettingsMap settings; | 2251 const unsigned char kV2FrameData[] = { |
2267 AddSpdySettingFromWireFormat( | |
2268 &settings, 0x00000000, 0x00000001); // 1st Setting | |
2269 AddSpdySettingFromWireFormat( | |
2270 &settings, 0x01000001, 0x00000002); // 2nd Setting | |
2271 AddSpdySettingFromWireFormat( | |
2272 &settings, 0x02000002, 0x00000003); // 3rd Setting | |
2273 AddSpdySettingFromWireFormat( | |
2274 &settings, 0x03000003, 0xff000004); // 4th Setting | |
2275 | |
2276 const unsigned char kV3FrameData[] = { // Also applies for V2. | |
2277 0x80, spdy_version_ch_, 0x00, 0x04, | 2252 0x80, spdy_version_ch_, 0x00, 0x04, |
2278 0x00, 0x00, 0x00, 0x24, | 2253 0x00, 0x00, 0x00, 0x24, |
2279 0x00, 0x00, 0x00, 0x04, | 2254 0x00, 0x00, 0x00, 0x04, |
2280 0x00, 0x00, 0x00, 0x00, // 1st Setting | 2255 0x01, 0x00, 0x00, 0x00, // 1st Setting |
2281 0x00, 0x00, 0x00, 0x01, | 2256 0x00, 0x00, 0x00, 0x05, |
2282 0x01, 0x00, 0x00, 0x01, // 2nd Setting | 2257 0x02, 0x00, 0x00, 0x00, // 2nd Setting |
2283 0x00, 0x00, 0x00, 0x02, | 2258 0x00, 0x00, 0x00, 0x06, |
2284 0x02, 0x00, 0x00, 0x02, // 3rd Setting | 2259 0x03, 0x00, 0x00, 0x00, // 3rd Setting |
2285 0x00, 0x00, 0x00, 0x03, | 2260 0x00, 0x00, 0x00, 0x07, |
2286 0x03, 0x00, 0x00, 0x03, // 4th Setting | 2261 0x04, 0x00, 0x00, 0x00, // 4th Setting |
2287 0xff, 0x00, 0x00, 0x04, | 2262 0x00, 0x00, 0x00, 0x08, |
2288 }; | 2263 }; |
| 2264 const unsigned char kV3FrameData[] = { |
| 2265 0x80, spdy_version_ch_, 0x00, 0x04, |
| 2266 0x00, 0x00, 0x00, 0x24, |
| 2267 0x00, 0x00, 0x00, 0x04, |
| 2268 0x00, 0x00, 0x00, 0x01, // 1st Setting |
| 2269 0x00, 0x00, 0x00, 0x05, |
| 2270 0x00, 0x00, 0x00, 0x02, // 2nd Setting |
| 2271 0x00, 0x00, 0x00, 0x06, |
| 2272 0x00, 0x00, 0x00, 0x03, // 3rd Setting |
| 2273 0x00, 0x00, 0x00, 0x07, |
| 2274 0x00, 0x00, 0x00, 0x04, // 4th Setting |
| 2275 0x00, 0x00, 0x00, 0x08, |
| 2276 }; |
| 2277 // These end up seemingly out of order because of the way that our internal |
| 2278 // ordering for settings_ir works. HTTP2 has no requirement on ordering on |
| 2279 // the wire. |
2289 const unsigned char kV4FrameData[] = { | 2280 const unsigned char kV4FrameData[] = { |
2290 0x00, 0x14, 0x04, 0x00, | 2281 0x00, 0x14, 0x04, 0x00, |
2291 0x00, 0x00, 0x00, 0x00, | 2282 0x00, 0x00, 0x00, 0x00, |
| 2283 0x03, // 3rd Setting |
| 2284 0x00, 0x00, 0x00, 0x07, |
| 2285 0x04, // 4th Setting |
| 2286 0x00, 0x00, 0x00, 0x08, |
2292 0x01, // 1st Setting | 2287 0x01, // 1st Setting |
2293 0x00, 0x00, 0x00, 0x01, | 2288 0x00, 0x00, 0x00, 0x05, |
2294 0x02, // 2nd Setting | 2289 0x02, // 2nd Setting |
2295 0x00, 0x00, 0x00, 0x02, | 2290 0x00, 0x00, 0x00, 0x06, |
2296 0x03, // 3rd Setting | |
2297 0x00, 0x00, 0x00, 0x03, | |
2298 0x04, // 4th Setting | |
2299 0xff, 0x00, 0x00, 0x04, | |
2300 }; | 2291 }; |
| 2292 |
2301 SpdySettingsIR settings_ir; | 2293 SpdySettingsIR settings_ir; |
2302 if (!IsSpdy4()) { | 2294 settings_ir.AddSetting(SpdyConstants::ParseSettingId(spdy_version_, 1), |
2303 for (SettingsMap::const_iterator it = settings.begin(); | 2295 false, // persist |
2304 it != settings.end(); | 2296 false, // persisted |
2305 ++it) { | 2297 5); |
2306 settings_ir.AddSetting(it->first, | 2298 settings_ir.AddSetting(SpdyConstants::ParseSettingId(spdy_version_, 2), |
2307 it->second.first & SETTINGS_FLAG_PLEASE_PERSIST, | 2299 false, // persist |
2308 it->second.first & SETTINGS_FLAG_PERSISTED, | 2300 false, // persisted |
2309 it->second.second); | 2301 6); |
2310 } | 2302 settings_ir.AddSetting(SpdyConstants::ParseSettingId(spdy_version_, 3), |
| 2303 false, // persist |
| 2304 false, // persisted |
| 2305 7); |
| 2306 settings_ir.AddSetting(SpdyConstants::ParseSettingId(spdy_version_, 4), |
| 2307 false, // persist |
| 2308 false, // persisted |
| 2309 8); |
| 2310 scoped_ptr<SpdyFrame> frame(framer.SerializeSettings(settings_ir)); |
| 2311 |
| 2312 if (IsSpdy2()) { |
| 2313 CompareFrame(kDescription, *frame, kV2FrameData, arraysize(kV2FrameData)); |
| 2314 } else if (IsSpdy3()) { |
| 2315 CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData)); |
2311 } else { | 2316 } else { |
2312 SpdySettingsIds kId = static_cast<SpdySettingsIds>(0x01); | |
2313 settings_ir.AddSetting(kId, 0, 0, 0x00000001); | |
2314 kId = static_cast<SpdySettingsIds>(0x02); | |
2315 settings_ir.AddSetting(kId, 0, 0, 0x00000002); | |
2316 kId = static_cast<SpdySettingsIds>(0x03); | |
2317 settings_ir.AddSetting(kId, 0, 0, 0x00000003); | |
2318 kId = static_cast<SpdySettingsIds>(0x04); | |
2319 settings_ir.AddSetting(kId, 0, 0, 0xff000004); | |
2320 } | |
2321 scoped_ptr<SpdyFrame> frame(framer.SerializeSettings(settings_ir)); | |
2322 if (IsSpdy4()) { | |
2323 CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData)); | 2317 CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData)); |
2324 } else { | |
2325 CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData)); | |
2326 } | 2318 } |
2327 } | 2319 } |
2328 | 2320 |
2329 { | 2321 { |
2330 const char kDescription[] = "Empty SETTINGS frame"; | 2322 const char kDescription[] = "Empty SETTINGS frame"; |
2331 | 2323 |
2332 const unsigned char kV3FrameData[] = { // Also applies for V2. | 2324 const unsigned char kV3FrameData[] = { // Also applies for V2. |
2333 0x80, spdy_version_ch_, 0x00, 0x04, | 2325 0x80, spdy_version_ch_, 0x00, 0x04, |
2334 0x00, 0x00, 0x00, 0x04, | 2326 0x00, 0x00, 0x00, 0x04, |
2335 0x00, 0x00, 0x00, 0x00, | 2327 0x00, 0x00, 0x00, 0x00, |
(...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3103 } | 3095 } |
3104 } | 3096 } |
3105 | 3097 |
3106 // Tests handling of SETTINGS frames with invalid length. | 3098 // Tests handling of SETTINGS frames with invalid length. |
3107 TEST_P(SpdyFramerTest, ReadBogusLenSettingsFrame) { | 3099 TEST_P(SpdyFramerTest, ReadBogusLenSettingsFrame) { |
3108 SpdyFramer framer(spdy_version_); | 3100 SpdyFramer framer(spdy_version_); |
3109 SpdySettingsIR settings_ir; | 3101 SpdySettingsIR settings_ir; |
3110 | 3102 |
3111 // Add a setting to pad the frame so that we don't get a buffer overflow when | 3103 // Add a setting to pad the frame so that we don't get a buffer overflow when |
3112 // calling SimulateInFramer() below. | 3104 // calling SimulateInFramer() below. |
3113 SettingsMap settings; | 3105 settings_ir.AddSetting(SETTINGS_INITIAL_WINDOW_SIZE, |
3114 settings[SETTINGS_UPLOAD_BANDWIDTH] = | 3106 false, |
3115 SettingsFlagsAndValue(SETTINGS_FLAG_PLEASE_PERSIST, 0x00000002); | |
3116 settings_ir.AddSetting(SETTINGS_UPLOAD_BANDWIDTH, | |
3117 true, // please persist | |
3118 false, | 3107 false, |
3119 0x00000002); | 3108 0x00000002); |
3120 scoped_ptr<SpdyFrame> control_frame(framer.SerializeSettings(settings_ir)); | 3109 scoped_ptr<SpdyFrame> control_frame(framer.SerializeSettings(settings_ir)); |
3121 const size_t kNewLength = 14; | 3110 const size_t kNewLength = 14; |
3122 SetFrameLength(control_frame.get(), kNewLength, spdy_version_); | 3111 SetFrameLength(control_frame.get(), kNewLength, spdy_version_); |
3123 TestSpdyVisitor visitor(spdy_version_); | 3112 TestSpdyVisitor visitor(spdy_version_); |
3124 visitor.use_compression_ = false; | 3113 visitor.use_compression_ = false; |
3125 visitor.SimulateInFramer( | 3114 visitor.SimulateInFramer( |
3126 reinterpret_cast<unsigned char*>(control_frame->data()), | 3115 reinterpret_cast<unsigned char*>(control_frame->data()), |
3127 framer.GetControlFrameHeaderSize() + kNewLength); | 3116 framer.GetControlFrameHeaderSize() + kNewLength); |
3128 // Should generate an error, since its not possible to have a | 3117 // Should generate an error, since its not possible to have a |
3129 // settings frame of length kNewLength. | 3118 // settings frame of length kNewLength. |
3130 EXPECT_EQ(1, visitor.error_count_); | 3119 EXPECT_EQ(1, visitor.error_count_); |
3131 } | 3120 } |
3132 | 3121 |
3133 // Tests handling of SETTINGS frames larger than the frame buffer size. | 3122 // Tests handling of SETTINGS frames larger than the frame buffer size. |
3134 TEST_P(SpdyFramerTest, ReadLargeSettingsFrame) { | 3123 TEST_P(SpdyFramerTest, ReadLargeSettingsFrame) { |
3135 SpdyFramer framer(spdy_version_); | 3124 SpdyFramer framer(spdy_version_); |
3136 SpdySettingsIR settings_ir; | 3125 SpdySettingsIR settings_ir; |
3137 SettingsMap settings; | 3126 settings_ir.AddSetting(SpdyConstants::ParseSettingId(spdy_version_, 1), |
3138 | 3127 false, // persist |
3139 SpdySettingsFlags flags = SETTINGS_FLAG_PLEASE_PERSIST; | 3128 false, // persisted |
3140 settings[SETTINGS_UPLOAD_BANDWIDTH] = | 3129 5); |
3141 SettingsFlagsAndValue(flags, 0x00000002); | 3130 settings_ir.AddSetting(SpdyConstants::ParseSettingId(spdy_version_, 2), |
3142 settings[SETTINGS_DOWNLOAD_BANDWIDTH] = | 3131 false, // persist |
3143 SettingsFlagsAndValue(flags, 0x00000003); | 3132 false, // persisted |
3144 settings[SETTINGS_ROUND_TRIP_TIME] = SettingsFlagsAndValue(flags, 0x00000004); | 3133 6); |
3145 for (SettingsMap::const_iterator it = settings.begin(); | 3134 settings_ir.AddSetting(SpdyConstants::ParseSettingId(spdy_version_, 3), |
3146 it != settings.end(); | 3135 false, // persist |
3147 ++it) { | 3136 false, // persisted |
3148 settings_ir.AddSetting(it->first, | 3137 7); |
3149 it->second.first & SETTINGS_FLAG_PLEASE_PERSIST, | |
3150 it->second.first & SETTINGS_FLAG_PERSISTED, | |
3151 it->second.second); | |
3152 } | |
3153 | 3138 |
3154 scoped_ptr<SpdyFrame> control_frame(framer.SerializeSettings(settings_ir)); | 3139 scoped_ptr<SpdyFrame> control_frame(framer.SerializeSettings(settings_ir)); |
3155 EXPECT_LT(SpdyFramer::kControlFrameBufferSize, | 3140 EXPECT_LT(SpdyFramer::kControlFrameBufferSize, |
3156 control_frame->size()); | 3141 control_frame->size()); |
3157 TestSpdyVisitor visitor(spdy_version_); | 3142 TestSpdyVisitor visitor(spdy_version_); |
3158 visitor.use_compression_ = false; | 3143 visitor.use_compression_ = false; |
3159 | 3144 |
3160 // Read all at once. | 3145 // Read all at once. |
3161 visitor.SimulateInFramer( | 3146 visitor.SimulateInFramer( |
3162 reinterpret_cast<unsigned char*>(control_frame->data()), | 3147 reinterpret_cast<unsigned char*>(control_frame->data()), |
(...skipping 1588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4751 SpdyBlockedIR blocked_ir(0); | 4736 SpdyBlockedIR blocked_ir(0); |
4752 scoped_ptr<SpdySerializedFrame> frame(framer.SerializeFrame(blocked_ir)); | 4737 scoped_ptr<SpdySerializedFrame> frame(framer.SerializeFrame(blocked_ir)); |
4753 framer.ProcessInput(frame->data(), framer.GetBlockedSize()); | 4738 framer.ProcessInput(frame->data(), framer.GetBlockedSize()); |
4754 | 4739 |
4755 EXPECT_EQ(SpdyFramer::SPDY_RESET, framer.state()); | 4740 EXPECT_EQ(SpdyFramer::SPDY_RESET, framer.state()); |
4756 EXPECT_EQ(SpdyFramer::SPDY_NO_ERROR, framer.error_code()) | 4741 EXPECT_EQ(SpdyFramer::SPDY_NO_ERROR, framer.error_code()) |
4757 << SpdyFramer::ErrorCodeToString(framer.error_code()); | 4742 << SpdyFramer::ErrorCodeToString(framer.error_code()); |
4758 } | 4743 } |
4759 | 4744 |
4760 } // namespace net | 4745 } // namespace net |
OLD | NEW |