Fix MEID unit test, catch init failure 40/2640/7
authorE. Scott Daniels <daniels@research.att.com>
Tue, 3 Mar 2020 20:30:59 +0000 (15:30 -0500)
committerE. Scott Daniels <daniels@research.att.com>
Tue, 3 Mar 2020 21:35:14 +0000 (16:35 -0500)
The RMR change to support routing based on the MEID (190665fe)
broke the related unit tests. This change fixes the unit test.

This change also adds a check to the init wrapper function to
ensure that an error is generated if RMR is unable to initialise
the underlying transport.

Issue-ID: RICAPP-74

Signed-off-by: E. Scott Daniels <daniels@research.att.com>
Change-Id: I1f3ca3f8e821cd1356fb5247404b8cafd22a6119

src/bindings/rmr-python/docs/release-notes.rst
src/bindings/rmr-python/rmr-version.yaml
src/bindings/rmr-python/rmr/exceptions.py
src/bindings/rmr-python/rmr/rmr.py
src/bindings/rmr-python/setup.py
src/bindings/rmr-python/tests/test_rmr.py

index 010cb50..79ea500 100644 (file)
@@ -8,6 +8,15 @@ and this project adheres to `Semantic
 Versioning <http://semver.org/>`__.
 
 
+[2.2.1] - 3/3/2020
+--------------------
+
+::
+
+    * Correct the MEID unit test (broken by RMR commit 190665fe)
+    * Change rmr_init to catch init failure reported by RMR
+
+
 [2.2.0] - 12/5/2019
 --------------------
 
index 0d9c301..5808bc4 100644 (file)
@@ -1,3 +1,3 @@
 # CI script installs RMR from PackageCloud using this version
 ---
-version: 1.10.2
+version: 1.13.1
index f427b7f..c1f1a88 100644 (file)
@@ -1,6 +1,6 @@
 # ==================================================================================
-#       Copyright (c) 2019 Nokia
-#       Copyright (c) 2018-2019 AT&T Intellectual Property.
+#       Copyright (c) 2019-2020 Nokia
+#       Copyright (c) 2018-2020 AT&T Intellectual Property.
 #
 #   Licensed under the Apache License, Version 2.0 (the "License");
 #   you may not use this file except in compliance with the License.
@@ -21,3 +21,11 @@ Custom Exceptions
 
 class BadBufferAllocation(BaseException):
     """a bad buffer was allocated, check the rmr context"""
+
+
+class MeidSizeOutOfRange(BaseException):
+    """an attempt to set the MEID with a buffer that was too large"""
+
+
+class InitFailed(BaseException):
+    """rmr init failure, the context is unusable"""
index 336dca3..03799d7 100644 (file)
@@ -1,7 +1,7 @@
 # vim: expandtab ts=4 sw=4:
 # ==================================================================================
-#       Copyright (c) 2019 Nokia
-#       Copyright (c) 2018-2019 AT&T Intellectual Property.
+#       Copyright (c) 2019-2020 Nokia
+#       Copyright (c) 2018-2020 AT&T Intellectual Property.
 #
 #   Licensed under the Apache License, Version 2.0 (the "License");
 #   you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@ import json
 from ctypes import RTLD_GLOBAL, Structure, c_int, POINTER, c_char, c_char_p, c_void_p, memmove, cast
 from ctypes import CDLL
 from ctypes import create_string_buffer
-from rmr.exceptions import BadBufferAllocation
+from rmr.exceptions import BadBufferAllocation, MeidSizeOutOfRange, InitFailed
 
 # https://docs.python.org/3.7/library/ctypes.html
 # https://stackoverflow.com/questions/2327344/ctypes-loading-a-c-shared-library-that-has-dependencies/30845750#30845750
@@ -169,8 +169,14 @@ def rmr_init(uproto_port, max_msg_size, flags):
     """
     Refer to rmr C documentation for rmr_init
     extern void* rmr_init(char* uproto_port, int max_msg_size, int flags)
+
+    This python function checks that the context is not None and raises
+    an excption if it is.
     """
-    return _rmr_init(uproto_port, max_msg_size, flags)
+    mrc = _rmr_init(uproto_port, max_msg_size, flags)
+    if mrc is None:
+        raise InitFailed()
+    return mrc
 
 
 _rmr_ready = rmr_c_lib.rmr_ready
@@ -391,7 +397,16 @@ def rmr_set_meid(ptr_mbuf, byte_str):
     """
     Refer to the rmr C documentation for rmr_bytes2meid
     extern int rmr_bytes2meid(rmr_mbuf_t* mbuf, unsigned char const* src, int len);
+
+    Caution:  the meid length supported in an RMR message is 32 bytes, but C applications
+    expect this to be a nil terminated string and thus only 31 bytes are actually available.
+
+    Raises: exceptions.MeidSizeOutOfRang
     """
+    max = _get_constants().get("RMR_MAX_MEID", 32)
+    if len(byte_str) >= max:
+        raise MeidSizeOutOfRange
+
     return _rmr_bytes2meid(ptr_mbuf, byte_str, len(byte_str))
 
 
@@ -421,7 +436,7 @@ def rmr_get_meid(ptr_mbuf):
     string:
         meid
     """
-    sz = _get_constants().get("RMR_MAX_MEID", 64)  # size for buffer to fill
+    sz = _get_constants().get("RMR_MAX_MEID", 32)  # size for buffer to fill
     buf = create_string_buffer(sz)
     _rmr_get_meid(ptr_mbuf, buf)
     return buf.value
index 4d666c4..472196e 100644 (file)
@@ -32,7 +32,7 @@ def _long_descr():
 
 setup(
     name="rmr",
-    version="2.2.0",
+    version="2.2.1",
     packages=find_packages(),
     author="Tommy Carpenter, E. Scott Daniels",
     description="Python wrapper for RIC RMR",
index b0e43ee..33b61f7 100644 (file)
@@ -1,7 +1,7 @@
 # vim: ts=4 sw=4 expandtab:
 # =================================================================================2
-#       Copyright (c) 2019 Nokia
-#       Copyright (c) 2018-2019 AT&T Intellectual Property.
+#       Copyright (c) 2019-2020 Nokia
+#       Copyright (c) 2018-2020 AT&T Intellectual Property.
 #
 #   Licensed under the Apache License, Version 2.0 (the "License");
 #   you may not use this file except in compliance with the License.
@@ -110,17 +110,23 @@ def test_meid():
     assert rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b"\x01\x02"
     assert len(rmr.rmr_get_meid(sbuf)) == 2
 
-    rmr.rmr_set_meid(sbuf, b"\x00" * 32)
+    rmr.rmr_set_meid(sbuf, b"\x00" * 31)
     assert rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b""  # NULL bytes get truncated
 
-    rmr.rmr_set_meid(sbuf, b"6" * 32)
-    assert rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b"6" * 32  # string in string out
+    rmr.rmr_set_meid(sbuf, b"6" * 31)
+    assert rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b"6" * 31  # string in string out
 
     rmr.rmr_set_meid(sbuf, b"\x01\x02")
     assert (
-        rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b"\x01\x02" + b"6" * 30
-    )  # bytes in string out, 6s left over
-    assert len(rmr.rmr_get_meid(sbuf)) == 32
+        rmr.rmr_get_meid(sbuf) == rmr.message_summary(sbuf)["meid"] == b"\x01\x02"
+    )  # Ctypes will chop at first nil, so expect only 2 bytes back
+
+    assert len(rmr.rmr_get_meid(sbuf)) == 2
+
+    # test that an exception is raised for buffers which are too long
+    with pytest.raises(exceptions.MeidSizeOutOfRange):
+        rmr.rmr_set_meid(sbuf, b"8" * 32)
+
 
 
 def test_rmr_set_get():