Skip to content

Commit

Permalink
improve tests for hmac store decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jochen Ott authored and fmarczin committed Apr 9, 2020
1 parent f7501a9 commit fee984c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
11 changes: 3 additions & 8 deletions simplekv/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def __init__(self, hm, source):
'(too small)')

def read(self, n=None):
if '' == self.buffer or 0 == n:
return ''
if b'' == self.buffer or 0 == n:
return b''

new_read = self.source.read(n) if n is not None else self.source.read()
finished = (n is None or len(new_read) != n)
Expand Down Expand Up @@ -94,11 +94,6 @@ def __init__(self, secret_key, decorated_store, hashfunc=hashlib.sha256):
self.__hashfunc = hashfunc
self.__secret_key = bytes(secret_key)

@property
def hmac_digestsize(self):
# returns, in bytes, the size of the digest
return self.hmac_mixin_hashfunc().digestsize

def __new_hmac(self, key, msg=None):
if not msg:
msg = b''
Expand Down Expand Up @@ -130,7 +125,7 @@ def get_file(self, key, file):
if isinstance(file, str):
try:
f = open(file, 'wb')
except OSError as e:
except (OSError, IOError) as e:
raise IOError('Error opening %s for writing: %r' % (
file, e
))
Expand Down
48 changes: 46 additions & 2 deletions tests/test_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,27 @@ def create_reader(self, stored_blob, secret_key, hashfunc):
def chunk_sizes(self, value):
return [10 ** n for n in xrange(2, 8)]

def test_close(self, create_reader):
reader = create_reader()
assert not reader.source.closed
reader.close()
assert reader.source.closed

def test_close_via_context(self, create_reader):
reader = create_reader()
assert not reader.source.closed
with reader as r:
assert r is reader
assert reader.source.closed

def test_reading_limit_0(self, create_reader):
reader = create_reader()
assert reader.read(0) == ''
assert reader.read(0) == ''
data = reader.read(0)
assert isinstance(data, bytes)
assert len(data) == 0
data = reader.read(0)
assert isinstance(data, bytes)
assert len(data) == 0

def test_reading_with_limit(self, secret_key, hashfunc, value,
create_reader, chunk_sizes):
Expand Down Expand Up @@ -100,6 +117,7 @@ def test_unbounded_read(self, value, create_reader):
# this only works with dicts, as we access the internal structures to
# manipulate values
class HMACDec(object):

@pytest.fixture
def hmacstore(self, secret_key, store):
return HMACDecorator(secret_key, store)
Expand All @@ -111,6 +129,32 @@ def test_get_fails_on_manipulation(self, hmacstore, key, value):
with pytest.raises(VerificationException):
hmacstore.get(key)

def test_copy_raises_not_implemented(self, store):
with pytest.raises(NotImplementedError):
HMACDecorator(b'secret', store).copy(u'src', u'dest')

def test_put_file_obj(self, key, value, hmacstore):
hmacstore.put_file(key, BytesIO(value))
assert hmacstore.get(key) == value

def test_put_file_str(self, key, value, hmacstore):
with tempfile.NamedTemporaryFile(mode='wb', delete=False) as f:
f.write(value)
hmacstore.put_file(key, f.name)
assert hmacstore.get(key) == value

def test_get_file_obj(self, key, value, hmacstore):
hmacstore.put(key, value)
b = BytesIO()
hmacstore.get_file(key, b)
assert b.getvalue() == value

def test_get_file_non_writable_target(self, key, value, hmacstore):
hmacstore.put(key, value)
path = '/tmp/this/file/does/not/exist'
with pytest.raises(IOError, match='Error opening {} for writing'.format(path)):
hmacstore.get_file(key, path)

def test_get_file_fails_on_manipulation(self, hmacstore, key, value):
hmacstore.put(key, value)
hmacstore.d[key] += b('a')
Expand Down

0 comments on commit fee984c

Please sign in to comment.