|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/util/base64.h" |
| 19 | +#include "arrow/testing/gtest_util.h" |
| 20 | + |
| 21 | +namespace arrow { |
| 22 | +namespace util { |
| 23 | + |
| 24 | +TEST(Base64DecodeTest, ValidInputs) { |
| 25 | + ASSERT_OK_AND_ASSIGN(auto empty, base64_decode("")); |
| 26 | + EXPECT_EQ(empty, ""); |
| 27 | + |
| 28 | + ASSERT_OK_AND_ASSIGN(auto two_paddings, base64_decode("Zg==")); |
| 29 | + EXPECT_EQ(two_paddings, "f"); |
| 30 | + |
| 31 | + ASSERT_OK_AND_ASSIGN(auto one_padding, base64_decode("Zm8=")); |
| 32 | + EXPECT_EQ(one_padding, "fo"); |
| 33 | + |
| 34 | + ASSERT_OK_AND_ASSIGN(auto no_padding, base64_decode("Zm9v")); |
| 35 | + EXPECT_EQ(no_padding, "foo"); |
| 36 | + |
| 37 | + ASSERT_OK_AND_ASSIGN(auto multiblock, base64_decode("SGVsbG8gd29ybGQ=")); |
| 38 | + EXPECT_EQ(multiblock, "Hello world"); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(Base64DecodeTest, BinaryOutput) { |
| 42 | + // 'A' maps to index 0 — same zero value used for padding slots |
| 43 | + // verifies the 'A' bug is not present |
| 44 | + ASSERT_OK_AND_ASSIGN(auto all_A, base64_decode("AAAA")); |
| 45 | + EXPECT_EQ(all_A, std::string("\x00\x00\x00", 3)); |
| 46 | + |
| 47 | + // Arbitrary non-ASCII output bytes |
| 48 | + ASSERT_OK_AND_ASSIGN(auto binary, base64_decode("AP8A")); |
| 49 | + EXPECT_EQ(binary, std::string("\x00\xff\x00", 3)); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(Base64DecodeTest, InvalidLength) { |
| 53 | + ASSERT_RAISES_WITH_MESSAGE( |
| 54 | + Invalid, "Invalid: Invalid base64 input: length is not a multiple of 4", |
| 55 | + base64_decode("abc")); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(Base64DecodeTest, InvalidCharacters) { |
| 59 | + ASSERT_RAISES_WITH_MESSAGE( |
| 60 | + Invalid, "Invalid: Invalid base64 input: character is not valid base64 character", |
| 61 | + base64_decode("ab$=")); |
| 62 | + |
| 63 | + // Non-ASCII byte |
| 64 | + std::string non_ascii = std::string("abc") + static_cast<char>(0xFF); |
| 65 | + ASSERT_RAISES_WITH_MESSAGE( |
| 66 | + Invalid, "Invalid: Invalid base64 input: character is not valid base64 character", |
| 67 | + base64_decode(non_ascii)); |
| 68 | + |
| 69 | + // Corruption mid-string across multiple blocks |
| 70 | + ASSERT_RAISES_WITH_MESSAGE( |
| 71 | + Invalid, "Invalid: Invalid base64 input: character is not valid base64 character", |
| 72 | + base64_decode("aGVs$G8gd29ybGQ=")); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(Base64DecodeTest, InvalidPadding) { |
| 76 | + // Padding in wrong position within block |
| 77 | + ASSERT_RAISES_WITH_MESSAGE(Invalid, |
| 78 | + "Invalid: Invalid base64 input: padding in wrong position", |
| 79 | + base64_decode("ab=c")); |
| 80 | + |
| 81 | + // 3 padding characters — exceeds maximum of 2 |
| 82 | + ASSERT_RAISES_WITH_MESSAGE(Invalid, |
| 83 | + "Invalid: Invalid base64 input: too many padding characters", |
| 84 | + base64_decode("a===")); |
| 85 | + |
| 86 | + // 4 padding characters |
| 87 | + ASSERT_RAISES_WITH_MESSAGE(Invalid, |
| 88 | + "Invalid: Invalid base64 input: too many padding characters", |
| 89 | + base64_decode("====")); |
| 90 | + |
| 91 | + // Padding in non-final block across multiple blocks |
| 92 | + ASSERT_RAISES_WITH_MESSAGE(Invalid, |
| 93 | + "Invalid: Invalid base64 input: padding in wrong position", |
| 94 | + base64_decode("Zm8=Zm8=")); |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace util |
| 98 | +} // namespace arrow |
0 commit comments