1 /*
2 * Copyright (c) 2022 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 "ecmascript/tests/test_helper.h"
17 #include "base/pt_base64.h"
18
19 using namespace panda::ecmascript::tooling;
20
21 namespace panda::test {
22 class PtBase64Test : public testing::Test {
23 public:
SetUpTestCase()24 static void SetUpTestCase()
25 {
26 GTEST_LOG_(INFO) << "SetUpTestCase";
27 }
28
TearDownTestCase()29 static void TearDownTestCase()
30 {
31 GTEST_LOG_(INFO) << "TearDownCase";
32 }
33
SetUp()34 void SetUp() override
35 {
36 }
37
TearDown()38 void TearDown() override
39 {
40 }
41 };
42
HWTEST_F_L0(PtBase64Test,ShortTextTest)43 HWTEST_F_L0(PtBase64Test, ShortTextTest)
44 {
45 std::string dec = "Hello";
46 std::string enc;
47 std::string des;
48 uint32_t len = PtBase64::Encode(dec, enc);
49 EXPECT_EQ(static_cast<int>(len), 8);
50 EXPECT_EQ(enc, "SGVsbG8=");
51
52 len = PtBase64::Decode(enc, des);
53 EXPECT_EQ(static_cast<int>(len), 5);
54 EXPECT_EQ(des, "Hello");
55 }
56
HWTEST_F_L0(PtBase64Test,LongTextTest)57 HWTEST_F_L0(PtBase64Test, LongTextTest)
58 {
59 std::string enc = "SWYgeW91IGNhbiBzZWUgdGhpcyBtZXNzYWdlLCBpdCBtZWFucyB0aGF0IFB0QmFzZTY0RGVjb2RlIHdvcmtzIHdlbGw=";
60 std::string dec = "If you can see this message, it means that PtBase64Decode works well";
61 std::string des;
62
63 uint32_t len = PtBase64::Encode(dec, des);
64 EXPECT_EQ(static_cast<int>(len), 92);
65 EXPECT_EQ(enc, des);
66
67 len = PtBase64::Decode(enc, des);
68 EXPECT_EQ(static_cast<int>(len), 68);
69 EXPECT_EQ(des, dec);
70 }
71
HWTEST_F_L0(PtBase64Test,ErrorTextTest)72 HWTEST_F_L0(PtBase64Test, ErrorTextTest)
73 {
74 std::string src = "SGVsbG8==";
75 std::string des;
76 PtBase64 ptBase64;
77 uint32_t len = ptBase64.Decode(src, des);
78 EXPECT_EQ(static_cast<int>(len), 0);
79 EXPECT_EQ(des, "");
80
81 len = PtBase64::Encode("", des);
82 EXPECT_EQ(static_cast<int>(len), 0);
83 EXPECT_EQ(des, "");
84 }
85 }