TestQuotedPrintable.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <LibIMAP/QuotedPrintable.h>
  8. #include <LibTest/TestCase.h>
  9. TEST_CASE(test_decode)
  10. {
  11. auto decode_equal = [](const char* input, const char* expected) {
  12. auto decoded = IMAP::decode_quoted_printable(StringView(input));
  13. EXPECT(String::copy(decoded) == String(expected));
  14. };
  15. auto decode_equal_byte_buffer = [](const char* input, const char* expected, size_t expected_length) {
  16. auto decoded = IMAP::decode_quoted_printable(StringView(input));
  17. EXPECT(decoded == *ByteBuffer::copy(expected, expected_length));
  18. };
  19. decode_equal("hello world", "hello world");
  20. decode_equal("=3D", "=");
  21. decode_equal("hello=\r\n world", "hello world");
  22. decode_equal("=68=65=6C=6C=6F=20=\r\n=77=6F=72=6C=64", "hello world");
  23. // Doesn't mistake hex sequences without a preceding '=' as an escape sequence.
  24. decode_equal("4A=4B=4C4D", "4AKL4D");
  25. // Allows lowercase escape sequences.
  26. decode_equal("=4a=4b=4c=4d=4e=4f", "JKLMNO");
  27. // Bytes for U+1F41E LADY BEETLE
  28. decode_equal_byte_buffer("=F0=9F=90=9E", "\xF0\x9F\x90\x9E", 4);
  29. // Illegal characters. If these aren't escaped, they are simply ignored.
  30. // Illegal characters are:
  31. // - ASCII control bytes that aren't tab, carriage return or new line
  32. // - Any byte above 0x7E
  33. StringBuilder illegal_character_builder;
  34. for (u16 byte = 0; byte <= 0xFF; ++byte) {
  35. if (byte > 0x7E || (is_ascii_control(byte) && byte != '\t' && byte != '\r' && byte != '\n'))
  36. illegal_character_builder.append(byte);
  37. }
  38. auto illegal_character_decode = IMAP::decode_quoted_printable(illegal_character_builder.to_string());
  39. EXPECT(illegal_character_decode.is_empty());
  40. }