|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "TestHelpers.h" |
| 9 | + |
| 10 | +#include "hermes/ADT/SafeInt.h" |
| 11 | +#include "hermes/VM/JSLib/Base64Util.h" |
| 12 | +#include "hermes/VM/StringBuilder.h" |
| 13 | + |
| 14 | +using namespace hermes::vm; |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +using Base64UtilTest = RuntimeTestFixture; |
| 19 | + |
| 20 | +#define EXPECT_ENCODED(original, expected) \ |
| 21 | + { \ |
| 22 | + uint64_t expectedLength = ((original.size() + 2) / 3) * 4; \ |
| 23 | + EXPECT_LE(expectedLength, std::numeric_limits<uint32_t>::max()); \ |
| 24 | + hermes::SafeUInt32 outputLength{static_cast<uint32_t>(expectedLength)}; \ |
| 25 | + CallResult<StringBuilder> builder = \ |
| 26 | + StringBuilder::createStringBuilder(runtime, outputLength, true); \ |
| 27 | + EXPECT_NE(builder, ExecutionStatus::EXCEPTION); \ |
| 28 | + \ |
| 29 | + bool success = base64Encode(original, *builder); \ |
| 30 | + EXPECT_TRUE(success); \ |
| 31 | + EXPECT_EQ( \ |
| 32 | + builder->getStringPrimitive()->getStringRef<char>(), \ |
| 33 | + createASCIIRef(expected)); \ |
| 34 | + } |
| 35 | + |
| 36 | +#define EXPECT_ENCODED_ASCII_AND_UTF16(original, expected) \ |
| 37 | + { \ |
| 38 | + ASCIIRef asciiRef = createASCIIRef(original); \ |
| 39 | + EXPECT_ENCODED(asciiRef, expected); \ |
| 40 | + \ |
| 41 | + std::vector<char16_t> converted(asciiRef.size() + 1); \ |
| 42 | + uint32_t i = 0; \ |
| 43 | + for (i = 0; i < asciiRef.size(); i++) { \ |
| 44 | + converted[i] = asciiRef[i]; \ |
| 45 | + } \ |
| 46 | + converted[i] = '\0'; \ |
| 47 | + EXPECT_ENCODED(createUTF16Ref(converted.data()), expected); \ |
| 48 | + } |
| 49 | + |
| 50 | +TEST_F(Base64UtilTest, EdgeCases) { |
| 51 | + EXPECT_ENCODED_ASCII_AND_UTF16("", ""); |
| 52 | +} |
| 53 | + |
| 54 | +TEST_F(Base64UtilTest, EncodePaddingRequired) { |
| 55 | + EXPECT_ENCODED_ASCII_AND_UTF16("a", "YQ=="); |
| 56 | + EXPECT_ENCODED_ASCII_AND_UTF16("ab", "YWI="); |
| 57 | + EXPECT_ENCODED_ASCII_AND_UTF16("abcd", "YWJjZA=="); |
| 58 | + EXPECT_ENCODED_ASCII_AND_UTF16("abcde", "YWJjZGU="); |
| 59 | + EXPECT_ENCODED_ASCII_AND_UTF16( |
| 60 | + "less is more than more", "bGVzcyBpcyBtb3JlIHRoYW4gbW9yZQ=="); |
| 61 | + EXPECT_ENCODED_ASCII_AND_UTF16("<>?su", "PD4/c3U="); |
| 62 | + |
| 63 | + EXPECT_ENCODED(UTF16Ref(std::array<char16_t, 1>{1}), "AQ=="); |
| 64 | + EXPECT_ENCODED(ASCIIRef(std::array<char, 1>{1}), "AQ=="); |
| 65 | + EXPECT_ENCODED(UTF16Ref(std::array<char16_t, 2>{1, 0}), "AQA="); |
| 66 | + EXPECT_ENCODED(ASCIIRef(std::array<char, 2>{1, 0}), "AQA="); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_F(Base64UtilTest, EncodePaddingNotNeeded) { |
| 70 | + EXPECT_ENCODED_ASCII_AND_UTF16("abc", "YWJj"); |
| 71 | + EXPECT_ENCODED_ASCII_AND_UTF16("abcdef", "YWJjZGVm"); |
| 72 | + |
| 73 | + EXPECT_ENCODED(UTF16Ref(std::array<char16_t, 3>{0, 0, 0}), "AAAA"); |
| 74 | + EXPECT_ENCODED(ASCIIRef(std::array<char, 3>{0, 0, 0}), "AAAA"); |
| 75 | + EXPECT_ENCODED(UTF16Ref(std::array<char16_t, 3>{1, 0, 0}), "AQAA"); |
| 76 | + EXPECT_ENCODED(ASCIIRef(std::array<char, 3>{1, 0, 0}), "AQAA"); |
| 77 | +} |
| 78 | + |
| 79 | +TEST_F(Base64UtilTest, EncodeInvalid) { |
| 80 | + // Just a long enough buffer. All calls in this function are expected to fail. |
| 81 | + hermes::SafeUInt32 outputLength{20}; |
| 82 | + CallResult<StringBuilder> builder = |
| 83 | + StringBuilder::createStringBuilder(runtime, outputLength, true); |
| 84 | + EXPECT_NE(builder, ExecutionStatus::EXCEPTION); |
| 85 | + EXPECT_FALSE(base64Encode(createUTF16Ref(u"\U0001F600"), *builder)); |
| 86 | + EXPECT_FALSE(base64Encode(createUTF16Ref(u"a\U0001F600"), *builder)); |
| 87 | + EXPECT_FALSE(base64Encode(createUTF16Ref(u"ab\U0001F600"), *builder)); |
| 88 | + EXPECT_FALSE(base64Encode(createUTF16Ref(u"abc\U0001F600"), *builder)); |
| 89 | + EXPECT_FALSE(base64Encode(createUTF16Ref(u"\U0001F600xyz"), *builder)); |
| 90 | + EXPECT_FALSE(base64Encode(createUTF16Ref(u"abc\U0001F600xyz"), *builder)); |
| 91 | +} |
| 92 | + |
| 93 | +} // end anonymous namespace |
0 commit comments