• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "base64_helper.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include <string>
21 
22 #include "log_print.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::NativePreferences;
26 namespace {
27 class Base64HelperTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase(void)35 void Base64HelperTest::SetUpTestCase(void)
36 {
37 }
38 
TearDownTestCase(void)39 void Base64HelperTest::TearDownTestCase(void)
40 {
41 }
42 
SetUp(void)43 void Base64HelperTest::SetUp(void)
44 {
45 }
46 
TearDown(void)47 void Base64HelperTest::TearDown(void)
48 {
49 }
50 
51 /**
52  * @tc.name: Base64HelperTest_0001
53  * @tc.desc: normal testcase of base64 encode
54  * @tc.type: FUNC
55  * @tc.author: xiuhongju
56  */
57 HWTEST_F(Base64HelperTest, Base64HelperTest_001, TestSize.Level1)
58 {
59     std::vector<uint8_t> emptyArray {};
60     std::vector<uint8_t> oneArray { 'a' };
61     std::vector<uint8_t> twoArray { 'a', 'b' };
62     std::vector<uint8_t> threeArray { 'a', 'b', 'c' };
63     auto result = Base64Helper::Encode(emptyArray);
64     EXPECT_EQ(result, "");
65 
66     result = Base64Helper::Encode(oneArray);
67     EXPECT_EQ(result.length(), 4);
68     EXPECT_EQ(result, "YQ==");
69 
70     result = Base64Helper::Encode(twoArray);
71     EXPECT_EQ(result.length(), 4);
72     EXPECT_EQ(result, "YWI=");
73 
74     result = Base64Helper::Encode(threeArray);
75     EXPECT_EQ(result.length(), 4);
76     EXPECT_EQ(result, "YWJj");
77 }
78 
79 /**
80  * @tc.name: Base64HelperTest_002
81  * @tc.desc: normal testcase of base64 decode
82  * @tc.type: FUNC
83  */
84 HWTEST_F(Base64HelperTest, Base64HelperTest_002, TestSize.Level1)
85 {
86     std::vector<uint8_t> oneArray { 1 };
87     std::vector<uint8_t> twoArray { 1, 2 };
88     std::vector<uint8_t> threeArray { 1, 2, 3 };
89     std::vector<uint8_t> result {};
90 
91     EXPECT_TRUE(Base64Helper::Decode(Base64Helper::Encode(oneArray), result));
92     EXPECT_EQ(result, oneArray);
93 
94     EXPECT_TRUE(Base64Helper::Decode(Base64Helper::Encode(twoArray), result));
95     EXPECT_EQ(result, twoArray);
96 
97     EXPECT_TRUE(Base64Helper::Decode(Base64Helper::Encode(threeArray), result));
98     EXPECT_EQ(result, threeArray);
99 }
100 
101 /**
102  * @tc.name: Base64HelperTest_003
103  * @tc.desc: error testcase of base64 decode
104  * @tc.type: FUNC
105  */
106 HWTEST_F(Base64HelperTest, Base64HelperTest_003, TestSize.Level1)
107 {
108     std::string wrongText = "abc";
109     std::vector<uint8_t> result {};
110     EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
111 
112     wrongText = "@bcd";
113     EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
114 
115     wrongText = "a@cd";
116     EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
117 
118     wrongText = "ab@d";
119     EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
120 
121     wrongText = "abc@";
122     EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
123 }
124 
125 /**
126  * @tc.name: Base64HelperTest_004
127  * @tc.desc: error testcase of base64 decode
128  * @tc.type: FUNC
129  */
130 HWTEST_F(Base64HelperTest, Base64HelperTest_004, TestSize.Level1)
131 {
132     std::vector<uint8_t> array;
133     for (size_t i = 0; i < 256; ++i) {
134         array.push_back(i);
135     }
136     std::string encodeStr = Base64Helper::Encode(array);
137     std::vector<uint8_t> decodeArray;
138     EXPECT_TRUE(Base64Helper::Decode(encodeStr, decodeArray));
139     EXPECT_TRUE(array == decodeArray);
140 }
141 }
142