diff --git a/fido/fido.py b/fido/fido.py index 51fbfee..fb64ba1 100755 --- a/fido/fido.py +++ b/fido/fido.py @@ -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 diff --git a/tests/test_fido.py b/tests/test_fido.py index 952a588..99ef971 100644 --- a/tests/test_fido.py +++ b/tests/test_fido.py @@ -1,13 +1,50 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import print_function +import io +import tempfile 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" def test_perf_timer(): timer = PerfTimer() sleep(3.6) duration = timer.duration() assert duration > 0 + +def test_file_identification(): + """Reference for Fido-based format identification + 1. Create a byte-stream with a known magic number and serialise to tempfile. + 2. Call identify_file(...) to identify the file against Fido's known formats. + """ + # Create a temporary file on the host operating system. + tmp = tempfile.mkstemp() + tmp_file = tmp[1] + + # Write to the file our known magic-number. + with open(tmp_file, "wb") as new_file: + new_file.write(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(tmp_file) + +def test_stream_identification(): + """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)