| 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_encoding_context.h" | 5 #include "net/spdy/hpack_encoding_context.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Try to process an indexed header with an invalid index. That should | 16 // Try to process an indexed header with an invalid index. That should |
| 17 // fail. | 17 // fail. |
| 18 TEST(HpackEncodingContextTest, IndexedHeaderInvalid) { | 18 TEST(HpackEncodingContextTest, IndexedHeaderInvalid) { |
| 19 HpackEncodingContext encoding_context; | 19 HpackEncodingContext encoding_context; |
| 20 | 20 |
| 21 int32 new_index = -1; | 21 uint32 new_index = 0; |
| 22 std::vector<uint32> removed_referenced_indices; | 22 std::vector<uint32> removed_referenced_indices; |
| 23 EXPECT_FALSE( | 23 EXPECT_FALSE( |
| 24 encoding_context.ProcessIndexedHeader(kuint32max, | 24 encoding_context.ProcessIndexedHeader(kuint32max, |
| 25 &new_index, | 25 &new_index, |
| 26 &removed_referenced_indices)); | 26 &removed_referenced_indices)); |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Try to process an indexed header with an index for a static | 29 // Try to process an indexed header with an index for a static |
| 30 // header. That should succeed and add a mutable copy into the header | 30 // header. That should succeed and add a mutable copy into the header |
| 31 // table. | 31 // table. |
| 32 TEST(HpackEncodingContextTest, IndexedHeaderStatic) { | 32 TEST(HpackEncodingContextTest, IndexedHeaderStatic) { |
| 33 HpackEncodingContext encoding_context; | 33 HpackEncodingContext encoding_context; |
| 34 | 34 |
| 35 std::string name = encoding_context.GetNameAt(1).as_string(); | 35 std::string name = encoding_context.GetNameAt(2).as_string(); |
| 36 std::string value = encoding_context.GetValueAt(1).as_string(); | 36 std::string value = encoding_context.GetValueAt(2).as_string(); |
| 37 EXPECT_NE(name, encoding_context.GetNameAt(0)); | 37 EXPECT_EQ(0u, encoding_context.GetMutableEntryCount()); |
| 38 EXPECT_NE(value, encoding_context.GetValueAt(0)); | 38 EXPECT_NE(name, encoding_context.GetNameAt(1)); |
| 39 EXPECT_NE(value, encoding_context.GetValueAt(1)); |
| 39 | 40 |
| 40 { | 41 { |
| 41 int32 new_index = -1; | 42 uint32 new_index = 0; |
| 43 std::vector<uint32> removed_referenced_indices; |
| 44 EXPECT_TRUE( |
| 45 encoding_context.ProcessIndexedHeader(2, |
| 46 &new_index, |
| 47 &removed_referenced_indices)); |
| 48 EXPECT_EQ(1u, new_index); |
| 49 EXPECT_TRUE(removed_referenced_indices.empty()); |
| 50 } |
| 51 EXPECT_EQ(1u, encoding_context.GetMutableEntryCount()); |
| 52 EXPECT_EQ(name, encoding_context.GetNameAt(1)); |
| 53 EXPECT_EQ(value, encoding_context.GetValueAt(1)); |
| 54 EXPECT_TRUE(encoding_context.IsReferencedAt(1)); |
| 55 |
| 56 { |
| 57 uint32 new_index = 0; |
| 42 std::vector<uint32> removed_referenced_indices; | 58 std::vector<uint32> removed_referenced_indices; |
| 43 EXPECT_TRUE( | 59 EXPECT_TRUE( |
| 44 encoding_context.ProcessIndexedHeader(1, | 60 encoding_context.ProcessIndexedHeader(1, |
| 45 &new_index, | 61 &new_index, |
| 46 &removed_referenced_indices)); | 62 &removed_referenced_indices)); |
| 47 EXPECT_EQ(0, new_index); | 63 EXPECT_EQ(1u, new_index); |
| 48 EXPECT_TRUE(removed_referenced_indices.empty()); | 64 EXPECT_TRUE(removed_referenced_indices.empty()); |
| 49 } | 65 } |
| 50 EXPECT_EQ(name, encoding_context.GetNameAt(0)); | 66 EXPECT_EQ(1u, encoding_context.GetMutableEntryCount()); |
| 51 EXPECT_EQ(value, encoding_context.GetValueAt(0)); | 67 EXPECT_EQ(name, encoding_context.GetNameAt(1)); |
| 52 EXPECT_TRUE(encoding_context.IsReferencedAt(0)); | 68 EXPECT_EQ(value, encoding_context.GetValueAt(1)); |
| 53 | 69 EXPECT_LE(1u, encoding_context.GetMutableEntryCount()); |
| 54 { | 70 EXPECT_FALSE(encoding_context.IsReferencedAt(1)); |
| 55 int32 new_index = -1; | |
| 56 std::vector<uint32> removed_referenced_indices; | |
| 57 EXPECT_TRUE( | |
| 58 encoding_context.ProcessIndexedHeader(0, | |
| 59 &new_index, | |
| 60 &removed_referenced_indices)); | |
| 61 EXPECT_EQ(0, new_index); | |
| 62 EXPECT_TRUE(removed_referenced_indices.empty()); | |
| 63 } | |
| 64 EXPECT_EQ(name, encoding_context.GetNameAt(0)); | |
| 65 EXPECT_EQ(value, encoding_context.GetValueAt(0)); | |
| 66 EXPECT_FALSE(encoding_context.IsReferencedAt(0)); | |
| 67 } | 71 } |
| 68 | 72 |
| 69 // Try to process an indexed header with an index for a static header | 73 // Try to process an indexed header with an index for a static header |
| 70 // and an encoding context where a copy of that header wouldn't | 74 // and an encoding context where a copy of that header wouldn't |
| 71 // fit. That should succeed without making a copy. | 75 // fit. That should succeed without making a copy. |
| 72 TEST(HpackEncodingContextTest, IndexedHeaderStaticCopyDoesNotFit) { | 76 TEST(HpackEncodingContextTest, IndexedHeaderStaticCopyDoesNotFit) { |
| 73 HpackEncodingContext encoding_context; | 77 HpackEncodingContext encoding_context; |
| 74 encoding_context.SetMaxSize(0); | 78 encoding_context.SetMaxSize(0); |
| 75 | 79 |
| 76 int32 new_index = -1; | 80 uint32 new_index = 0; |
| 77 std::vector<uint32> removed_referenced_indices; | 81 std::vector<uint32> removed_referenced_indices; |
| 78 EXPECT_TRUE( | 82 EXPECT_TRUE( |
| 79 encoding_context.ProcessIndexedHeader(1, | 83 encoding_context.ProcessIndexedHeader(1, |
| 80 &new_index, | 84 &new_index, |
| 81 &removed_referenced_indices)); | 85 &removed_referenced_indices)); |
| 82 EXPECT_EQ(-1, new_index); | 86 EXPECT_EQ(0u, new_index); |
| 83 EXPECT_TRUE(removed_referenced_indices.empty()); | 87 EXPECT_TRUE(removed_referenced_indices.empty()); |
| 88 EXPECT_EQ(0u, encoding_context.GetMutableEntryCount()); |
| 84 } | 89 } |
| 85 | 90 |
| 86 // NOTE: It's too onerous to try to test invalid input to | 91 // NOTE: It's too onerous to try to test invalid input to |
| 87 // ProcessLiteralHeaderWithIncrementalIndexing(); that would require | 92 // ProcessLiteralHeaderWithIncrementalIndexing(); that would require |
| 88 // making a really large (>4GB of memory) string. | 93 // making a really large (>4GB of memory) string. |
| 89 | 94 |
| 90 // Try to process a reasonably-sized literal header with incremental | 95 // Try to process a reasonably-sized literal header with incremental |
| 91 // indexing. It should succeed. | 96 // indexing. It should succeed. |
| 92 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexing) { | 97 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexing) { |
| 93 HpackEncodingContext encoding_context; | 98 HpackEncodingContext encoding_context; |
| 94 | 99 |
| 95 int32 index = -1; | 100 uint32 index = 0; |
| 96 std::vector<uint32> removed_referenced_indices; | 101 std::vector<uint32> removed_referenced_indices; |
| 97 EXPECT_TRUE( | 102 EXPECT_TRUE( |
| 98 encoding_context.ProcessLiteralHeaderWithIncrementalIndexing( | 103 encoding_context.ProcessLiteralHeaderWithIncrementalIndexing( |
| 99 "name", "value", &index, &removed_referenced_indices)); | 104 "name", "value", &index, &removed_referenced_indices)); |
| 100 EXPECT_EQ(0, index); | 105 EXPECT_EQ(1u, index); |
| 101 EXPECT_TRUE(removed_referenced_indices.empty()); | 106 EXPECT_TRUE(removed_referenced_indices.empty()); |
| 102 EXPECT_EQ("name", encoding_context.GetNameAt(0).as_string()); | 107 EXPECT_EQ("name", encoding_context.GetNameAt(1).as_string()); |
| 103 EXPECT_EQ("value", encoding_context.GetValueAt(0).as_string()); | 108 EXPECT_EQ("value", encoding_context.GetValueAt(1).as_string()); |
| 104 EXPECT_TRUE(encoding_context.IsReferencedAt(0)); | 109 EXPECT_TRUE(encoding_context.IsReferencedAt(1)); |
| 110 EXPECT_EQ(1u, encoding_context.GetMutableEntryCount()); |
| 105 } | 111 } |
| 106 | 112 |
| 107 // Try to process a literal header with incremental indexing that is | 113 // Try to process a literal header with incremental indexing that is |
| 108 // too large for the header table. It should succeed without indexing | 114 // too large for the header table. It should succeed without indexing |
| 109 // into the table. | 115 // into the table. |
| 110 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexingDoesNotFit) { | 116 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexingDoesNotFit) { |
| 111 HpackEncodingContext encoding_context; | 117 HpackEncodingContext encoding_context; |
| 112 encoding_context.SetMaxSize(0); | 118 encoding_context.SetMaxSize(0); |
| 113 | 119 |
| 114 int32 index = -1; | 120 uint32 index = 0; |
| 115 std::vector<uint32> removed_referenced_indices; | 121 std::vector<uint32> removed_referenced_indices; |
| 116 EXPECT_TRUE( | 122 EXPECT_TRUE( |
| 117 encoding_context.ProcessLiteralHeaderWithIncrementalIndexing( | 123 encoding_context.ProcessLiteralHeaderWithIncrementalIndexing( |
| 118 "name", "value", &index, &removed_referenced_indices)); | 124 "name", "value", &index, &removed_referenced_indices)); |
| 119 EXPECT_EQ(-1, index); | 125 EXPECT_EQ(0u, index); |
| 120 EXPECT_TRUE(removed_referenced_indices.empty()); | 126 EXPECT_TRUE(removed_referenced_indices.empty()); |
| 127 EXPECT_EQ(0u, encoding_context.GetMutableEntryCount()); |
| 121 } | 128 } |
| 122 | 129 |
| 123 } // namespace | 130 } // namespace |
| 124 | 131 |
| 125 } // namespace net | 132 } // namespace net |
| OLD | NEW |