From 380223191a61ff7f6aa4e44e157e5a2f2d4dc27b Mon Sep 17 00:00:00 2001 From: Carmine Scarpitta Date: Fri, 31 May 2024 04:49:26 -0500 Subject: [PATCH] Add more test cases to extend coverage Signed-off-by: Carmine Scarpitta --- .../fpmsyncd/receive_srv6_localsids_ut.cpp | 215 +++++++++++++----- .../fpmsyncd/ut_helpers_fpmsyncd.cpp | 52 +++-- .../mock_tests/fpmsyncd/ut_helpers_fpmsyncd.h | 4 +- 3 files changed, 185 insertions(+), 86 deletions(-) diff --git a/tests/mock_tests/fpmsyncd/receive_srv6_localsids_ut.cpp b/tests/mock_tests/fpmsyncd/receive_srv6_localsids_ut.cpp index 929f669155..752cdf3038 100644 --- a/tests/mock_tests/fpmsyncd/receive_srv6_localsids_ut.cpp +++ b/tests/mock_tests/fpmsyncd/receive_srv6_localsids_ut.cpp @@ -62,10 +62,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_END; struct nlmsg *nl_obj = create_srv6_localsid_nlmsg(RTM_NEWSRV6LOCALSID, &_localsid, _block_len, _node_len, _func_len, _arg_len, _action); @@ -105,10 +105,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_END_X; IpAddress _adj = IpAddress("2001:db8:1::1"); @@ -155,10 +155,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_END_T; char *_vrf = "Vrf10"; @@ -205,10 +205,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_END_DX6; IpAddress _adj = IpAddress("2001:db8:1::1"); @@ -254,10 +254,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_END_DX4; IpAddress _adj = IpAddress("10.0.0.1"); @@ -304,10 +304,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_END_DT4; char *_vrf = "Vrf10"; @@ -354,10 +354,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_END_DT6; char *_vrf = "Vrf10"; @@ -404,10 +404,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_END_DT46; char *_vrf = "Vrf10"; @@ -454,10 +454,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_UN; struct nlmsg *nl_obj = create_srv6_localsid_nlmsg(RTM_NEWSRV6LOCALSID, &_localsid, _block_len, _node_len, _func_len, _arg_len, _action); @@ -497,10 +497,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_UA; IpAddress _adj = IpAddress("2001:db8:1::1"); @@ -547,10 +547,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_UDX6; IpAddress _adj = IpAddress("2001:db8:1::1"); @@ -597,10 +597,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_UDX4; IpAddress _adj = IpAddress("10.0.0.1"); @@ -647,10 +647,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_UDT4; char *_vrf = "Vrf10"; @@ -697,10 +697,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_UDT6; char *_vrf = "Vrf10"; @@ -747,10 +747,10 @@ namespace ut_fpmsyncd /* Create a Netlink object containing an SRv6 Local SID */ IpAddress _localsid = IpAddress("fc00:0:1:1::"); - uint8_t _block_len = 32; - uint8_t _node_len = 16; - uint8_t _func_len = 16; - uint8_t _arg_len = 0; + int8_t _block_len = 32; + int8_t _node_len = 16; + int8_t _func_len = 16; + int8_t _arg_len = 0; uint32_t _action = SRV6_LOCALSID_ACTION_UDT46; char *_vrf = "Vrf10"; @@ -789,4 +789,99 @@ namespace ut_fpmsyncd /* Destroy the Netlink object and free the memory */ free_nlobj(nl_obj); } + + /* Test Receiving a route containing an invalid SRv6 Local SID */ + TEST_F(FpmSyncdSRv6LocalSIDsTest, RecevingRouteWithSRv6LocalSIDInvalid) + { + ASSERT_NE(m_routeSync, nullptr); + + struct nlmsg *nl_obj; + IpAddress _localsid = IpAddress("fc00:0:1:1::"); + int8_t _block_len; + int8_t _node_len; + int8_t _func_len; + int8_t _arg_len; + uint32_t _action = SRV6_LOCALSID_ACTION_UN; + std::string action; + + /* Create a Netlink object containing an SRv6 Local SID with missing block length */ + _block_len = -1; + _node_len = 16; + _func_len = 16; + _arg_len = 0; + + nl_obj = create_srv6_localsid_nlmsg(RTM_NEWSRV6LOCALSID, &_localsid, _block_len, _node_len, _func_len, _arg_len, _action); + if (!nl_obj) + printf("Error\n\n"); + + /* Send the Netlink object to the FpmLink */ + m_fpmLink->processRawMsg(&nl_obj->n); + + /* Ensure that fpmsyncd does not create an entry in APP_DB (because local SID is invalid)*/ + ASSERT_EQ(m_srv6LocalSidTable->hget("32:16:16:0:fc00:0:1:1::", "action", action), false); + + /* Destroy the Netlink object and free the memory */ + free_nlobj(nl_obj); + + + /* Create a Netlink object containing an SRv6 Local SID with missing node length */ + _block_len = 32; + _node_len = -1; + _func_len = 16; + _arg_len = 0; + + nl_obj = create_srv6_localsid_nlmsg(RTM_NEWSRV6LOCALSID, &_localsid, _block_len, _node_len, _func_len, _arg_len, _action); + if (!nl_obj) + printf("Error\n\n"); + + /* Send the Netlink object to the FpmLink */ + m_fpmLink->processRawMsg(&nl_obj->n); + + /* Ensure that fpmsyncd does not create an entry in APP_DB (because local SID is invalid)*/ + ASSERT_EQ(m_srv6LocalSidTable->hget("32:16:16:0:fc00:0:1:1::", "action", action), false); + + /* Destroy the Netlink object and free the memory */ + free_nlobj(nl_obj); + + + /* Create a Netlink object containing an SRv6 Local SID with missing function length */ + _block_len = 32; + _node_len = 16; + _func_len = -1; + _arg_len = 0; + + nl_obj = create_srv6_localsid_nlmsg(RTM_NEWSRV6LOCALSID, &_localsid, _block_len, _node_len, _func_len, _arg_len, _action); + if (!nl_obj) + printf("Error\n\n"); + + /* Send the Netlink object to the FpmLink */ + m_fpmLink->processRawMsg(&nl_obj->n); + + /* Ensure that fpmsyncd does not create an entry in APP_DB (because local SID is invalid)*/ + ASSERT_EQ(m_srv6LocalSidTable->hget("32:16:16:0:fc00:0:1:1::", "action", action), false); + + /* Destroy the Netlink object and free the memory */ + free_nlobj(nl_obj); + + + /* Create a Netlink object containing an SRv6 Local SID with missing argument length */ + _block_len = 32; + _node_len = 16; + _func_len = 16; + _arg_len = -1; + + nl_obj = create_srv6_localsid_nlmsg(RTM_NEWSRV6LOCALSID, &_localsid, _block_len, _node_len, _func_len, _arg_len, _action); + if (!nl_obj) + printf("Error\n\n"); + + /* Send the Netlink object to the FpmLink */ + m_fpmLink->processRawMsg(&nl_obj->n); + + /* Check that fpmsyncd created the correct entries in APP_DB (with default argument length)*/ + ASSERT_EQ(m_srv6LocalSidTable->hget("32:16:16:0:fc00:0:1:1::", "action", action), true); + + /* Destroy the Netlink object and free the memory */ + free_nlobj(nl_obj); + + } } \ No newline at end of file diff --git a/tests/mock_tests/fpmsyncd/ut_helpers_fpmsyncd.cpp b/tests/mock_tests/fpmsyncd/ut_helpers_fpmsyncd.cpp index d474b0c250..254466037d 100644 --- a/tests/mock_tests/fpmsyncd/ut_helpers_fpmsyncd.cpp +++ b/tests/mock_tests/fpmsyncd/ut_helpers_fpmsyncd.cpp @@ -190,10 +190,10 @@ namespace ut_fpmsyncd struct nlmsg *create_srv6_localsid_nlmsg( uint16_t cmd, IpAddress *localsid, - uint8_t block_len, - uint8_t node_len, - uint8_t func_len, - uint8_t arg_len, + int8_t block_len, + int8_t node_len, + int8_t func_len, + int8_t arg_len, uint32_t action, char *vrf, IpAddress *adj, @@ -253,32 +253,36 @@ namespace ut_fpmsyncd SRV6_LOCALSID_FORMAT); /* Add block bits length */ - if (!nl_attr_put8( - &nl_obj->n, sizeof(*nl_obj), - SRV6_LOCALSID_FORMAT_BLOCK_LEN, - block_len)) - return NULL; + if (block_len >= 0) + if (!nl_attr_put8( + &nl_obj->n, sizeof(*nl_obj), + SRV6_LOCALSID_FORMAT_BLOCK_LEN, + block_len)) + return NULL; /* Add node bits length */ - if (!nl_attr_put8( - &nl_obj->n, sizeof(*nl_obj), - SRV6_LOCALSID_FORMAT_NODE_LEN, - node_len)) - return NULL; + if (node_len >= 0) + if (!nl_attr_put8( + &nl_obj->n, sizeof(*nl_obj), + SRV6_LOCALSID_FORMAT_NODE_LEN, + node_len)) + return NULL; /* Add function bits length */ - if (!nl_attr_put8( - &nl_obj->n, sizeof(*nl_obj), - SRV6_LOCALSID_FORMAT_FUNC_LEN, - func_len)) - return NULL; + if (func_len >= 0) + if (!nl_attr_put8( + &nl_obj->n, sizeof(*nl_obj), + SRV6_LOCALSID_FORMAT_FUNC_LEN, + func_len)) + return NULL; /* Add argument bits length */ - if (!nl_attr_put8( - &nl_obj->n, sizeof(*nl_obj), - SRV6_LOCALSID_FORMAT_ARG_LEN, - arg_len)) - return NULL; + if (arg_len >= 0) + if (!nl_attr_put8( + &nl_obj->n, sizeof(*nl_obj), + SRV6_LOCALSID_FORMAT_ARG_LEN, + arg_len)) + return NULL; nl_attr_nest_end(&nl_obj->n, nest); diff --git a/tests/mock_tests/fpmsyncd/ut_helpers_fpmsyncd.h b/tests/mock_tests/fpmsyncd/ut_helpers_fpmsyncd.h index 5b410654b1..49725c00ea 100644 --- a/tests/mock_tests/fpmsyncd/ut_helpers_fpmsyncd.h +++ b/tests/mock_tests/fpmsyncd/ut_helpers_fpmsyncd.h @@ -98,8 +98,8 @@ namespace ut_fpmsyncd struct nlmsg *create_srv6_vpn_route_nlmsg(uint16_t cmd, IpPrefix *dst, IpAddress *encap_src_addr, IpAddress *vpn_sid, uint16_t table_id = 10); /* Build a Netlink object containing an SRv6 Local SID */ - struct nlmsg *create_srv6_localsid_nlmsg(uint16_t cmd, IpAddress *localsid, uint8_t block_len, - uint8_t node_len, uint8_t func_len, uint8_t arg_len, + struct nlmsg *create_srv6_localsid_nlmsg(uint16_t cmd, IpAddress *localsid, int8_t block_len, + int8_t node_len, int8_t func_len, int8_t arg_len, uint32_t action, char *vrf = NULL, IpAddress *nh = NULL, uint16_t table_id = 10); /* Free the memory allocated for a Netlink object */