proxygen
StructuredHeadersBufferTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004-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  */
11 #include <string>
14 #include <common/encode/Base64.h>
15 
16 namespace proxygen{
17 
18 using namespace facebook;
19 
21 };
22 
23 TEST_F(StructuredHeadersBufferTest, TestBinaryContent) {
24  std::string input = "*bWF4aW0gaXMgdGhlIGJlc3Q=*";
25  StructuredHeadersBuffer shd(input);
27  auto err = shd.parseItem(output);
29  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::BINARYCONTENT);
30  EXPECT_EQ(output, std::string("maxim is the best"));
31 }
32 
33 TEST_F(StructuredHeadersBufferTest, TestBinaryContentIllegalCharacters) {
34  std::string input = "*()645\t this is not a b64 encoded string ((({]}}}))*";
35  StructuredHeadersBuffer shd(input);
37  auto err = shd.parseItem(output);
39 }
40 
41 TEST_F(StructuredHeadersBufferTest, TestBinaryContentNoEndingAsterisk) {
42  std::string input = "*seattle";
43  StructuredHeadersBuffer shd(input);
45  auto err = shd.parseItem(output);
47 }
48 
49 TEST_F(StructuredHeadersBufferTest, TestBinaryContentEmpty) {
50  std::string input = "**";
51  StructuredHeadersBuffer shd(input);
53  auto err = shd.parseItem(output);
55  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::BINARYCONTENT);
56  EXPECT_EQ(output, std::string(""));
57 }
58 
60  std::string input = "abcdefg";
61  StructuredHeadersBuffer shd(input);
63  auto err = shd.parseIdentifier(output);
65  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::IDENTIFIER);
66  EXPECT_EQ(output, std::string("abcdefg"));
67 }
68 
69 TEST_F(StructuredHeadersBufferTest, TestIdentifierAllLegalCharacters) {
70  std::string input = "a0_-*/";
71  StructuredHeadersBuffer shd(input);
73  auto err = shd.parseIdentifier(output);
75  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::IDENTIFIER);
76  EXPECT_EQ(output, std::string("a0_-*/"));
77 }
78 
79 TEST_F(StructuredHeadersBufferTest, TestIdentifierBeginningUnderscore) {
80  std::string input = "_af09d____****";
81  StructuredHeadersBuffer shd(input);
83  auto err = shd.parseIdentifier(output);
85 }
86 
88  std::string input = "\"fsdfsdf\"sdfsdf\"";
89  StructuredHeadersBuffer shd(input);
91  auto err = shd.parseItem(output);
94  EXPECT_EQ(output, std::string("fsdfsdf"));
95 }
96 
97 TEST_F(StructuredHeadersBufferTest, TestStringEscapedQuote) {
98  std::string input = "\"abc\\\"def\"";
99  StructuredHeadersBuffer shd(input);
101  auto err = shd.parseItem(output);
104  EXPECT_EQ(output, std::string("abc\"def"));
105 }
106 
107 TEST_F(StructuredHeadersBufferTest, TestStringEscapedBackslash) {
108  std::string input = "\"abc\\\\def\"";
109  StructuredHeadersBuffer shd(input);
111  auto err = shd.parseItem(output);
114  EXPECT_EQ(output, std::string("abc\\def"));
115 }
116 
117 TEST_F(StructuredHeadersBufferTest, TestStringStrayBackslash) {
118  std::string input = "\"abc\\def\"";
119  StructuredHeadersBuffer shd(input);
121  auto err = shd.parseItem(output);
123 }
124 
125 TEST_F(StructuredHeadersBufferTest, TestStringInvalidCharacter) {
126  std::string input = "\"abcdefg\thij\"";
127  StructuredHeadersBuffer shd(input);
129  auto err = shd.parseItem(output);
131 }
132 
133 TEST_F(StructuredHeadersBufferTest, TestStringParsingRepeated) {
134  std::string input = "\"proxy\"\"gen\"";
135  StructuredHeadersBuffer shd(input);
137  auto err = shd.parseItem(output);
140  EXPECT_EQ(output, std::string("proxy"));
141 
142  err = shd.parseItem(output);
145  EXPECT_EQ(output, std::string("gen"));
146 }
147 
149  std::string input = "843593";
150  StructuredHeadersBuffer shd(input);
152  auto err = shd.parseItem(output);
154  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::INT64);
155  EXPECT_EQ(output, int64_t(843593));
156 }
157 
158 TEST_F(StructuredHeadersBufferTest, TestIntegerTwoNegatives) {
159  std::string input = "--843593";
160  StructuredHeadersBuffer shd(input);
162  auto err = shd.parseItem(output);
164 }
165 
166 TEST_F(StructuredHeadersBufferTest, TestIntegerEmptyAfterNegative) {
167  std::string input = "-";
168  StructuredHeadersBuffer shd(input);
170  auto err = shd.parseItem(output);
172 }
173 
174 TEST_F(StructuredHeadersBufferTest, TestIntegerNegative) {
175  std::string input = "-843593";
176  StructuredHeadersBuffer shd(input);
178  auto err = shd.parseItem(output);
180  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::INT64);
181  EXPECT_EQ(output, int64_t(-843593));
182 }
183 
184 TEST_F(StructuredHeadersBufferTest, TestIntegerOverflow) {
185  std::string input = "9223372036854775808";
186  StructuredHeadersBuffer shd(input);
188  auto err = shd.parseItem(output);
190 }
191 
192 TEST_F(StructuredHeadersBufferTest, TestIntegerHighBorderline) {
193  std::string input = "9223372036854775807";
194  StructuredHeadersBuffer shd(input);
196  auto err = shd.parseItem(output);
198  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::INT64);
200 }
201 
202 TEST_F(StructuredHeadersBufferTest, TestIntegerLowBorderline) {
203  std::string input = "-9223372036854775808";
204  StructuredHeadersBuffer shd(input);
206  auto err = shd.parseItem(output);
208  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::INT64);
210 }
211 
212 TEST_F(StructuredHeadersBufferTest, TestIntegerUnderflow) {
213  std::string input = "-9223372036854775809";
214  StructuredHeadersBuffer shd(input);
216  auto err = shd.parseItem(output);
218 }
219 
221  std::string input = "3.1415926536";
222  StructuredHeadersBuffer shd(input);
224  auto err = shd.parseItem(output);
226  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::DOUBLE);
227  EXPECT_EQ(output, 3.1415926536);
228 }
229 
230 TEST_F(StructuredHeadersBufferTest, TestFloatPrecedingWhitespace) {
231  std::string input = " \t\t 66000.5645";
232  StructuredHeadersBuffer shd(input);
234  auto err = shd.parseItem(output);
236  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::DOUBLE);
237  EXPECT_EQ(output, 66000.5645);
238 }
239 
240 TEST_F(StructuredHeadersBufferTest, TestFloatNoDigitPrecedingDecimal) {
241  std::string input = ".1415926536";
242  StructuredHeadersBuffer shd(input);
244  auto err = shd.parseItem(output);
246 }
247 
248 TEST_F(StructuredHeadersBufferTest, TestIntegerTooManyChars) {
249  std::string input = "10000000000000000000"; // has 20 characters
250  StructuredHeadersBuffer shd(input);
252  auto err = shd.parseItem(output);
254 }
255 
256 TEST_F(StructuredHeadersBufferTest, TestFloatTooManyChars) {
257  std::string input = "111111111.1111111"; // has 17 characters
258  StructuredHeadersBuffer shd(input);
260  auto err = shd.parseItem(output);
262 }
263 
264 TEST_F(StructuredHeadersBufferTest, TestFloatBorderlineNumChars) {
265  std::string input = "111111111.111111"; // has 16 characters
266  StructuredHeadersBuffer shd(input);
268  auto err = shd.parseItem(output);
269  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::DOUBLE);
271 }
272 
273 TEST_F(StructuredHeadersBufferTest, TestFloatEndsWithDecimal) {
274  std::string input = "100.";
275  StructuredHeadersBuffer shd(input);
277  auto err = shd.parseItem(output);
279 }
280 
282  std::string input = ",5345346";
283  StructuredHeadersBuffer shd(input);
285  shd.removeSymbol(",", true);
286  auto err = shd.parseItem(output);
288  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::INT64);
289  EXPECT_EQ(output, int64_t(5345346));
290 }
291 
292 TEST_F(StructuredHeadersBufferTest, TestConsumeEquals) {
293  std::string input = "=456346.646";
294  StructuredHeadersBuffer shd(input);
296  shd.removeSymbol("=", true);
297  auto err = shd.parseItem(output);
299  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::DOUBLE);
300  EXPECT_EQ(output, 456346.646);
301 }
302 
304  std::string input = "asfgsdfg,asfgsdfg,";
305  StructuredHeadersBuffer shd(input);
306  for (int i = 0; i < 2; i++) {
308  auto err = shd.parseIdentifier(output);
310  EXPECT_EQ(output.tag, StructuredHeaderItem::Type::IDENTIFIER);
311  EXPECT_EQ(output, std::string("asfgsdfg"));
312  shd.removeSymbol(",", true);
313  }
314 }
315 
316 TEST_F(StructuredHeadersBufferTest, TestInequalityOperator) {
317  StructuredHeaderItem integerItem;
318  integerItem.value = int64_t(999);
319 
320  StructuredHeaderItem doubleItem;
321  doubleItem.value = 11.43;
322 
323  StructuredHeaderItem stringItem;
324  stringItem.value = std::string("hi");
325 
326  EXPECT_NE(integerItem, int64_t(998));
327  EXPECT_NE(doubleItem, double(11.44));
328  EXPECT_NE(stringItem, std::string("bye"));
329 }
330 
331 }
DecodeError removeSymbol(const std::string &symbol, bool strict)
LogLevel max
Definition: LogLevel.cpp:31
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define STRING
LogLevel min
Definition: LogLevel.cpp:30
boost::variant< int64_t, double, std::string > value
StructuredHeaders::DecodeError parseItem(StructuredHeaderItem &result)
const char * string
Definition: Conv.cpp:212
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST_F(HeaderTableTests, IndexTranslation)
StructuredHeaders::DecodeError parseIdentifier(StructuredHeaderItem &result)