Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [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 | |
| 17 | package org.onosproject.lisp.msg.protocols; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.testing.EqualsTester; |
| 21 | import io.netty.buffer.ByteBuf; |
| 22 | import io.netty.buffer.Unpooled; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.DeserializationException; |
| 26 | import org.onlab.packet.IP; |
| 27 | import org.onlab.packet.IPv4; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.UDP; |
| 30 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 31 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 32 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 33 | import org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.DefaultEcmBuilder; |
| 34 | import org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.EcmReader; |
| 35 | import org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.EcmWriter; |
| 36 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.DefaultRegisterBuilder; |
| 37 | import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl.EcmBuilder; |
| 38 | import org.onosproject.lisp.msg.protocols.LispMapRegister.RegisterBuilder; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 39 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 40 | import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder; |
| 41 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 42 | |
| 43 | import java.util.List; |
| 44 | |
| 45 | import static org.hamcrest.MatcherAssert.assertThat; |
| 46 | import static org.hamcrest.Matchers.is; |
| 47 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 48 | |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Unit tests for DefaultLispEncapsulatedControl class. |
| 51 | */ |
| 52 | public final class DefaultLispEncapsulatedControlTest { |
| 53 | |
| 54 | private static final String ECM1_SRC_IP = "192.168.1.1"; |
| 55 | private static final String ECM1_DST_IP = "192.168.1.2"; |
| 56 | private static final String ECM2_SRC_IP = "192.168.2.1"; |
| 57 | private static final String ECM2_DST_IP = "192.168.2.2"; |
| 58 | private static final String RECORD_EID = "1.1.1.1"; |
| 59 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 60 | private static final String AUTH_KEY = "onos"; |
| 61 | |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 62 | private LispEncapsulatedControl ecm1; |
| 63 | private LispEncapsulatedControl sameAsEcm1; |
| 64 | private LispEncapsulatedControl ecm2; |
| 65 | |
| 66 | @Before |
| 67 | public void setup() { |
| 68 | |
| 69 | //Creates ecm1 |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 70 | EcmBuilder builder1 = new DefaultEcmBuilder(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 71 | |
| 72 | IP innerIp1 = new IPv4().setSourceAddress(ECM1_SRC_IP) |
| 73 | .setDestinationAddress(ECM1_DST_IP) |
| 74 | .setProtocol(IPv4.PROTOCOL_UDP).setVersion((byte) (4 & 0xf)); |
| 75 | UDP innerUdp1 = new UDP().setSourcePort(1) |
| 76 | .setDestinationPort(2); |
| 77 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 78 | RegisterBuilder msgBuilder = new DefaultRegisterBuilder(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 79 | |
| 80 | List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), |
| 81 | getMapRecord()); |
| 82 | |
| 83 | LispMapRegister innerMsg1 = msgBuilder.withIsProxyMapReply(true) |
| 84 | .withIsWantMapNotify(false) |
| 85 | .withKeyId((short) 1) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 86 | .withAuthKey(AUTH_KEY) |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 87 | .withNonce(1L) |
| 88 | .withMapRecords(records1) |
| 89 | .build(); |
| 90 | |
| 91 | ecm1 = builder1.isSecurity(false) |
| 92 | .innerIpHeader(innerIp1) |
| 93 | .innerUdpHeader(innerUdp1) |
| 94 | .innerLispMessage(innerMsg1) |
| 95 | .build(); |
| 96 | |
| 97 | //Creates sameAsEcm1 |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 98 | EcmBuilder builder2 = new DefaultEcmBuilder(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 99 | |
| 100 | IP innerIp2 = new IPv4().setSourceAddress(ECM1_SRC_IP) |
| 101 | .setDestinationAddress(ECM1_DST_IP) |
| 102 | .setProtocol(IPv4.PROTOCOL_UDP); |
| 103 | UDP innerUdp2 = new UDP().setSourcePort(1) |
| 104 | .setDestinationPort(2); |
| 105 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 106 | RegisterBuilder msgBuilder2 = new DefaultRegisterBuilder(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 107 | |
| 108 | List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), |
| 109 | getMapRecord()); |
| 110 | |
| 111 | LispMapRegister innerMsg2 = msgBuilder2.withIsProxyMapReply(true) |
| 112 | .withIsWantMapNotify(false) |
| 113 | .withKeyId((short) 1) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 114 | .withAuthKey(AUTH_KEY) |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 115 | .withNonce(1L) |
| 116 | .withMapRecords(records2) |
| 117 | .build(); |
| 118 | |
| 119 | sameAsEcm1 = builder2.isSecurity(false) |
| 120 | .innerIpHeader(innerIp2) |
| 121 | .innerUdpHeader(innerUdp2) |
| 122 | .innerLispMessage(innerMsg2) |
| 123 | .build(); |
| 124 | |
| 125 | //Creates ecm2 |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 126 | EcmBuilder builder3 = new DefaultEcmBuilder(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 127 | |
| 128 | IP innerIp3 = new IPv4().setSourceAddress(ECM2_SRC_IP) |
| 129 | .setDestinationAddress(ECM2_DST_IP) |
| 130 | .setProtocol(IPv4.PROTOCOL_UDP); |
| 131 | UDP innerUdp3 = new UDP().setSourcePort(10) |
| 132 | .setDestinationPort(20); |
| 133 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 134 | RegisterBuilder msgBuilder3 = new DefaultRegisterBuilder(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 135 | |
| 136 | List<LispMapRecord> records3 = ImmutableList.of(getMapRecord(), |
| 137 | getMapRecord()); |
| 138 | |
| 139 | LispMapRegister innerMsg3 = msgBuilder3.withIsProxyMapReply(true) |
| 140 | .withIsWantMapNotify(false) |
| 141 | .withKeyId((short) 2) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 142 | .withAuthKey(AUTH_KEY) |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 143 | .withNonce(1L) |
| 144 | .withMapRecords(records3) |
| 145 | .build(); |
| 146 | |
| 147 | ecm2 = builder3.isSecurity(false) |
| 148 | .innerIpHeader(innerIp3) |
| 149 | .innerUdpHeader(innerUdp3) |
| 150 | .innerLispMessage(innerMsg3) |
| 151 | .build(); |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void testEquality() { |
| 156 | new EqualsTester() |
| 157 | .addEqualityGroup(ecm1, sameAsEcm1) |
| 158 | .addEqualityGroup(ecm2).testEquals(); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testConstruction() { |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 163 | DefaultLispEncapsulatedControl ecm = (DefaultLispEncapsulatedControl) ecm1; |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 164 | |
| 165 | assertThat("Inner Ip versions are not match", |
| 166 | ecm.innerIpHeader().getVersion(), is((byte) 4)); |
| 167 | assertThat("Inner Ip protocols are not match", |
| 168 | ((IPv4) ecm.innerIpHeader()).getProtocol(), |
| 169 | is(IPv4.PROTOCOL_UDP)); |
| 170 | assertThat("Inner IP source addresses are not match", |
| 171 | ((IPv4) ecm.innerIpHeader()).getSourceAddress(), |
| 172 | is(IPv4.toIPv4Address(ECM1_SRC_IP))); |
| 173 | assertThat("Inner IP destination addresses are not match", |
| 174 | ((IPv4) ecm.innerIpHeader()).getDestinationAddress(), |
| 175 | is(IPv4.toIPv4Address(ECM1_DST_IP))); |
| 176 | assertThat("Inner UDP source ports are not match", |
| 177 | ecm.innerUdp().getSourcePort(), is(1)); |
| 178 | assertThat("Inner UDP destination ports are not match", |
| 179 | ecm.innerUdp().getDestinationPort(), is(2)); |
| 180 | assertThat("Inner LISP control messages are not match", |
| 181 | ecm.getControlMessage().getType(), |
| 182 | is(LispType.LISP_MAP_REGISTER)); |
| 183 | } |
| 184 | |
| 185 | @Test |
| 186 | public void testSerialization() throws LispReaderException, |
| 187 | LispWriterException, LispParseError, DeserializationException { |
| 188 | |
| 189 | ByteBuf byteBuf = Unpooled.buffer(); |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 190 | EcmWriter writer = new EcmWriter(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 191 | writer.writeTo(byteBuf, ecm1); |
| 192 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 193 | EcmReader reader = new EcmReader(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 194 | |
| 195 | LispEncapsulatedControl deserialized = reader.readFrom(byteBuf); |
| 196 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 197 | new EqualsTester().addEqualityGroup(ecm1, deserialized).testEquals(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | private LispMapRecord getMapRecord() { |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 201 | MapRecordBuilder builder1 = new DefaultMapRecordBuilder(); |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 202 | |
| 203 | LispIpv4Address ipv4Locator1 = |
| 204 | new LispIpv4Address(IpAddress.valueOf(RECORD_EID)); |
| 205 | |
| 206 | return builder1 |
| 207 | .withRecordTtl(100) |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 208 | .withIsAuthoritative(true) |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 209 | .withMapVersionNumber((short) 1) |
| 210 | .withMaskLength((byte) 0x01) |
| 211 | .withAction(LispMapReplyAction.NativelyForward) |
| 212 | .withEidPrefixAfi(ipv4Locator1) |
| 213 | .build(); |
| 214 | } |
| 215 | } |