Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / base64url_unittest.cc
index 45aa4a8..92d2781 100644 (file)
@@ -1,16 +1,78 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
+// Copyright 2015 The Chromium Authors
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
 #include "base/base64url.h"
 
-#include "base/macros.h"
+#include "base/ranges/algorithm.h"
+#include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using testing::ElementsAreArray;
+using testing::Optional;
+
 namespace base {
 
 namespace {
 
+TEST(Base64UrlTest, BinaryIncludePaddingPolicy) {
+  const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF};
+
+  std::string binary_encoded_with_padding;
+  Base64UrlEncode(kData, Base64UrlEncodePolicy::INCLUDE_PADDING,
+                  &binary_encoded_with_padding);
+
+  // Check that encoding the same binary data through the StringPiece interface
+  // gives the same result.
+  std::string string_encoded_with_padding;
+  Base64UrlEncode(
+      StringPiece(reinterpret_cast<const char*>(kData), sizeof(kData)),
+      Base64UrlEncodePolicy::INCLUDE_PADDING, &string_encoded_with_padding);
+  EXPECT_EQ(binary_encoded_with_padding, string_encoded_with_padding);
+
+  // Check that decoding the result gives the same binary data.
+  EXPECT_THAT(Base64UrlDecode(string_encoded_with_padding,
+                              Base64UrlDecodePolicy::REQUIRE_PADDING),
+              Optional(ElementsAreArray(kData)));
+
+  EXPECT_THAT(Base64UrlDecode(string_encoded_with_padding,
+                              Base64UrlDecodePolicy::IGNORE_PADDING),
+              Optional(ElementsAreArray(kData)));
+
+  EXPECT_THAT(Base64UrlDecode(string_encoded_with_padding,
+                              Base64UrlDecodePolicy::DISALLOW_PADDING),
+              absl::nullopt);
+}
+
+TEST(Base64UrlTest, BinaryOmitPaddingPolicy) {
+  const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF};
+
+  std::string binary_encoded_without_padding;
+  Base64UrlEncode(kData, Base64UrlEncodePolicy::OMIT_PADDING,
+                  &binary_encoded_without_padding);
+
+  // Check that encoding the same binary data through the StringPiece interface
+  // gives the same result.
+  std::string string_encoded_without_padding;
+  Base64UrlEncode(
+      StringPiece(reinterpret_cast<const char*>(kData), sizeof(kData)),
+      Base64UrlEncodePolicy::OMIT_PADDING, &string_encoded_without_padding);
+  EXPECT_EQ(binary_encoded_without_padding, string_encoded_without_padding);
+
+  // Check that decoding the result gives the same binary data.
+  EXPECT_THAT(Base64UrlDecode(string_encoded_without_padding,
+                              Base64UrlDecodePolicy::DISALLOW_PADDING),
+              Optional(ElementsAreArray(kData)));
+
+  EXPECT_THAT(Base64UrlDecode(string_encoded_without_padding,
+                              Base64UrlDecodePolicy::IGNORE_PADDING),
+              Optional(ElementsAreArray(kData)));
+
+  EXPECT_THAT(Base64UrlDecode(string_encoded_without_padding,
+                              Base64UrlDecodePolicy::REQUIRE_PADDING),
+              absl::nullopt);
+}
+
 TEST(Base64UrlTest, EncodeIncludePaddingPolicy) {
   std::string output;
   Base64UrlEncode("hello?world", Base64UrlEncodePolicy::INCLUDE_PADDING,
@@ -76,6 +138,16 @@ TEST(Base64UrlTest, DecodeIgnorePaddingPolicy) {
   EXPECT_EQ("hello?world", output);
 }
 
+TEST(Base64UrlTest, DecodeIntoVector) {
+  ASSERT_FALSE(
+      Base64UrlDecode("invalid=", Base64UrlDecodePolicy::DISALLOW_PADDING));
+
+  static constexpr uint8_t kExpected[] = {'1', '2', '3', '4'};
+  absl::optional<std::vector<uint8_t>> result =
+      Base64UrlDecode("MTIzNA", Base64UrlDecodePolicy::DISALLOW_PADDING);
+  ASSERT_TRUE(ranges::equal(*result, kExpected));
+}
+
 TEST(Base64UrlTest, DecodeDisallowPaddingPolicy) {
   std::string output;
   ASSERT_FALSE(Base64UrlDecode(