Improve SDL API function argument validation 84/3684/2
authorTimo Tietavainen <timo.tietavainen@nokia.com>
Wed, 13 May 2020 11:21:09 +0000 (14:21 +0300)
committerTimo Tietavainen <timo.tietavainen@nokia.com>
Wed, 13 May 2020 11:41:02 +0000 (14:41 +0300)
Improve SDL API set() function argument validation to cover also dictionary items.

Issue-ID: RIC-379.

Signed-off-by: Timo Tietavainen <timo.tietavainen@nokia.com>
Change-Id: I006ec530e0127a731f96154088e29b3b9d0e49c6

docs/release-notes.rst
ricsdl-package/ricsdl/__init__.py
ricsdl-package/ricsdl/syncstorage.py
ricsdl-package/tests/test_syncstorage.py

index 257c772..bd884a6 100644 (file)
@@ -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.
index 8d4b484..b4e3750 100644 (file)
@@ -31,7 +31,7 @@ from .exceptions import (
 )
 
 
-__version__ = '2.0.3'
+__version__ = '2.0.4'
 
 
 __all__ = [
index 29adb17..0a2d9b3 100644 (file)
@@ -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)))
index 29fd5d4..7fb08dc 100644 (file)
@@ -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