Jian Li | 5201576 | 2016-10-04 17:51:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.lisp.msg.types; |
| 17 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableList; |
Jian Li | 5201576 | 2016-10-04 17:51:16 +0900 | [diff] [blame] | 19 | import com.google.common.testing.EqualsTester; |
| 20 | import io.netty.buffer.ByteBuf; |
| 21 | import io.netty.buffer.Unpooled; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 26 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 27 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 28 | import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatAddressBuilder; |
| 29 | import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatLcafAddressReader; |
| 30 | import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatLcafAddressWriter; |
| 31 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame^] | 32 | import java.util.List; |
| 33 | |
Jian Li | 5201576 | 2016-10-04 17:51:16 +0900 | [diff] [blame] | 34 | import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | import static org.hamcrest.Matchers.is; |
| 36 | |
| 37 | /** |
| 38 | * Unit tests for LispNatLcafAddress class. |
| 39 | */ |
| 40 | public class LispNatLcafAddressTest { |
| 41 | |
| 42 | private LispNatLcafAddress address1; |
| 43 | private LispNatLcafAddress sameAsAddress1; |
| 44 | private LispNatLcafAddress address2; |
| 45 | |
| 46 | @Before |
| 47 | public void setup() { |
| 48 | |
| 49 | NatAddressBuilder builder1 = new NatAddressBuilder(); |
| 50 | |
| 51 | short msUdpPortNumber1 = 80; |
| 52 | short etrUdpPortNumber1 = 100; |
| 53 | LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 54 | LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2")); |
| 55 | LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3")); |
| 56 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame^] | 57 | LispIpv4Address rtrRloc11 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1")); |
| 58 | LispIpv4Address rtrRloc12 = new LispIpv4Address(IpAddress.valueOf("10.1.1.2")); |
| 59 | List<LispAfiAddress> rtrRlocs1 = ImmutableList.of(rtrRloc11, rtrRloc12); |
| 60 | |
Jian Li | 5201576 | 2016-10-04 17:51:16 +0900 | [diff] [blame] | 61 | address1 = builder1 |
| 62 | .withLength((short) 0) |
| 63 | .withMsUdpPortNumber(msUdpPortNumber1) |
| 64 | .withEtrUdpPortNumber(etrUdpPortNumber1) |
| 65 | .withGlobalEtrRlocAddress(globalEtrRlocAddress1) |
| 66 | .withMsRlocAddress(msRlocAddress1) |
| 67 | .withPrivateEtrRlocAddress(privateEtrRlocAddress1) |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame^] | 68 | .withRtrRlocAddresses(rtrRlocs1) |
Jian Li | 5201576 | 2016-10-04 17:51:16 +0900 | [diff] [blame] | 69 | .build(); |
| 70 | |
| 71 | NatAddressBuilder builder2 = new NatAddressBuilder(); |
| 72 | |
| 73 | sameAsAddress1 = builder2 |
| 74 | .withLength((short) 0) |
| 75 | .withMsUdpPortNumber(msUdpPortNumber1) |
| 76 | .withEtrUdpPortNumber(etrUdpPortNumber1) |
| 77 | .withGlobalEtrRlocAddress(globalEtrRlocAddress1) |
| 78 | .withMsRlocAddress(msRlocAddress1) |
| 79 | .withPrivateEtrRlocAddress(privateEtrRlocAddress1) |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame^] | 80 | .withRtrRlocAddresses(rtrRlocs1) |
Jian Li | 5201576 | 2016-10-04 17:51:16 +0900 | [diff] [blame] | 81 | .build(); |
| 82 | |
| 83 | NatAddressBuilder builder3 = new NatAddressBuilder(); |
| 84 | |
| 85 | short msUdpPortNumber2 = 81; |
| 86 | short etrUdpPortNumber2 = 101; |
| 87 | LispIpv4Address globalEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1")); |
| 88 | LispIpv4Address msRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2")); |
| 89 | LispIpv4Address privateEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.3")); |
| 90 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame^] | 91 | LispIpv4Address rtrRloc21 = new LispIpv4Address(IpAddress.valueOf("10.1.2.1")); |
| 92 | LispIpv4Address rtrRloc22 = new LispIpv4Address(IpAddress.valueOf("10.1.2.2")); |
| 93 | List<LispAfiAddress> rtrRlocs2 = ImmutableList.of(rtrRloc21, rtrRloc22); |
| 94 | |
Jian Li | 5201576 | 2016-10-04 17:51:16 +0900 | [diff] [blame] | 95 | address2 = builder3 |
| 96 | .withLength((short) 0) |
| 97 | .withMsUdpPortNumber(msUdpPortNumber2) |
| 98 | .withEtrUdpPortNumber(etrUdpPortNumber2) |
| 99 | .withGlobalEtrRlocAddress(globalEtrRlocAddress2) |
| 100 | .withMsRlocAddress(msRlocAddress2) |
| 101 | .withPrivateEtrRlocAddress(privateEtrRlocAddress2) |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame^] | 102 | .withRtrRlocAddresses(rtrRlocs2) |
Jian Li | 5201576 | 2016-10-04 17:51:16 +0900 | [diff] [blame] | 103 | .build(); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testEquality() { |
| 108 | new EqualsTester() |
| 109 | .addEqualityGroup(address1, sameAsAddress1) |
| 110 | .addEqualityGroup(address2).testEquals(); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testConstruction() { |
| 115 | LispNatLcafAddress natLcafAddress = address1; |
| 116 | |
| 117 | LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 118 | LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2")); |
| 119 | LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3")); |
| 120 | |
| 121 | assertThat(natLcafAddress.getLength(), is((short) 0)); |
| 122 | assertThat(natLcafAddress.getMsUdpPortNumber(), is((short) 80)); |
| 123 | assertThat(natLcafAddress.getEtrUdpPortNumber(), is((short) 100)); |
| 124 | assertThat(natLcafAddress.getGlobalEtrRlocAddress(), is(globalEtrRlocAddress1)); |
| 125 | assertThat(natLcafAddress.getMsRlocAddress(), is(msRlocAddress1)); |
| 126 | assertThat(natLcafAddress.getPrivateEtrRlocAddress(), is(privateEtrRlocAddress1)); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testSerialization() throws LispWriterException, LispParseError, LispReaderException { |
| 131 | ByteBuf byteBuf = Unpooled.buffer(); |
| 132 | |
| 133 | NatLcafAddressWriter writer = new NatLcafAddressWriter(); |
| 134 | writer.writeTo(byteBuf, address1); |
| 135 | |
| 136 | NatLcafAddressReader reader = new NatLcafAddressReader(); |
| 137 | LispNatLcafAddress deserialized = reader.readFrom(byteBuf); |
| 138 | |
| 139 | new EqualsTester() |
| 140 | .addEqualityGroup(address1, deserialized).testEquals(); |
| 141 | } |
| 142 | } |