1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/base64url.h"
6 
7 #include "base/macros.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 
10 namespace base {
11 
12 namespace {
13 
TEST(Base64UrlTest,EncodeIncludePaddingPolicy)14 TEST(Base64UrlTest, EncodeIncludePaddingPolicy) {
15   std::string output;
16   Base64UrlEncode("hello?world", Base64UrlEncodePolicy::INCLUDE_PADDING,
17                   &output);
18 
19   // Base64 version: aGVsbG8/d29ybGQ=
20   EXPECT_EQ("aGVsbG8_d29ybGQ=", output);
21 
22   // Test for behavior for very short and empty strings.
23   Base64UrlEncode("??", Base64UrlEncodePolicy::INCLUDE_PADDING, &output);
24   EXPECT_EQ("Pz8=", output);
25 
26   Base64UrlEncode("", Base64UrlEncodePolicy::INCLUDE_PADDING, &output);
27   EXPECT_EQ("", output);
28 }
29 
TEST(Base64UrlTest,EncodeOmitPaddingPolicy)30 TEST(Base64UrlTest, EncodeOmitPaddingPolicy) {
31   std::string output;
32   Base64UrlEncode("hello?world", Base64UrlEncodePolicy::OMIT_PADDING, &output);
33 
34   // base64 version: aGVsbG8/d29ybGQ=
35   EXPECT_EQ("aGVsbG8_d29ybGQ", output);
36 
37   // Test for behavior for very short and empty strings.
38   Base64UrlEncode("??", Base64UrlEncodePolicy::OMIT_PADDING, &output);
39   EXPECT_EQ("Pz8", output);
40 
41   Base64UrlEncode("", Base64UrlEncodePolicy::OMIT_PADDING, &output);
42   EXPECT_EQ("", output);
43 }
44 
TEST(Base64UrlTest,DecodeRequirePaddingPolicy)45 TEST(Base64UrlTest, DecodeRequirePaddingPolicy) {
46   std::string output;
47   ASSERT_TRUE(Base64UrlDecode("aGVsbG8_d29ybGQ=",
48                               Base64UrlDecodePolicy::REQUIRE_PADDING, &output));
49 
50   EXPECT_EQ("hello?world", output);
51 
52   ASSERT_FALSE(Base64UrlDecode(
53       "aGVsbG8_d29ybGQ", Base64UrlDecodePolicy::REQUIRE_PADDING, &output));
54 
55   // Test for behavior for very short and empty strings.
56   ASSERT_TRUE(
57       Base64UrlDecode("Pz8=", Base64UrlDecodePolicy::REQUIRE_PADDING, &output));
58   EXPECT_EQ("??", output);
59 
60   ASSERT_TRUE(
61       Base64UrlDecode("", Base64UrlDecodePolicy::REQUIRE_PADDING, &output));
62   EXPECT_EQ("", output);
63 }
64 
TEST(Base64UrlTest,DecodeIgnorePaddingPolicy)65 TEST(Base64UrlTest, DecodeIgnorePaddingPolicy) {
66   std::string output;
67   ASSERT_TRUE(Base64UrlDecode("aGVsbG8_d29ybGQ",
68                               Base64UrlDecodePolicy::IGNORE_PADDING, &output));
69 
70   EXPECT_EQ("hello?world", output);
71 
72   // Including the padding is accepted as well.
73   ASSERT_TRUE(Base64UrlDecode("aGVsbG8_d29ybGQ=",
74                               Base64UrlDecodePolicy::IGNORE_PADDING, &output));
75 
76   EXPECT_EQ("hello?world", output);
77 }
78 
TEST(Base64UrlTest,DecodeDisallowPaddingPolicy)79 TEST(Base64UrlTest, DecodeDisallowPaddingPolicy) {
80   std::string output;
81   ASSERT_FALSE(Base64UrlDecode(
82       "aGVsbG8_d29ybGQ=", Base64UrlDecodePolicy::DISALLOW_PADDING, &output));
83 
84   // The policy will allow the input when padding has been omitted.
85   ASSERT_TRUE(Base64UrlDecode(
86       "aGVsbG8_d29ybGQ", Base64UrlDecodePolicy::DISALLOW_PADDING, &output));
87 
88   EXPECT_EQ("hello?world", output);
89 }
90 
TEST(Base64UrlTest,DecodeDisallowsBase64Alphabet)91 TEST(Base64UrlTest, DecodeDisallowsBase64Alphabet) {
92   std::string output;
93 
94   // The "/" character is part of the conventional base64 alphabet, but has been
95   // substituted with "_" in the base64url alphabet.
96   ASSERT_FALSE(Base64UrlDecode(
97       "aGVsbG8/d29ybGQ=", Base64UrlDecodePolicy::REQUIRE_PADDING, &output));
98 }
99 
TEST(Base64UrlTest,DecodeDisallowsPaddingOnly)100 TEST(Base64UrlTest, DecodeDisallowsPaddingOnly) {
101   std::string output;
102 
103   ASSERT_FALSE(Base64UrlDecode(
104       "=", Base64UrlDecodePolicy::IGNORE_PADDING, &output));
105   ASSERT_FALSE(Base64UrlDecode(
106       "==", Base64UrlDecodePolicy::IGNORE_PADDING, &output));
107   ASSERT_FALSE(Base64UrlDecode(
108       "===", Base64UrlDecodePolicy::IGNORE_PADDING, &output));
109   ASSERT_FALSE(Base64UrlDecode(
110       "====", Base64UrlDecodePolicy::IGNORE_PADDING, &output));
111 }
112 
113 }  // namespace
114 
115 }  // namespace base
116