blob: 9d1448d6c899d74924d93b9b1260004040385b38 [file] [log] [blame]
Yoonseon Hanca814bf2016-09-12 11:37:48 -07001/*
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
17package org.onosproject.lisp.msg.protocols;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.testing.EqualsTester;
21import io.netty.buffer.ByteBuf;
22import io.netty.buffer.Unpooled;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.DeserializationException;
26import org.onlab.packet.IP;
27import org.onlab.packet.IPv4;
28import org.onlab.packet.IpAddress;
29import org.onlab.packet.UDP;
30import org.onosproject.lisp.msg.exceptions.LispParseError;
31import org.onosproject.lisp.msg.exceptions.LispReaderException;
32import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li5e505c62016-12-05 02:44:24 +090033import org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.DefaultEcmBuilder;
34import org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.EcmReader;
35import org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.EcmWriter;
36import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.DefaultRegisterBuilder;
37import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl.EcmBuilder;
38import org.onosproject.lisp.msg.protocols.LispMapRegister.RegisterBuilder;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070039import org.onosproject.lisp.msg.types.LispIpv4Address;
Jian Li5e505c62016-12-05 02:44:24 +090040import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
41import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
Yoonseon Hanca814bf2016-09-12 11:37:48 -070042
43import java.util.List;
44
45import static org.hamcrest.MatcherAssert.assertThat;
46import static org.hamcrest.Matchers.is;
47
Jian Li5e505c62016-12-05 02:44:24 +090048
Yoonseon Hanca814bf2016-09-12 11:37:48 -070049/**
50 * Unit tests for DefaultLispEncapsulatedControl class.
51 */
52public 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 Lid1a109e2016-11-12 09:00:42 +090060 private static final String AUTH_KEY = "onos";
61
Yoonseon Hanca814bf2016-09-12 11:37:48 -070062 private LispEncapsulatedControl ecm1;
63 private LispEncapsulatedControl sameAsEcm1;
64 private LispEncapsulatedControl ecm2;
65
66 @Before
67 public void setup() {
68
69 //Creates ecm1
Jian Li5e505c62016-12-05 02:44:24 +090070 EcmBuilder builder1 = new DefaultEcmBuilder();
Yoonseon Hanca814bf2016-09-12 11:37:48 -070071
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 Li5e505c62016-12-05 02:44:24 +090078 RegisterBuilder msgBuilder = new DefaultRegisterBuilder();
Yoonseon Hanca814bf2016-09-12 11:37:48 -070079
80 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(),
81 getMapRecord());
82
83 LispMapRegister innerMsg1 = msgBuilder.withIsProxyMapReply(true)
84 .withIsWantMapNotify(false)
85 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090086 .withAuthKey(AUTH_KEY)
Yoonseon Hanca814bf2016-09-12 11:37:48 -070087 .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 Li5e505c62016-12-05 02:44:24 +090098 EcmBuilder builder2 = new DefaultEcmBuilder();
Yoonseon Hanca814bf2016-09-12 11:37:48 -070099
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 Li5e505c62016-12-05 02:44:24 +0900106 RegisterBuilder msgBuilder2 = new DefaultRegisterBuilder();
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700107
108 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(),
109 getMapRecord());
110
111 LispMapRegister innerMsg2 = msgBuilder2.withIsProxyMapReply(true)
112 .withIsWantMapNotify(false)
113 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +0900114 .withAuthKey(AUTH_KEY)
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700115 .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 Li5e505c62016-12-05 02:44:24 +0900126 EcmBuilder builder3 = new DefaultEcmBuilder();
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700127
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 Li5e505c62016-12-05 02:44:24 +0900134 RegisterBuilder msgBuilder3 = new DefaultRegisterBuilder();
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700135
136 List<LispMapRecord> records3 = ImmutableList.of(getMapRecord(),
137 getMapRecord());
138
139 LispMapRegister innerMsg3 = msgBuilder3.withIsProxyMapReply(true)
140 .withIsWantMapNotify(false)
141 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +0900142 .withAuthKey(AUTH_KEY)
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700143 .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 Li5e505c62016-12-05 02:44:24 +0900163 DefaultLispEncapsulatedControl ecm = (DefaultLispEncapsulatedControl) ecm1;
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700164
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 Li5e505c62016-12-05 02:44:24 +0900190 EcmWriter writer = new EcmWriter();
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700191 writer.writeTo(byteBuf, ecm1);
192
Jian Li5e505c62016-12-05 02:44:24 +0900193 EcmReader reader = new EcmReader();
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700194
195 LispEncapsulatedControl deserialized = reader.readFrom(byteBuf);
196
Jian Li5e505c62016-12-05 02:44:24 +0900197 new EqualsTester().addEqualityGroup(ecm1, deserialized).testEquals();
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700198 }
199
200 private LispMapRecord getMapRecord() {
Jian Li5e505c62016-12-05 02:44:24 +0900201 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700202
203 LispIpv4Address ipv4Locator1 =
204 new LispIpv4Address(IpAddress.valueOf(RECORD_EID));
205
206 return builder1
207 .withRecordTtl(100)
Jian Li672ebda2017-02-06 20:21:04 +0900208 .withIsAuthoritative(true)
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700209 .withMapVersionNumber((short) 1)
210 .withMaskLength((byte) 0x01)
211 .withAction(LispMapReplyAction.NativelyForward)
212 .withEidPrefixAfi(ipv4Locator1)
213 .build();
214 }
215}