blob: 79e4a5c2e8f88528a6ee1da28560984568e44b7c [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
45 private LispMapRequest request1;
46 private LispMapRequest sameAsRequest1;
47 private LispMapRequest request2;
48
49 @Before
50 public void setup() {
51
Jian Lie4ba2a42016-08-29 20:24:15 +090052 RequestBuilder builder1 = new DefaultRequestBuilder();
53
54 LispIpv4Address ipv4Eid1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
55
56 LispIpv4Address ipv4Rloc1 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1"));
57 LispIpv4Address ipv4Rloc2 = new LispIpv4Address(IpAddress.valueOf("10.1.1.2"));
58
59 List<LispAfiAddress> rlocs1 = ImmutableList.of(ipv4Rloc1, ipv4Rloc2);
Jian Lib26d3502016-08-31 01:43:24 +090060 List<LispEidRecord> records1 = ImmutableList.of(getEidRecord(), getEidRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090061
62 request1 = builder1
63 .withIsAuthoritative(true)
64 .withIsMapDataPresent(true)
65 .withIsPitr(false)
66 .withIsProbe(false)
67 .withIsSmr(true)
68 .withIsSmrInvoked(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090069 .withSourceEid(ipv4Eid1)
70 .withItrRlocs(rlocs1)
Jian Lib26d3502016-08-31 01:43:24 +090071 .withEidRecords(records1)
Jian Li18f3bce2016-08-04 17:36:41 +090072 .withNonce(1L)
Jian Li18f3bce2016-08-04 17:36:41 +090073 .build();
74
Jian Lie4ba2a42016-08-29 20:24:15 +090075 RequestBuilder builder2 = new DefaultRequestBuilder();
Jian Lib26d3502016-08-31 01:43:24 +090076 List<LispEidRecord> records2 = ImmutableList.of(getEidRecord(), getEidRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090077
78 sameAsRequest1 = builder2
79 .withIsAuthoritative(true)
80 .withIsMapDataPresent(true)
81 .withIsPitr(false)
82 .withIsProbe(false)
83 .withIsSmr(true)
84 .withIsSmrInvoked(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090085 .withSourceEid(ipv4Eid1)
86 .withItrRlocs(rlocs1)
Jian Lib26d3502016-08-31 01:43:24 +090087 .withEidRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090088 .withNonce(1L)
Jian Li18f3bce2016-08-04 17:36:41 +090089 .build();
90
Jian Lie4ba2a42016-08-29 20:24:15 +090091 RequestBuilder builder3 = new DefaultRequestBuilder();
92
93 LispIpv4Address ipv4Eid2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
94
Jian Lib26d3502016-08-31 01:43:24 +090095 LispIpv4Address ipv4Rloc3 = new LispIpv4Address(IpAddress.valueOf("20.1.1.1"));
96 LispIpv4Address ipv4Rloc4 = new LispIpv4Address(IpAddress.valueOf("20.1.1.2"));
Jian Lie4ba2a42016-08-29 20:24:15 +090097
98 List<LispAfiAddress> rlocs2 = ImmutableList.of(ipv4Rloc3, ipv4Rloc4);
Jian Li18f3bce2016-08-04 17:36:41 +090099
100 request2 = builder3
101 .withIsAuthoritative(false)
102 .withIsMapDataPresent(false)
103 .withIsPitr(true)
104 .withIsProbe(true)
105 .withIsSmr(false)
106 .withIsSmrInvoked(true)
Jian Lie4ba2a42016-08-29 20:24:15 +0900107 .withSourceEid(ipv4Eid2)
108 .withItrRlocs(rlocs2)
Jian Li18f3bce2016-08-04 17:36:41 +0900109 .withNonce(2L)
Jian Li18f3bce2016-08-04 17:36:41 +0900110 .build();
111 }
112
Jian Lib26d3502016-08-31 01:43:24 +0900113 private LispEidRecord getEidRecord() {
114 LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf("20.1.1.1"));
115 return new LispEidRecord((byte) 24, eid);
116 }
117
Jian Li18f3bce2016-08-04 17:36:41 +0900118 @Test
119 public void testEquality() {
120 new EqualsTester()
121 .addEqualityGroup(request1, sameAsRequest1)
122 .addEqualityGroup(request2).testEquals();
123 }
124
125 @Test
126 public void testConstruction() {
127 DefaultLispMapRequest request = (DefaultLispMapRequest) request1;
128
129 assertThat(request.isAuthoritative(), is(true));
130 assertThat(request.isMapDataPresent(), is(true));
131 assertThat(request.isPitr(), is(false));
132 assertThat(request.isProbe(), is(false));
133 assertThat(request.isSmr(), is(true));
134 assertThat(request.isSmrInvoked(), is(false));
135 assertThat(request.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900136 assertThat(request.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900137 }
138
139 @Test
140 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
141 ByteBuf byteBuf = Unpooled.buffer();
142 RequestWriter writer = new RequestWriter();
143 writer.writeTo(byteBuf, request1);
144
145 RequestReader reader = new RequestReader();
146 LispMapRequest deserialized = reader.readFrom(byteBuf);
147
Jian Li5e505c62016-12-05 02:44:24 +0900148 new EqualsTester().addEqualityGroup(request1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900149 }
150}