require "vnd.stalwart.testsuite"; require "variables"; require "replace"; require "mime"; require "foreverypart"; require "editheader"; require "extracttext"; test "Replace" { set "message" text: From: Hendrik To: Harrie Date: Sat, 11 Oct 2010 00:31:44 +0200 Subject: Harrie is een prutser Content-Type: multipart/mixed; boundary=AA X-Test: AA This is a multi-part message in MIME format. --AA Content-Type: multipart/mixed; boundary=BB X-Test: BB This is a multi-part message in MIME format. --BB Content-Type: text/plain; charset="us-ascii" X-Test: CC Hello --BB Content-Type: text/plain; charset="us-ascii" X-Test: DD Hello again --BB-- This is the end of MIME multipart. --AA Content-Type: text/plain; charset="us-ascii" X-Test: EE And again --AA-- This is the end of MIME multipart. . ; # A message without changes should be regenerated exactly # as the original. test_set "message" "${message}"; test_assert_message "${message}"; # Replace main part test_result_reset; test_set "message" "${message}"; replace :from "Unknown Sender " :subject "Contents removed" "Your message contents were removed."; test_assert_message "Original-From: Hendrik To: Harrie Date: Sat, 11 Oct 2010 00:31:44 +0200 Original-Subject: Harrie is een prutser From: Unknown Sender Subject: Contents removed Message-ID: Content-Type: text/plain; charset=utf-8 Your message contents were removed."; # Replace main part test_result_reset; test_set "message" "${message}"; replace :mime "Content-Type: text/html

Your message contents were removed.

"; test_assert_message "From: Hendrik To: Harrie Date: Sat, 11 Oct 2010 00:31:44 +0200 Subject: Harrie is een prutser From: MAILER-DAEMON Message-ID: Content-Type: text/html

Your message contents were removed.

"; # Replace text parts test_result_reset; test_set "message" "${message}"; foreverypart { if header :mime :type "content-type" "text" { replace "Your text content was censored!"; } } test_assert_message "From: Hendrik To: Harrie Date: Sat, 11 Oct 2010 00:31:44 +0200 Subject: Harrie is een prutser Content-Type: multipart/mixed; boundary=AA X-Test: AA This is a multi-part message in MIME format. --AA Content-Type: multipart/mixed; boundary=BB X-Test: BB This is a multi-part message in MIME format. --BB Content-Type: text/plain; charset=utf-8 Your text content was censored! --BB Content-Type: text/plain; charset=utf-8 Your text content was censored! --BB-- This is the end of MIME multipart. --AA Content-Type: text/plain; charset=utf-8 Your text content was censored! --AA-- This is the end of MIME multipart. "; # Do not allow two levels of nested parts test_result_reset; test_set "message" "${message}"; foreverypart { foreverypart { if header :mime :type "content-type" "multipart" { replace "Nested parts are not allowed in this system."; addheader :last "X-Replaced" "true"; } } } test_assert_message "From: Hendrik To: Harrie Date: Sat, 11 Oct 2010 00:31:44 +0200 Subject: Harrie is een prutser Content-Type: multipart/mixed; boundary=AA X-Test: AA This is a multi-part message in MIME format. --AA Content-Type: text/plain; charset=utf-8 X-Replaced: true Nested parts are not allowed in this system. --AA Content-Type: text/plain; charset=\"us-ascii\" X-Test: EE And again --AA-- This is the end of MIME multipart. "; # Multiple replacements test_result_reset; set "message" text: From: Hendrik To: Harrie Date: Sat, 11 Oct 2010 00:31:44 +0200 Subject: Harrie is een prutser Content-Type: multipart/mixed; boundary=AA This is a multi-part message in MIME format. --AA Content-Type: multipart/mixed; boundary=BB This is a multi-part message in MIME format. --BB Content-Type: text/plain; charset="us-ascii" This is the first message part containing plain text. --BB Content-Type: text/plain; charset="us-ascii" This is another plain text message part. --BB-- This is the end of MIME multipart. --AA Content-Type: text/html; charset="us-ascii" This is a piece of HTML text. --AA-- This is the end of MIME multipart. . ; test_set "message" "${message}"; set "counter" "a"; foreverypart { if header :mime :contenttype "content-type" "text/html" { extracttext :upper "text_content"; replace "${text_content}"; } set :length "part_num" "${counter}"; addheader :last "X-Part-Number" "${part_num}"; set "counter" "${counter}a"; } test_assert_message "From: Hendrik To: Harrie Date: Sat, 11 Oct 2010 00:31:44 +0200 Subject: Harrie is een prutser Content-Type: multipart/mixed; boundary=AA X-Part-Number: 1 This is a multi-part message in MIME format. --AA Content-Type: multipart/mixed; boundary=BB X-Part-Number: 2 This is a multi-part message in MIME format. --BB Content-Type: text/plain; charset=\"us-ascii\" X-Part-Number: 3 This is the first message part containing plain text. --BB Content-Type: text/plain; charset=\"us-ascii\" X-Part-Number: 4 This is another plain text message part. --BB-- This is the end of MIME multipart. --AA Content-Type: text/plain; charset=utf-8 X-Part-Number: 5 THIS IS A PIECE OF HTML TEXT. --AA-- This is the end of MIME multipart. "; # Mixed content test_result_reset; set "message" text: From: Whomever To: Someone Date: Sat, 10 Oct 2009 00:30:04 +0200 Subject: whatever Content-Type: multipart/mixed; boundary=outer This is a multi-part message in MIME format. --outer Content-Type: multipart/alternative; boundary=inner This is a nested multi-part message in MIME format. --inner Content-Type: text/plain; charset="us-ascii" Hello --inner Content-Type: text/html; charset="us-ascii" Hello --inner-- This is the end of the inner MIME multipart. --outer Content-Type: message/rfc822 From: Someone Else Subject: Hello, this is an elaborate request for you to finally say hello already! Please say Hello --outer-- This is the end of the outer MIME multipart. . ; # A message without changes should be regenerated exactly # as the original. test_set "message" "${message}"; test_assert_message "${message}"; foreverypart { if header :mime :type "content-type" "text" { extracttext :upper "text_content"; replace "${text_content}"; } elsif header :mime :contenttype "content-type" "message/rfc822" { replace "Nested messages have been disabled."; } } test_assert_message "From: Whomever To: Someone Date: Sat, 10 Oct 2009 00:30:04 +0200 Subject: whatever Content-Type: multipart/mixed; boundary=outer This is a multi-part message in MIME format. --outer Content-Type: multipart/alternative; boundary=inner This is a nested multi-part message in MIME format. --inner Content-Type: text/plain; charset=utf-8 HELLO --inner Content-Type: text/plain; charset=utf-8 HELLO --inner-- This is the end of the inner MIME multipart. --outer Content-Type: text/plain; charset=utf-8 Nested messages have been disabled. --outer-- This is the end of the outer MIME multipart. "; }