Fix a bug in SocketAddress where "a.b.c.d:1" and "b.b.c.d:1" are incorrectly considered equal.

BUG=3558
R=mallinath@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/18749004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@6639 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
jiayl@webrtc.org
2014-07-09 19:14:24 +00:00
parent 7af12be781
commit 4b1f330b4f
2 changed files with 21 additions and 14 deletions

View File

@ -244,25 +244,20 @@ bool SocketAddress::operator==(const SocketAddress& addr) const {
}
bool SocketAddress::operator<(const SocketAddress& addr) const {
if (ip_ < addr.ip_)
return true;
else if (addr.ip_ < ip_)
return false;
if (ip_ != addr.ip_)
return ip_ < addr.ip_;
// We only check hostnames if both IPs are zero. This matches EqualIPs()
if (addr.IsAnyIP()) {
if (hostname_ < addr.hostname_)
return true;
else if (addr.hostname_ < hostname_)
return false;
}
// We only check hostnames if both IPs are ANY or unspecified. This matches
// EqualIPs().
if ((IPIsAny(ip_) || IPIsUnspec(ip_)) && hostname_ != addr.hostname_)
return hostname_ < addr.hostname_;
return port_ < addr.port_;
}
bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
return (ip_ == addr.ip_) &&
((!IPIsAny(ip_)) || (hostname_ == addr.hostname_));
((!IPIsAny(ip_) && !IPIsUnspec(ip_)) || (hostname_ == addr.hostname_));
}
bool SocketAddress::EqualPorts(const SocketAddress& addr) const {

View File

@ -290,10 +290,18 @@ TEST(SocketAddressTest, TestEqualityOperators) {
addr2 = SocketAddress("fe80::1", 5678);
EXPECT_PRED2(AreUnequal, addr1, addr2);
SocketAddress addr3("a.b.c.d", 1);
SocketAddress addr4("b.b.c.d", 1);
EXPECT_PRED2(AreUnequal, addr3, addr4);
EXPECT_PRED2(AreEqual, addr3, addr3);
addr3.SetIP(addr1.ip());
addr4.SetIP(addr1.ip());
EXPECT_PRED2(AreEqual,addr3, addr4);
}
bool IsLessThan(const SocketAddress& addr1,
const SocketAddress& addr2) {
bool IsLessThan(const SocketAddress& addr1, const SocketAddress& addr2) {
return addr1 < addr2 &&
!(addr2 < addr1) &&
!(addr1 == addr2);
@ -324,6 +332,10 @@ TEST(SocketAddressTest, TestComparisonOperator) {
addr2 = SocketAddress("fe80::1", 5678);
EXPECT_FALSE(addr1 < addr2);
EXPECT_FALSE(addr2 < addr1);
SocketAddress addr3("a.b.c.d", 1);
SocketAddress addr4("b.b.c.d", 1);
EXPECT_PRED2(IsLessThan, addr3, addr4);
}
TEST(SocketAddressTest, TestToSensitiveString) {