blob: 6a4fd54891d55976ddcdd4cc3873b815726615c6 [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;
27import org.onosproject.lisp.msg.exceptions.LispWriterException;
28import org.onosproject.lisp.msg.types.LispAfiAddress;
29import org.onosproject.lisp.msg.types.LispIpv4Address;
30
31import java.util.List;
Jian Li18f3bce2016-08-04 17:36:41 +090032
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.is;
Jian Lie4ba2a42016-08-29 20:24:15 +090035import static org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.*;
Jian Li18f3bce2016-08-04 17:36:41 +090036
37/**
38 * Unit tests for DefaultLispMapRequest class.
39 */
Jian Li47671902016-08-11 01:18:18 +090040public final class DefaultLispMapRequestTest {
Jian Li18f3bce2016-08-04 17:36:41 +090041
42 private LispMapRequest request1;
43 private LispMapRequest sameAsRequest1;
44 private LispMapRequest request2;
45
46 @Before
47 public void setup() {
48
Jian Lie4ba2a42016-08-29 20:24:15 +090049 RequestBuilder builder1 = new DefaultRequestBuilder();
50
51 LispIpv4Address ipv4Eid1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
52
53 LispIpv4Address ipv4Rloc1 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1"));
54 LispIpv4Address ipv4Rloc2 = new LispIpv4Address(IpAddress.valueOf("10.1.1.2"));
55
56 List<LispAfiAddress> rlocs1 = ImmutableList.of(ipv4Rloc1, ipv4Rloc2);
Jian Lib26d3502016-08-31 01:43:24 +090057 List<LispEidRecord> records1 = ImmutableList.of(getEidRecord(), getEidRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090058
59 request1 = builder1
60 .withIsAuthoritative(true)
61 .withIsMapDataPresent(true)
62 .withIsPitr(false)
63 .withIsProbe(false)
64 .withIsSmr(true)
65 .withIsSmrInvoked(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090066 .withSourceEid(ipv4Eid1)
67 .withItrRlocs(rlocs1)
Jian Lib26d3502016-08-31 01:43:24 +090068 .withEidRecords(records1)
Jian Li18f3bce2016-08-04 17:36:41 +090069 .withNonce(1L)
Jian Li18f3bce2016-08-04 17:36:41 +090070 .build();
71
Jian Lie4ba2a42016-08-29 20:24:15 +090072 RequestBuilder builder2 = new DefaultRequestBuilder();
Jian Lib26d3502016-08-31 01:43:24 +090073 List<LispEidRecord> records2 = ImmutableList.of(getEidRecord(), getEidRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090074
75 sameAsRequest1 = builder2
76 .withIsAuthoritative(true)
77 .withIsMapDataPresent(true)
78 .withIsPitr(false)
79 .withIsProbe(false)
80 .withIsSmr(true)
81 .withIsSmrInvoked(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090082 .withSourceEid(ipv4Eid1)
83 .withItrRlocs(rlocs1)
Jian Lib26d3502016-08-31 01:43:24 +090084 .withEidRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090085 .withNonce(1L)
Jian Li18f3bce2016-08-04 17:36:41 +090086 .build();
87
Jian Lie4ba2a42016-08-29 20:24:15 +090088 RequestBuilder builder3 = new DefaultRequestBuilder();
89
90 LispIpv4Address ipv4Eid2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
91
Jian Lib26d3502016-08-31 01:43:24 +090092 LispIpv4Address ipv4Rloc3 = new LispIpv4Address(IpAddress.valueOf("20.1.1.1"));
93 LispIpv4Address ipv4Rloc4 = new LispIpv4Address(IpAddress.valueOf("20.1.1.2"));
Jian Lie4ba2a42016-08-29 20:24:15 +090094
95 List<LispAfiAddress> rlocs2 = ImmutableList.of(ipv4Rloc3, ipv4Rloc4);
Jian Li18f3bce2016-08-04 17:36:41 +090096
97 request2 = builder3
98 .withIsAuthoritative(false)
99 .withIsMapDataPresent(false)
100 .withIsPitr(true)
101 .withIsProbe(true)
102 .withIsSmr(false)
103 .withIsSmrInvoked(true)
Jian Lie4ba2a42016-08-29 20:24:15 +0900104 .withSourceEid(ipv4Eid2)
105 .withItrRlocs(rlocs2)
Jian Li18f3bce2016-08-04 17:36:41 +0900106 .withNonce(2L)
Jian Li18f3bce2016-08-04 17:36:41 +0900107 .build();
108 }
109
Jian Lib26d3502016-08-31 01:43:24 +0900110 private LispEidRecord getEidRecord() {
111 LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf("20.1.1.1"));
112 return new LispEidRecord((byte) 24, eid);
113 }
114
Jian Li18f3bce2016-08-04 17:36:41 +0900115 @Test
116 public void testEquality() {
117 new EqualsTester()
118 .addEqualityGroup(request1, sameAsRequest1)
119 .addEqualityGroup(request2).testEquals();
120 }
121
122 @Test
123 public void testConstruction() {
124 DefaultLispMapRequest request = (DefaultLispMapRequest) request1;
125
126 assertThat(request.isAuthoritative(), is(true));
127 assertThat(request.isMapDataPresent(), is(true));
128 assertThat(request.isPitr(), is(false));
129 assertThat(request.isProbe(), is(false));
130 assertThat(request.isSmr(), is(true));
131 assertThat(request.isSmrInvoked(), is(false));
132 assertThat(request.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900133 assertThat(request.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900134 }
135
136 @Test
137 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
138 ByteBuf byteBuf = Unpooled.buffer();
139 RequestWriter writer = new RequestWriter();
140 writer.writeTo(byteBuf, request1);
141
142 RequestReader reader = new RequestReader();
143 LispMapRequest deserialized = reader.readFrom(byteBuf);
144
145 new EqualsTester()
146 .addEqualityGroup(request1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900147 }
148}