From: Timo Tietavainen Date: Wed, 13 May 2020 11:21:09 +0000 (+0300) Subject: Improve SDL API function argument validation X-Git-Tag: 2.0.4~1 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=83a3e59661fc8d48095ae7ff0040b88763633e68;p=ric-plt%2Fsdlpy.git Improve SDL API function argument validation Improve SDL API set() function argument validation to cover also dictionary items. Issue-ID: RIC-379. Signed-off-by: Timo Tietavainen Change-Id: I006ec530e0127a731f96154088e29b3b9d0e49c6 --- diff --git a/docs/release-notes.rst b/docs/release-notes.rst index 257c772..bd884a6 100644 --- a/docs/release-notes.rst +++ b/docs/release-notes.rst @@ -33,6 +33,10 @@ This document provides the release notes of the ricsdl library. Version history --------------- +[2.0.4] - 2020-05-13 + +* Enhance SDL API set() function argument validation to cover also dictionary items. + [2.0.3] - 2020-01-22 * Add a new SDL storage API function `is_active()` to check healthiness of SDL instance. diff --git a/ricsdl-package/ricsdl/__init__.py b/ricsdl-package/ricsdl/__init__.py index 8d4b484..b4e3750 100644 --- a/ricsdl-package/ricsdl/__init__.py +++ b/ricsdl-package/ricsdl/__init__.py @@ -31,7 +31,7 @@ from .exceptions import ( ) -__version__ = '2.0.3' +__version__ = '2.0.4' __all__ = [ diff --git a/ricsdl-package/ricsdl/syncstorage.py b/ricsdl-package/ricsdl/syncstorage.py index 29adb17..0a2d9b3 100644 --- a/ricsdl-package/ricsdl/syncstorage.py +++ b/ricsdl-package/ricsdl/syncstorage.py @@ -143,6 +143,7 @@ class SyncStorage(SyncStorageAbc): @func_arg_checker(SdlTypeError, 1, ns=str, data_map=dict) def set(self, ns: str, data_map: Dict[str, bytes]) -> None: + self._validate_key_value_dict(data_map) self.__dbbackend.set(ns, data_map) @func_arg_checker(SdlTypeError, 1, ns=str, key=str, old_data=bytes, new_data=bytes) @@ -216,3 +217,11 @@ class SyncStorage(SyncStorageAbc): def get_configuration(self) -> _Configuration: """Return configuration what was valid when the SDL instance was initiated.""" return self.__configuration + + @classmethod + def _validate_key_value_dict(cls, kv): + for k, v in kv.items(): + if not isinstance(k, str): + raise SdlTypeError(r"Wrong dict key type: {}={}. Must be: str".format(k, type(k))) + if not isinstance(v, bytes): + raise SdlTypeError(r"Wrong dict value type: {}={}. Must be: bytes".format(v, type(v))) diff --git a/ricsdl-package/tests/test_syncstorage.py b/ricsdl-package/tests/test_syncstorage.py index 29fd5d4..7fb08dc 100644 --- a/ricsdl-package/tests/test_syncstorage.py +++ b/ricsdl-package/tests/test_syncstorage.py @@ -74,6 +74,10 @@ class TestSyncStorage: self.storage.set(123, {'a': b'v1'}) with pytest.raises(SdlTypeError): self.storage.set('ns', [1, 2]) + with pytest.raises(SdlTypeError): + self.storage.set('ns', {0xbad: b'v1'}) + with pytest.raises(SdlTypeError): + self.storage.set('ns', {'a': 0xbad}) def test_set_if_function_success(self): self.mock_db_backend.set_if.return_value = True