proxygen
HPACKHeaderTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
13 #include <sstream>
14 
15 using namespace proxygen;
16 using namespace std;
17 using namespace testing;
18 
20 };
21 
23  HPACKHeader h(":path", "/");
24  EXPECT_EQ(h.bytes(), 32 + 5 + 1);
25 }
26 
27 TEST_F(HPACKHeaderTests, Operators) {
28  HPACKHeader h0(":path", "/");
29  HPACKHeader h1(":path", "/");
30  HPACKHeader h2(":path", "/index.php");
31  HPACKHeader h3("x-fb-debug", "test");
32  // ==
33  EXPECT_TRUE(h0 == h1);
34  EXPECT_FALSE(h1 == h2);
35  // <
36  EXPECT_FALSE(h1 < h1);
37  EXPECT_TRUE(h1 < h2);
38  EXPECT_TRUE(h1 < h3);
39  // >
40  EXPECT_FALSE(h2 > h2);
41  EXPECT_TRUE(h3 > h2);
42  EXPECT_TRUE(h2 > h1);
43 
44  stringstream out;
45  out << h1;
46  EXPECT_EQ(out.str(), ":path: /");
47 }
48 
50  HPACKHeader h1(":path", "");
51  HPACKHeader h2(":path", "/");
52  EXPECT_FALSE(h1.hasValue());
53  EXPECT_TRUE(h2.hasValue());
54 }
55 
56 TEST_F(HPACKHeaderTests, HeaderIndexingStrategyBasic) {
57  HeaderIndexingStrategy indexingStrat;
58  HPACKHeader path(":path", "index.php?q=42");
59  EXPECT_FALSE(indexingStrat.indexHeader(path));
60  HPACKHeader cdn(":path", "/hprofile-ak-prn1/49496_6024432_1026115112_n.jpg");
61  EXPECT_FALSE(indexingStrat.indexHeader(cdn));
62  HPACKHeader clen("content-length", "512");
63  EXPECT_FALSE(indexingStrat.indexHeader(clen));
64  HPACKHeader data("data", "value");
65  EXPECT_TRUE(indexingStrat.indexHeader(data));
66 }
67 
69 };
70 
72  //return a HPACKHeaderName that goes destroyed
73  HPACKHeaderName headerName(name);
74  return headerName;
75 }
76 
77 TEST_F(HPACKHeaderNameTest, TestConstructor) {
78  // Test constructor
79  HPACKHeaderName name1("accept-encoding");
80  HPACKHeaderName name2("content-length");
81  HPACKHeaderName name3("uncommon-name");
82  HPACKHeaderName name4("uncommon-name-2");
83  EXPECT_EQ(name1.get(), "accept-encoding");
84  EXPECT_EQ(name2.get(), "content-length");
85  EXPECT_EQ(name3.get(), "uncommon-name");
86  EXPECT_EQ(name4.get(), "uncommon-name-2");
87 }
88 
89 TEST_F(HPACKHeaderNameTest, TestCopyConstructor) {
90  HPACKHeaderName name1("accept-encoding");
91  HPACKHeaderName name2("uncommon-name");
92 
93  // Test copy constructor
94  HPACKHeaderName name3(name1);
95  HPACKHeaderName name4(name2);
96  HPACKHeaderName name5(destroyedHPACKHeaderName("x-fb-debug"));
97  HPACKHeaderName name6(destroyedHPACKHeaderName("uncommon-name"));
98  EXPECT_EQ(name3.get(), "accept-encoding");
99  EXPECT_EQ(name4.get(), "uncommon-name");
100  EXPECT_EQ(name5.get(), "x-fb-debug");
101  EXPECT_EQ(name6.get(), "uncommon-name");
102 }
103 
104 TEST_F(HPACKHeaderNameTest, TestMoveConstructor) {
105  HPACKHeaderName name1("accept-encoding");
106  HPACKHeaderName name2("uncommon-name");
107 
108  // Test move constructor
109  HPACKHeaderName name3(std::move(name1));
110  HPACKHeaderName name4(std::move(name2));
111  EXPECT_EQ(name3.get(), "accept-encoding");
112  EXPECT_EQ(name4.get(), "uncommon-name");
113 }
114 
115 TEST_F(HPACKHeaderNameTest, TestAssignmentOperators) {
116  std::string testHeaderName = "accept-encoding";
117 
118  HPACKHeaderName name1(testHeaderName);
119  EXPECT_EQ(name1.get(), testHeaderName);
120 
121  // Explicitly test some self assignment overloads
122 #ifdef __clang__
123 #pragma clang diagnostic push
124 #pragma clang diagnostic ignored "-Wunknown-warning-option"
125 #pragma clang diagnostic ignored "-Wself-assign-overloaded"
126 #endif
127  name1 = name1;
128 #ifdef __clang__
129 #pragma clang diagnostic pop
130 #endif
131  EXPECT_EQ(name1.get(), testHeaderName);
132  HPACKHeaderName* pName1 = &name1;
133  // Specifically require a temporary above to throw off the compiler/lint:
134  // explicitly moving variable of type 'proxygen::HPACKHeaderName' to itself
135  name1 = std::move(*pName1);
136  EXPECT_EQ(name1.get(), testHeaderName);
137 
138  std::string otherHeaderName = "uncommon-name";
139  HPACKHeaderName name2(otherHeaderName);
140  name1 = name2;
141  EXPECT_EQ(name1.get(), otherHeaderName);
142  EXPECT_EQ(name2.get(), otherHeaderName);
143 
144  HPACKHeaderName name3(testHeaderName);
145  name1 = std::move(name3);
146  EXPECT_EQ(name1.get(), testHeaderName);
147 
148  name1 = otherHeaderName;
149  EXPECT_EQ(name1.get(), otherHeaderName);
150 }
151 
153  // Test size()
154  HPACKHeaderName name1("accept-encoding");
155  HPACKHeaderName name2("uncommon-header_now");
156  EXPECT_EQ(name1.size(), 15);
157  EXPECT_EQ(name2.size(), 19);
158 }
159 
160 TEST_F(HPACKHeaderNameTest, TestOperators) {
161  // Test operators
162  HPACKHeaderName name1("aaa");
163  HPACKHeaderName name2("bbb");
164  HPACKHeaderName name3("aaa");
165  HPACKHeaderName name4("bbb");
166  CHECK(name1 == name3);
167  CHECK(name1 != name2);
168  CHECK(name1 < name2);
169  CHECK(name2 > name1);
170  CHECK(name1 >= name3);
171  CHECK(name2 >= name1);
172  CHECK(name2 <= name4);
173  CHECK(name1 <= name2);
174 }
175 
176 TEST_F(HPACKHeaderNameTest, TestIsCommonHeader) {
177  for (uint64_t j = 0; j < HTTPCommonHeaders::num_header_codes; ++j) {
178  HTTPHeaderCode code = static_cast<HTTPHeaderCode>(j);
179  HPACKHeader testHPACKHeader(
181 
182  bool checkResult = j >= HTTPHeaderCodeCommonOffset;
183  EXPECT_EQ(testHPACKHeader.name.isCommonHeader(), checkResult);
184  }
185  std::string externalHeader = "externalHeader";
186  HPACKHeader testHPACKHeader(externalHeader, "");
187  EXPECT_FALSE(testHPACKHeader.name.isCommonHeader());
188 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
const char * name
Definition: http_parser.c:437
HPACKHeaderName destroyedHPACKHeaderName(std::string name)
uint32_t bytes() const
Definition: HPACKHeader.h:49
virtual bool indexHeader(const HPACKHeader &header) const
static const std::string * getPointerToHeaderName(HTTPHeaderCode code, HTTPCommonHeaderTableType type=TABLE_CAMELCASE)
HPACKHeaderName name
Definition: HPACKHeader.h:82
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hasValue() const
Definition: HPACKHeader.h:78
static constexpr uint64_t num_header_codes
const char * string
Definition: Conv.cpp:212
const uint8_t HTTPHeaderCodeCommonOffset
TEST_F(HeaderTableTests, IndexTranslation)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
const std::string & get() const