summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Pipek <jan.pipek@gmail.com>2019-09-10 13:15:28 +0200
committerIlya Etingof <etingof@gmail.com>2019-11-15 19:31:42 +0100
commit3cf920db9e1c41fe5c4b834a263d3e0fe06e4440 (patch)
tree2d580af6ae0b7c3b4edee78f6908a01ab3ba8e13
parent0005c889b2e9b5a33e0109372c31474da610ebfd (diff)
downloadpyasn1-git-3cf920db9e1c41fe5c4b834a263d3e0fe06e4440.tar.gz
Update tests with more streams for ber.decoder
-rw-r--r--tests/codec/ber/test_decoder.py55
1 files changed, 53 insertions, 2 deletions
diff --git a/tests/codec/ber/test_decoder.py b/tests/codec/ber/test_decoder.py
index aee69a8..0686c6d 100644
--- a/tests/codec/ber/test_decoder.py
+++ b/tests/codec/ber/test_decoder.py
@@ -4,10 +4,12 @@
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pyasn1/license.html
#
+import gzip
import io
import os
import sys
import tempfile
+import zipfile
try:
import unittest2 as unittest
@@ -24,7 +26,7 @@ from pyasn1.type import char
from pyasn1.codec.ber import decoder
from pyasn1.codec.ber import eoo
from pyasn1.compat.octets import ints2octs, str2octs, null
-from pyasn1.error import PyAsn1Error, SubstrateUnderrunError
+from pyasn1.error import PyAsn1Error, SubstrateUnderrunError, UnsupportedSubstrateError
class LargeTagDecoderTestCase(BaseTestCase):
@@ -1666,7 +1668,6 @@ class BinaryFileTestCase(BaseTestCase):
with open(path, "wb") as out:
out.write(ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0, 7)))
-
with open(path, "rb") as source:
with self.assertRaises(SubstrateUnderrunError):
_ = list(decoder.decodeStream(source))
@@ -1674,6 +1675,56 @@ class BinaryFileTestCase(BaseTestCase):
os.remove(path)
+class BytesIOTestCase(BaseTestCase):
+ def testRead(self):
+ source = ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
+ stream = io.BytesIO(source)
+ values = list(decoder.decodeStream(stream))
+ assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
+
+
+class UnicodeTestCase(BaseTestCase):
+ def testFail(self):
+ # This ensures that unicode objects in Python 2 & str objects in Python 3.7 cannot be parsed.
+ source = ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)).decode("latin-1")
+ with self.assertRaises(UnsupportedSubstrateError):
+ _ = next(decoder.decodeStream(source))
+
+
+class CompressedFilesTestCase(BaseTestCase):
+ def testGzip(self):
+ _, path = tempfile.mkstemp(suffix=".gz")
+ try:
+ with gzip.open(path, "wb") as out:
+ out.write(ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)))
+
+ with gzip.open(path, "rb") as source:
+ values = list(decoder.decodeStream(source))
+
+ assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
+ finally:
+ os.remove(path)
+
+ def testZipfile(self):
+ # File from ZIP archive is a good example of non-seekable stream in Python 2.7
+ # In Python 3.7, it is a seekable stream.
+ _, path = tempfile.mkstemp(suffix=".zip")
+ try:
+ with zipfile.ZipFile(path, "w") as myzip:
+ myzip.writestr("data", ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)))
+
+ with zipfile.ZipFile(path, "r") as myzip:
+ with myzip.open("data", "r") as source:
+ if sys.version_info < (3,):
+ with self.assertRaises(UnsupportedSubstrateError):
+ _ = list(decoder.decodeStream(source))
+ else:
+ values = list(decoder.decodeStream(source))
+ assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
+ finally:
+ os.remove(path)
+
+
suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
if __name__ == '__main__':