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