proxygen
StructuredHeadersUtilitiesTest.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>
13 
14 namespace proxygen {
15 namespace StructuredHeaders {
16 
18 };
19 
21  for (uint32_t i = 0; i < 256; i++) {
22  uint8_t c = (uint8_t) i;
23  if (c >= 'a' && c <= 'z') {
25  } else {
27  }
28  }
29 }
30 
31 TEST_F(StructuredHeadersUtilitiesTest, TestIsValidIdentifierChar) {
32  for (uint32_t i = 0; i < 256; i++) {
33  uint8_t c = (uint8_t) i;
34  if ((c >= 'a' && c <= 'z') ||
35  (c >= '0' && c <= '9') ||
36  (c == '_' || c == '-' || c == '*' || c == '/')) {
38  } else {
40  }
41  }
42 }
43 
45  test_isValidEncodedBinaryContentChar_alphanumeric) {
50 }
51 
53  test_isValidEncodedBinaryContentChar_allowed_symbols) {
57 }
58 
60  test_isValidEncodedBinaryContentChar_disallowed_symbols) {
65 }
66 
67 TEST_F(StructuredHeadersUtilitiesTest, TestIsValidStringCharAllowed) {
75 }
76 
77 TEST_F(StructuredHeadersUtilitiesTest, TestIsValidStringCharDisallowed) {
82 }
83 
84 TEST_F(StructuredHeadersUtilitiesTest, TestIsValidIdentifierAllowed) {
86  EXPECT_TRUE(isValidIdentifier("a_0-*/"));
87  EXPECT_TRUE(isValidIdentifier("abc___xyz"));
88 }
89 
90 TEST_F(StructuredHeadersUtilitiesTest, TestIsValidIdentifierDisallowed) {
95 }
96 
97 TEST_F(StructuredHeadersUtilitiesTest, TestIsValidStringAllowed) {
98  EXPECT_TRUE(isValidString("a cat."));
99  EXPECT_TRUE(isValidString("!~)($@^^) g"));
100  EXPECT_TRUE(isValidString("\\\"\"\\"));
102 }
103 
104 TEST_F(StructuredHeadersUtilitiesTest, TestIsValidStringDisallowed) {
105  EXPECT_FALSE(isValidString("a\tcat."));
106  EXPECT_FALSE(isValidString("\x10 aaaaaaa"));
107  EXPECT_FALSE(isValidString("chocolate\x11"));
108  EXPECT_FALSE(isValidString("pota\nto"));
109 }
110 
111 TEST_F(StructuredHeadersUtilitiesTest, TestGoodBinaryContent) {
113  EXPECT_TRUE(isValidEncodedBinaryContent("ZGZzZGZmc2Rm"));
115 }
116 
117 TEST_F(StructuredHeadersUtilitiesTest, TestBadBinaryContent) {
122 }
123 
124 TEST_F(StructuredHeadersUtilitiesTest, Test_DecodeBinaryContent) {
125  std::string input1 = "ZnJ1aXQ=";
126  std::string input2 = "dG9tYXRv";
127  std::string input3 = "ZWdncw==";
128  EXPECT_EQ(decodeBase64(input1), "fruit");
129  EXPECT_EQ(decodeBase64(input2), "tomato");
130  EXPECT_EQ(decodeBase64(input3), "eggs");
131 }
132 
133 TEST_F(StructuredHeadersUtilitiesTest, Test_EncodeBinaryContent) {
134  std::string input1 = "fruit";
135  std::string input2 = "tomato";
136  std::string input3 = "eggs";
137  EXPECT_EQ(encodeBase64(input1), "ZnJ1aXQ=");
138  EXPECT_EQ(encodeBase64(input2), "dG9tYXRv");
139  EXPECT_EQ(encodeBase64(input3), "ZWdncw==");
140 }
141 
142 TEST_F(StructuredHeadersUtilitiesTest, Test_BinaryContentEmpty) {
143  std::string input1 = "";
144  std::string input2 = "";
145  EXPECT_EQ(encodeBase64(input1), "");
146  EXPECT_EQ(decodeBase64(input2), "");
147 }
148 
149 TEST_F(StructuredHeadersUtilitiesTest, TestItemTypeMatchesContentGood) {
151  item.value = "\"potato\"";
154 
155  item.value = "a_800";
158 
161 
162  item.value = "hello";
165 
166  item.value = int64_t(88);
169 
170  item.value = double(88.8);
173 }
174 
175 TEST_F(StructuredHeadersUtilitiesTest, TestItemTypeMatchesContentBad) {
177 
178  item.value = "hello";
183 
184  item.value = int64_t(68);
193 
194  item.value = double(68.8);
203 }
204 
205 }
206 }
bool isValidIdentifier(const std::string &s)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F(StructuredHeadersUtilitiesTest, TestLcalpha)
bool itemTypeMatchesContent(const StructuredHeaderItem &input)
std::string encodeBase64(const std::string &input)
boost::variant< int64_t, double, std::string > value
std::string decodeBase64(const std::string &encoded)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
bool isValidEncodedBinaryContent(const std::string &s)
bool isValidString(const std::string &s)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
char c