diff --git a/lib/rift/Annex.py b/lib/rift/Annex.py index 3d61b718..fbfc0ab7 100644 --- a/lib/rift/Annex.py +++ b/lib/rift/Annex.py @@ -231,12 +231,14 @@ def is_pointer(cls, filepath): Return true if content of file at filepath looks like a valid digest identifier. """ - meta = os.stat(filepath) - - # MD5 or SHA3 256 - if meta.st_size in (32, 64): - with open(filepath, encoding='utf-8') as fh: - identifier = fh.read(meta.st_size) + with open(filepath, encoding='utf-8') as fh: + identifier = fh.read() + # Remove possible trailing whitespace, newline and carriage return + # characters. + identifier = identifier.rstrip() + + # Check size corresponds to MD5 (32) or SHA3 256 (64). + if len(identifier) in (32, 64): return all(byte in string.hexdigits for byte in identifier) return False diff --git a/tests/Annex.py b/tests/Annex.py index b997fe2c..da155dcc 100644 --- a/tests/Annex.py +++ b/tests/Annex.py @@ -146,6 +146,31 @@ def test_is_pointer_valid_identifier(self): temp_file = make_temp_file(correct_identifier) self.assertTrue(Annex.is_pointer(temp_file.name)) + def test_is_pointer_valid_identifier_with_line_feed(self): + """ Test if is_pointer correctly detect a valid identifier with a line feed """ + + correct_identifier = '7CF2DB5EC261A0FA27A502D3196A6F60\n' + temp_file = make_temp_file(correct_identifier) + self.assertTrue(Annex.is_pointer(temp_file.name)) + + def test_is_pointer_valid_identifier_with_carriage_return(self): + """ + Test if is_pointer correctly detect a valid identifier with a carriage return. + """ + + correct_identifier = '7CF2DB5EC261A0FA27A502D3196A6F60\r\n' + temp_file = make_temp_file(correct_identifier) + self.assertTrue(Annex.is_pointer(temp_file.name)) + + def test_is_pointer_valid_identifier_with_whitespace(self): + """ + Test if is_pointer correctly detect a valid identifier with a whitespace. + """ + + correct_identifier = '7CF2DB5EC261A0FA27A502D3196A6F60 ' + temp_file = make_temp_file(correct_identifier) + self.assertTrue(Annex.is_pointer(temp_file.name)) + def test_is_pointer_invalid_identifier(self): """ Test if is_pointer correctly detect a invalid identifier """