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.require: SR000CU2BL
56 * @tc.author: xiuhongju
57 */
58 HWTEST_F(Base64HelperTest, Base64HelperTest_001, TestSize.Level1)
59 {
60 std::vector<uint8_t> emptyArray {};
61 std::vector<uint8_t> oneArray { 'a' };
62 std::vector<uint8_t> twoArray { 'a', 'b' };
63 std::vector<uint8_t> threeArray { 'a', 'b', 'c' };
64 auto result = Base64Helper::Encode(emptyArray);
65 EXPECT_EQ(result, "");
66
67 result = Base64Helper::Encode(oneArray);
68 EXPECT_EQ(result.length(), 4);
69 EXPECT_EQ(result, "YQ==");
70
71 result = Base64Helper::Encode(twoArray);
72 EXPECT_EQ(result.length(), 4);
73 EXPECT_EQ(result, "YWI=");
74
75 result = Base64Helper::Encode(threeArray);
76 EXPECT_EQ(result.length(), 4);
77 EXPECT_EQ(result, "YWJj");
78 }
79
80 /**
81 * @tc.name: Base64HelperTest_002
82 * @tc.desc: normal testcase of base64 decode
83 * @tc.type: FUNC
84 */
85 HWTEST_F(Base64HelperTest, Base64HelperTest_002, TestSize.Level1)
86 {
87 std::vector<uint8_t> oneArray { 1 };
88 std::vector<uint8_t> twoArray { 1, 2 };
89 std::vector<uint8_t> threeArray { 1, 2, 3 };
90 std::vector<uint8_t> result {};
91
92 EXPECT_TRUE(Base64Helper::Decode(Base64Helper::Encode(oneArray), result));
93 EXPECT_EQ(result, oneArray);
94
95 EXPECT_TRUE(Base64Helper::Decode(Base64Helper::Encode(twoArray), result));
96 EXPECT_EQ(result, twoArray);
97
98 EXPECT_TRUE(Base64Helper::Decode(Base64Helper::Encode(threeArray), result));
99 EXPECT_EQ(result, threeArray);
100 }
101
102 /**
103 * @tc.name: Base64HelperTest_003
104 * @tc.desc: error testcase of base64 decode
105 * @tc.type: FUNC
106 */
107 HWTEST_F(Base64HelperTest, Base64HelperTest_003, TestSize.Level1)
108 {
109 std::string wrongText = "abc";
110 std::vector<uint8_t> result {};
111 EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
112
113 wrongText = "@bcd";
114 EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
115
116 wrongText = "a@cd";
117 EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
118
119 wrongText = "ab@d";
120 EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
121
122 wrongText = "abc@";
123 EXPECT_FALSE(Base64Helper::Decode(wrongText, result));
124 }
125
126 /**
127 * @tc.name: Base64HelperTest_004
128 * @tc.desc: error testcase of base64 decode
129 * @tc.type: FUNC
130 */
131 HWTEST_F(Base64HelperTest, Base64HelperTest_004, TestSize.Level1)
132 {
133 std::vector<uint8_t> array;
134 for (size_t i = 0; i < 256; ++i) {
135 array.push_back(i);
136 }
137 std::string encodeStr = Base64Helper::Encode(array);
138 std::vector<uint8_t> decodeArray;
139 EXPECT_TRUE(Base64Helper::Decode(encodeStr, decodeArray));
140 EXPECT_TRUE(array == decodeArray);
141 }
142 }
143