From 2f5590bf38e5995a36f770c04cfbf31eb9070eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 18 Jun 2019 13:46:57 +0200 Subject: olm: Allow decryption functions to define how to handle unicode decode errors. This patch changes the decryption functions not to fail if there was an unicode decode error while converting the decrypted bytes plaintext into a native python string. Characters that cannot be decoded as unicode are now replaced with the unicode replacement character (U+FFFD). The old behaviour of raising an UnicodeDecodeError can be achieved by passing the "strict" error handling scheme to the decrypt function. --- python/tests/group_session_test.py | 17 +++++++++++++++++ python/tests/pk_test.py | 10 ++++++++++ python/tests/session_test.py | 11 +++++++++++ 3 files changed, 38 insertions(+) (limited to 'python/tests') diff --git a/python/tests/group_session_test.py b/python/tests/group_session_test.py index c17e84f..3942024 100644 --- a/python/tests/group_session_test.py +++ b/python/tests/group_session_test.py @@ -1,3 +1,7 @@ +# -*- coding: utf-8 -*- + +from builtins import bytes + import pytest from olm import InboundGroupSession, OlmGroupSessionError, OutboundGroupSession @@ -112,3 +116,16 @@ class TestClass(object): outbound = OutboundGroupSession() inbound = InboundGroupSession(outbound.session_key) del inbound + + def test_invalid_unicode_decrypt(self): + outbound = OutboundGroupSession() + inbound = InboundGroupSession(outbound.session_key) + + text = outbound.encrypt(bytes([0xed])) + plaintext, _ = inbound.decrypt(text) + + print(plaintext) + assert plaintext == "�" + + plaintext, _ = inbound.decrypt(text, "ignore") + assert plaintext == "" diff --git a/python/tests/pk_test.py b/python/tests/pk_test.py index fe3b4b6..749d2eb 100644 --- a/python/tests/pk_test.py +++ b/python/tests/pk_test.py @@ -1,3 +1,6 @@ +# -*- coding: utf-8 -*- +from builtins import bytes + import pytest from olm import (PkDecryption, PkDecryptionError, PkEncryption, PkSigning, @@ -55,3 +58,10 @@ class TestClass(object): message = "This statement is true" signature = signing.sign(message) ed25519_verify(signing.public_key, message, signature) + + def test_invalid_unicode_decrypt(self): + decryption = PkDecryption() + encryption = PkEncryption(decryption.public_key) + message = encryption.encrypt(bytes([0xed])) + plaintext = decryption.decrypt(message) + assert plaintext == "�" diff --git a/python/tests/session_test.py b/python/tests/session_test.py index ab1c38b..56a6b83 100644 --- a/python/tests/session_test.py +++ b/python/tests/session_test.py @@ -1,3 +1,6 @@ +# -*- coding: utf-8 -*- +from builtins import bytes + import pytest from olm import (Account, InboundSession, OlmMessage, OlmPreKeyMessage, @@ -141,3 +144,11 @@ class TestClass(object): new_message = new_session.encrypt(plaintext) assert bob_session.matches(new_message) is False + + def test_invalid_unicode_decrypt(self): + alice, bob, session = self._create_session() + message = session.encrypt(bytes([0xed])) + + bob_session = InboundSession(bob, message) + plaintext = bob_session.decrypt(message) + assert plaintext == "�" -- cgit v1.2.3 From 28350d612e4f2cbb7cdf3187a871a0e9f3c04724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 20 Jun 2019 13:38:35 +0200 Subject: tests: Use Unicode literals in the Unicode decoding tests. This is needed because the function returns Unicode strings and the comparison will fail under python2 unless Unicode literals are used. --- python/tests/group_session_test.py | 2 +- python/tests/pk_test.py | 2 +- python/tests/session_test.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'python/tests') diff --git a/python/tests/group_session_test.py b/python/tests/group_session_test.py index 3942024..b45fbb6 100644 --- a/python/tests/group_session_test.py +++ b/python/tests/group_session_test.py @@ -125,7 +125,7 @@ class TestClass(object): plaintext, _ = inbound.decrypt(text) print(plaintext) - assert plaintext == "�" + assert plaintext == u"�" plaintext, _ = inbound.decrypt(text, "ignore") assert plaintext == "" diff --git a/python/tests/pk_test.py b/python/tests/pk_test.py index 749d2eb..b82f420 100644 --- a/python/tests/pk_test.py +++ b/python/tests/pk_test.py @@ -64,4 +64,4 @@ class TestClass(object): encryption = PkEncryption(decryption.public_key) message = encryption.encrypt(bytes([0xed])) plaintext = decryption.decrypt(message) - assert plaintext == "�" + assert plaintext == u"�" diff --git a/python/tests/session_test.py b/python/tests/session_test.py index 56a6b83..dce37c5 100644 --- a/python/tests/session_test.py +++ b/python/tests/session_test.py @@ -151,4 +151,4 @@ class TestClass(object): bob_session = InboundSession(bob, message) plaintext = bob_session.decrypt(message) - assert plaintext == "�" + assert plaintext == u"�" -- cgit v1.2.3 From 61175c969b1de3ecd8c25478c69d6d1883dfa211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 20 Jun 2019 14:05:36 +0200 Subject: tests: Simplify the input parameter for the Unicode decoding tests. --- python/tests/group_session_test.py | 5 +---- python/tests/pk_test.py | 4 +--- python/tests/session_test.py | 4 +--- 3 files changed, 3 insertions(+), 10 deletions(-) (limited to 'python/tests') diff --git a/python/tests/group_session_test.py b/python/tests/group_session_test.py index b45fbb6..4632a60 100644 --- a/python/tests/group_session_test.py +++ b/python/tests/group_session_test.py @@ -1,7 +1,4 @@ # -*- coding: utf-8 -*- - -from builtins import bytes - import pytest from olm import InboundGroupSession, OlmGroupSessionError, OutboundGroupSession @@ -121,7 +118,7 @@ class TestClass(object): outbound = OutboundGroupSession() inbound = InboundGroupSession(outbound.session_key) - text = outbound.encrypt(bytes([0xed])) + text = outbound.encrypt(b"\xed") plaintext, _ = inbound.decrypt(text) print(plaintext) diff --git a/python/tests/pk_test.py b/python/tests/pk_test.py index b82f420..ef87465 100644 --- a/python/tests/pk_test.py +++ b/python/tests/pk_test.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from builtins import bytes - import pytest from olm import (PkDecryption, PkDecryptionError, PkEncryption, PkSigning, @@ -62,6 +60,6 @@ class TestClass(object): def test_invalid_unicode_decrypt(self): decryption = PkDecryption() encryption = PkEncryption(decryption.public_key) - message = encryption.encrypt(bytes([0xed])) + message = encryption.encrypt(b"\xed") plaintext = decryption.decrypt(message) assert plaintext == u"�" diff --git a/python/tests/session_test.py b/python/tests/session_test.py index dce37c5..b856585 100644 --- a/python/tests/session_test.py +++ b/python/tests/session_test.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from builtins import bytes - import pytest from olm import (Account, InboundSession, OlmMessage, OlmPreKeyMessage, @@ -147,7 +145,7 @@ class TestClass(object): def test_invalid_unicode_decrypt(self): alice, bob, session = self._create_session() - message = session.encrypt(bytes([0xed])) + message = session.encrypt(b"\xed") bob_session = InboundSession(bob, message) plaintext = bob_session.decrypt(message) -- cgit v1.2.3