getSocketAddressList leading spaces handling and tests

This commit is contained in:
Matej Sekoranja
2014-11-17 09:56:40 +01:00
parent b3c030d946
commit 0a82f3c60a
2 changed files with 40 additions and 4 deletions

View File

@@ -146,8 +146,13 @@ InetAddrVector* getSocketAddressList(const std::string & list, int defaultPort,
const InetAddrVector* appendList) {
InetAddrVector* iav = new InetAddrVector();
// parse string
// skip leading spaces
size_t len = list.length();
size_t subStart = 0;
while (subStart < len && isspace(list[subStart]))
subStart++;
// parse string
size_t subEnd;
while((subEnd = list.find(' ', subStart))!=std::string::npos) {
string address = list.substr(subStart, (subEnd-subStart));
@@ -157,7 +162,7 @@ InetAddrVector* getSocketAddressList(const std::string & list, int defaultPort,
subStart = list.find_first_not_of(" \t\r\n\v", subEnd);
}
if(subStart!=std::string::npos&&list.length()>0) {
if(subStart!=std::string::npos && subStart<len) {
osiSockAddr addr;
if (aToIPAddr(list.substr(subStart).c_str(), defaultPort, &addr.ia) == 0)
iav->push_back(addr);

View File

@@ -74,7 +74,38 @@ void test_getSocketAddressList()
testOk1(htons(555) == addr.ia.sin_port);
testOk1(htonl(0xC0A80304) == addr.ia.sin_addr.s_addr);
testOk1("192.168.3.4:555" == inetAddressToString(addr));
// empty
auto_ptr<InetAddrVector> vec2(getSocketAddressList("", 1111));
testOk1(static_cast<size_t>(0) == vec2->size());
// just spaces
auto_ptr<InetAddrVector> vec3(getSocketAddressList(" ", 1111));
testOk1(static_cast<size_t>(0) == vec3->size());
// leading spaces
auto_ptr<InetAddrVector> vec4(getSocketAddressList(" 127.0.0.1 10.10.12.11:1234 192.168.3.4", 555));
testOk1(static_cast<size_t>(3) == vec4->size());
addr = vec4->at(0);
testOk1(AF_INET == addr.ia.sin_family);
testOk1(htons(555) == addr.ia.sin_port);
testOk1(htonl(0x7F000001) == addr.ia.sin_addr.s_addr);
testOk1("127.0.0.1:555" == inetAddressToString(addr));
addr = vec4->at(1);
testOk1(AF_INET == addr.ia.sin_family);
testOk1(htons(1234) == addr.ia.sin_port);
testOk1(htonl(0x0A0A0C0B) == addr.ia.sin_addr.s_addr);
testOk1("10.10.12.11:1234" == inetAddressToString(addr));
addr = vec4->at(2);
testOk1(AF_INET == addr.ia.sin_family);
testOk1(htons(555) == addr.ia.sin_port);
testOk1(htonl(0xC0A80304) == addr.ia.sin_addr.s_addr);
testOk1("192.168.3.4:555" == inetAddressToString(addr));
}
void test_ipv4AddressToInt()
@@ -329,7 +360,7 @@ void test_multicastLoopback()
MAIN(testInetAddressUtils)
{
testPlan(68);
testPlan(83);
testDiag("Tests for InetAddress utils");
test_getSocketAddressList();