Skip to content
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
14 changes: 8 additions & 6 deletions lib/rift/Annex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/Annex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest: add the same test for spaces

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 3137f79.

"""
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 """

Expand Down