blob: 088c4f86811712fbf5b1728d247b45f5536d12d6 [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 Li18f3bce2016-08-04 17:36:41 +090057
58 request1 = builder1
59 .withIsAuthoritative(true)
60 .withIsMapDataPresent(true)
61 .withIsPitr(false)
62 .withIsProbe(false)
63 .withIsSmr(true)
64 .withIsSmrInvoked(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090065 .withSourceEid(ipv4Eid1)
66 .withItrRlocs(rlocs1)
Jian Li18f3bce2016-08-04 17:36:41 +090067 .withNonce(1L)
Jian Lie4ba2a42016-08-29 20:24:15 +090068 .withRecordCount((byte) 0)
Jian Li18f3bce2016-08-04 17:36:41 +090069 .build();
70
Jian Lie4ba2a42016-08-29 20:24:15 +090071 RequestBuilder builder2 = new DefaultRequestBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090072
73 sameAsRequest1 = builder2
74 .withIsAuthoritative(true)
75 .withIsMapDataPresent(true)
76 .withIsPitr(false)
77 .withIsProbe(false)
78 .withIsSmr(true)
79 .withIsSmrInvoked(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090080 .withSourceEid(ipv4Eid1)
81 .withItrRlocs(rlocs1)
Jian Li18f3bce2016-08-04 17:36:41 +090082 .withNonce(1L)
Jian Lie4ba2a42016-08-29 20:24:15 +090083 .withRecordCount((byte) 0)
Jian Li18f3bce2016-08-04 17:36:41 +090084 .build();
85
Jian Lie4ba2a42016-08-29 20:24:15 +090086 RequestBuilder builder3 = new DefaultRequestBuilder();
87
88 LispIpv4Address ipv4Eid2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
89
90 LispIpv4Address ipv4Rloc3 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1"));
91 LispIpv4Address ipv4Rloc4 = new LispIpv4Address(IpAddress.valueOf("10.1.1.2"));
92
93 List<LispAfiAddress> rlocs2 = ImmutableList.of(ipv4Rloc3, ipv4Rloc4);
Jian Li18f3bce2016-08-04 17:36:41 +090094
95 request2 = builder3
96 .withIsAuthoritative(false)
97 .withIsMapDataPresent(false)
98 .withIsPitr(true)
99 .withIsProbe(true)
100 .withIsSmr(false)
101 .withIsSmrInvoked(true)
Jian Lie4ba2a42016-08-29 20:24:15 +0900102 .withSourceEid(ipv4Eid2)
103 .withItrRlocs(rlocs2)
Jian Li18f3bce2016-08-04 17:36:41 +0900104 .withNonce(2L)
105 .withRecordCount((byte) 0x02)
106 .build();
107 }
108
109 @Test
110 public void testEquality() {
111 new EqualsTester()
112 .addEqualityGroup(request1, sameAsRequest1)
113 .addEqualityGroup(request2).testEquals();
114 }
115
116 @Test
117 public void testConstruction() {
118 DefaultLispMapRequest request = (DefaultLispMapRequest) request1;
119
120 assertThat(request.isAuthoritative(), is(true));
121 assertThat(request.isMapDataPresent(), is(true));
122 assertThat(request.isPitr(), is(false));
123 assertThat(request.isProbe(), is(false));
124 assertThat(request.isSmr(), is(true));
125 assertThat(request.isSmrInvoked(), is(false));
126 assertThat(request.getNonce(), is(1L));
Jian Lie4ba2a42016-08-29 20:24:15 +0900127 assertThat(request.getRecordCount(), is((byte) 0));
128 }
129
130 @Test
131 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
132 ByteBuf byteBuf = Unpooled.buffer();
133 RequestWriter writer = new RequestWriter();
134 writer.writeTo(byteBuf, request1);
135
136 RequestReader reader = new RequestReader();
137 LispMapRequest deserialized = reader.readFrom(byteBuf);
138
139 new EqualsTester()
140 .addEqualityGroup(request1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900141 }
142}