| 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 "net/spdy/spdy_protocol.h" | 5 #include "net/spdy/spdy_protocol.h" |
| 6 | 6 |
| 7 #include <iostream> | 7 #include <iostream> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "net/spdy/spdy_bitmasks.h" | 11 #include "net/spdy/spdy_bitmasks.h" |
| 12 #include "net/spdy/spdy_framer.h" | 12 #include "net/spdy/spdy_framer.h" |
| 13 #include "net/spdy/spdy_test_utils.h" | 13 #include "net/spdy/spdy_test_utils.h" |
| 14 #include "net/test/gtest_util.h" | 14 #include "net/test/gtest_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using std::ostream; | 17 using std::ostream; |
| 18 using std::string; |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 | 21 |
| 21 ostream& operator<<(ostream& os, const SpdyStreamPrecedence precedence) { | 22 ostream& operator<<(ostream& os, const SpdyStreamPrecedence precedence) { |
| 22 if (precedence.is_spdy3_priority()) { | 23 if (precedence.is_spdy3_priority()) { |
| 23 os << "SpdyStreamPrecedence[spdy3_priority=" << precedence.spdy3_priority() | 24 os << "SpdyStreamPrecedence[spdy3_priority=" << precedence.spdy3_priority() |
| 24 << "]"; | 25 << "]"; |
| 25 } else { | 26 } else { |
| 26 os << "SpdyStreamPrecedence[parent_id=" << precedence.parent_id() | 27 os << "SpdyStreamPrecedence[parent_id=" << precedence.parent_id() |
| 27 << ", weight=" << precedence.weight() | 28 << ", weight=" << precedence.weight() |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 EXPECT_EQ(d1.data_len(), 0ul); | 81 EXPECT_EQ(d1.data_len(), 0ul); |
| 81 EXPECT_NE(d1.data(), nullptr); | 82 EXPECT_NE(d1.data(), nullptr); |
| 82 | 83 |
| 83 // Confirms makes a copy of char array. | 84 // Confirms makes a copy of char array. |
| 84 const char s2[] = "something"; | 85 const char s2[] = "something"; |
| 85 SpdyDataIR d2(2, s2); | 86 SpdyDataIR d2(2, s2); |
| 86 EXPECT_EQ(base::StringPiece(d2.data(), d2.data_len()), s2); | 87 EXPECT_EQ(base::StringPiece(d2.data(), d2.data_len()), s2); |
| 87 EXPECT_NE(base::StringPiece(d1.data(), d1.data_len()), s2); | 88 EXPECT_NE(base::StringPiece(d1.data(), d1.data_len()), s2); |
| 88 | 89 |
| 89 // Confirm copies a const string. | 90 // Confirm copies a const string. |
| 90 const std::string foo = "foo"; | 91 const string foo = "foo"; |
| 91 SpdyDataIR d3(3, foo); | 92 SpdyDataIR d3(3, foo); |
| 92 EXPECT_EQ(foo, d3.data()); | 93 EXPECT_EQ(foo, d3.data()); |
| 93 | 94 |
| 94 // Confirm copies a non-const string. | 95 // Confirm copies a non-const string. |
| 95 std::string bar = "bar"; | 96 string bar = "bar"; |
| 96 SpdyDataIR d4(4, bar); | 97 SpdyDataIR d4(4, bar); |
| 97 EXPECT_EQ("bar", bar); | 98 EXPECT_EQ("bar", bar); |
| 98 EXPECT_EQ("bar", base::StringPiece(d4.data(), d4.data_len())); | 99 EXPECT_EQ("bar", base::StringPiece(d4.data(), d4.data_len())); |
| 99 | 100 |
| 100 // Confirm moves an rvalue reference. Note that the test string "baz" is too | 101 // Confirm moves an rvalue reference. Note that the test string "baz" is too |
| 101 // short to trigger the move optimization, and instead a copy occurs. | 102 // short to trigger the move optimization, and instead a copy occurs. |
| 102 std::string baz = "the quick brown fox"; | 103 string baz = "the quick brown fox"; |
| 103 SpdyDataIR d5(5, std::move(baz)); | 104 SpdyDataIR d5(5, std::move(baz)); |
| 104 EXPECT_EQ("", baz); | 105 EXPECT_EQ("", baz); |
| 105 EXPECT_EQ(base::StringPiece(d5.data(), d5.data_len()), "the quick brown fox"); | 106 EXPECT_EQ(base::StringPiece(d5.data(), d5.data_len()), "the quick brown fox"); |
| 106 | 107 |
| 107 // Confirms makes a copy of string literal. | 108 // Confirms makes a copy of string literal. |
| 108 SpdyDataIR d7(7, "something else"); | 109 SpdyDataIR d7(7, "something else"); |
| 109 EXPECT_EQ(base::StringPiece(d7.data(), d7.data_len()), "something else"); | 110 EXPECT_EQ(base::StringPiece(d7.data(), d7.data_len()), "something else"); |
| 110 } | 111 } |
| 111 | 112 |
| 112 TEST(SpdyProtocolTest, ClampSpdy3Priority) { | 113 TEST(SpdyProtocolTest, ClampSpdy3Priority) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 EXPECT_EQ(3u, Http2WeightToSpdy3Priority(111)); | 147 EXPECT_EQ(3u, Http2WeightToSpdy3Priority(111)); |
| 147 EXPECT_EQ(4u, Http2WeightToSpdy3Priority(110)); | 148 EXPECT_EQ(4u, Http2WeightToSpdy3Priority(110)); |
| 148 EXPECT_EQ(4u, Http2WeightToSpdy3Priority(75)); | 149 EXPECT_EQ(4u, Http2WeightToSpdy3Priority(75)); |
| 149 EXPECT_EQ(5u, Http2WeightToSpdy3Priority(74)); | 150 EXPECT_EQ(5u, Http2WeightToSpdy3Priority(74)); |
| 150 EXPECT_EQ(5u, Http2WeightToSpdy3Priority(38)); | 151 EXPECT_EQ(5u, Http2WeightToSpdy3Priority(38)); |
| 151 EXPECT_EQ(6u, Http2WeightToSpdy3Priority(37)); | 152 EXPECT_EQ(6u, Http2WeightToSpdy3Priority(37)); |
| 152 EXPECT_EQ(6u, Http2WeightToSpdy3Priority(2)); | 153 EXPECT_EQ(6u, Http2WeightToSpdy3Priority(2)); |
| 153 EXPECT_EQ(7u, Http2WeightToSpdy3Priority(1)); | 154 EXPECT_EQ(7u, Http2WeightToSpdy3Priority(1)); |
| 154 } | 155 } |
| 155 | 156 |
| 157 TEST(SpdyProtocolTest, ParseSettingsId) { |
| 158 SpdySettingsIds setting_id; |
| 159 EXPECT_FALSE(SpdyConstants::ParseSettingsId(0, &setting_id)); |
| 160 EXPECT_TRUE(SpdyConstants::ParseSettingsId(1, &setting_id)); |
| 161 EXPECT_EQ(SETTINGS_HEADER_TABLE_SIZE, setting_id); |
| 162 EXPECT_TRUE(SpdyConstants::ParseSettingsId(2, &setting_id)); |
| 163 EXPECT_EQ(SETTINGS_ENABLE_PUSH, setting_id); |
| 164 EXPECT_TRUE(SpdyConstants::ParseSettingsId(3, &setting_id)); |
| 165 EXPECT_EQ(SETTINGS_MAX_CONCURRENT_STREAMS, setting_id); |
| 166 EXPECT_TRUE(SpdyConstants::ParseSettingsId(4, &setting_id)); |
| 167 EXPECT_EQ(SETTINGS_INITIAL_WINDOW_SIZE, setting_id); |
| 168 EXPECT_TRUE(SpdyConstants::ParseSettingsId(5, &setting_id)); |
| 169 EXPECT_EQ(SETTINGS_MAX_FRAME_SIZE, setting_id); |
| 170 EXPECT_TRUE(SpdyConstants::ParseSettingsId(6, &setting_id)); |
| 171 EXPECT_EQ(SETTINGS_MAX_HEADER_LIST_SIZE, setting_id); |
| 172 EXPECT_FALSE(SpdyConstants::ParseSettingsId(7, &setting_id)); |
| 173 } |
| 174 |
| 175 TEST(SpdyProtocolTest, SettingsIdToString) { |
| 176 struct { |
| 177 SpdySettingsIds setting_id; |
| 178 bool expected_bool; |
| 179 const string expected_string; |
| 180 } test_cases[] = { |
| 181 {static_cast<SpdySettingsIds>(0), false, "SETTINGS_UNKNOWN"}, |
| 182 {SETTINGS_HEADER_TABLE_SIZE, true, "SETTINGS_HEADER_TABLE_SIZE"}, |
| 183 {SETTINGS_ENABLE_PUSH, true, "SETTINGS_ENABLE_PUSH"}, |
| 184 {SETTINGS_MAX_CONCURRENT_STREAMS, true, |
| 185 "SETTINGS_MAX_CONCURRENT_STREAMS"}, |
| 186 {SETTINGS_INITIAL_WINDOW_SIZE, true, "SETTINGS_INITIAL_WINDOW_SIZE"}, |
| 187 {SETTINGS_MAX_FRAME_SIZE, true, "SETTINGS_MAX_FRAME_SIZE"}, |
| 188 {SETTINGS_MAX_HEADER_LIST_SIZE, true, "SETTINGS_MAX_HEADER_LIST_SIZE"}, |
| 189 {static_cast<SpdySettingsIds>(7), false, "SETTINGS_UNKNOWN"}}; |
| 190 for (auto test_case : test_cases) { |
| 191 const char* settings_id_string; |
| 192 EXPECT_EQ(test_case.expected_bool, |
| 193 SpdyConstants::SettingsIdToString(test_case.setting_id, |
| 194 &settings_id_string)); |
| 195 EXPECT_EQ(test_case.expected_string, settings_id_string); |
| 196 } |
| 197 } |
| 198 |
| 156 TEST(SpdyStreamPrecedenceTest, Basic) { | 199 TEST(SpdyStreamPrecedenceTest, Basic) { |
| 157 SpdyStreamPrecedence spdy3_prec(2); | 200 SpdyStreamPrecedence spdy3_prec(2); |
| 158 EXPECT_TRUE(spdy3_prec.is_spdy3_priority()); | 201 EXPECT_TRUE(spdy3_prec.is_spdy3_priority()); |
| 159 EXPECT_EQ(2, spdy3_prec.spdy3_priority()); | 202 EXPECT_EQ(2, spdy3_prec.spdy3_priority()); |
| 160 EXPECT_EQ(kHttp2RootStreamId, spdy3_prec.parent_id()); | 203 EXPECT_EQ(kHttp2RootStreamId, spdy3_prec.parent_id()); |
| 161 EXPECT_EQ(Spdy3PriorityToHttp2Weight(2), spdy3_prec.weight()); | 204 EXPECT_EQ(Spdy3PriorityToHttp2Weight(2), spdy3_prec.weight()); |
| 162 EXPECT_FALSE(spdy3_prec.is_exclusive()); | 205 EXPECT_FALSE(spdy3_prec.is_exclusive()); |
| 163 | 206 |
| 164 for (bool is_exclusive : {true, false}) { | 207 for (bool is_exclusive : {true, false}) { |
| 165 SpdyStreamPrecedence h2_prec(7, 123, is_exclusive); | 208 SpdyStreamPrecedence h2_prec(7, 123, is_exclusive); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 SpdyStreamPrecedence(1, 2, true)); | 263 SpdyStreamPrecedence(1, 2, true)); |
| 221 | 264 |
| 222 SpdyStreamPrecedence spdy3_prec(3); | 265 SpdyStreamPrecedence spdy3_prec(3); |
| 223 SpdyStreamPrecedence h2_prec(spdy3_prec.parent_id(), spdy3_prec.weight(), | 266 SpdyStreamPrecedence h2_prec(spdy3_prec.parent_id(), spdy3_prec.weight(), |
| 224 spdy3_prec.is_exclusive()); | 267 spdy3_prec.is_exclusive()); |
| 225 EXPECT_NE(spdy3_prec, h2_prec); | 268 EXPECT_NE(spdy3_prec, h2_prec); |
| 226 } | 269 } |
| 227 | 270 |
| 228 } // namespace test | 271 } // namespace test |
| 229 } // namespace net | 272 } // namespace net |
| OLD | NEW |