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 
62 
63 }
64