blob: ee52c5bfdda62bcdca3aaf3a94c84ed48ea2d267 [file] [log] [blame]
Jian Li18f3bce2016-08-04 17:36:41 +09001/*
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 */
16package org.onosproject.lisp.msg.protocols;
17
Jian Lie4ba2a42016-08-29 20:24:15 +090018import com.google.common.collect.ImmutableList;
Jian Li18f3bce2016-08-04 17:36:41 +090019import com.google.common.testing.EqualsTester;
Jian Lie4ba2a42016-08-29 20:24:15 +090020import io.netty.buffer.ByteBuf;
21import io.netty.buffer.Unpooled;
Jian Li18f3bce2016-08-04 17:36:41 +090022import org.junit.Before;
23import org.junit.Test;
Jian Lie4ba2a42016-08-29 20:24:15 +090024import org.onlab.packet.IpAddress;
25import org.onosproject.lisp.msg.exceptions.LispParseError;
26import org.onosproject.lisp.msg.exceptions.LispReaderException;
Jian Li5e505c62016-12-05 02:44:24 +090027import org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.DefaultRequestBuilder;
28import org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.RequestReader;
29import org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.RequestWriter;
30import org.onosproject.lisp.msg.protocols.LispMapRequest.RequestBuilder;
Jian Lie4ba2a42016-08-29 20:24:15 +090031import org.onosproject.lisp.msg.types.LispAfiAddress;
Jian Li5e505c62016-12-05 02:44:24 +090032import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Lie4ba2a42016-08-29 20:24:15 +090033import org.onosproject.lisp.msg.types.LispIpv4Address;
34
35import java.util.List;
Jian Li18f3bce2016-08-04 17:36:41 +090036
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.Matchers.is;
39
40/**
41 * Unit tests for DefaultLispMapRequest class.
42 */
Jian Li47671902016-08-11 01:18:18 +090043public final class DefaultLispMapRequestTest {
Jian Li18f3bce2016-08-04 17:36:41 +090044
Jian Li672ebda2017-02-06 20:21:04 +090045 private static final String EID_IP_ADDRESS_1 = "192.168.1.1";
46 private static final String EID_IP_ADDRESS_2 = "192.168.1.1";
47
48 private static final String RLOC_IP_ADDRESS_1_1 = "10.1.1.1";
49 private static final String RLOC_IP_ADDRESS_1_2 = "10.1.1.2";
50 private static final String RLOC_IP_ADDRESS_2_1 = "20.1.1.1";
51 private static final String RLOC_IP_ADDRESS_2_2 = "20.1.1.2";
52
Jian Li18f3bce2016-08-04 17:36:41 +090053 private LispMapRequest request1;
54 private LispMapRequest sameAsRequest1;
55 private LispMapRequest request2;
56
57 @Before
58 public void setup() {
59
Jian Lie4ba2a42016-08-29 20:24:15 +090060 RequestBuilder builder1 = new DefaultRequestBuilder();
61
Jian Li672ebda2017-02-06 20:21:04 +090062 LispIpv4Address ipv4Eid1 = new LispIpv4Address(IpAddress.valueOf(EID_IP_ADDRESS_1));
Jian Lie4ba2a42016-08-29 20:24:15 +090063
Jian Li672ebda2017-02-06 20:21:04 +090064 LispIpv4Address ipv4Rloc1 = new LispIpv4Address(IpAddress.valueOf(RLOC_IP_ADDRESS_1_1));
65 LispIpv4Address ipv4Rloc2 = new LispIpv4Address(IpAddress.valueOf(RLOC_IP_ADDRESS_1_2));
Jian Lie4ba2a42016-08-29 20:24:15 +090066
67 List<LispAfiAddress> rlocs1 = ImmutableList.of(ipv4Rloc1, ipv4Rloc2);
Jian Lib26d3502016-08-31 01:43:24 +090068 List<LispEidRecord> records1 = ImmutableList.of(getEidRecord(), getEidRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090069
70 request1 = builder1
71 .withIsAuthoritative(true)
72 .withIsMapDataPresent(true)
73 .withIsPitr(false)
74 .withIsProbe(false)
75 .withIsSmr(true)
76 .withIsSmrInvoked(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090077 .withSourceEid(ipv4Eid1)
78 .withItrRlocs(rlocs1)
Jian Lib26d3502016-08-31 01:43:24 +090079 .withEidRecords(records1)
Jian Li18f3bce2016-08-04 17:36:41 +090080 .withNonce(1L)
Jian Li3e1bac22016-12-06 02:08:12 +090081 .withReplyRecord(1)
Jian Li18f3bce2016-08-04 17:36:41 +090082 .build();
83
Jian Lie4ba2a42016-08-29 20:24:15 +090084 RequestBuilder builder2 = new DefaultRequestBuilder();
Jian Lib26d3502016-08-31 01:43:24 +090085 List<LispEidRecord> records2 = ImmutableList.of(getEidRecord(), getEidRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090086
87 sameAsRequest1 = builder2
88 .withIsAuthoritative(true)
89 .withIsMapDataPresent(true)
90 .withIsPitr(false)
91 .withIsProbe(false)
92 .withIsSmr(true)
93 .withIsSmrInvoked(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090094 .withSourceEid(ipv4Eid1)
95 .withItrRlocs(rlocs1)
Jian Lib26d3502016-08-31 01:43:24 +090096 .withEidRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090097 .withNonce(1L)
Jian Li3e1bac22016-12-06 02:08:12 +090098 .withReplyRecord(1)
Jian Li18f3bce2016-08-04 17:36:41 +090099 .build();
100
Jian Lie4ba2a42016-08-29 20:24:15 +0900101 RequestBuilder builder3 = new DefaultRequestBuilder();
102
Jian Li672ebda2017-02-06 20:21:04 +0900103 LispIpv4Address ipv4Eid2 = new LispIpv4Address(IpAddress.valueOf(EID_IP_ADDRESS_2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900104
Jian Li672ebda2017-02-06 20:21:04 +0900105 LispIpv4Address ipv4Rloc3 = new LispIpv4Address(IpAddress.valueOf(RLOC_IP_ADDRESS_2_1));
106 LispIpv4Address ipv4Rloc4 = new LispIpv4Address(IpAddress.valueOf(RLOC_IP_ADDRESS_2_2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900107
108 List<LispAfiAddress> rlocs2 = ImmutableList.of(ipv4Rloc3, ipv4Rloc4);
Jian Li18f3bce2016-08-04 17:36:41 +0900109
110 request2 = builder3
111 .withIsAuthoritative(false)
112 .withIsMapDataPresent(false)
113 .withIsPitr(true)
114 .withIsProbe(true)
115 .withIsSmr(false)
116 .withIsSmrInvoked(true)
Jian Lie4ba2a42016-08-29 20:24:15 +0900117 .withSourceEid(ipv4Eid2)
118 .withItrRlocs(rlocs2)
Jian Li18f3bce2016-08-04 17:36:41 +0900119 .withNonce(2L)
Jian Li3e1bac22016-12-06 02:08:12 +0900120 .withReplyRecord(2)
Jian Li18f3bce2016-08-04 17:36:41 +0900121 .build();
122 }
123
Jian Lib26d3502016-08-31 01:43:24 +0900124 private LispEidRecord getEidRecord() {
Jian Li672ebda2017-02-06 20:21:04 +0900125 LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf(RLOC_IP_ADDRESS_2_1));
Jian Lib26d3502016-08-31 01:43:24 +0900126 return new LispEidRecord((byte) 24, eid);
127 }
128
Jian Li18f3bce2016-08-04 17:36:41 +0900129 @Test
130 public void testEquality() {
131 new EqualsTester()
132 .addEqualityGroup(request1, sameAsRequest1)
133 .addEqualityGroup(request2).testEquals();
134 }
135
136 @Test
137 public void testConstruction() {
138 DefaultLispMapRequest request = (DefaultLispMapRequest) request1;
139
140 assertThat(request.isAuthoritative(), is(true));
141 assertThat(request.isMapDataPresent(), is(true));
142 assertThat(request.isPitr(), is(false));
143 assertThat(request.isProbe(), is(false));
144 assertThat(request.isSmr(), is(true));
145 assertThat(request.isSmrInvoked(), is(false));
146 assertThat(request.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900147 assertThat(request.getRecordCount(), is(2));
Jian Li3e1bac22016-12-06 02:08:12 +0900148 assertThat(request.getReplyRecord(), is(1));
Jian Lie4ba2a42016-08-29 20:24:15 +0900149 }
150
151 @Test
152 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
153 ByteBuf byteBuf = Unpooled.buffer();
154 RequestWriter writer = new RequestWriter();
155 writer.writeTo(byteBuf, request1);
156
157 RequestReader reader = new RequestReader();
158 LispMapRequest deserialized = reader.readFrom(byteBuf);
159
Jian Li5e505c62016-12-05 02:44:24 +0900160 new EqualsTester().addEqualityGroup(request1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900161 }
162}