-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa47f32
commit 8107d33
Showing
3 changed files
with
130 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <gtest/gtest.h> | ||
// #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 <gtest/gtest.h> | ||
#include "velox/common/encode/Base32.h" | ||
#include <cstring> | ||
|
||
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."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters