[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | 978df34 | 2009-11-24 06:21:53 | [diff] [blame] | 5 | #include "base/base64.h" |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 6 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 9 | namespace base { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 10 | |
| 11 | TEST(Base64Test, Basic) { |
| 12 | const std::string kText = "hello world"; |
| 13 | const std::string kBase64Text = "aGVsbG8gd29ybGQ="; |
| 14 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 15 | std::string encoded; |
| 16 | std::string decoded; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 17 | bool ok; |
| 18 | |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 19 | Base64Encode(kText, &encoded); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 20 | EXPECT_EQ(kBase64Text, encoded); |
| 21 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 22 | ok = Base64Decode(encoded, &decoded); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | EXPECT_TRUE(ok); |
| 24 | EXPECT_EQ(kText, decoded); |
| 25 | } |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 26 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 27 | TEST(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 | |
georgesak | 85e05a73 | 2014-11-11 17:19:43 | [diff] [blame] | 41 | TEST(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] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 54 | } // namespace base |