From 13bda602f72585ebdb9950dc2ca87d388d8eb613 Mon Sep 17 00:00:00 2001 From: Adi Roiban Date: Sat, 15 Aug 2020 12:56:19 +0100 Subject: [PATCH 1/2] Initial fix for binary messages. Missing tests for Message.build --- pyas2lib/as2.py | 6 +++- pyas2lib/tests/test_utils.py | 67 ++++++++++++++++++++++++++++++++++-- pyas2lib/utils.py | 8 ++--- 3 files changed, 74 insertions(+), 7 deletions(-) diff --git a/pyas2lib/as2.py b/pyas2lib/as2.py index a89f2ca..877c553 100644 --- a/pyas2lib/as2.py +++ b/pyas2lib/as2.py @@ -381,7 +381,11 @@ def build( self.payload = email_message.Message() self.payload.set_payload(data) self.payload.set_type(content_type) - encoders.encode_7or8bit(self.payload) + + if content_type.lower().startswith('application/octet-stream'): + self.payload['Content-Transfer-Encoding'] = 'binary' + else: + encoders.encode_7or8bit(self.payload) if filename: self.payload.add_header( diff --git a/pyas2lib/tests/test_utils.py b/pyas2lib/tests/test_utils.py index 23b31d2..10411e0 100644 --- a/pyas2lib/tests/test_utils.py +++ b/pyas2lib/tests/test_utils.py @@ -15,8 +15,10 @@ def test_quoting(): assert utils.quote_as2name("PYAS2 LIB") == '"PYAS2 LIB"' -def test_bytes_generator(): - """Test the email bytes generator class.""" +def test_mime_to_bytes_empty_message(): + """ + It will generate the headers with an empty body + """ message = Message() message.set_type("application/pkcs7-mime") assert ( @@ -25,6 +27,67 @@ def test_bytes_generator(): ) +def test_mime_to_bytes_unix_text(): + """ + For non-binary content types, + it converts unix new lines to network newlines. + """ + message = Message() + message.set_type("application/xml") + message.set_payload('Some line.\nAnother line.') + + result = utils.mime_to_bytes(message) + + assert ( + b"MIME-Version: 1.0\r\n" + b"Content-Type: application/xml\r\n" + b"\r\n" + b"Some line.\r\n" + b"Another line." + ) == result + + +def test_mime_to_bytes_octet_stream(): + """ + For binary octet-stream content types, + it does not converts unix new lines to network newlines. + """ + message = Message() + message.set_type("application/octet-stream") + message.set_payload('Some line.\nAnother line.\n') + + result = utils.mime_to_bytes(message) + + assert ( + b"MIME-Version: 1.0\r\n" + b"Content-Type: application/octet-stream\r\n" + b"\r\n" + b"Some line.\n" + b"Another line.\n" + ) == result + + +def test_mime_to_bytes_binary(): + """ + It makes no conversion for binary content encoding. + """ + message = Message() + message.set_type("any/type") + message['Content-Transfer-Encoding'] = 'binary' + message.set_payload('Some line.\nAnother line.\n') + + result = utils.mime_to_bytes(message) + + assert ( + b"MIME-Version: 1.0\r\n" + b"Content-Type: any/type\r\n" + b"Content-Transfer-Encoding: binary\r\n" + b"\r\n" + b"Some line.\n" + b"Another line.\n" + ) == result + + def test_make_boundary(): """Test the function for creating a boundary for multipart messages.""" assert utils.make_mime_boundary(text="123456") is not None diff --git a/pyas2lib/utils.py b/pyas2lib/utils.py index 200294c..cec1c58 100644 --- a/pyas2lib/utils.py +++ b/pyas2lib/utils.py @@ -45,10 +45,10 @@ def _handle_text(self, msg): Handle writing the binary messages to prevent default behaviour of newline replacements. """ - if msg.get_content_type() == "application/octet-stream" or ( - msg.get("Content-Transfer-Encoding") == "binary" - and msg.get_content_subtype() in ["pkcs7-mime", "pkcs7-signature",] - ): + if ( + msg.get_content_type() == "application/octet-stream" + or msg.get("Content-Transfer-Encoding") == "binary" + ): payload = msg.get_payload(decode=True) if payload is None: return From 9f5d27cd4b1e5d86c3fe328fcfd1347f93857543 Mon Sep 17 00:00:00 2001 From: abhishekram Date: Sun, 1 Nov 2020 16:53:08 +0530 Subject: [PATCH 2/2] run black to fix linting --- pyas2lib/as2.py | 4 ++-- pyas2lib/tests/test_utils.py | 8 ++++---- pyas2lib/utils.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pyas2lib/as2.py b/pyas2lib/as2.py index 6b0aec9..4a42abc 100644 --- a/pyas2lib/as2.py +++ b/pyas2lib/as2.py @@ -382,8 +382,8 @@ def build( self.payload.set_payload(data) self.payload.set_type(content_type) - if content_type.lower().startswith('application/octet-stream'): - self.payload['Content-Transfer-Encoding'] = 'binary' + if content_type.lower().startswith("application/octet-stream"): + self.payload["Content-Transfer-Encoding"] = "binary" else: encoders.encode_7or8bit(self.payload) diff --git a/pyas2lib/tests/test_utils.py b/pyas2lib/tests/test_utils.py index 10411e0..72a9e7a 100644 --- a/pyas2lib/tests/test_utils.py +++ b/pyas2lib/tests/test_utils.py @@ -34,7 +34,7 @@ def test_mime_to_bytes_unix_text(): """ message = Message() message.set_type("application/xml") - message.set_payload('Some line.\nAnother line.') + message.set_payload("Some line.\nAnother line.") result = utils.mime_to_bytes(message) @@ -54,7 +54,7 @@ def test_mime_to_bytes_octet_stream(): """ message = Message() message.set_type("application/octet-stream") - message.set_payload('Some line.\nAnother line.\n') + message.set_payload("Some line.\nAnother line.\n") result = utils.mime_to_bytes(message) @@ -73,8 +73,8 @@ def test_mime_to_bytes_binary(): """ message = Message() message.set_type("any/type") - message['Content-Transfer-Encoding'] = 'binary' - message.set_payload('Some line.\nAnother line.\n') + message["Content-Transfer-Encoding"] = "binary" + message.set_payload("Some line.\nAnother line.\n") result = utils.mime_to_bytes(message) diff --git a/pyas2lib/utils.py b/pyas2lib/utils.py index cec1c58..dd2bbe6 100644 --- a/pyas2lib/utils.py +++ b/pyas2lib/utils.py @@ -48,7 +48,7 @@ def _handle_text(self, msg): if ( msg.get_content_type() == "application/octet-stream" or msg.get("Content-Transfer-Encoding") == "binary" - ): + ): payload = msg.get_payload(decode=True) if payload is None: return