From dd9c8c445f3215f74de932095b48baa4dc6ae50f Mon Sep 17 00:00:00 2001 From: will0brien <70724174+will0brien@users.noreply.github.com> Date: Sun, 4 Oct 2020 11:50:59 -0400 Subject: [PATCH 01/13] Fixed issue with makefile --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index b05592b..96c9fa4 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ virtualenv: @$(SYSTEM_PYTHON) -m venv venv @echo "Installing all dependencies..." $(VENV_PIP) install --upgrade pip + $(VENV_PIP) install --upgrade setuptools wheel $(VENV_PIP) install -r $(REQUIREMENTS) all: uninstall install From 6550fcad9645ac1fc27273ca0ba8636d3026eb76 Mon Sep 17 00:00:00 2001 From: will0brien <70724174+will0brien@users.noreply.github.com> Date: Fri, 9 Oct 2020 00:51:18 -0400 Subject: [PATCH 02/13] Enter --- .DS_Store | Bin 0 -> 8196 bytes .vscode/settings.json | 3 ++ docs/.DS_Store | Bin 0 -> 6148 bytes makefile~ | 71 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 .DS_Store create mode 100644 .vscode/settings.json create mode 100644 docs/.DS_Store create mode 100644 makefile~ diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..136b3998fec1e7ba385d5cd41dc17a26e8ba829d GIT binary patch literal 8196 zcmeHMO>Wab6n@jD#0f%VQK?ya0TPQUN-1g=2+5?$A`&1(5G(+tPVLgDsU2b`X$h&a z;Sg{DB#y!fI0y%T@6C)N+i8%xK@{diGv9dTedG9>zdVPCL~ZD85tWI^LSigkM|Od6 zJEsL((KR=L0(_zY`Sgy4)J4gdQNSo*6fg=H1&jiJqXIay*{mh!zG^kOQNSqhUn;=+ zgN?-KXq+jOTL&^30swQUmWDFo0Lig6IvQsRB^7@CUbhO*-4sCi zMJJ)}EOdk-6rxVl$6jMYMx`qm?(bWt-|I$kwfbGGtmf9Pk*Z zw2s1YD|X{yRC5Qf`0A+}A9Zy3c^LNQWw-O9>yO)|^}R5N-EQCyB)RMPaC!Z*8+hTk z77hb1lKr$)gJl)0Lc3I+Om^;8H|_21>U7hd?Czp`Z+mAtEm#}3w;nY2PtM*?K1|O) z$wwuj_aR~dllB=Gb0J2pLC_0>x2R+PA_P4p7sB?bhjImYmKIBFz%MCko*q$?_NYNm zs6~y8_s5KP0e!o4jJ^-?5{B<_o?-l0U_3+L52;R%ffew*G(!&am&7oaE;&tj_>+7K z6UxVg8sh1fDUkHf!#}{&9OAR67c1}^?|*fQWkktLv^B`IWo%%{AKw2#EbelvxVf-Lnz0N3+B3{ke>${a`IOd)Du=0$*%!DL2( HKdQhlU^6Y4 literal 0 HcmV?d00001 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..500bc70 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.linting.pylintEnabled": true +} \ No newline at end of file diff --git a/docs/.DS_Store b/docs/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a2e2c00b7458117b72fe2490cd2228e3dce1b3dd GIT binary patch literal 6148 zcmeHK!A{#i5S{MBJ86VV-tP(mor*L6UV$zioP1C%5AS zu0!jZt{pZO2ZQyu%@wcJY7SSt!Nvy0YpwO+u%;K5R=0MK&Mvo&W5p=WEWM45qsu^(wMFCMj6nG{Dc)8K9o@sm;Rum8g{#^xVeelo;ea6zE zT{^I_M*zesrmbO2zl>DlG5U<9L+n9Qt`yOgDn4Q;SB`m)c|K$5(3OMY!-wKKD?Xtp zdv_e)vvg3tLuo|;QD9twDRGwDFO6Z=HBJ+G{=Z rBXlz2l@9MqFtA%Oa%n4GM7M@{j{-!Wv2=(TH2D#*GDsr|yr=?qdUAIS literal 0 HcmV?d00001 diff --git a/makefile~ b/makefile~ new file mode 100644 index 0000000..b05592b --- /dev/null +++ b/makefile~ @@ -0,0 +1,71 @@ + +ROOT_DIR:=(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) +REQUIREMENTS=requirements.txt +# Detects which OS is being used +# Only relevant for virtual environment creation +ifeq ($(OS), Windows_NT) + SYSTEM_PYTHON=py +else + SYSTEM_PYTHON=python3 +endif + +VENV_ROOT=venv +VENV_BIN=$(VENV_ROOT)/bin +VENV_PIP=$(VENV_BIN)/pip3 +VENV_PYTHON=$(VENV_BIN)/python + +virtualenv: + @echo "Making virtual environment..." + @$(SYSTEM_PYTHON) -m venv venv + @echo "Installing all dependencies..." + $(VENV_PIP) install --upgrade pip + $(VENV_PIP) install -r $(REQUIREMENTS) + +all: uninstall install + +install: virtualenv + @echo "Installing sniffpy in the system" + @$(VENV_PIP) install -e . + @echo " " + @echo "/////////////////////////////////////// " + @echo " " + @echo " _____ _ __ __ " + @echo " / ____| (_)/ _|/ _| " + @echo " | (___ _ __ _| |_| |_ _ __ _ _ " + @echo " \___ \| '_ \| | _| _| '_ \| | | | " + @echo " ____) | | | | | | | | | |_) | |_| | " + @echo " |_____/|_| |_|_|_| |_| | .__/ \__, | " + @echo " | | __/ | " + @echo " |_| |___/ " + @echo " " + @echo "/////////////////////////////////////// " + @echo " " + @echo " " + + +uninstall: + @echo "Uninstalling sniffpy in the system" + $(VENV_PIP) uninstall sniffpy + @echo "Succesfully uninstalled sniffpy" + +stylecheck: base_stylecheck test_stylecheck + @echo "Done with stylecheck" + +base_stylecheck: + @echo "Checking codebase coding style" + @$(VENV_BIN)/pylint -v sniffpy + @echo " " + @echo " " + @echo " " + +test_stylecheck: + @echo "Checking coding style of tests" + @$(VENV_BIN)/pylint --rcfile .pylintrctest tests + @echo " " + @echo " " + @echo " " + +test: FORCE + $(VENV_BIN)/py.test + +FORCE: ; From 302dc99b0e2d197b30164811257d2e12e5d6d017 Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Sun, 25 Oct 2020 13:12:19 -0400 Subject: [PATCH 03/13] Enter --- tests/test_match.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/tests/test_match.py b/tests/test_match.py index dd5b027..1752f7b 100644 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -31,7 +31,7 @@ def test_match_pattern(): class TestImageMatching: - """ Class to test pattern matching of image Mimetypes""" + """Class to test pattern matching of image Mimetypes""" mime_types = ['image/gif', 'image/png', 'image/jpeg', 'undefined/undefined'] content = [ @@ -43,13 +43,13 @@ class TestImageMatching: @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) def test_match_image_pattern(self, mime, resource): - """ Tests the most importnat image MIMEs with simulated content""" + """ Tests the most important image MIMEs with simulated content""" computed_type = match.match_image_type_pattern(resource) actual_type = parse_mime_type(mime) assert computed_type == actual_type @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["image"])) - def test_match_image_pattern_wfile(self, expected_type, resource): + def test_match_image_pattern_file(self, expected_type, resource): computed_type = match.match_image_type_pattern(resource) assert computed_type == expected_type @@ -76,3 +76,30 @@ def test_match_video_audio_type_pattern(self, expected_type, resource): def test_match_font_type_pattern(self, expected_type, resource): computed_type = match.match_font_type_pattern(resource) assert computed_type == expected_type + +class FontTypeMatching: + """Class to test pattern matching of font MIME types""" + + mime_types = ['application/vnd.ms-fontobject','font/tff', 'font/otf', 'font/collection', 'font/woff', 'font/woff2'] + content = [ + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x4C\x50', + b'\x00\x01\x00\x00', + b'\x4F\x54\x54\x4F', + b'\x74\x74\x63\x66', + b'\x77\x4F\x46\x46', + b'\x77\x4F\x46\x32' + ] + + @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) + def test_match_font_pattern(self, mime, resource): + computed_type = match.match_font_type_pattern(resource) + actual_type = parse_mime_type(mime) + assert computed_type == actual_type + + @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["font"])) + def test_match_font_pattern_file(self, expected_type, resource): + computed_type = match.match_font_type_pattern(resource) + assert computed_type == expected_type \ No newline at end of file From d3ca6e13541b1ecbe7cbd572cda2adf7e41e4b64 Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Sun, 25 Oct 2020 15:42:44 -0400 Subject: [PATCH 04/13] git --- tests/test_match.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_match.py b/tests/test_match.py index 1752f7b..0c1b721 100644 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -102,4 +102,5 @@ def test_match_font_pattern(self, mime, resource): @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["font"])) def test_match_font_pattern_file(self, expected_type, resource): computed_type = match.match_font_type_pattern(resource) - assert computed_type == expected_type \ No newline at end of file + assert computed_type == expected_type + \ No newline at end of file From af1f87080fe91cfce867ccb7704156fc0e763388 Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Mon, 26 Oct 2020 01:08:06 -0400 Subject: [PATCH 05/13] Adding tests for fonts and changed syntax to conform with PEP8 --- tests/test_match.py | 18 +++++++++--------- tests/test_sniff.py | 1 + 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/test_match.py b/tests/test_match.py index 0c1b721..5b56ee5 100644 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -54,17 +54,17 @@ def test_match_image_pattern_file(self, expected_type, resource): assert computed_type == expected_type class TestAudioVideoMatching: - + "Testing for pattern matching of audio and video MIME types" def test_is_mp4_pattern(self): mp4_file_path = os.path.join(TEST_FILES_PATH, 'video/mp4.mp4') - with open(mp4_file_path, 'rb') as f: - resource = f.read() + with open(mp4_file_path, 'rb') as file: + resource = file.read() assert match.is_mp4_pattern(resource) def test_is_webm_pattern(self): webm_file_path = os.path.join(TEST_FILES_PATH, 'video/webm.webm') - with open(webm_file_path, 'rb') as f: - resource = f.read() + with open(webm_file_path, 'rb') as file: + resource = file.read() assert match.is_webm_pattern(resource) @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["audio", "video"])) @@ -79,8 +79,9 @@ def test_match_font_type_pattern(self, expected_type, resource): class FontTypeMatching: """Class to test pattern matching of font MIME types""" - - mime_types = ['application/vnd.ms-fontobject','font/tff', 'font/otf', 'font/collection', 'font/woff', 'font/woff2'] + + mime_types = ['application/vnd.ms-fontobject', 'font/tff', 'font/otf', 'font/collection', + 'font/woff', 'font/woff2'] content = [ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', @@ -98,9 +99,8 @@ def test_match_font_pattern(self, mime, resource): computed_type = match.match_font_type_pattern(resource) actual_type = parse_mime_type(mime) assert computed_type == actual_type - + @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["font"])) def test_match_font_pattern_file(self, expected_type, resource): computed_type = match.match_font_type_pattern(resource) assert computed_type == expected_type - \ No newline at end of file diff --git a/tests/test_sniff.py b/tests/test_sniff.py index 834ddaf..0b78377 100644 --- a/tests/test_sniff.py +++ b/tests/test_sniff.py @@ -7,6 +7,7 @@ from tests.resources import get_resource_test_list class TestSniffing: + """Implments tests pertaining to the sniff module""" mime_types = [ 'text/plain', 'text/plain', From 8922927117515df38f80a73c52ba19213f908847 Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Sun, 1 Nov 2020 14:53:12 -0500 Subject: [PATCH 06/13] making code publish ready --- sniffpy/utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sniffpy/utils.py b/sniffpy/utils.py index 30c4e2f..e7f1d22 100644 --- a/sniffpy/utils.py +++ b/sniffpy/utils.py @@ -1,8 +1,9 @@ """ Moudule with functions or utilities""" +from typing import Tuple import sniffpy.constants as const -def parse_vint(sequence: bytes, index: int) -> (int, int): +def parse_vint(sequence: bytes, index: int) -> Tuple[int, int]: """ Implementation of https://mimesniff.spec.whatwg.org/#parse-a-vint""" mask = 128 max_len = 8 @@ -112,9 +113,9 @@ def match_padded_sequence( return False i = 0 - print(len(sequence)) + #print(len(sequence)) while i + offset + len(pattern) < len(sequence): - print(sequence[offset + i: offset + i + len(pattern)]) + #print(sequence[offset + i: offset + i + len(pattern)]) if sequence[offset + i: offset + i + len(pattern)] == pattern: return True if sequence[offset + i] != 0: From 7284bd242e77d810df5084c505325566f961363e Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Sun, 1 Nov 2020 15:47:04 -0500 Subject: [PATCH 07/13] preparing code for publishing --- example.py | 2 +- setup.py | 3 ++- sniffpy/ref.py | 7 +++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/example.py b/example.py index 883723d..5d5d2a0 100644 --- a/example.py +++ b/example.py @@ -1,5 +1,5 @@ -import sniffpy import requests +import sniffpy r = requests.get("https://httpbin.org/image/jpeg") mime_type = sniffpy.sniff(r.content) #returns a MIMEType object diff --git a/setup.py b/setup.py index 3af5175..a1e67e2 100644 --- a/setup.py +++ b/setup.py @@ -5,13 +5,14 @@ setuptools.setup( name="sniffpy", - version="0.1-dev", + version="1.0.0", author="Codeprentice", author_email="nb2838@columbia.edu", description="A package for mime-sniffing", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/codeprentice-org/sniffpy", + download_url = 'https://github.com/woberton/sniffpy/archive/1.0.0.tar.gz', packages=setuptools.find_packages(), classifiers=[ "Programming Language :: Python :: 3", diff --git a/sniffpy/ref.py b/sniffpy/ref.py index 9f910e1..4a1fcf9 100644 --- a/sniffpy/ref.py +++ b/sniffpy/ref.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Tuple def check_condition(code_point: str, condition: List[str]) -> bool: @@ -12,8 +12,7 @@ def check_condition(code_point: str, condition: List[str]) -> bool: def collect_code_points(str_input: str, condition: List[str], pos: int, - exclusion: bool = True) -> (str, - int): + exclusion: bool = True) -> Tuple[str, int]: result = [] while pos != len(str_input) and check_condition( str_input[pos], condition) ^ exclusion: @@ -23,7 +22,7 @@ def collect_code_points(str_input: str, def collect_http_quoted_string( - str_input: str, pos: int, exact_value: bool = False) -> (str, int): + str_input: str, pos: int, exact_value: bool = False) -> Tuple[str, int]: position_start = pos value = "" assert str_input[pos] == '"' From 31d323e7e255443150464e4ac2387316a6f64a9e Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Sun, 1 Nov 2020 15:53:56 -0500 Subject: [PATCH 08/13] Enter --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a1e67e2..ed34cf2 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/codeprentice-org/sniffpy", - download_url = 'https://github.com/woberton/sniffpy/archive/1.0.0.tar.gz', + download_url='https://github.com/woberton/sniffpy/archive/1.0.0.tar.gz', packages=setuptools.find_packages(), classifiers=[ "Programming Language :: Python :: 3", From 5d5569cc1c9c2541ffc2cd9e619179f8919a5d37 Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Sun, 1 Nov 2020 16:10:13 -0500 Subject: [PATCH 09/13] style changes for publishing --- example.py | 4 ++++ sniffpy/sniff.py | 9 +++++---- sniffpy/utils.py | 3 +++ tests/test_sniff.py | 4 ++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/example.py b/example.py index 5d5d2a0..c46de21 100644 --- a/example.py +++ b/example.py @@ -1,5 +1,9 @@ +<<<<<<< Updated upstream import requests import sniffpy +======= +import requests, sniffpy +>>>>>>> Stashed changes r = requests.get("https://httpbin.org/image/jpeg") mime_type = sniffpy.sniff(r.content) #returns a MIMEType object diff --git a/sniffpy/sniff.py b/sniffpy/sniff.py index 15ef553..9e65a13 100644 --- a/sniffpy/sniff.py +++ b/sniffpy/sniff.py @@ -1,3 +1,4 @@ +from typing import Tuple from . import match from . import mimetype from .mimetype import MIMEType @@ -6,7 +7,7 @@ #The following functions are helper functions for sniff_mislabeled_feed #TODO: Rewrite the skip_* functions into one single neat parameterized function -def skip_comment(sequence: bytes, i: int, length: int) -> (int, bool): +def skip_comment(sequence: bytes, i: int, length: int) -> Tuple[int, bool]: """ Skips XML in the sequence (resource) They are in the form @@ -21,7 +22,7 @@ def skip_comment(sequence: bytes, i: int, length: int) -> (int, bool): i += 1 return i, False -def skip_markup_declaration(sequence: bytes, i: int, length: int) -> (int, bool): +def skip_markup_declaration(sequence: bytes, i: int, length: int) -> Tuple[int, bool]: """ Skips XML markup declarations in the sequence (resource) in the form @@ -36,7 +37,7 @@ def skip_markup_declaration(sequence: bytes, i: int, length: int) -> (int, bool) i += 1 return i, False -def skip_processing_instruction(sequence: bytes, i: int, length: int) -> (int, bool): +def skip_processing_instruction(sequence: bytes, i: int, length: int) -> Tuple[int, bool]: """ Skips XML processing instruction in the sequence (resource) They are in the form @@ -51,7 +52,7 @@ def skip_processing_instruction(sequence: bytes, i: int, length: int) -> (int, b i += 1 return i, False -def handle_rdf(sequence: bytes, i: int, length: int) -> (int, MIMEType): +def handle_rdf(sequence: bytes, i: int, length: int) -> Tuple[int, MIMEType]: """ Handles Resource Description Framework First checks whether it is an RDF diff --git a/sniffpy/utils.py b/sniffpy/utils.py index e7f1d22..52133e8 100644 --- a/sniffpy/utils.py +++ b/sniffpy/utils.py @@ -2,7 +2,10 @@ from typing import Tuple import sniffpy.constants as const +<<<<<<< Updated upstream +======= +>>>>>>> Stashed changes def parse_vint(sequence: bytes, index: int) -> Tuple[int, int]: """ Implementation of https://mimesniff.spec.whatwg.org/#parse-a-vint""" mask = 128 diff --git a/tests/test_sniff.py b/tests/test_sniff.py index 0b78377..531cff8 100644 --- a/tests/test_sniff.py +++ b/tests/test_sniff.py @@ -7,7 +7,11 @@ from tests.resources import get_resource_test_list class TestSniffing: +<<<<<<< Updated upstream """Implments tests pertaining to the sniff module""" +======= + """Implments tests pertaining to the sniff module""" +>>>>>>> Stashed changes mime_types = [ 'text/plain', 'text/plain', From ddc5cca048b1a14ddf343896beb9583b8231ee10 Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Sun, 1 Nov 2020 16:11:45 -0500 Subject: [PATCH 10/13] Enter --- example.py | 4 ---- sniffpy/utils.py | 4 ---- tests/test_sniff.py | 4 ---- 3 files changed, 12 deletions(-) diff --git a/example.py b/example.py index c46de21..5d5d2a0 100644 --- a/example.py +++ b/example.py @@ -1,9 +1,5 @@ -<<<<<<< Updated upstream import requests import sniffpy -======= -import requests, sniffpy ->>>>>>> Stashed changes r = requests.get("https://httpbin.org/image/jpeg") mime_type = sniffpy.sniff(r.content) #returns a MIMEType object diff --git a/sniffpy/utils.py b/sniffpy/utils.py index 52133e8..5f85d5a 100644 --- a/sniffpy/utils.py +++ b/sniffpy/utils.py @@ -2,10 +2,6 @@ from typing import Tuple import sniffpy.constants as const -<<<<<<< Updated upstream - -======= ->>>>>>> Stashed changes def parse_vint(sequence: bytes, index: int) -> Tuple[int, int]: """ Implementation of https://mimesniff.spec.whatwg.org/#parse-a-vint""" mask = 128 diff --git a/tests/test_sniff.py b/tests/test_sniff.py index 531cff8..0b78377 100644 --- a/tests/test_sniff.py +++ b/tests/test_sniff.py @@ -7,11 +7,7 @@ from tests.resources import get_resource_test_list class TestSniffing: -<<<<<<< Updated upstream """Implments tests pertaining to the sniff module""" -======= - """Implments tests pertaining to the sniff module""" ->>>>>>> Stashed changes mime_types = [ 'text/plain', 'text/plain', From 5985fdaf1bd11b49d80838ffddbf803b2381c6c1 Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Fri, 6 Nov 2020 21:06:37 -0500 Subject: [PATCH 11/13] test for audio and video files --- tests/test_match.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_match.py b/tests/test_match.py index 5b56ee5..62bdf6b 100644 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -104,3 +104,29 @@ def test_match_font_pattern(self, mime, resource): def test_match_font_pattern_file(self, expected_type, resource): computed_type = match.match_font_type_pattern(resource) assert computed_type == expected_type + +class VideoAudioTypeMatching: + """Class to test pattern matching of audio MIME types""" + + mime_types = ['audio/basic', 'audio/aff', 'audio/mpeg', 'audio/ogg', + 'font/midi', 'audio/avi, audio/wave'] + content = [ + b'\x2E\x73\x6E\x64', + b'\x46\x4F\x52\x4D\x00\x00\x00\x00\x41\x49\x46\x46', + b'\x49\x44\x33', + b'\x4F\x67\x67\x53\x00', + b'\x4D\x54\x68\x64\x00\x00\x00\x06', + b'\x52\x49\x46\x46\x00\x00\x00\x00\x41\x56\x49\x20', + b'\x52\x49\x46\x46\x00\x00\x00\x00\x57\x41\x56\x45' + ] + + @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) + def test_match_font_pattern(self, mime, resource): + computed_type = match.match_video_audio_type_pattern(resource) + actual_type = parse_mime_type(mime) + assert computed_type == actual_type + + @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["audio"])) + def test_match_font_pattern_file(self, expected_type, resource): + computed_type = match.match_video_audio_type_pattern(resource) + assert computed_type == expected_type From ba961ebc279b1d897e6bde31a4b6838fb8cb0401 Mon Sep 17 00:00:00 2001 From: Will O'Brien <70724174+woberton@users.noreply.github.com> Date: Fri, 6 Nov 2020 21:15:34 -0500 Subject: [PATCH 12/13] Delete test_match.py --- tests/test_match.py | 132 -------------------------------------------- 1 file changed, 132 deletions(-) delete mode 100644 tests/test_match.py diff --git a/tests/test_match.py b/tests/test_match.py deleted file mode 100644 index 62bdf6b..0000000 --- a/tests/test_match.py +++ /dev/null @@ -1,132 +0,0 @@ -""" This module should implement tests pertaining the match module in the package""" -import os - -import pytest - -import sniffpy.match as match -from sniffpy.mimetype import parse_mime_type -from tests.resources import TEST_FILES_PATH, get_resource_test_list - -def test_match_pattern(): - """Tests whether match_pattern implementation works""" - ignore = b'\x01\x02' - sequence = b'\x01\x02\xff' - mask = b'\xdd' - pattern = b'\xdd' - true_value = match.match_pattern( - resource=sequence, - ignored=ignore, - mask=mask, - pattern=pattern - ) - sequence = b'\x01\x00\x02\xff' - false_value = match.match_pattern( - resource=sequence, - ignored=ignore, - mask=mask, - pattern=pattern - ) - assert not false_value - assert true_value - - -class TestImageMatching: - """Class to test pattern matching of image Mimetypes""" - - mime_types = ['image/gif', 'image/png', 'image/jpeg', 'undefined/undefined'] - content = [ - b'\x47\x49\x46\x38\x39\x61\x32\xa4\x90', - b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x43', - b'\xff\xd8\xff\x78\x98\x23\x32\xfa\x89', - b'\xfa\xd8\xff\x78\x98\x23\x32\xfa\x89' - ] - - @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) - def test_match_image_pattern(self, mime, resource): - """ Tests the most important image MIMEs with simulated content""" - computed_type = match.match_image_type_pattern(resource) - actual_type = parse_mime_type(mime) - assert computed_type == actual_type - - @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["image"])) - def test_match_image_pattern_file(self, expected_type, resource): - computed_type = match.match_image_type_pattern(resource) - assert computed_type == expected_type - -class TestAudioVideoMatching: - "Testing for pattern matching of audio and video MIME types" - def test_is_mp4_pattern(self): - mp4_file_path = os.path.join(TEST_FILES_PATH, 'video/mp4.mp4') - with open(mp4_file_path, 'rb') as file: - resource = file.read() - assert match.is_mp4_pattern(resource) - - def test_is_webm_pattern(self): - webm_file_path = os.path.join(TEST_FILES_PATH, 'video/webm.webm') - with open(webm_file_path, 'rb') as file: - resource = file.read() - assert match.is_webm_pattern(resource) - - @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["audio", "video"])) - def test_match_video_audio_type_pattern(self, expected_type, resource): - computed_type = match.match_video_audio_type_pattern(resource) - assert computed_type == expected_type - - @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["font"])) - def test_match_font_type_pattern(self, expected_type, resource): - computed_type = match.match_font_type_pattern(resource) - assert computed_type == expected_type - -class FontTypeMatching: - """Class to test pattern matching of font MIME types""" - - mime_types = ['application/vnd.ms-fontobject', 'font/tff', 'font/otf', 'font/collection', - 'font/woff', 'font/woff2'] - content = [ - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x4C\x50', - b'\x00\x01\x00\x00', - b'\x4F\x54\x54\x4F', - b'\x74\x74\x63\x66', - b'\x77\x4F\x46\x46', - b'\x77\x4F\x46\x32' - ] - - @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) - def test_match_font_pattern(self, mime, resource): - computed_type = match.match_font_type_pattern(resource) - actual_type = parse_mime_type(mime) - assert computed_type == actual_type - - @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["font"])) - def test_match_font_pattern_file(self, expected_type, resource): - computed_type = match.match_font_type_pattern(resource) - assert computed_type == expected_type - -class VideoAudioTypeMatching: - """Class to test pattern matching of audio MIME types""" - - mime_types = ['audio/basic', 'audio/aff', 'audio/mpeg', 'audio/ogg', - 'font/midi', 'audio/avi, audio/wave'] - content = [ - b'\x2E\x73\x6E\x64', - b'\x46\x4F\x52\x4D\x00\x00\x00\x00\x41\x49\x46\x46', - b'\x49\x44\x33', - b'\x4F\x67\x67\x53\x00', - b'\x4D\x54\x68\x64\x00\x00\x00\x06', - b'\x52\x49\x46\x46\x00\x00\x00\x00\x41\x56\x49\x20', - b'\x52\x49\x46\x46\x00\x00\x00\x00\x57\x41\x56\x45' - ] - - @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) - def test_match_font_pattern(self, mime, resource): - computed_type = match.match_video_audio_type_pattern(resource) - actual_type = parse_mime_type(mime) - assert computed_type == actual_type - - @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["audio"])) - def test_match_font_pattern_file(self, expected_type, resource): - computed_type = match.match_video_audio_type_pattern(resource) - assert computed_type == expected_type From dea83de6e59e87d1f846cc459aa90aa4adab157f Mon Sep 17 00:00:00 2001 From: William O'Brien Date: Fri, 6 Nov 2020 21:19:10 -0500 Subject: [PATCH 13/13] test_match.py --- tests/test_match.py | 106 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 tests/test_match.py diff --git a/tests/test_match.py b/tests/test_match.py new file mode 100644 index 0000000..5b56ee5 --- /dev/null +++ b/tests/test_match.py @@ -0,0 +1,106 @@ +""" This module should implement tests pertaining the match module in the package""" +import os + +import pytest + +import sniffpy.match as match +from sniffpy.mimetype import parse_mime_type +from tests.resources import TEST_FILES_PATH, get_resource_test_list + +def test_match_pattern(): + """Tests whether match_pattern implementation works""" + ignore = b'\x01\x02' + sequence = b'\x01\x02\xff' + mask = b'\xdd' + pattern = b'\xdd' + true_value = match.match_pattern( + resource=sequence, + ignored=ignore, + mask=mask, + pattern=pattern + ) + sequence = b'\x01\x00\x02\xff' + false_value = match.match_pattern( + resource=sequence, + ignored=ignore, + mask=mask, + pattern=pattern + ) + assert not false_value + assert true_value + + +class TestImageMatching: + """Class to test pattern matching of image Mimetypes""" + + mime_types = ['image/gif', 'image/png', 'image/jpeg', 'undefined/undefined'] + content = [ + b'\x47\x49\x46\x38\x39\x61\x32\xa4\x90', + b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x43', + b'\xff\xd8\xff\x78\x98\x23\x32\xfa\x89', + b'\xfa\xd8\xff\x78\x98\x23\x32\xfa\x89' + ] + + @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) + def test_match_image_pattern(self, mime, resource): + """ Tests the most important image MIMEs with simulated content""" + computed_type = match.match_image_type_pattern(resource) + actual_type = parse_mime_type(mime) + assert computed_type == actual_type + + @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["image"])) + def test_match_image_pattern_file(self, expected_type, resource): + computed_type = match.match_image_type_pattern(resource) + assert computed_type == expected_type + +class TestAudioVideoMatching: + "Testing for pattern matching of audio and video MIME types" + def test_is_mp4_pattern(self): + mp4_file_path = os.path.join(TEST_FILES_PATH, 'video/mp4.mp4') + with open(mp4_file_path, 'rb') as file: + resource = file.read() + assert match.is_mp4_pattern(resource) + + def test_is_webm_pattern(self): + webm_file_path = os.path.join(TEST_FILES_PATH, 'video/webm.webm') + with open(webm_file_path, 'rb') as file: + resource = file.read() + assert match.is_webm_pattern(resource) + + @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["audio", "video"])) + def test_match_video_audio_type_pattern(self, expected_type, resource): + computed_type = match.match_video_audio_type_pattern(resource) + assert computed_type == expected_type + + @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["font"])) + def test_match_font_type_pattern(self, expected_type, resource): + computed_type = match.match_font_type_pattern(resource) + assert computed_type == expected_type + +class FontTypeMatching: + """Class to test pattern matching of font MIME types""" + + mime_types = ['application/vnd.ms-fontobject', 'font/tff', 'font/otf', 'font/collection', + 'font/woff', 'font/woff2'] + content = [ + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x4C\x50', + b'\x00\x01\x00\x00', + b'\x4F\x54\x54\x4F', + b'\x74\x74\x63\x66', + b'\x77\x4F\x46\x46', + b'\x77\x4F\x46\x32' + ] + + @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) + def test_match_font_pattern(self, mime, resource): + computed_type = match.match_font_type_pattern(resource) + actual_type = parse_mime_type(mime) + assert computed_type == actual_type + + @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["font"])) + def test_match_font_pattern_file(self, expected_type, resource): + computed_type = match.match_font_type_pattern(resource) + assert computed_type == expected_type