Skip to content

Commit

Permalink
[request parser] Add unit tests for request parser for multiple values (
Browse files Browse the repository at this point in the history
sonic-net#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 sonic-net#1758.

How I verified it
Run unit tests locally and confirm the tests pass with the given conditions and constraints.
  • Loading branch information
shi-su authored Jun 14, 2021
1 parent e56c492 commit ec96602
Showing 1 changed file with 171 additions and 1 deletion.
172 changes: 171 additions & 1 deletion tests/request_parser_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,4 +1590,174 @@ TEST(request_parser, invalid_ipv6_addr_key_item)
FAIL() << "Got unexpected exception";
}
}
}
}

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<std::string>{"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<std::string> expected_ep{ "1.1.1.1", "2.2.2.2", "3.3.3.3" };
std::vector<uint64_t> expected_vni{ 11111, 11112, 11113 };
std::vector<std::string> 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<std::string>{"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<std::string> expected_ep{ "fc00::72", "fc00::76", "fc00::7a" };
std::vector<uint64_t> expected_vni{ 11111, 11112, 11113 };
std::vector<std::string> 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";
}
}

0 comments on commit ec96602

Please sign in to comment.