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.
5 #include "base/base64url.h"
7 #include "base/macros.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(Base64UrlTest, EncodeIncludePaddingPolicy) {
16 Base64UrlEncode("hello?world", Base64UrlEncodePolicy::INCLUDE_PADDING,
19 // Base64 version: aGVsbG8/d29ybGQ=
20 EXPECT_EQ("aGVsbG8_d29ybGQ=", output);
22 // Test for behavior for very short and empty strings.
23 Base64UrlEncode("??", Base64UrlEncodePolicy::INCLUDE_PADDING, &output);
24 EXPECT_EQ("Pz8=", output);
26 Base64UrlEncode("", Base64UrlEncodePolicy::INCLUDE_PADDING, &output);
27 EXPECT_EQ("", output);
30 TEST(Base64UrlTest, EncodeOmitPaddingPolicy) {
32 Base64UrlEncode("hello?world", Base64UrlEncodePolicy::OMIT_PADDING, &output);
34 // base64 version: aGVsbG8/d29ybGQ=
35 EXPECT_EQ("aGVsbG8_d29ybGQ", output);
37 // Test for behavior for very short and empty strings.
38 Base64UrlEncode("??", Base64UrlEncodePolicy::OMIT_PADDING, &output);
39 EXPECT_EQ("Pz8", output);
41 Base64UrlEncode("", Base64UrlEncodePolicy::OMIT_PADDING, &output);
42 EXPECT_EQ("", output);
45 TEST(Base64UrlTest, DecodeRequirePaddingPolicy) {
47 ASSERT_TRUE(Base64UrlDecode("aGVsbG8_d29ybGQ=",
48 Base64UrlDecodePolicy::REQUIRE_PADDING, &output));
50 EXPECT_EQ("hello?world", output);
52 ASSERT_FALSE(Base64UrlDecode(
53 "aGVsbG8_d29ybGQ", Base64UrlDecodePolicy::REQUIRE_PADDING, &output));
55 // Test for behavior for very short and empty strings.
57 Base64UrlDecode("Pz8=", Base64UrlDecodePolicy::REQUIRE_PADDING, &output));
58 EXPECT_EQ("??", output);
61 Base64UrlDecode("", Base64UrlDecodePolicy::REQUIRE_PADDING, &output));
62 EXPECT_EQ("", output);
65 TEST(Base64UrlTest, DecodeIgnorePaddingPolicy) {
67 ASSERT_TRUE(Base64UrlDecode("aGVsbG8_d29ybGQ",
68 Base64UrlDecodePolicy::IGNORE_PADDING, &output));
70 EXPECT_EQ("hello?world", output);
72 // Including the padding is accepted as well.
73 ASSERT_TRUE(Base64UrlDecode("aGVsbG8_d29ybGQ=",
74 Base64UrlDecodePolicy::IGNORE_PADDING, &output));
76 EXPECT_EQ("hello?world", output);
79 TEST(Base64UrlTest, DecodeDisallowPaddingPolicy) {
81 ASSERT_FALSE(Base64UrlDecode(
82 "aGVsbG8_d29ybGQ=", Base64UrlDecodePolicy::DISALLOW_PADDING, &output));
84 // The policy will allow the input when padding has been omitted.
85 ASSERT_TRUE(Base64UrlDecode(
86 "aGVsbG8_d29ybGQ", Base64UrlDecodePolicy::DISALLOW_PADDING, &output));
88 EXPECT_EQ("hello?world", output);
91 TEST(Base64UrlTest, DecodeDisallowsBase64Alphabet) {
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));
100 TEST(Base64UrlTest, DecodeDisallowsPaddingOnly) {
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));