blob: 2a27f8b6f617f205a9a6bb74905591ac894db8d0 [file] [log] [blame]
[email protected]24f111a2012-09-23 04:51:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]978df342009-11-24 06:21:535#include "base/base64.h"
[email protected]24f111a2012-09-23 04:51:046
initial.commit586acc5fe2008-07-26 22:42:527#include "testing/gtest/include/gtest/gtest.h"
8
[email protected]24f111a2012-09-23 04:51:049namespace base {
initial.commit586acc5fe2008-07-26 22:42:5210
11TEST(Base64Test, Basic) {
12 const std::string kText = "hello world";
13 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
14
[email protected]24f111a2012-09-23 04:51:0415 std::string encoded;
16 std::string decoded;
initial.commit586acc5fe2008-07-26 22:42:5217 bool ok;
18
[email protected]33fca122013-12-11 01:48:5019 Base64Encode(kText, &encoded);
initial.commit586acc5fe2008-07-26 22:42:5220 EXPECT_EQ(kBase64Text, encoded);
21
[email protected]24f111a2012-09-23 04:51:0422 ok = Base64Decode(encoded, &decoded);
initial.commit586acc5fe2008-07-26 22:42:5223 EXPECT_TRUE(ok);
24 EXPECT_EQ(kText, decoded);
25}
[email protected]24f111a2012-09-23 04:51:0426
Collin Bakere21f723d2019-09-05 20:05:4127TEST(Base64Test, Binary) {
28 const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF};
29
30 std::string binary_encoded = Base64Encode(make_span(kData));
31
32 // Check that encoding the same data through the StringPiece interface gives
33 // the same results.
34 std::string string_piece_encoded;
35 Base64Encode(StringPiece(reinterpret_cast<const char*>(kData), sizeof(kData)),
36 &string_piece_encoded);
37
38 EXPECT_EQ(binary_encoded, string_piece_encoded);
39}
40
georgesak85e05a732014-11-11 17:19:4341TEST(Base64Test, InPlace) {
42 const std::string kText = "hello world";
43 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
44 std::string text(kText);
45
46 Base64Encode(text, &text);
47 EXPECT_EQ(kBase64Text, text);
48
49 bool ok = Base64Decode(text, &text);
50 EXPECT_TRUE(ok);
51 EXPECT_EQ(text, kText);
52}
53
[email protected]24f111a2012-09-23 04:51:0454} // namespace base