TestMessageHeaderEncoding.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2023, Valtteri Koskivuori <vkoskiv@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <LibIMAP/MessageHeaderEncoding.h>
  8. #include <LibTest/TestCase.h>
  9. TEST_CASE(test_decode)
  10. {
  11. auto decode_equal = [](StringView input, StringView expected) {
  12. auto decoded = MUST(IMAP::decode_rfc2047_encoded_words(input));
  13. EXPECT_EQ(StringView(decoded), StringView(expected));
  14. };
  15. // Underscores should end up as spaces
  16. decode_equal("=?utf-8?Q?Spaces_should_be_spaces_!?="sv, "Spaces should be spaces !"sv);
  17. // RFC 2047 Section 8 "Examples", https://datatracker.ietf.org/doc/html/rfc2047#section-8
  18. decode_equal("=?ISO-8859-1?Q?a?="sv, "a"sv);
  19. decode_equal("=?ISO-8859-1?Q?a?= b"sv, "a b"sv);
  20. // White space between adjacent 'encoded-word's is not displayed.
  21. decode_equal("=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?="sv, "ab"sv);
  22. // Even multiple SPACEs between 'encoded-word's are ignored for the purpose of display.
  23. decode_equal("=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?="sv, "ab"sv);
  24. decode_equal("=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?= =?ISO-8859-1?Q?c?==?ISO-8859-1?Q?d?="sv, "abcd"sv);
  25. // Any amount of linear-space-white between 'encoded-word's, even if it includes a CRLF followed by one or more SPACEs, is ignored for the purposes of display.
  26. decode_equal("=?utf-8?Q?a?=\r\n=?utf-8?Q?b?= \r\n=?utf-8?Q?c?=\r\n =?utf-8?Q?d?="sv, "abcd"sv);
  27. // In order to cause a SPACE to be displayed within a portion of encoded text, the SPACE MUST be encoded as part of the 'encoded-word'.
  28. decode_equal("=?ISO-8859-1?Q?a_b?="sv, "a b"sv);
  29. // In order to cause a SPACE to be displayed between two strings of encoded text, the SPACE MAY be encoded as part of one of the 'encoded-word's.
  30. decode_equal("=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?="sv, "a b"sv);
  31. // More examples from the RFC document, a nice mix of different charsets & encodings.
  32. auto long_input = "From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>"
  33. "To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>"
  34. "CC: =?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>"
  35. "Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?="
  36. "=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?="sv;
  37. auto long_expected = "From: Keith Moore <moore@cs.utk.edu>"
  38. "To: Keld Jørn Simonsen <keld@dkuug.dk>"
  39. "CC: André Pirard <PIRARD@vm1.ulg.ac.be>"
  40. "Subject: If you can read this you understand the example."sv;
  41. decode_equal(long_input, long_expected);
  42. }