• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <string>
18 #include <vector>
19 
20 #include <gtest/gtest.h>
21 
22 #include "common/libs/utils/base64.h"
23 
24 namespace cuttlefish {
25 
TEST(Base64Test,EncodeMult3)26 TEST(Base64Test, EncodeMult3) {
27   std::string in = "foobar";
28   std::string expected("Zm9vYmFy");
29   std::string out;
30   ASSERT_TRUE(EncodeBase64(in.c_str(), in.size(), &out));
31   ASSERT_EQ(out.size(), expected.size());
32   ASSERT_EQ(out, expected);
33 }
34 
TEST(Base64Test,EncodeNonMult3)35 TEST(Base64Test, EncodeNonMult3) {
36   std::string in = "foobar1";
37   std::string expected("Zm9vYmFyMQ==");
38   std::string out;
39   ASSERT_TRUE(EncodeBase64(in.c_str(), in.size(), &out));
40    ASSERT_EQ(out.size(), expected.size());
41   ASSERT_EQ(out, expected);
42 }
43 
TEST(Base64Test,DecodeMult3)44 TEST(Base64Test, DecodeMult3) {
45   std::string in = "Zm9vYmFy";
46   std::vector<uint8_t> expected{'f','o','o','b','a','r'};
47   std::vector<uint8_t> out;
48   ASSERT_TRUE(DecodeBase64(in, &out));
49   ASSERT_EQ(out.size(), expected.size());
50   ASSERT_EQ(out, expected);
51 }
52 
TEST(Base64Test,DecodeNonMult3)53 TEST(Base64Test, DecodeNonMult3) {
54   std::string in = "Zm9vYmFyMQ==";
55   std::vector<uint8_t> expected{'f','o','o','b','a','r','1'};
56   std::vector<uint8_t> out;
57   ASSERT_TRUE(DecodeBase64(in, &out));
58   ASSERT_EQ(out.size(), expected.size());
59   ASSERT_EQ(out, expected);
60 }
61 
TEST(Base64Test,EncodeOneZero)62 TEST(Base64Test, EncodeOneZero) {
63   std::vector<uint8_t> in = {0};
64   std::string string_encoding;
65 
66   ASSERT_TRUE(EncodeBase64(in.data(), in.size(), &string_encoding));
67 
68   std::vector<uint8_t> out;
69   ASSERT_TRUE(DecodeBase64(string_encoding, &out));
70 
71   ASSERT_EQ(in, out);
72 }
73 
TEST(Base64Test,EncodeTwoZeroes)74 TEST(Base64Test, EncodeTwoZeroes) {
75   std::vector<uint8_t> in = {0, 0};
76   std::string string_encoding;
77 
78   ASSERT_TRUE(EncodeBase64(in.data(), in.size(), &string_encoding));
79 
80   std::vector<uint8_t> out;
81   ASSERT_TRUE(DecodeBase64(string_encoding, &out));
82 
83   ASSERT_EQ(in, out);
84 }
85 
TEST(Base64Test,EncodeThreeZeroes)86 TEST(Base64Test, EncodeThreeZeroes) {
87   std::vector<uint8_t> in = {0, 0, 0};
88   std::string string_encoding;
89 
90   ASSERT_TRUE(EncodeBase64(in.data(), in.size(), &string_encoding));
91 
92   std::vector<uint8_t> out;
93   ASSERT_TRUE(DecodeBase64(string_encoding, &out));
94 
95   ASSERT_EQ(in, out);
96 }
97 }
98