proxygen
Base64Test.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  */
12 
13 using namespace testing;
14 using namespace proxygen;
15 using std::string;
16 
17 namespace {
18 folly::ByteRange range(const char *str, int64_t len = -1) {
19  return folly::ByteRange((const unsigned char *)str,
20  len >= 0 ? len : strlen(str));
21 }
22 }
23 
24 TEST(Base64, BasicEncode) {
25  EXPECT_EQ(Base64::encode(range("a")), "YQ==");
26  EXPECT_EQ(Base64::encode(range("ab")), "YWI=");
27  EXPECT_EQ(Base64::encode(range("abc")), "YWJj");
28 }
29 
30 TEST(Base64, BasicDecode) {
31  EXPECT_EQ(Base64::decode("YQ==", 2), "a");
32  EXPECT_EQ(Base64::decode("YWI=", 1), "ab");
33  EXPECT_EQ(Base64::decode("YWJj", 0), "abc");
34 }
35 
36 TEST(Base64, Encode) {
37  EXPECT_EQ(Base64::urlEncode(range("hello world")), "aGVsbG8gd29ybGQ");
38 }
39 
40 TEST(Base64, EncodeDecodeNL) {
41  EXPECT_EQ(Base64::urlEncode(range("hello \nworld")), "aGVsbG8gCndvcmxk");
42  EXPECT_EQ(Base64::urlDecode("aGVsbG8gCndvcmxk"), "hello \nworld");
43 }
44 
45 TEST(Base64, Decode) {
46  EXPECT_EQ(Base64::urlDecode("aGVsbG8gd29ybGQ"), "hello world");
47 }
48 
49 TEST(Base64, DecodeEmpty) {
50  EXPECT_EQ(Base64::urlDecode(""), "");
51 }
52 
53 TEST(Base64, DecodeGarbage) {
54  EXPECT_EQ(Base64::urlDecode("[[[[["), "");
55 }
56 
57 TEST(Base64, DecodePadding) {
58  EXPECT_EQ(Base64::urlDecode("YQ"), "a");
59  EXPECT_EQ(Base64::urlDecode("YWE"), "aa");
60  EXPECT_EQ(Base64::urlDecode("YWFh"), "aaa");
61 }
62 
63 TEST(Base64, EncodeDecodeHighBits) {
64  EXPECT_EQ(Base64::urlDecode("_--_"), std::string("\xff\xef\xbf", 3));
65  EXPECT_EQ(Base64::urlEncode(range("\xff\xef\xbf", 3)), "_--_");
66 }
unique_ptr< IOBuf > encode(vector< HPACKHeader > &headers, HPACKEncoder &encoder)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TokenBindingMessage decode(folly::io::Cursor &cursor)
Definition: Types.cpp:132
Gen range(Value begin, Value end)
Definition: Base.h:467
TEST(GTestEnvVarTest, Dummy)
const char * string
Definition: Conv.cpp:212
Range< const unsigned char * > ByteRange
Definition: Range.h:1163