| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/spdy/spdy_protocol.h" | |
| 6 | |
| 7 #include <iostream> | |
| 8 #include <limits> | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "net/spdy/spdy_bitmasks.h" | |
| 12 #include "net/spdy/spdy_framer.h" | |
| 13 #include "net/spdy/spdy_test_utils.h" | |
| 14 #include "net/test/gtest_util.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 std::ostream& operator<<(std::ostream& os, | |
| 20 const SpdyStreamPrecedence precedence) { | |
| 21 if (precedence.is_spdy3_priority()) { | |
| 22 os << "SpdyStreamPrecedence[spdy3_priority=" << precedence.spdy3_priority() | |
| 23 << "]"; | |
| 24 } else { | |
| 25 os << "SpdyStreamPrecedence[parent_id=" << precedence.parent_id() | |
| 26 << ", weight=" << precedence.weight() | |
| 27 << ", is_exclusive=" << precedence.is_exclusive() << "]"; | |
| 28 } | |
| 29 return os; | |
| 30 } | |
| 31 | |
| 32 namespace test { | |
| 33 | |
| 34 TEST(SpdyProtocolTest, ClampSpdy3Priority) { | |
| 35 EXPECT_SPDY_BUG(EXPECT_EQ(7, ClampSpdy3Priority(8)), "Invalid priority: 8"); | |
| 36 EXPECT_EQ(kV3LowestPriority, ClampSpdy3Priority(kV3LowestPriority)); | |
| 37 EXPECT_EQ(kV3HighestPriority, ClampSpdy3Priority(kV3HighestPriority)); | |
| 38 } | |
| 39 | |
| 40 TEST(SpdyProtocolTest, ClampHttp2Weight) { | |
| 41 EXPECT_SPDY_BUG(EXPECT_EQ(kHttp2MinStreamWeight, ClampHttp2Weight(0)), | |
| 42 "Invalid weight: 0"); | |
| 43 EXPECT_SPDY_BUG(EXPECT_EQ(kHttp2MaxStreamWeight, ClampHttp2Weight(300)), | |
| 44 "Invalid weight: 300"); | |
| 45 EXPECT_EQ(kHttp2MinStreamWeight, ClampHttp2Weight(kHttp2MinStreamWeight)); | |
| 46 EXPECT_EQ(kHttp2MaxStreamWeight, ClampHttp2Weight(kHttp2MaxStreamWeight)); | |
| 47 } | |
| 48 | |
| 49 TEST(SpdyProtocolTest, Spdy3PriorityToHttp2Weight) { | |
| 50 EXPECT_EQ(256, Spdy3PriorityToHttp2Weight(0)); | |
| 51 EXPECT_EQ(220, Spdy3PriorityToHttp2Weight(1)); | |
| 52 EXPECT_EQ(183, Spdy3PriorityToHttp2Weight(2)); | |
| 53 EXPECT_EQ(147, Spdy3PriorityToHttp2Weight(3)); | |
| 54 EXPECT_EQ(110, Spdy3PriorityToHttp2Weight(4)); | |
| 55 EXPECT_EQ(74, Spdy3PriorityToHttp2Weight(5)); | |
| 56 EXPECT_EQ(37, Spdy3PriorityToHttp2Weight(6)); | |
| 57 EXPECT_EQ(1, Spdy3PriorityToHttp2Weight(7)); | |
| 58 } | |
| 59 | |
| 60 TEST(SpdyProtocolTest, Http2WeightToSpdy3Priority) { | |
| 61 EXPECT_EQ(0u, Http2WeightToSpdy3Priority(256)); | |
| 62 EXPECT_EQ(0u, Http2WeightToSpdy3Priority(221)); | |
| 63 EXPECT_EQ(1u, Http2WeightToSpdy3Priority(220)); | |
| 64 EXPECT_EQ(1u, Http2WeightToSpdy3Priority(184)); | |
| 65 EXPECT_EQ(2u, Http2WeightToSpdy3Priority(183)); | |
| 66 EXPECT_EQ(2u, Http2WeightToSpdy3Priority(148)); | |
| 67 EXPECT_EQ(3u, Http2WeightToSpdy3Priority(147)); | |
| 68 EXPECT_EQ(3u, Http2WeightToSpdy3Priority(111)); | |
| 69 EXPECT_EQ(4u, Http2WeightToSpdy3Priority(110)); | |
| 70 EXPECT_EQ(4u, Http2WeightToSpdy3Priority(75)); | |
| 71 EXPECT_EQ(5u, Http2WeightToSpdy3Priority(74)); | |
| 72 EXPECT_EQ(5u, Http2WeightToSpdy3Priority(38)); | |
| 73 EXPECT_EQ(6u, Http2WeightToSpdy3Priority(37)); | |
| 74 EXPECT_EQ(6u, Http2WeightToSpdy3Priority(2)); | |
| 75 EXPECT_EQ(7u, Http2WeightToSpdy3Priority(1)); | |
| 76 } | |
| 77 | |
| 78 TEST(SpdyProtocolTest, IsValidHTTP2FrameStreamId) { | |
| 79 // Stream-specific frames must have non-zero stream ids | |
| 80 EXPECT_TRUE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::DATA)); | |
| 81 EXPECT_FALSE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::DATA)); | |
| 82 EXPECT_TRUE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::HEADERS)); | |
| 83 EXPECT_FALSE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::HEADERS)); | |
| 84 EXPECT_TRUE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::PRIORITY)); | |
| 85 EXPECT_FALSE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::PRIORITY)); | |
| 86 EXPECT_TRUE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::RST_STREAM)); | |
| 87 EXPECT_FALSE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::RST_STREAM)); | |
| 88 EXPECT_TRUE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::CONTINUATION)); | |
| 89 EXPECT_FALSE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::CONTINUATION)); | |
| 90 EXPECT_TRUE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::PUSH_PROMISE)); | |
| 91 EXPECT_FALSE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::PUSH_PROMISE)); | |
| 92 | |
| 93 // Connection-level frames must have zero stream ids | |
| 94 EXPECT_FALSE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::GOAWAY)); | |
| 95 EXPECT_TRUE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::GOAWAY)); | |
| 96 EXPECT_FALSE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::SETTINGS)); | |
| 97 EXPECT_TRUE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::SETTINGS)); | |
| 98 EXPECT_FALSE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::PING)); | |
| 99 EXPECT_TRUE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::PING)); | |
| 100 | |
| 101 // Frames that are neither stream-specific nor connection-level | |
| 102 // should not have their stream id declared invalid | |
| 103 EXPECT_TRUE(IsValidHTTP2FrameStreamId(1, SpdyFrameType::WINDOW_UPDATE)); | |
| 104 EXPECT_TRUE(IsValidHTTP2FrameStreamId(0, SpdyFrameType::WINDOW_UPDATE)); | |
| 105 } | |
| 106 | |
| 107 TEST(SpdyProtocolTest, ParseSettingsId) { | |
| 108 SpdySettingsIds setting_id; | |
| 109 EXPECT_FALSE(ParseSettingsId(0, &setting_id)); | |
| 110 EXPECT_TRUE(ParseSettingsId(1, &setting_id)); | |
| 111 EXPECT_EQ(SETTINGS_HEADER_TABLE_SIZE, setting_id); | |
| 112 EXPECT_TRUE(ParseSettingsId(2, &setting_id)); | |
| 113 EXPECT_EQ(SETTINGS_ENABLE_PUSH, setting_id); | |
| 114 EXPECT_TRUE(ParseSettingsId(3, &setting_id)); | |
| 115 EXPECT_EQ(SETTINGS_MAX_CONCURRENT_STREAMS, setting_id); | |
| 116 EXPECT_TRUE(ParseSettingsId(4, &setting_id)); | |
| 117 EXPECT_EQ(SETTINGS_INITIAL_WINDOW_SIZE, setting_id); | |
| 118 EXPECT_TRUE(ParseSettingsId(5, &setting_id)); | |
| 119 EXPECT_EQ(SETTINGS_MAX_FRAME_SIZE, setting_id); | |
| 120 EXPECT_TRUE(ParseSettingsId(6, &setting_id)); | |
| 121 EXPECT_EQ(SETTINGS_MAX_HEADER_LIST_SIZE, setting_id); | |
| 122 EXPECT_FALSE(ParseSettingsId(7, &setting_id)); | |
| 123 } | |
| 124 | |
| 125 TEST(SpdyProtocolTest, SettingsIdToString) { | |
| 126 struct { | |
| 127 SpdySettingsIds setting_id; | |
| 128 bool expected_bool; | |
| 129 const SpdyString expected_string; | |
| 130 } test_cases[] = { | |
| 131 {static_cast<SpdySettingsIds>(0), false, "SETTINGS_UNKNOWN"}, | |
| 132 {SETTINGS_HEADER_TABLE_SIZE, true, "SETTINGS_HEADER_TABLE_SIZE"}, | |
| 133 {SETTINGS_ENABLE_PUSH, true, "SETTINGS_ENABLE_PUSH"}, | |
| 134 {SETTINGS_MAX_CONCURRENT_STREAMS, true, | |
| 135 "SETTINGS_MAX_CONCURRENT_STREAMS"}, | |
| 136 {SETTINGS_INITIAL_WINDOW_SIZE, true, "SETTINGS_INITIAL_WINDOW_SIZE"}, | |
| 137 {SETTINGS_MAX_FRAME_SIZE, true, "SETTINGS_MAX_FRAME_SIZE"}, | |
| 138 {SETTINGS_MAX_HEADER_LIST_SIZE, true, "SETTINGS_MAX_HEADER_LIST_SIZE"}, | |
| 139 {static_cast<SpdySettingsIds>(7), false, "SETTINGS_UNKNOWN"}}; | |
| 140 for (auto test_case : test_cases) { | |
| 141 const char* settings_id_string; | |
| 142 EXPECT_EQ(test_case.expected_bool, | |
| 143 SettingsIdToString(test_case.setting_id, &settings_id_string)); | |
| 144 EXPECT_EQ(test_case.expected_string, settings_id_string); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 TEST(SpdyStreamPrecedenceTest, Basic) { | |
| 149 SpdyStreamPrecedence spdy3_prec(2); | |
| 150 EXPECT_TRUE(spdy3_prec.is_spdy3_priority()); | |
| 151 EXPECT_EQ(2, spdy3_prec.spdy3_priority()); | |
| 152 EXPECT_EQ(kHttp2RootStreamId, spdy3_prec.parent_id()); | |
| 153 EXPECT_EQ(Spdy3PriorityToHttp2Weight(2), spdy3_prec.weight()); | |
| 154 EXPECT_FALSE(spdy3_prec.is_exclusive()); | |
| 155 | |
| 156 for (bool is_exclusive : {true, false}) { | |
| 157 SpdyStreamPrecedence h2_prec(7, 123, is_exclusive); | |
| 158 EXPECT_FALSE(h2_prec.is_spdy3_priority()); | |
| 159 EXPECT_EQ(Http2WeightToSpdy3Priority(123), h2_prec.spdy3_priority()); | |
| 160 EXPECT_EQ(7u, h2_prec.parent_id()); | |
| 161 EXPECT_EQ(123, h2_prec.weight()); | |
| 162 EXPECT_EQ(is_exclusive, h2_prec.is_exclusive()); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 TEST(SpdyStreamPrecedenceTest, Clamping) { | |
| 167 EXPECT_SPDY_BUG(EXPECT_EQ(7, SpdyStreamPrecedence(8).spdy3_priority()), | |
| 168 "Invalid priority: 8"); | |
| 169 EXPECT_SPDY_BUG(EXPECT_EQ(kHttp2MinStreamWeight, | |
| 170 SpdyStreamPrecedence(3, 0, false).weight()), | |
| 171 "Invalid weight: 0"); | |
| 172 EXPECT_SPDY_BUG(EXPECT_EQ(kHttp2MaxStreamWeight, | |
| 173 SpdyStreamPrecedence(3, 300, false).weight()), | |
| 174 "Invalid weight: 300"); | |
| 175 } | |
| 176 | |
| 177 TEST(SpdyStreamPrecedenceTest, Copying) { | |
| 178 SpdyStreamPrecedence prec1(3); | |
| 179 SpdyStreamPrecedence copy1(prec1); | |
| 180 EXPECT_TRUE(copy1.is_spdy3_priority()); | |
| 181 EXPECT_EQ(3, copy1.spdy3_priority()); | |
| 182 | |
| 183 SpdyStreamPrecedence prec2(4, 5, true); | |
| 184 SpdyStreamPrecedence copy2(prec2); | |
| 185 EXPECT_FALSE(copy2.is_spdy3_priority()); | |
| 186 EXPECT_EQ(4u, copy2.parent_id()); | |
| 187 EXPECT_EQ(5, copy2.weight()); | |
| 188 EXPECT_TRUE(copy2.is_exclusive()); | |
| 189 | |
| 190 copy1 = prec2; | |
| 191 EXPECT_FALSE(copy1.is_spdy3_priority()); | |
| 192 EXPECT_EQ(4u, copy1.parent_id()); | |
| 193 EXPECT_EQ(5, copy1.weight()); | |
| 194 EXPECT_TRUE(copy1.is_exclusive()); | |
| 195 | |
| 196 copy2 = prec1; | |
| 197 EXPECT_TRUE(copy2.is_spdy3_priority()); | |
| 198 EXPECT_EQ(3, copy2.spdy3_priority()); | |
| 199 } | |
| 200 | |
| 201 TEST(SpdyStreamPrecedenceTest, Equals) { | |
| 202 EXPECT_EQ(SpdyStreamPrecedence(3), SpdyStreamPrecedence(3)); | |
| 203 EXPECT_NE(SpdyStreamPrecedence(3), SpdyStreamPrecedence(4)); | |
| 204 | |
| 205 EXPECT_EQ(SpdyStreamPrecedence(1, 2, false), | |
| 206 SpdyStreamPrecedence(1, 2, false)); | |
| 207 EXPECT_NE(SpdyStreamPrecedence(1, 2, false), | |
| 208 SpdyStreamPrecedence(2, 2, false)); | |
| 209 EXPECT_NE(SpdyStreamPrecedence(1, 2, false), | |
| 210 SpdyStreamPrecedence(1, 3, false)); | |
| 211 EXPECT_NE(SpdyStreamPrecedence(1, 2, false), | |
| 212 SpdyStreamPrecedence(1, 2, true)); | |
| 213 | |
| 214 SpdyStreamPrecedence spdy3_prec(3); | |
| 215 SpdyStreamPrecedence h2_prec(spdy3_prec.parent_id(), spdy3_prec.weight(), | |
| 216 spdy3_prec.is_exclusive()); | |
| 217 EXPECT_NE(spdy3_prec, h2_prec); | |
| 218 } | |
| 219 | |
| 220 TEST(SpdyDataIRTest, Construct) { | |
| 221 // Confirm that it makes a string of zero length from a | |
| 222 // SpdyStringPiece(nullptr). | |
| 223 SpdyStringPiece s1; | |
| 224 SpdyDataIR d1(1, s1); | |
| 225 EXPECT_EQ(d1.data_len(), 0ul); | |
| 226 EXPECT_NE(d1.data(), nullptr); | |
| 227 | |
| 228 // Confirms makes a copy of char array. | |
| 229 const char s2[] = "something"; | |
| 230 SpdyDataIR d2(2, s2); | |
| 231 EXPECT_EQ(SpdyStringPiece(d2.data(), d2.data_len()), s2); | |
| 232 EXPECT_NE(SpdyStringPiece(d1.data(), d1.data_len()), s2); | |
| 233 | |
| 234 // Confirm copies a const string. | |
| 235 const SpdyString foo = "foo"; | |
| 236 SpdyDataIR d3(3, foo); | |
| 237 EXPECT_EQ(foo, d3.data()); | |
| 238 | |
| 239 // Confirm copies a non-const string. | |
| 240 SpdyString bar = "bar"; | |
| 241 SpdyDataIR d4(4, bar); | |
| 242 EXPECT_EQ("bar", bar); | |
| 243 EXPECT_EQ("bar", SpdyStringPiece(d4.data(), d4.data_len())); | |
| 244 | |
| 245 // Confirm moves an rvalue reference. Note that the test string "baz" is too | |
| 246 // short to trigger the move optimization, and instead a copy occurs. | |
| 247 SpdyString baz = "the quick brown fox"; | |
| 248 SpdyDataIR d5(5, std::move(baz)); | |
| 249 EXPECT_EQ("", baz); | |
| 250 EXPECT_EQ(SpdyStringPiece(d5.data(), d5.data_len()), "the quick brown fox"); | |
| 251 | |
| 252 // Confirms makes a copy of string literal. | |
| 253 SpdyDataIR d7(7, "something else"); | |
| 254 EXPECT_EQ(SpdyStringPiece(d7.data(), d7.data_len()), "something else"); | |
| 255 } | |
| 256 | |
| 257 } // namespace test | |
| 258 } // namespace net | |
| OLD | NEW |