From ec966023f17389dfe6a14c3b8cf5312addf62d80 Mon Sep 17 00:00:00 2001 From: Shi Su <67605788+shi-su@users.noreply.github.com> Date: Mon, 14 Jun 2021 12:13:10 -0700 Subject: [PATCH] [request parser] Add unit tests for request parser for multiple values (#1766) What I did Add unit tests for request parser to parse multiple values. Why I did it Add unit tests for the feature added in PR #1758. How I verified it Run unit tests locally and confirm the tests pass with the given conditions and constraints. --- tests/request_parser_ut.cpp | 172 +++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 1 deletion(-) diff --git a/tests/request_parser_ut.cpp b/tests/request_parser_ut.cpp index 6f6c822731e8..8409dc5b6645 100644 --- a/tests/request_parser_ut.cpp +++ b/tests/request_parser_ut.cpp @@ -1590,4 +1590,174 @@ TEST(request_parser, invalid_ipv6_addr_key_item) FAIL() << "Got unexpected exception"; } } -} \ No newline at end of file +} + +const request_description_t request_description_multi_value = { + { REQ_T_STRING }, + { + { "endpoint", REQ_T_IP_LIST }, + { "vni", REQ_T_UINT_LIST }, + { "mac_address", REQ_T_MAC_ADDRESS_LIST }, + }, + { } // no mandatory attributes +}; + +class TestRequestMultiValue : public Request +{ +public: + TestRequestMultiValue() : Request(request_description_multi_value, ':') { } +}; + +TEST(request_parser, multipleValues) +{ + size_t value_size = 3; + KeyOpFieldsValuesTuple t {"key1", "SET", + { + { "endpoint", "1.1.1.1,2.2.2.2,3.3.3.3" }, + { "vni", "11111,11112,11113" }, + { "mac_address", "02:03:04:05:06:07,12:13:14:15:16:17,22:23:24:25:26:27" }, + } + }; + + try + { + TestRequestMultiValue request; + + EXPECT_NO_THROW(request.parse(t)); + + EXPECT_STREQ(request.getOperation().c_str(), "SET"); + EXPECT_STREQ(request.getFullKey().c_str(), "key1"); + EXPECT_STREQ(request.getKeyString(0).c_str(), "key1"); + EXPECT_TRUE(request.getAttrFieldNames() == (std::unordered_set{"endpoint", "vni", "mac_address"})); + auto ep_list = request.getAttrIPList("endpoint"); + auto vni_list = request.getAttrUintList("vni"); + auto mac_list = request.getAttrMacAddressList("mac_address"); + std::vector expected_ep{ "1.1.1.1", "2.2.2.2", "3.3.3.3" }; + std::vector expected_vni{ 11111, 11112, 11113 }; + std::vector expected_mac{ "02:03:04:05:06:07", "12:13:14:15:16:17", "22:23:24:25:26:27" }; + EXPECT_EQ(ep_list.size(), value_size); + EXPECT_EQ(vni_list.size(), value_size); + EXPECT_EQ(mac_list.size(), value_size); + for (size_t idx = 0; idx < value_size; idx++) + { + EXPECT_STREQ(ep_list[idx].to_string().c_str(), expected_ep[idx].c_str()); + EXPECT_EQ(vni_list[idx], expected_vni[idx]); + EXPECT_STREQ(mac_list[idx].to_string().c_str(), expected_mac[idx].c_str()); + } + } + catch (const std::exception& e) + { + FAIL() << "Got unexpected exception " << e.what(); + } + catch (...) + { + FAIL() << "Got unexpected exception"; + } +} + +TEST(request_parser, multipleValuesIpv6) +{ + size_t value_size = 3; + KeyOpFieldsValuesTuple t {"key1", "SET", + { + { "endpoint", "fc00::72,fc00::76,fc00::7a" }, + { "vni", "11111,11112,11113" }, + { "mac_address", "02:03:04:05:06:07,12:13:14:15:16:17,22:23:24:25:26:27" }, + } + }; + + try + { + TestRequestMultiValue request; + + EXPECT_NO_THROW(request.parse(t)); + + EXPECT_STREQ(request.getOperation().c_str(), "SET"); + EXPECT_STREQ(request.getFullKey().c_str(), "key1"); + EXPECT_STREQ(request.getKeyString(0).c_str(), "key1"); + EXPECT_TRUE(request.getAttrFieldNames() == (std::unordered_set{"endpoint", "vni", "mac_address"})); + auto ep_list = request.getAttrIPList("endpoint"); + auto vni_list = request.getAttrUintList("vni"); + auto mac_list = request.getAttrMacAddressList("mac_address"); + std::vector expected_ep{ "fc00::72", "fc00::76", "fc00::7a" }; + std::vector expected_vni{ 11111, 11112, 11113 }; + std::vector expected_mac{ "02:03:04:05:06:07", "12:13:14:15:16:17", "22:23:24:25:26:27" }; + EXPECT_EQ(ep_list.size(), value_size); + EXPECT_EQ(vni_list.size(), value_size); + EXPECT_EQ(mac_list.size(), value_size); + for (size_t idx = 0; idx < value_size; idx++) + { + EXPECT_STREQ(ep_list[idx].to_string().c_str(), expected_ep[idx].c_str()); + EXPECT_EQ(vni_list[idx], expected_vni[idx]); + EXPECT_STREQ(mac_list[idx].to_string().c_str(), expected_mac[idx].c_str()); + } + } + catch (const std::exception& e) + { + FAIL() << "Got unexpected exception " << e.what(); + } + catch (...) + { + FAIL() << "Got unexpected exception"; + } +} + +TEST(request_parser, multipleValuesInvalidIP) +{ + KeyOpFieldsValuesTuple t {"key1", "SET", + { + { "endpoint", "1.1.1.1,invalid_ip,3.3.3.3" }, + { "vni", "11111,11112,11113" }, + { "mac_address", "02:03:04:05:06:07,12:13:14:15:16:17,22:23:24:25:26:27" }, + } + }; + + try + { + TestRequestMultiValue request; + request.parse(t); + FAIL() << "Expected std::invalid_argument"; + } + catch (const std::invalid_argument& e) + { + EXPECT_STREQ(e.what(), "Invalid ip address list: 1.1.1.1,invalid_ip,3.3.3.3"); + } + catch (const std::exception& e) + { + FAIL() << "Got unexpected exception " << e.what(); + } + catch (...) + { + FAIL() << "Expected std::invalid_argument, not other exception"; + } +} + +TEST(request_parser, multipleValuesInvalidMac) +{ + KeyOpFieldsValuesTuple t {"key1", "SET", + { + { "endpoint", "1.1.1.1,2.2.2.2,3.3.3.3" }, + { "vni", "11111,11112,11113" }, + { "mac_address", "02:03:04:05:06:07,invalid_mac,22:23:24:25:26:27" }, + } + }; + + try + { + TestRequestMultiValue request; + request.parse(t); + FAIL() << "Expected std::invalid_argument"; + } + catch (const std::invalid_argument& e) + { + EXPECT_STREQ(e.what(), "Invalid mac address list: 02:03:04:05:06:07,invalid_mac,22:23:24:25:26:27"); + } + catch (const std::exception& e) + { + FAIL() << "Got unexpected exception " << e.what(); + } + catch (...) + { + FAIL() << "Expected std::invalid_argument, not other exception"; + } +}