1 // Copyright 2018 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.
4 
5 #include "content/browser/devtools/protocol/base_string_adapter.h"
6 
7 #include <vector>
8 #include "testing/gtest/include/gtest/gtest.h"
9 
10 namespace content {
11 namespace protocol {
12 namespace {
TEST(ProtocolBinaryTest,base64EmptyArgs)13 TEST(ProtocolBinaryTest, base64EmptyArgs) {
14   EXPECT_EQ(protocol::String(), Binary().toBase64());
15 
16   bool success = false;
17   Binary decoded = Binary::fromBase64("", &success);
18   EXPECT_TRUE(success);
19   EXPECT_EQ(
20       std::vector<uint8_t>(),
21       std::vector<uint8_t>(decoded.data(), decoded.data() + decoded.size()));
22 }
23 
TEST(ProtocolStringTest,AllBytesBase64Roundtrip)24 TEST(ProtocolStringTest, AllBytesBase64Roundtrip) {
25   std::vector<uint8_t> all_bytes;
26   for (int ii = 0; ii < 255; ++ii)
27     all_bytes.push_back(ii);
28   Binary binary = Binary::fromVector(all_bytes);
29   bool success = false;
30   Binary decoded = Binary::fromBase64(binary.toBase64(), &success);
31   EXPECT_TRUE(success);
32   std::vector<uint8_t> decoded_bytes(decoded.data(),
33                                      decoded.data() + decoded.size());
34   EXPECT_EQ(all_bytes, decoded_bytes);
35 }
36 
TEST(ProtocolStringTest,HelloWorldBase64Roundtrip)37 TEST(ProtocolStringTest, HelloWorldBase64Roundtrip) {
38   const char* kMsg = "Hello, world.";
39   std::vector<uint8_t> msg(kMsg, kMsg + strlen(kMsg));
40   EXPECT_EQ(strlen(kMsg), msg.size());
41 
42   protocol::String encoded = Binary::fromVector(msg).toBase64();
43   EXPECT_EQ("SGVsbG8sIHdvcmxkLg==", encoded);
44   bool success = false;
45   Binary decoded_binary = Binary::fromBase64(encoded, &success);
46   EXPECT_TRUE(success);
47   std::vector<uint8_t> decoded(decoded_binary.data(),
48                                decoded_binary.data() + decoded_binary.size());
49   EXPECT_EQ(msg, decoded);
50 }
51 
TEST(ProtocolBinaryTest,InvalidBase64Decode)52 TEST(ProtocolBinaryTest, InvalidBase64Decode) {
53   bool success = true;
54   Binary binary = Binary::fromBase64("This is not base64.", &success);
55   EXPECT_FALSE(success);
56 }
57 }  // namespace
58 }  // namespace protocol
59 }  // namespace content
60