aboutsummaryrefslogtreecommitdiff
path: root/python/tests
diff options
context:
space:
mode:
authorDamir Jelić <poljar@termina.org.uk>2019-06-18 13:46:57 +0200
committerDamir Jelić <poljar@termina.org.uk>2019-06-18 13:50:46 +0200
commit2f5590bf38e5995a36f770c04cfbf31eb9070eca (patch)
tree299e68cd3b9ea33f212cd1a7c4fcf0bbbca4bfc8 /python/tests
parente1a4e6ebf1568935a57ba8cec48e43dd7c1ebcd3 (diff)
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.
Diffstat (limited to 'python/tests')
-rw-r--r--python/tests/group_session_test.py17
-rw-r--r--python/tests/pk_test.py10
-rw-r--r--python/tests/session_test.py11
3 files changed, 38 insertions, 0 deletions
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 == "�"