Skip to content

Testing2 #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.linting.pylintEnabled": true
}
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added docs/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
71 changes: 71 additions & 0 deletions makefile~
Original file line number Diff line number Diff line change
@@ -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: ;
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

setuptools.setup(
name="sniffpy",
version="0.1-dev",
version="1.0.0",
author="Codeprentice",
author_email="[email protected]",
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",
Expand Down
7 changes: 3 additions & 4 deletions sniffpy/ref.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Tuple


def check_condition(code_point: str, condition: List[str]) -> bool:
Expand All @@ -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:
Expand All @@ -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] == '"'
Expand Down
9 changes: 5 additions & 4 deletions sniffpy/sniff.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Tuple
from . import match
from . import mimetype
from .mimetype import MIMEType
Expand All @@ -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 <!-- comment -->
Expand All @@ -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 <! DECLARATION >
Expand All @@ -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 <? instruction ?>
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions sniffpy/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
""" 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
Expand Down Expand Up @@ -112,9 +112,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:
Expand Down
44 changes: 36 additions & 8 deletions tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -43,28 +43,28 @@ 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

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"]))
Expand All @@ -76,3 +76,31 @@ 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
1 change: 1 addition & 0 deletions tests/test_sniff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down