OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/hpack_encoder.h" | 5 #include "net/spdy/hpack_encoder.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 81 |
82 using std::map; | 82 using std::map; |
83 using testing::ElementsAre; | 83 using testing::ElementsAre; |
84 | 84 |
85 class HpackEncoderTest : public ::testing::Test { | 85 class HpackEncoderTest : public ::testing::Test { |
86 protected: | 86 protected: |
87 typedef test::HpackEncoderPeer::Representations Representations; | 87 typedef test::HpackEncoderPeer::Representations Representations; |
88 | 88 |
89 HpackEncoderTest() | 89 HpackEncoderTest() |
90 : encoder_(ObtainHpackHuffmanTable()), | 90 : encoder_(ObtainHpackHuffmanTable()), |
91 peer_(&encoder_) {} | 91 peer_(&encoder_), |
| 92 static_(peer_.table()->GetByIndex(1)) {} |
92 | 93 |
93 virtual void SetUp() { | 94 virtual void SetUp() { |
94 static_ = peer_.table()->GetByIndex(1); | |
95 // Populate dynamic entries into the table fixture. For simplicity each | 95 // Populate dynamic entries into the table fixture. For simplicity each |
96 // entry has name.size() + value.size() == 10. | 96 // entry has name.size() + value.size() == 10. |
97 key_1_ = peer_.table()->TryAddEntry("key1", "value1"); | 97 key_1_ = peer_.table()->TryAddEntry("key1", "value1"); |
98 key_2_ = peer_.table()->TryAddEntry("key2", "value2"); | 98 key_2_ = peer_.table()->TryAddEntry("key2", "value2"); |
99 cookie_a_ = peer_.table()->TryAddEntry("cookie", "a=bb"); | 99 cookie_a_ = peer_.table()->TryAddEntry("cookie", "a=bb"); |
100 cookie_c_ = peer_.table()->TryAddEntry("cookie", "c=dd"); | 100 cookie_c_ = peer_.table()->TryAddEntry("cookie", "c=dd"); |
101 | 101 |
102 // No further insertions may occur without evictions. | 102 // No further insertions may occur without evictions. |
103 peer_.table()->SetMaxSize(peer_.table()->size()); | 103 peer_.table()->SetMaxSize(peer_.table()->size()); |
104 | 104 |
105 // Disable Huffman coding by default. Most tests don't care about it. | 105 // Disable Huffman coding by default. Most tests don't care about it. |
106 peer_.set_allow_huffman_compression(false); | 106 peer_.set_allow_huffman_compression(false); |
107 } | 107 } |
108 | 108 |
109 void ExpectIndex(size_t index) { | 109 void ExpectIndex(size_t index) { |
110 expected_.AppendPrefix(kIndexedOpcode); | 110 expected_.AppendPrefix(kIndexedOpcode); |
111 expected_.AppendUint32(index); | 111 expected_.AppendUint32(index); |
112 } | 112 } |
113 void ExpectIndexedLiteral(HpackEntry* key_entry, StringPiece value) { | 113 void ExpectIndexedLiteral(const HpackEntry* key_entry, StringPiece value) { |
114 expected_.AppendPrefix(kLiteralIncrementalIndexOpcode); | 114 expected_.AppendPrefix(kLiteralIncrementalIndexOpcode); |
115 expected_.AppendUint32(IndexOf(key_entry)); | 115 expected_.AppendUint32(IndexOf(key_entry)); |
116 expected_.AppendPrefix(kStringLiteralIdentityEncoded); | 116 expected_.AppendPrefix(kStringLiteralIdentityEncoded); |
117 expected_.AppendUint32(value.size()); | 117 expected_.AppendUint32(value.size()); |
118 expected_.AppendBytes(value); | 118 expected_.AppendBytes(value); |
119 } | 119 } |
120 void ExpectIndexedLiteral(StringPiece name, StringPiece value) { | 120 void ExpectIndexedLiteral(StringPiece name, StringPiece value) { |
121 expected_.AppendPrefix(kLiteralIncrementalIndexOpcode); | 121 expected_.AppendPrefix(kLiteralIncrementalIndexOpcode); |
122 expected_.AppendUint32(0); | 122 expected_.AppendUint32(0); |
123 expected_.AppendPrefix(kStringLiteralIdentityEncoded); | 123 expected_.AppendPrefix(kStringLiteralIdentityEncoded); |
(...skipping 15 matching lines...) Expand all Loading... |
139 } | 139 } |
140 void CompareWithExpectedEncoding(const map<string, string>& header_set) { | 140 void CompareWithExpectedEncoding(const map<string, string>& header_set) { |
141 string expected_out, actual_out; | 141 string expected_out, actual_out; |
142 expected_.TakeString(&expected_out); | 142 expected_.TakeString(&expected_out); |
143 EXPECT_TRUE(encoder_.EncodeHeaderSet(header_set, &actual_out)); | 143 EXPECT_TRUE(encoder_.EncodeHeaderSet(header_set, &actual_out)); |
144 EXPECT_EQ(expected_out, actual_out); | 144 EXPECT_EQ(expected_out, actual_out); |
145 } | 145 } |
146 size_t IndexOf(HpackEntry* entry) { | 146 size_t IndexOf(HpackEntry* entry) { |
147 return peer_.table()->IndexOf(entry); | 147 return peer_.table()->IndexOf(entry); |
148 } | 148 } |
| 149 size_t IndexOf(const HpackEntry* entry) { |
| 150 return peer_.table()->IndexOf(entry); |
| 151 } |
149 | 152 |
150 HpackEncoder encoder_; | 153 HpackEncoder encoder_; |
151 test::HpackEncoderPeer peer_; | 154 test::HpackEncoderPeer peer_; |
152 | 155 |
153 HpackEntry* static_; | 156 const HpackEntry* static_; |
154 HpackEntry* key_1_; | 157 const HpackEntry* key_1_; |
155 HpackEntry* key_2_; | 158 const HpackEntry* key_2_; |
156 HpackEntry* cookie_a_; | 159 const HpackEntry* cookie_a_; |
157 HpackEntry* cookie_c_; | 160 const HpackEntry* cookie_c_; |
158 | 161 |
159 HpackOutputStream expected_; | 162 HpackOutputStream expected_; |
160 }; | 163 }; |
161 | 164 |
162 TEST_F(HpackEncoderTest, SingleDynamicIndex) { | 165 TEST_F(HpackEncoderTest, SingleDynamicIndex) { |
163 ExpectIndex(IndexOf(key_2_)); | 166 ExpectIndex(IndexOf(key_2_)); |
164 | 167 |
165 map<string, string> headers; | 168 map<string, string> headers; |
166 headers[key_2_->name()] = key_2_->value(); | 169 headers[key_2_->name()] = key_2_->value(); |
167 CompareWithExpectedEncoding(headers); | 170 CompareWithExpectedEncoding(headers); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 expect[static_cast<uint8>('\xff')] = 1; | 412 expect[static_cast<uint8>('\xff')] = 1; |
410 expect[static_cast<uint8>('b')] = 1; | 413 expect[static_cast<uint8>('b')] = 1; |
411 | 414 |
412 EXPECT_EQ(expect, counts); | 415 EXPECT_EQ(expect, counts); |
413 EXPECT_EQ(9u, total_counts); | 416 EXPECT_EQ(9u, total_counts); |
414 } | 417 } |
415 | 418 |
416 } // namespace | 419 } // namespace |
417 | 420 |
418 } // namespace net | 421 } // namespace net |
OLD | NEW |