From a00b5fc7b720e9fc0f3873a22adf9c610f7c69a0 Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 21 Jul 2021 02:03:36 +0100 Subject: [PATCH] Tests: Add tests for the quoted printable decoder --- Tests/CMakeLists.txt | 1 + Tests/LibIMAP/CMakeLists.txt | 5 +++ Tests/LibIMAP/TestQuotedPrintable.cpp | 49 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 Tests/LibIMAP/CMakeLists.txt create mode 100644 Tests/LibIMAP/TestQuotedPrintable.cpp diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index d31836b5f2..b9680ca844 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -6,6 +6,7 @@ add_subdirectory(LibCore) add_subdirectory(LibCpp) add_subdirectory(LibELF) add_subdirectory(LibGfx) +add_subdirectory(LibIMAP) add_subdirectory(LibJS) add_subdirectory(LibM) add_subdirectory(LibPthread) diff --git a/Tests/LibIMAP/CMakeLists.txt b/Tests/LibIMAP/CMakeLists.txt new file mode 100644 index 0000000000..c8a213d427 --- /dev/null +++ b/Tests/LibIMAP/CMakeLists.txt @@ -0,0 +1,5 @@ +file(GLOB TEST_SOURCES CONFIGURE_DEPENDS "*.cpp") + +foreach(source ${TEST_SOURCES}) + serenity_test(${source} LibIMAP LIBS LibIMAP) +endforeach() diff --git a/Tests/LibIMAP/TestQuotedPrintable.cpp b/Tests/LibIMAP/TestQuotedPrintable.cpp new file mode 100644 index 0000000000..e52d542533 --- /dev/null +++ b/Tests/LibIMAP/TestQuotedPrintable.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021, Luke Wilde + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +TEST_CASE(test_decode) +{ + auto decode_equal = [](const char* input, const char* expected) { + auto decoded = IMAP::decode_quoted_printable(StringView(input)); + EXPECT(String::copy(decoded) == String(expected)); + }; + + auto decode_equal_byte_buffer = [](const char* input, const char* expected, size_t expected_length) { + auto decoded = IMAP::decode_quoted_printable(StringView(input)); + EXPECT(decoded == ByteBuffer::copy(expected, expected_length)); + }; + + decode_equal("hello world", "hello world"); + decode_equal("=3D", "="); + decode_equal("hello=\r\n world", "hello world"); + decode_equal("=68=65=6C=6C=6F=20=\r\n=77=6F=72=6C=64", "hello world"); + + // Doesn't mistake hex sequences without a preceding '=' as an escape sequence. + decode_equal("4A=4B=4C4D", "4AKL4D"); + + // Allows lowercase escape sequences. + decode_equal("=4a=4b=4c=4d=4e=4f", "JKLMNO"); + + // Bytes for U+1F41E LADY BEETLE + decode_equal_byte_buffer("=F0=9F=90=9E", "\xF0\x9F\x90\x9E", 4); + + // Illegal characters. If these aren't escaped, they are simply ignored. + // Illegal characters are: + // - ASCII control bytes that aren't tab, carriage return or new line + // - Any byte above 0x7E + StringBuilder illegal_character_builder; + for (u16 byte = 0; byte <= 0xFF; ++byte) { + if (byte > 0x7E || (is_ascii_control(byte) && byte != '\t' && byte != '\r' && byte != '\n')) + illegal_character_builder.append(byte); + } + + auto illegal_character_decode = IMAP::decode_quoted_printable(illegal_character_builder.to_string()); + EXPECT(illegal_character_decode.is_empty()); +}