1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-26 01:37:20 +00:00

rename valid_msgpacked_item to valid_msgpacked_dict

the code is generic, it can also be used for other msgpacked dictionaries.
This commit is contained in:
Thomas Waldmann 2016-06-12 15:07:49 +02:00
parent 90d621ce35
commit 866417853d
2 changed files with 9 additions and 9 deletions

View file

@ -595,8 +595,8 @@ def _open_rb(path):
REQUIRED_ITEM_KEYS = frozenset([b'path', b'mtime', ]) REQUIRED_ITEM_KEYS = frozenset([b'path', b'mtime', ])
def valid_msgpacked_item(d, item_keys_serialized): def valid_msgpacked_dict(d, keys_serialized):
"""check if the data <d> looks like a msgpacked item metadata dict""" """check if the data <d> looks like a msgpacked dict"""
d_len = len(d) d_len = len(d)
if d_len == 0: if d_len == 0:
return False return False
@ -606,7 +606,7 @@ def valid_msgpacked_item(d, item_keys_serialized):
offs = 3 offs = 3
else: else:
# object is not a map (dict) # object is not a map (dict)
# note: we must not have item dicts with > 2^16-1 elements # note: we must not have dicts with > 2^16-1 elements
return False return False
if d_len <= offs: if d_len <= offs:
return False return False
@ -620,7 +620,7 @@ def valid_msgpacked_item(d, item_keys_serialized):
return False return False
# is the bytestring any of the expected key names? # is the bytestring any of the expected key names?
key_serialized = d[offs:] key_serialized = d[offs:]
return any(key_serialized.startswith(pattern) for pattern in item_keys_serialized) return any(key_serialized.startswith(pattern) for pattern in keys_serialized)
class RobustUnpacker: class RobustUnpacker:
@ -654,7 +654,7 @@ def __next__(self):
if not data: if not data:
raise StopIteration raise StopIteration
# Abort early if the data does not look like a serialized item dict # Abort early if the data does not look like a serialized item dict
if not valid_msgpacked_item(data, self.item_keys): if not valid_msgpacked_dict(data, self.item_keys):
data = data[1:] data = data[1:]
continue continue
self._unpacker = msgpack.Unpacker(object_hook=StableDict) self._unpacker = msgpack.Unpacker(object_hook=StableDict)

View file

@ -4,7 +4,7 @@
import msgpack import msgpack
import pytest import pytest
from ..archive import Archive, CacheChunkBuffer, RobustUnpacker, valid_msgpacked_item, ITEM_KEYS from ..archive import Archive, CacheChunkBuffer, RobustUnpacker, valid_msgpacked_dict, ITEM_KEYS
from ..key import PlaintextKey from ..key import PlaintextKey
from ..helpers import Manifest from ..helpers import Manifest
from . import BaseTestCase from . import BaseTestCase
@ -127,7 +127,7 @@ def item_keys_serialized():
[42, 23.42, True, b'foobar', {b'foo': b'bar'}, [b'foo', b'bar'], (b'foo', b'bar')] [42, 23.42, True, b'foobar', {b'foo': b'bar'}, [b'foo', b'bar'], (b'foo', b'bar')]
)]) )])
def test_invalid_msgpacked_item(packed, item_keys_serialized): def test_invalid_msgpacked_item(packed, item_keys_serialized):
assert not valid_msgpacked_item(packed, item_keys_serialized) assert not valid_msgpacked_dict(packed, item_keys_serialized)
@pytest.mark.parametrize('packed', @pytest.mark.parametrize('packed',
@ -137,11 +137,11 @@ def test_invalid_msgpacked_item(packed, item_keys_serialized):
dict((k, b'x' * 1000) for k in ITEM_KEYS), # as big (key count and volume) as it gets dict((k, b'x' * 1000) for k in ITEM_KEYS), # as big (key count and volume) as it gets
]]) ]])
def test_valid_msgpacked_items(packed, item_keys_serialized): def test_valid_msgpacked_items(packed, item_keys_serialized):
assert valid_msgpacked_item(packed, item_keys_serialized) assert valid_msgpacked_dict(packed, item_keys_serialized)
def test_key_length_msgpacked_items(): def test_key_length_msgpacked_items():
key = b'x' * 32 # 31 bytes is the limit for fixstr msgpack type key = b'x' * 32 # 31 bytes is the limit for fixstr msgpack type
data = {key: b''} data = {key: b''}
item_keys_serialized = [msgpack.packb(key), ] item_keys_serialized = [msgpack.packb(key), ]
assert valid_msgpacked_item(msgpack.packb(data), item_keys_serialized) assert valid_msgpacked_dict(msgpack.packb(data), item_keys_serialized)