Repository: microsoft/terminal
File: src/terminal/parser/ut_parser/Base64Test.cpp (C++)

Code before:

using namespace WEX::Common;
using namespace WEX::Logging;
using namespace WEX::TestExecution;

namespace Microsoft
{
    namespace Console
    {
        namespace VirtualTerminal
        {
            class Base64Test;
        };
    };
};

using namespace Microsoft::Console::VirtualTerminal;

class Microsoft::Console::VirtualTerminal::Base64Test
{
    TEST_CLASS(Base64Test);
    
    TEST_METHOD(DecodeFuzz)
    {
        // NOTE: Modify testRounds to get the feeling of running a fuzz test on Base64::Decode.
        static constexpr auto testRounds = 8;
        pcg_engines::oneseq_dxsm_64_32 rng{ til::gen_random<uint64_t>() };

        // Fills referenceData with random ASCII characters.
        // We use ASCII as Base64::Decode uses til:u8u16 internally and I don't want to test that.
        char referenceData[128];
        {
            uint32_t randomData[sizeof(referenceData) / sizeof(uint32_t)];
            for (auto& i : randomData)
            {
                i = rng();
            }

            const std::string_view randomDataView{ reinterpret_cast<const char*>(randomData), sizeof(randomData) };
            auto out = std::begin(referenceData);

            for (const auto& ch : randomDataView)
            {
                *out++ = static_cast<char>(ch & 0x7f);
            }
        }

        wchar_t wideReferenceData[std::size(referenceData)];
        std::copy_n(std::begin(referenceData), std::size(referenceData), std::begin(wideReferenceData));

        std::wstring encoded;

Review comment (suggestion):
I've run this test 10 times with 1M rounds each.
It tests the decode function with valid base64 strings of random length between 0 and 128 bytes, with and without trailing `=`.

What's missing are tests for invalid base64. However given the pointer arithmetic and lack of if conditions based on input in my loop I'm confident that it behaves correctly in such situations.

Code after:

using namespace WEX::Common;
using namespace WEX::Logging;
using namespace WEX::TestExecution;

namespace Microsoft
{
    namespace Console
    {
        namespace VirtualTerminal
        {
            class Base64Test;
        };
    };
};

using namespace Microsoft::Console::VirtualTerminal;

class Microsoft::Console::VirtualTerminal::Base64Test
{
    TEST_CLASS(Base64Test);

    TEST_METHOD(DecodeFuzz)
    {
        // NOTE: Modify testRounds to get the feeling of running a fuzz test on Base64::Decode.
        static constexpr auto testRounds = 8;
        pcg_engines::oneseq_dxsm_64_32 rng{ til::gen_random<uint64_t>() };

        // Fills referenceData with random ASCII characters.
        // We use ASCII as Base64::Decode uses til:u8u16 internally and I don't want to test that.
        char referenceData[128];
        {
            uint32_t randomData[sizeof(referenceData) / sizeof(uint32_t)];
            for (auto& i : randomData)
            {
                i = rng();
            }

            const std::string_view randomDataView{ reinterpret_cast<const char*>(randomData), sizeof(randomData) };
            auto out = std::begin(referenceData);

            for (const auto& ch : randomDataView)
            {
                *out++ = static_cast<char>(ch & 0x7f);
            }
        }

        wchar_t wideReferenceData[std::size(referenceData)];
        std::copy_n(std::begin(referenceData), std::size(referenceData), std::begin(wideReferenceData));

        std::wstring encoded;