From 8107d33193d511b66bf36060e944bac157154e70 Mon Sep 17 00:00:00 2001 From: Joe Abraham Date: Wed, 18 Sep 2024 19:02:09 +0530 Subject: [PATCH] Update test cases --- velox/common/encode/Base32.h | 6 + velox/common/encode/tests/Base32Test.cpp | 288 ++++++++---------- .../prestosql/tests/BinaryFunctionsTest.cpp | 4 +- 3 files changed, 130 insertions(+), 168 deletions(-) diff --git a/velox/common/encode/Base32.h b/velox/common/encode/Base32.h index 2e6de2518c2d0..5d38b0565a907 100644 --- a/velox/common/encode/Base32.h +++ b/velox/common/encode/Base32.h @@ -69,6 +69,12 @@ class Base32 { char* output, size_t outputSize, const Base32::ReverseIndex& reverseIndex); + + VELOX_FRIEND_TEST(Base32Test, Base32ReverseLookupValidChar); + VELOX_FRIEND_TEST(Base32Test, Base32ReverseLookupInvalidChar); + VELOX_FRIEND_TEST(Base32Test, DecodeImplValidInput); + VELOX_FRIEND_TEST(Base32Test, DecodeImplInvalidInputLength); + VELOX_FRIEND_TEST(Base32Test, DecodeImplOutputBufferTooSmall); }; } // namespace facebook::velox::encoding diff --git a/velox/common/encode/tests/Base32Test.cpp b/velox/common/encode/tests/Base32Test.cpp index 42411906c4fac..b2d8a063cd063 100644 --- a/velox/common/encode/tests/Base32Test.cpp +++ b/velox/common/encode/tests/Base32Test.cpp @@ -1,166 +1,122 @@ -///* -// * Copyright (c) Facebook, Inc. and its affiliates. -// * -// * Licensed under the Apache License, Version 2.0 (the "License"); -// * you may not use this file except in compliance with the License. -// * You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ -// -// #include "velox/common/encode/Base32.h" -// -// #include -// #include "velox/common/base/Status.h" -// #include "velox/common/base/tests/GTestUtils.h" -// -// namespace facebook::velox::encoding { -// -// class Base32Test : public ::testing::Test { -// protected: -// void checkDecodedSize( -// const std::string& encodedString, -// size_t expectedEncodedSize, -// size_t expectedDecodedSize) { -// size_t encodedSize = expectedEncodedSize; -// size_t decodedSize = 0; -// EXPECT_EQ( -// Status::OK(), -// Base32::calculateDecodedSize(encodedString, encodedSize, -// decodedSize)); -// EXPECT_EQ(expectedEncodedSize, encodedSize); -// EXPECT_EQ(expectedDecodedSize, decodedSize); -// } -//}; -// -// TEST_F(Base32Test, fromBase32) { -// EXPECT_EQ("Hello, World!", Base32::decode("NBSWY3DPEB3W64TMMQ======")); -// EXPECT_EQ("Base32 encoding is fun.", -// Base32::decode("JBSWY3DPFQQHO33SNRSCC===...")); EXPECT_EQ("Simple text", -// Base32::decode("MFRGG====")); EXPECT_EQ("1234567890", -// Base32::decode("AEJOM2RSGQ======")); -// -// // Check encoded strings without padding -// EXPECT_EQ("Hello, World!", Base32::decode("NBSWY3DPEB3W64TMMQ")); -// EXPECT_EQ("Base32 encoding is fun.", -// Base32::decode("JBSWY3DPFQQHO33SNRSCC...")); EXPECT_EQ("Simple text", -// Base32::decode("MFRGG")); EXPECT_EQ("1234567890", -// Base32::decode("AEJOM2RSGQ")); -//} -// -// TEST_F(Base32Test, calculateDecodedSizeProperSize) { -// checkDecodedSize("NBSWY3DPEB3W64TMMQ======", 24, 18); -// checkDecodedSize("NBSWY3DPEB3W64TMMQ", 18, 18); -// checkDecodedSize("MFRA====", 8, 4); -// checkDecodedSize("MFRGG===", 8, 5); -// checkDecodedSize("AEJOM2RSGQ======", 14, 10); -// checkDecodedSize("AEJOM2RSGQ", 10, 10); -//} -// -// TEST_F(Base32Test, calculateDecodedSizeImproperSize) { -// size_t encodedSize{21}; -// size_t decodedSize; -// -// EXPECT_EQ( -// Status::UserError( -// "Base32::decode() - invalid input string: string length is not a -// multiple of 8."), -// Base32::calculateDecodedSize( -// "NBSWY3DPEB3W64TMMQ=====", encodedSize, decodedSize)); -//} -// -// TEST_F(Base32Test, testDecodeImpl) { -// constexpr const Base32::ReverseIndex reverseTable = { -// 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, -// 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, -// 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, -// 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, -// 255, 255, 255, 255, -// // ... (fill in with appropriate values) -// }; -// -// auto testDecode = [&](const std::string_view input, -// char* output1, -// size_t outputSize, -// Status expectedStatus) { -// EXPECT_EQ( -// Base32::decodeImpl( -// input, input.size(), output1, outputSize, reverseTable), -// expectedStatus); -// }; -// -// // Predefine buffer sizes and reuse. -// char output1[20] = {}; -// char output2[11] = {}; -// char output3[1] = {}; -// char output4[1] = {}; -// char output5[1] = {}; -// -// // Invalid characters in the input string -// testDecode( -// "NBSWY3DPEB3W64TMMQ$", -// output1, -// sizeof(output1), -// Status::UserError( -// "Base32::decode() - invalid input string: contains invalid -// characters.")); -// -// // All characters are padding characters -// testDecode("====", output1, sizeof(output1), Status::OK()); -// -// // Invalid input size -// testDecode( -// "N", -// output1, -// sizeof(output1), -// Status::UserError( -// "Base32::decode() - invalid input string: string length cannot be 1 -// more than a multiple of 8.")); -// -// // Valid input without padding characters -// testDecode("NBSWY3DPEB3W64TMMQ", output1, sizeof(output1), Status::OK()); -// EXPECT_STREQ(output1, "Hello, World!"); -// -// // Valid input with padding characters -// testDecode("NBSWY3DPEB3W64TMMQ======", output2, sizeof(output2), -// Status::OK()); EXPECT_STREQ(output2, "Hello, World!"); -// -// // Empty input string -// testDecode("", output3, sizeof(output3), Status::OK()); -// EXPECT_STREQ(output3, ""); -// -// // Invalid input size -// testDecode( -// "NBSWY3DPEB3W64TMMQ=======", -// output1, -// sizeof(output1), -// Status::UserError( -// "Base32::decode() - invalid input string: string length is not a -// multiple of 8.")); -// -// // Whitespace in the input string -// testDecode( -// " NBSWY 3DP E B3W64TMMQ= ", -// output1, -// sizeof(output1), -// Status::UserError( -// "Base32::decode() - invalid input string: contains invalid -// characters.")); -// -// // Insufficient buffer size -// testDecode( -// "NBSWY3DPEB3W64TMMQ======", -// output5, -// sizeof(output4), -// Status::UserError( -// "Base32::decode() - invalid output string: output string is too -// small.")); -//} -// -//} // namespace facebook::velox::encoding +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "velox/common/encode/Base32.h" +#include + +namespace facebook::velox::encoding{ + +constexpr Base32::ReverseIndex kBase32ReverseIndexTable = { + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255}; + +// Test cases for Base32::calculateDecodedSize +TEST(Base32Test, CalculateDecodedSizeEmptyInput) { + std::string_view input = ""; + size_t inputSize = 0; + size_t decodedSize = 0; + + auto status = Base32::calculateDecodedSize(input, inputSize, decodedSize); + EXPECT_TRUE(status.ok()); + EXPECT_EQ(decodedSize, 0); +} + +TEST(Base32Test, CalculateDecodedSizePaddedInput) { + std::string_view input = "MY======"; // Base32 encoded "f" + size_t inputSize = input.size(); + size_t decodedSize = 0; + + auto status = Base32::calculateDecodedSize(input, inputSize, decodedSize); + EXPECT_TRUE(status.ok()); + EXPECT_EQ(decodedSize, 1); // "f" is 1 byte +} + +TEST(Base32Test, CalculateDecodedSizeUnpaddedInput) { + std::string_view input = "MZXW6YTBOI======"; + size_t inputSize = input.size(); + size_t decodedSize = 0; + + auto status = Base32::calculateDecodedSize(input, inputSize, decodedSize); + EXPECT_TRUE(status.ok()); + EXPECT_EQ(decodedSize, 6); +} + +// Test cases for Base32::base32ReverseLookup +TEST(Base32Test, Base32ReverseLookupValidChar) { + Status status; + uint8_t result = Base32::base32ReverseLookup('M', kBase32ReverseIndexTable, status); + EXPECT_TRUE(status.ok()); + EXPECT_EQ(result, 12); +} + +TEST(Base32Test, Base32ReverseLookupInvalidChar) { + Status status; + uint8_t result = Base32::base32ReverseLookup('@', kBase32ReverseIndexTable, status); // '@' is not in Base32 charset + EXPECT_FALSE(status.ok()); + EXPECT_EQ(result, 0); +} + +// Test cases for Base32::decodeImpl +TEST(Base32Test, DecodeImplValidInput) { + std::string_view input = "MZXW6YTBOI======"; + size_t inputSize = input.size(); + char output[6] = {0}; + size_t outputSize = sizeof(output); + + auto status = Base32::decodeImpl(input, inputSize, output, outputSize, kBase32ReverseIndexTable); + EXPECT_TRUE(status.ok()); + EXPECT_STREQ(output, "foobar"); +} + +TEST(Base32Test, DecodeImplInvalidInputLength) { + std::string_view input = "MZXW6"; + size_t inputSize = 3; + char output[5]; + size_t outputSize = sizeof(output); + + auto status = Base32::decodeImpl(input, inputSize, output, outputSize, kBase32ReverseIndexTable); + EXPECT_FALSE(status.ok()); +} + +TEST(Base32Test, DecodeImplOutputBufferTooSmall) { + std::string_view input = "MZXW6YQ="; // Base32 encoded "foobar" + size_t inputSize = input.size(); + char output[3]; // Too small for decoded output + size_t outputSize = sizeof(output); + + auto status = Base32::decodeImpl(input, inputSize, output, outputSize, kBase32ReverseIndexTable); + EXPECT_FALSE(status.ok()); + EXPECT_EQ(status.message(), "Base32::decode() - output buffer too small."); +} + +} \ No newline at end of file diff --git a/velox/functions/prestosql/tests/BinaryFunctionsTest.cpp b/velox/functions/prestosql/tests/BinaryFunctionsTest.cpp index b212f8699cf25..9cbc5cbafa5c4 100644 --- a/velox/functions/prestosql/tests/BinaryFunctionsTest.cpp +++ b/velox/functions/prestosql/tests/BinaryFunctionsTest.cpp @@ -443,10 +443,10 @@ TEST_F(BinaryFunctionsTest, fromBase64) { VELOX_ASSERT_USER_THROW( fromBase64("YQ="), - "Base64::decode() - invalid input string: string length is not a multiple of 4."); + "invalid input string: string length is not a multiple of 4."); VELOX_ASSERT_USER_THROW( fromBase64("YQ==="), - "Base64::decode() - invalid input string: string length is not a multiple of 4."); + "invalid input string: string length is not a multiple of 4."); // Check encoded strings without padding EXPECT_EQ("a", fromBase64("YQ"));