Skip to content
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

FIX: Develop out FIDO tests with pytest #191

Closed
wants to merge 7 commits into from
Closed
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
7 changes: 4 additions & 3 deletions fido/fido.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,11 @@ def blocking_read(self, file, bytes_to_read):
buffer = b''
while bytes_read < bytes_to_read:
readbuffer = file.read(bytes_to_read - bytes_read)
last_read_len = len(readbuffer)
buffer += readbuffer
bytes_read = len(buffer)
# break out if EOF is reached.
if readbuffer == '':
bytes_read += last_read_len
# break out if EOF is reached, that is zero bytes read.
if last_read_len < 1:
break
return buffer

Expand Down
65 changes: 65 additions & 0 deletions tests/test_fido.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,78 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function

import csv
import io
from time import sleep

from fido import fido
from fido.fido import PerfTimer

# Magic number for fmt/1000.
MAGIC = b"\x5A\x58\x54\x61\x70\x65\x21\x1A\x01"

# Expected positive PUID.
PUID = "fmt/1000"

# Expected result.
OK = "OK"


def test_perf_timer():
timer = PerfTimer()
sleep(3.6)
duration = timer.duration()
assert duration > 0


def test_file_identification(tmp_path, capsys):
"""Reference for Fido-based format identification
1. Create a byte-stream with a known magic number and serialize to tempfile.
2. Call identify_file(...) to identify the file against Fido's known formats.
"""
# Create a temporary file and write our skeleton file out to it.
tmp_file = tmp_path / "tmp_file"
tmp_file.write_bytes(MAGIC)

# Create a Fido instance and call identify_file. The identify_file function
# will create and manage a file for itself.
f = fido.Fido()
f.identify_file(str(tmp_file))

# Capture the stdout returned by Fido and make assertions about its
# validity.
captured = capsys.readouterr()
assert captured.err == ""
reader = csv.reader(io.StringIO(captured.out), delimiter=",")
assert reader is not None
row = next(reader)
assert row[0] == OK, "row hasn't returned a positive identification"
assert row[2] == PUID, "row doesn't contain expected PUID value"
assert int(row[5]) == len(MAGIC), "row doesn't contain stream length"


def test_stream_identification(capsys):
"""Reference for Fido-based format identification
1. Create a byte-stream with a known magic number.
2. Call identify_stream(...) to identify the file against Fido's known formats.
"""
# Create the stream object with the known magic-number.
fstream = io.BytesIO(MAGIC)

# Create a Fido instance and call identify_stream. The identify_stream function
# will work on the stream as-is. This could be an open file handle that the
# caller is managing for itself.
f = fido.Fido()
f.identify_stream(fstream, "filename to display", extension=False)

# Capture the stdout returned by Fido and make assertions about its
# validity.
captured = capsys.readouterr()
assert captured.err == ""
reader = csv.reader(io.StringIO(captured.out), delimiter=",")
assert reader is not None
row = next(reader)
assert row[0] == OK, "row hasn't returned a positive identification"
assert row[2] == PUID, "row doesn't contain expected PUID value"
assert int(row[5]) == len(MAGIC), "row doesn't contain stream length"