Jian Li
Committed by Gerrit Code Review

Add unit test for LISP address to check obj equality & construction

This commit adds various unit tests of LISP addresses in a way to
check object equality and construction.
This commit also makes minor changes to existing LISP address
classes to allow the test cases to pass all unit tests.

Change-Id: Ice9f9634bdd243baaf1eed1539ea8d5808dda01d
......@@ -17,6 +17,8 @@ package org.onosproject.lisp.msg.types;
import org.onlab.packet.IpAddress;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkArgument;
/**
......@@ -33,4 +35,23 @@ public class LispIpv4Address extends LispIpAddress {
super(address, AddressFamilyIdentifierEnum.IP);
checkArgument(address.isIp4());
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof LispIpv4Address) {
final LispIpv4Address other = (LispIpv4Address) obj;
return Objects.equals(this.address, other.address) &&
Objects.equals(this.getAfi(), other.getAfi());
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(address, getAfi());
}
}
......
......@@ -17,6 +17,8 @@ package org.onosproject.lisp.msg.types;
import org.onlab.packet.IpAddress;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkArgument;
/**
......@@ -33,4 +35,23 @@ public class LispIpv6Address extends LispIpAddress {
super(address, AddressFamilyIdentifierEnum.IP);
checkArgument(address.isIp6());
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof LispIpv6Address) {
final LispIpv6Address other = (LispIpv6Address) obj;
return Objects.equals(this.address, other.address) &&
Objects.equals(this.getAfi(), other.getAfi());
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(address, getAfi());
}
}
......
......@@ -15,6 +15,8 @@
*/
package org.onosproject.lisp.msg.types;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
......@@ -61,6 +63,7 @@ public class LispListLcafAddress extends LispLcafAddress {
*/
public LispListLcafAddress(List<LispAfiAddress> addresses) {
super(LispCanonicalAddressFormatEnum.LIST, LENGTH);
this.addresses = addresses;
}
/**
......@@ -69,7 +72,7 @@ public class LispListLcafAddress extends LispLcafAddress {
* @return a set of AFI addresses
*/
public List<LispAfiAddress> getAddresses() {
return addresses;
return ImmutableList.copyOf(addresses);
}
@Override
......
......@@ -17,6 +17,8 @@ package org.onosproject.lisp.msg.types;
import org.onlab.packet.MacAddress;
import java.util.Objects;
/**
* MAC address that is used by LISP Locator.
*/
......@@ -50,7 +52,16 @@ public class LispMacAddress extends LispAfiAddress {
@Override
public boolean equals(Object obj) {
return address.equals(obj);
if (this == obj) {
return true;
}
if (obj instanceof LispMacAddress) {
final LispMacAddress other = (LispMacAddress) obj;
return Objects.equals(this.address, other.address) &&
Objects.equals(this.getAfi(), other.getAfi());
}
return false;
}
@Override
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispAppDataLcafAddress class.
*/
public class LispAppDataLcafAddressTest {
private LispAppDataLcafAddress address1;
private LispAppDataLcafAddress sameAsAddress1;
private LispAppDataLcafAddress address2;
@Before
public void setup() {
LispAfiAddress ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
address1 = new LispAppDataLcafAddress((byte) 0x01, 1, (short) 10, (short) 20, ipv4Address1);
sameAsAddress1 = new LispAppDataLcafAddress((byte) 0x01, 1, (short) 10, (short) 20, ipv4Address1);
LispAfiAddress ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
address2 = new LispAppDataLcafAddress((byte) 0x02, 2, (short) 20, (short) 40, ipv4Address2);
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispAppDataLcafAddress appDataLcafAddress = address1;
LispAfiAddress ipv4Address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
assertThat(appDataLcafAddress.getProtocol(), is((byte) 0x01));
assertThat(appDataLcafAddress.getIpTos(), is(1));
assertThat(appDataLcafAddress.getLocalPort(), is((short) 10));
assertThat(appDataLcafAddress.getRemotePort(), is((short) 20));
assertThat(appDataLcafAddress.getAddress(), is(ipv4Address));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispAsAddress class.
*/
public class LispAsAddressTest {
private LispAsAddress address1;
private LispAsAddress sameAsAddress1;
private LispAsAddress address2;
@Before
public void setup() {
address1 = new LispAsAddress(1);
sameAsAddress1 = new LispAsAddress(1);
address2 = new LispAsAddress(2);
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispAsAddress asAddress = address1;
assertThat(asAddress.getASNum(), is(1));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispDistinguishedNameAddress class.
*/
public class LispDistinguishedNameAddressTest {
private LispDistinguishedNameAddress address1;
private LispDistinguishedNameAddress sameAsAddress1;
private LispDistinguishedNameAddress address2;
@Before
public void setup() {
address1 = new LispDistinguishedNameAddress("distAddress1");
sameAsAddress1 = new LispDistinguishedNameAddress("distAddress1");
address2 = new LispDistinguishedNameAddress("distAddress2");
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispDistinguishedNameAddress distinguishedNameAddress = address1;
assertThat(distinguishedNameAddress.getDistinguishedName(), is("distAddress1"));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispIpv4Address class.
*/
public class LispIpv4AddressTest {
private LispIpv4Address address1;
private LispIpv4Address sameAsAddress1;
private LispIpv4Address address2;
@Before
public void setup() {
address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
sameAsAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispIpv4Address ipv4Address = address1;
assertThat(ipv4Address.getAddress(), is(IpAddress.valueOf("192.168.1.1")));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispIpv6Address class.
*/
public class LispIpv6AddressTest {
private LispIpv6Address address1;
private LispIpv6Address sameAsAddress1;
private LispIpv6Address address2;
@Before
public void setup() {
address1 = new LispIpv6Address(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"));
sameAsAddress1 = new LispIpv6Address(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"));
address2 = new LispIpv6Address(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8886"));
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispIpv6Address ipv6Address = address1;
assertThat(ipv6Address.getAddress(), is(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885")));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispLcafAddress class.
*/
public class LispLcafAddressTest {
private LispLcafAddress address1;
private LispLcafAddress sameAsAddress1;
private LispLcafAddress address2;
@Before
public void setup() {
address1 = new LispLcafAddress(LispCanonicalAddressFormatEnum.NAT,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01);
sameAsAddress1 = new LispLcafAddress(LispCanonicalAddressFormatEnum.NAT,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01);
address2 = new LispLcafAddress(LispCanonicalAddressFormatEnum.NAT,
(byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x02);
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispLcafAddress lcafAddress = address1;
assertThat(lcafAddress.getType(), is(LispCanonicalAddressFormatEnum.NAT));
assertThat(lcafAddress.getReserved1(), is((byte) 0x01));
assertThat(lcafAddress.getReserved2(), is((byte) 0x01));
assertThat(lcafAddress.getFlag(), is((byte) 0x01));
assertThat(lcafAddress.getLength(), is((byte) 0x01));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.collect.Lists;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispListLcafAddress class.
*/
public class LispListLcafAddressTest {
private LispListLcafAddress address1;
private LispListLcafAddress sameAsAddress1;
private LispListLcafAddress address2;
@Before
public void setup() {
LispAfiAddress ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
LispAfiAddress ipv6Address1 = new LispIpv6Address(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"));
List<LispAfiAddress> afiAddresses1 = Lists.newArrayList();
afiAddresses1.add(ipv4Address1);
afiAddresses1.add(ipv6Address1);
address1 = new LispListLcafAddress(afiAddresses1);
sameAsAddress1 = new LispListLcafAddress(afiAddresses1);
LispAfiAddress ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
LispAfiAddress ipv6Address2 = new LispIpv6Address(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8886"));
List<LispAfiAddress> afiAddresses2 = Lists.newArrayList();
afiAddresses2.add(ipv4Address2);
afiAddresses2.add(ipv6Address2);
address2 = new LispListLcafAddress(afiAddresses2);
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispListLcafAddress listLcafAddress = address1;
LispAfiAddress ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
LispAfiAddress ipv6Address1 = new LispIpv6Address(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"));
List<LispAfiAddress> afiAddresses1 = Lists.newArrayList();
afiAddresses1.add(ipv4Address1);
afiAddresses1.add(ipv6Address1);
assertThat(listLcafAddress.getAddresses(), is(afiAddresses1));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.MacAddress;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispMacAddress class.
*/
public class LispMacAddressTest {
private LispMacAddress address1;
private LispMacAddress sameAsAddress1;
private LispMacAddress address2;
@Before
public void setup() {
address1 = new LispMacAddress(MacAddress.valueOf("00:00:00:00:00:01"));
sameAsAddress1 = new LispMacAddress(MacAddress.valueOf("00:00:00:00:00:01"));
address2 = new LispMacAddress(MacAddress.valueOf("00:00:00:00:00:02"));
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispMacAddress macAddress = address1;
assertThat(macAddress.getAddress(), is(MacAddress.valueOf("00:00:00:00:00:01")));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispSegmentLcafAddress class.
*/
public class LispSegmentLcafAddressTest {
private LispSegmentLcafAddress address1;
private LispSegmentLcafAddress sameAsAddress1;
private LispSegmentLcafAddress address2;
@Before
public void setup() {
LispIpv4Address ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
address1 = new LispSegmentLcafAddress((byte) 0x01, 1, ipv4Address1);
sameAsAddress1 = new LispSegmentLcafAddress((byte) 0x01, 1, ipv4Address1);
LispIpv4Address ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
address2 = new LispSegmentLcafAddress((byte) 0x02, 2, ipv4Address2);
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispSegmentLcafAddress segmentLcafAddress = address1;
LispIpv4Address ipv4Address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
assertThat(segmentLcafAddress.getIdMaskLength(), is((byte) 0x01));
assertThat(segmentLcafAddress.getInstanceId(), is(1));
assertThat(segmentLcafAddress.getAddress(), is(ipv4Address));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for LispSourceDestLcafAddress class.
*/
public class LispSourceDestLcafAddressTest {
private LispSourceDestLcafAddress address1;
private LispSourceDestLcafAddress sameAsAddress1;
private LispSourceDestLcafAddress address2;
@Before
public void setup() {
LispIpv4Address srcAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
LispIpv4Address dstAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
address1 = new LispSourceDestLcafAddress((short) 1, (byte) 0x01,
(byte) 0x01, srcAddress1, dstAddress1);
sameAsAddress1 = new LispSourceDestLcafAddress((short) 1, (byte) 0x01,
(byte) 0x01, srcAddress1, dstAddress1);
LispIpv4Address srcAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
LispIpv4Address dstAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2"));
address2 = new LispSourceDestLcafAddress((short) 2, (byte) 0x02,
(byte) 0x02, srcAddress2, dstAddress2);
}
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(address1, sameAsAddress1)
.addEqualityGroup(address2).testEquals();
}
@Test
public void testConstruction() {
LispSourceDestLcafAddress sourceDestLcafAddress = address1;
LispIpv4Address srcAddress = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
LispIpv4Address dstAddress = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
assertThat(sourceDestLcafAddress.getReserved(), is((short) 1));
assertThat(sourceDestLcafAddress.getSrcMaskLength(), is((byte) 0x01));
assertThat(sourceDestLcafAddress.getDstMaskLength(), is((byte) 0x01));
assertThat(sourceDestLcafAddress.getSrcPrefix(), is(srcAddress));
assertThat(sourceDestLcafAddress.getDstPrefix(), is(dstAddress));
}
}