Jian Li | e47e21a | 2017-03-18 13:39:51 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.drivers.lisp.extensions; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onosproject.mapping.addresses.MappingAddress; |
| 24 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 25 | |
| 26 | import java.util.List; |
| 27 | |
| 28 | import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | import static org.hamcrest.Matchers.is; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for LispNatAddress extension class. |
| 33 | */ |
| 34 | public class LispNatAddressTest { |
| 35 | |
| 36 | private static final IpPrefix IP_ADDRESS_1 = IpPrefix.valueOf("1.2.3.4/24"); |
| 37 | private static final IpPrefix IP_ADDRESS_2 = IpPrefix.valueOf("5.6.7.8/24"); |
| 38 | |
| 39 | private static final IpPrefix RTR_ADDRESS_11 = IpPrefix.valueOf("10.1.1.1/24"); |
| 40 | private static final IpPrefix RTR_ADDRESS_12 = IpPrefix.valueOf("10.1.1.2/24"); |
| 41 | |
| 42 | private static final IpPrefix RTR_ADDRESS_21 = IpPrefix.valueOf("10.1.2.1/24"); |
| 43 | private static final IpPrefix RTR_ADDRESS_22 = IpPrefix.valueOf("10.1.2.2/24"); |
| 44 | |
| 45 | private static final short MS_UDP_PORT_NUMBER = 80; |
| 46 | private static final short ETR_UDP_PORT_NUMBER = 100; |
| 47 | |
| 48 | private LispNatAddress address1; |
| 49 | private LispNatAddress sameAsAddress1; |
| 50 | private LispNatAddress address2; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | |
| 55 | MappingAddress ma1 = MappingAddresses.ipv4MappingAddress(IP_ADDRESS_1); |
| 56 | |
| 57 | MappingAddress rtr11 = MappingAddresses.ipv4MappingAddress(RTR_ADDRESS_11); |
| 58 | MappingAddress rtr12 = MappingAddresses.ipv4MappingAddress(RTR_ADDRESS_12); |
| 59 | |
| 60 | List<MappingAddress> rtrRlocs1 = ImmutableList.of(rtr11, rtr12); |
| 61 | |
| 62 | address1 = new LispNatAddress.Builder() |
| 63 | .withMsUdpPortNumber(MS_UDP_PORT_NUMBER) |
| 64 | .withEtrUdpPortNumber(ETR_UDP_PORT_NUMBER) |
| 65 | .withGlobalEtrRlocAddress(ma1) |
| 66 | .withMsRlocAddress(ma1) |
| 67 | .withPrivateEtrRlocAddress(ma1) |
| 68 | .withRtrRlocAddresses(rtrRlocs1) |
| 69 | .build(); |
| 70 | |
| 71 | sameAsAddress1 = new LispNatAddress.Builder() |
| 72 | .withMsUdpPortNumber(MS_UDP_PORT_NUMBER) |
| 73 | .withEtrUdpPortNumber(ETR_UDP_PORT_NUMBER) |
| 74 | .withGlobalEtrRlocAddress(ma1) |
| 75 | .withMsRlocAddress(ma1) |
| 76 | .withPrivateEtrRlocAddress(ma1) |
| 77 | .withRtrRlocAddresses(rtrRlocs1) |
| 78 | .build(); |
| 79 | |
| 80 | MappingAddress ma2 = MappingAddresses.ipv4MappingAddress(IP_ADDRESS_2); |
| 81 | |
| 82 | MappingAddress rtr21 = MappingAddresses.ipv4MappingAddress(RTR_ADDRESS_21); |
| 83 | MappingAddress rtr22 = MappingAddresses.ipv4MappingAddress(RTR_ADDRESS_22); |
| 84 | |
| 85 | List<MappingAddress> rtrRlocs2 = ImmutableList.of(rtr21, rtr22); |
| 86 | |
| 87 | |
| 88 | address2 = new LispNatAddress.Builder() |
| 89 | .withMsUdpPortNumber(MS_UDP_PORT_NUMBER) |
| 90 | .withEtrUdpPortNumber(ETR_UDP_PORT_NUMBER) |
| 91 | .withGlobalEtrRlocAddress(ma2) |
| 92 | .withMsRlocAddress(ma2) |
| 93 | .withPrivateEtrRlocAddress(ma2) |
| 94 | .withRtrRlocAddresses(rtrRlocs2) |
| 95 | .build(); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testEquality() { |
| 100 | new EqualsTester() |
| 101 | .addEqualityGroup(address1, sameAsAddress1) |
| 102 | .addEqualityGroup(address2).testEquals(); |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void testConstruction() { |
| 107 | LispNatAddress address = address1; |
| 108 | |
| 109 | MappingAddress ma = MappingAddresses.ipv4MappingAddress(IP_ADDRESS_1); |
| 110 | |
| 111 | assertThat(address.getMsUdpPortNumber(), is(MS_UDP_PORT_NUMBER)); |
| 112 | assertThat(address.getEtrUdpPortNumber(), is(ETR_UDP_PORT_NUMBER)); |
| 113 | assertThat(address.getGlobalEtrRlocAddress(), is(ma)); |
| 114 | assertThat(address.getMsRlocAddress(), is(ma)); |
| 115 | assertThat(address.getPrivateEtrRlocAddress(), is(ma)); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testSerialization() { |
| 120 | LispNatAddress other = new LispNatAddress(); |
| 121 | other.deserialize(address1.serialize()); |
| 122 | |
| 123 | new EqualsTester() |
| 124 | .addEqualityGroup(address1, other) |
| 125 | .testEquals(); |
| 126 | } |
| 127 | } |