Skip to content

Commit

Permalink
Consume FIDO result as CSV and make assertions
Browse files Browse the repository at this point in the history
Bring in a CSV reader to consume the FIDO output and make various
assertions about the result's validity.
  • Loading branch information
ross-spencer committed May 25, 2020
1 parent f5e65ea commit f5adcce
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions tests/test_fido.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

import csv
import io
from time import sleep

Expand All @@ -11,6 +12,12 @@
# 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()
Expand All @@ -19,9 +26,9 @@ def test_perf_timer():
assert duration > 0


def test_file_identification(tmp_path):
def test_file_identification(tmp_path, capsys):
"""Reference for Fido-based format identification
1. Create a byte-stream with a known magic number and serialise to tempfile.
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.
Expand All @@ -33,16 +40,39 @@ def test_file_identification(tmp_path):
f = fido.Fido()
f.identify_file(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():
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"

0 comments on commit f5adcce

Please sign in to comment.