1 // Copyright (c) 2012 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/base64.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(Base64Test, Basic) {
12 const std::string kText = "hello world";
13 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
19 Base64Encode(kText, &encoded);
20 EXPECT_EQ(kBase64Text, encoded);
22 ok = Base64Decode(encoded, &decoded);
24 EXPECT_EQ(kText, decoded);
27 TEST(Base64Test, Binary) {
28 const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF};
30 std::string binary_encoded = Base64Encode(make_span(kData));
32 // Check that encoding the same data through the StringPiece interface gives
34 std::string string_piece_encoded;
35 Base64Encode(StringPiece(reinterpret_cast<const char*>(kData), sizeof(kData)),
36 &string_piece_encoded);
38 EXPECT_EQ(binary_encoded, string_piece_encoded);
41 TEST(Base64Test, InPlace) {
42 const std::string kText = "hello world";
43 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
44 std::string text(kText);
46 Base64Encode(text, &text);
47 EXPECT_EQ(kBase64Text, text);
49 bool ok = Base64Decode(text, &text);
51 EXPECT_EQ(text, kText);