aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSachin Kelkar <sachinkel19@gmail.com>2017-02-03 15:02:35 +0530
committerSachin Kelkar <sachinkel19@gmail.com>2017-02-03 15:02:35 +0530
commit0674485e76b14377922d6763e546fa1466d0452f (patch)
treeb33b12d5579521253e4732e03ad91049a9e3aa4c
parent88dbfd525719229154a7badcd7628857b4e16af4 (diff)
downloadmitmproxy-0674485e76b14377922d6763e546fa1466d0452f.tar.gz
mitmproxy-0674485e76b14377922d6763e546fa1466d0452f.tar.bz2
mitmproxy-0674485e76b14377922d6763e546fa1466d0452f.zip
Fix and cleanup
-rw-r--r--mitmproxy/contentviews/image/image_parser.py18
-rw-r--r--mitmproxy/contrib/kaitaistruct/png.py (renamed from mitmproxy/contentviews/image/png.py)4
-rw-r--r--test/mitmproxy/contentviews/test_image_parser.py140
3 files changed, 69 insertions, 93 deletions
diff --git a/mitmproxy/contentviews/image/image_parser.py b/mitmproxy/contentviews/image/image_parser.py
index 3a375006..11d66c61 100644
--- a/mitmproxy/contentviews/image/image_parser.py
+++ b/mitmproxy/contentviews/image/image_parser.py
@@ -2,25 +2,27 @@ import typing
from kaitaistruct import KaitaiStream
-from . import png
+from mitmproxy.contrib.kaitaistruct import png
Metadata = typing.List[typing.Tuple[str, str]]
def parse_png(data: bytes) -> Metadata:
img = png.Png(KaitaiStream(data))
- parts = [tuple(['Format', 'Portable network graphics'])]
- parts.append(tuple(['Size', str(img.ihdr.width) + " x " + str(img.ihdr.height) + " px"]))
+ parts = [
+ ('Format', 'Portable network graphics')
+ ]
+ parts.append(('Size', "{0} x {1} px".format(img.ihdr.width, img.ihdr.height)))
for chunk in img.chunks:
if chunk.type == 'gAMA':
- parts.append(tuple(['gamma', str(chunk.body.gamma_int / 100000)]))
+ parts.append(('gamma', str(chunk.body.gamma_int / 100000)))
elif chunk.type == 'pHYs':
aspectx = chunk.body.pixels_per_unit_x
aspecty = chunk.body.pixels_per_unit_y
- parts.append(tuple(['aspect', str(aspectx) + " x " + str(aspecty)]))
+ parts.append(('aspect', "{0} x {1}".format(aspectx, aspecty)))
elif chunk.type == 'tEXt':
- parts.append(tuple([chunk.body.keyword, chunk.body.text]))
+ parts.append((chunk.body.keyword, chunk.body.text))
elif chunk.type == 'iTXt':
- parts.append(tuple([chunk.body.keyword, chunk.body.text]))
+ parts.append((chunk.body.keyword, chunk.body.text))
elif chunk.type == 'zTXt':
- parts.append(tuple([chunk.body.keyword, chunk.body.text_datastream.decode('iso8859-1')]))
+ parts.append((chunk.body.keyword, chunk.body.text_datastream.decode('iso8859-1')))
return parts
diff --git a/mitmproxy/contentviews/image/png.py b/mitmproxy/contrib/kaitaistruct/png.py
index 53d39799..5b0e3ca3 100644
--- a/mitmproxy/contentviews/image/png.py
+++ b/mitmproxy/contrib/kaitaistruct/png.py
@@ -1,5 +1,5 @@
# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
-# The source was png.ksy from here - https://github.com/kaitai-io/kaitai_struct_formats/tree/master/image
+# The source was png.ksy from here - https://github.com/kaitai-io/kaitai_struct_formats/blob/9370c720b7d2ad329102d89bdc880ba6a706ef26/image/png.ksy
import array
import struct
@@ -262,7 +262,7 @@ class Png(KaitaiStruct):
self.keyword = self._io.read_strz("UTF-8", 0, False, True, True)
self.compression_flag = self._io.read_u1()
self.compression_method = self._io.read_u1()
- self.language_tag = self._io.read_strz("iso8859-1", 0, False, True, True)
+ self.language_tag = self._io.read_strz("ASCII", 0, False, True, True)
self.translated_keyword = self._io.read_strz("UTF-8", 0, False, True, True)
self.text = self._io.read_str_eos("UTF-8")
diff --git a/test/mitmproxy/contentviews/test_image_parser.py b/test/mitmproxy/contentviews/test_image_parser.py
index 3d987b9d..ba215b93 100644
--- a/test/mitmproxy/contentviews/test_image_parser.py
+++ b/test/mitmproxy/contentviews/test_image_parser.py
@@ -1,88 +1,62 @@
import io
+import pytest
+
from mitmproxy.contentviews.image import image_parser
from mitmproxy.test import tutils
-class TestPngParser:
- def test_png_size(self):
- img = "mitmproxy/data/image.png"
- with open(tutils.test_data.path(img), "rb") as f:
- parts = image_parser.parse_png(io.BytesIO(f.read()))
- assert parts
- assert tuple(['Size', '174 x 174 px']) in parts
- assert tuple(["Format", "Portable network graphics"]) in parts
-
- def test_no_textual_data(self):
- img = "mitmproxy/data/png_parser/ct0n0g04.png"
- with open(tutils.test_data.path(img), "rb") as f:
- parts = image_parser.parse_png(io.BytesIO(f.read()))
- assert parts
- metadata = [
- ('Format', 'Portable network graphics'),
- ('Size', '32 x 32 px'),
- ('gamma', '1.0')
- ]
- parts = [data for data in parts if data not in metadata]
- assert not parts
-
- def test_textual_data(self):
- img = "mitmproxy/data/png_parser/ct1n0g04.png"
- expected = [
- ('Title', 'PngSuite'),
- ('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'),
- ('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'),
- ('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'),
- ('Software', 'Created on a NeXTstation color using "pnmtopng".'),
- ('Disclaimer', 'Freeware.')
- ]
- with open(tutils.test_data.path(img), "rb") as f:
- parts = image_parser.parse_png(io.BytesIO(f.read()))
- assert parts
- for data in expected:
- assert data in parts
-
- def test_compressed_textual_data(self):
- img = "mitmproxy/data/png_parser/ctzn0g04.png"
- expected = [
- ('Title', 'PngSuite'),
- ('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'),
- ('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'),
- ('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'),
- ('Software', 'Created on a NeXTstation color using "pnmtopng".'),
- ('Disclaimer', 'Freeware.')
- ]
- with open(tutils.test_data.path(img), "rb") as f:
- parts = image_parser.parse_png(io.BytesIO(f.read()))
- assert parts
- for data in expected:
- assert data in parts
-
- def test_international_textual_data(self):
- img = "mitmproxy/data/png_parser/cten0g04.png"
- expected = [
- ('Author', 'Willem van Schaik (willem@schaik.com)'),
- ('Copyright', 'Copyright Willem van Schaik, Canada 2011'),
- ('Description', 'A compilation of a set of images created to test the various color-types of the PNG format. Included are black&white, color, paletted, with alpha channel, with transparency formats. All bit-depths allowed according to the spec are present.'),
- ('Software', 'Created on a NeXTstation color using "pnmtopng".'),
- ('Disclaimer', 'Freeware.')
- ]
- with open(tutils.test_data.path(img), "rb") as f:
- parts = image_parser.parse_png(io.BytesIO(f.read()))
- print(parts)
- assert parts
- for data in expected:
- assert data in parts
-
- def test_gamma(self):
- img = "mitmproxy/data/png_parser/g07n0g16.png"
- with open(tutils.test_data.path(img), "rb") as f:
- parts = image_parser.parse_png(io.BytesIO(f.read()))
- assert parts
- assert ('gamma', '0.7') in parts
-
- def test_aspect(self):
- img = "mitmproxy/data/png_parser/aspect.png"
- with open(tutils.test_data.path(img), "rb") as f:
- parts = image_parser.parse_png(io.BytesIO(f.read()))
- assert parts
- assert ('aspect', '72 x 72') in parts
+@pytest.mark.parametrize("filename, metadata", {
+ # no textual data
+ "mitmproxy/data/png_parser/ct0n0g04.png": [
+ ('Format', 'Portable network graphics'),
+ ('Size', '32 x 32 px'),
+ ('gamma', '1.0')
+ ],
+ # with textual data
+ "mitmproxy/data/png_parser/ct1n0g04.png": [
+ ('Format', 'Portable network graphics'),
+ ('Size', '32 x 32 px'),
+ ('gamma', '1.0'),
+ ('Title', 'PngSuite'),
+ ('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'),
+ ('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'),
+ ('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'), ('Software', 'Created on a NeXTstation color using "pnmtopng".'), ('Disclaimer', 'Freeware.')
+ ],
+ # with compressed textual data
+ "mitmproxy/data/png_parser/ctzn0g04.png": [
+ ('Format', 'Portable network graphics'),
+ ('Size', '32 x 32 px'),
+ ('gamma', '1.0'),
+ ('Title', 'PngSuite'),
+ ('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'),
+ ('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'),
+ ('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'), ('Software', 'Created on a NeXTstation color using "pnmtopng".'), ('Disclaimer', 'Freeware.')
+ ],
+ # UTF-8 international text - english
+ "mitmproxy/data/png_parser/cten0g04.png": [
+ ('Format', 'Portable network graphics'),
+ ('Size', '32 x 32 px'),
+ ('gamma', '1.0'),
+ ('Title', 'PngSuite'),
+ ('Author', 'Willem van Schaik (willem@schaik.com)'),
+ ('Copyright', 'Copyright Willem van Schaik, Canada 2011'),
+ ('Description', 'A compilation of a set of images created to test the various color-types of the PNG format. Included are black&white, color, paletted, with alpha channel, with transparency formats. All bit-depths allowed according to the spec are present.'), ('Software', 'Created on a NeXTstation color using "pnmtopng".'), ('Disclaimer', 'Freeware.')
+ ],
+ # check gamma value
+ "mitmproxy/data/png_parser/g07n0g16.png": [
+ ('Format', 'Portable network graphics'),
+ ('Size', '32 x 32 px'),
+ ('gamma', '0.7')
+ ],
+ # check aspect value
+ "mitmproxy/data/png_parser/aspect.png": [
+ ('Format', 'Portable network graphics'),
+ ('Size', '1280 x 798 px'),
+ ('aspect', '72 x 72'),
+ ('date:create', '2012-07-11T14:04:52-07:00'),
+ ('date:modify', '2012-07-11T14:04:52-07:00')
+ ],
+}.items())
+def test_parse_png(filename, metadata):
+ with open(tutils.test_data.path(filename), "rb") as f:
+ assert metadata == image_parser.parse_png(io.BytesIO(f.read()))