1 /* 2 * Copyright (c) 2024 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 <gtest/gtest.h> 17 18 #include <base/containers/string.h> 19 #include <base/containers/string_view.h> 20 #include <base/containers/vector.h> 21 #include <base/util/base64_decode.h> 22 #include <base/util/base64_encode.h> 23 24 using namespace testing::ext; 25 class Base64Test : public testing::Test { 26 public: SetUpTestSuite()27 static void SetUpTestSuite() {} TearDownTestSuite()28 static void TearDownTestSuite() {} SetUp()29 void SetUp() override {} TearDown()30 void TearDown() override {} 31 }; 32 33 constexpr const BASE_NS::string_view DATA[] = { 34 "Many hands make light work.", 35 "light work.", 36 "light work", 37 "light wor", 38 "light wo", 39 "light w", 40 }; 41 42 constexpr const BASE_NS::string_view BASE64[] = { "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu", 43 "bGlnaHQgd29yay4=", "bGlnaHQgd29yaw==", "bGlnaHQgd29y", "bGlnaHQgd28=", "bGlnaHQgdw==" }; 44 45 constexpr const BASE_NS::string_view BAD_DATA[] = { "bad input" }; 46 47 constexpr const BASE_NS::string_view BAD_BASE64[] = { 48 "YmFkICAgaW5wdXQ= " // bad ending 49 }; 50 51 HWTEST_F(Base64Test, Encode, TestSize.Level1) 52 { 53 auto result = BASE64; 54 for (const auto& str : DATA) { 55 BASE_NS::string encoded = BASE_NS::Base64Encode(BASE_NS::array_view<const uint8_t>( 56 static_cast<const uint8_t*>(static_cast<const void*>(str.data())), str.size())); 57 EXPECT_EQ(*result++, encoded); 58 } 59 auto resultBad = BAD_BASE64; 60 for (const auto& str : BAD_DATA) { 61 BASE_NS::string encoded = BASE_NS::Base64Encode(BASE_NS::array_view<const uint8_t>( 62 static_cast<const uint8_t*>(static_cast<const void*>(str.data())), str.size())); 63 BASE_NS::string_view compareValue = *resultBad++; 64 auto trim_pos = compareValue.find(' '); 65 if (trim_pos != compareValue.npos) 66 compareValue.remove_suffix(compareValue.size() - trim_pos); 67 EXPECT_EQ(compareValue, encoded); 68 } 69 } 70 71 HWTEST_F(Base64Test, Decode, TestSize.Level1) 72 { 73 auto result = DATA; 74 for (const auto& str : BASE64) { 75 BASE_NS::vector<uint8_t> decoded = BASE_NS::Base64Decode(str); 76 EXPECT_EQ(*result++, 77 BASE_NS::string_view(static_cast<const char*>(static_cast<const void*>(decoded.data())), decoded.size())); 78 } 79 auto resultBad = BAD_DATA; 80 for (const auto& str : BAD_BASE64) { 81 BASE_NS::vector<uint8_t> decoded = BASE_NS::Base64Decode(str); 82 BASE_NS::string_view compareValue = *resultBad++; 83 auto compareU = BASE_NS::array_view<const uint8_t>( 84 static_cast<const uint8_t*>(static_cast<const void*>(compareValue.data())), compareValue.size()); 85 for (size_t i = 0; i < decoded.size(); i += 3) { 86 if (decoded[i] + decoded[i + 1] + decoded[i + 2] != 0) { 87 for (size_t j = 0; j < 3; j++) { 88 EXPECT_EQ(compareU[j], decoded[j]); 89 } 90 } 91 } 92 } 93 } 94