blob: 8bd7b2e99def5ced8f56fcfc16a16be1959deb00 [file] [log] [blame]
Jian Li18f3bce2016-08-04 17:36:41 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Li18f3bce2016-08-04 17:36:41 +09003 *
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 Lib26d3502016-08-31 01:43:24 +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 Lib26d3502016-08-31 01:43:24 +090024import org.onlab.packet.IpAddress;
Jian Lie4ba2a42016-08-29 20:24:15 +090025import 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.protocols.DefaultLispMapReply.ReplyReader;
29import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyWriter;
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080030import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
31import org.onosproject.lisp.msg.protocols.LispMapReply.ReplyBuilder;
Jian Lib26d3502016-08-31 01:43:24 +090032import org.onosproject.lisp.msg.types.LispIpv4Address;
33
34import java.util.List;
Jian Li18f3bce2016-08-04 17:36:41 +090035
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.is;
Jian Lib26d3502016-08-31 01:43:24 +090038import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
Jian Lib26d3502016-08-31 01:43:24 +090039import static org.onosproject.lisp.msg.protocols.DefaultLispMapReply.DefaultReplyBuilder;
Jian Li18f3bce2016-08-04 17:36:41 +090040
41/**
42 * Unit tests for DefaultLispMapReply class.
43 */
Jian Li47671902016-08-11 01:18:18 +090044public final class DefaultLispMapReplyTest {
Jian Li18f3bce2016-08-04 17:36:41 +090045
Jian Li672ebda2017-02-06 20:21:04 +090046 private static final String IP_ADDRESS = "192.168.1.1";
47
Jian Li18f3bce2016-08-04 17:36:41 +090048 private LispMapReply reply1;
49 private LispMapReply sameAsReply1;
50 private LispMapReply reply2;
51
52 @Before
53 public void setup() {
54
Jian Lib26d3502016-08-31 01:43:24 +090055 ReplyBuilder builder1 = new DefaultReplyBuilder();
56
57 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090058
59 reply1 = builder1
60 .withIsEtr(true)
61 .withIsProbe(false)
62 .withIsSecurity(true)
63 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090064 .withMapRecords(records1)
Jian Li18f3bce2016-08-04 17:36:41 +090065 .build();
66
Jian Lib26d3502016-08-31 01:43:24 +090067 ReplyBuilder builder2 = new DefaultReplyBuilder();
68
69 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090070
71 sameAsReply1 = builder2
72 .withIsEtr(true)
73 .withIsProbe(false)
74 .withIsSecurity(true)
75 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090076 .withMapRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090077 .build();
78
Jian Lib26d3502016-08-31 01:43:24 +090079 ReplyBuilder builder3 = new DefaultReplyBuilder();
80
Jian Li18f3bce2016-08-04 17:36:41 +090081 reply2 = builder3
82 .withIsEtr(false)
83 .withIsProbe(true)
84 .withIsSecurity(false)
85 .withNonce(2L)
Jian Li18f3bce2016-08-04 17:36:41 +090086 .build();
87 }
88
Jian Lib26d3502016-08-31 01:43:24 +090089 private LispMapRecord getMapRecord() {
90 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
91
Jian Li672ebda2017-02-06 20:21:04 +090092 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS));
Jian Lib26d3502016-08-31 01:43:24 +090093
94 return builder1
95 .withRecordTtl(100)
Jian Li672ebda2017-02-06 20:21:04 +090096 .withIsAuthoritative(true)
Jian Lib26d3502016-08-31 01:43:24 +090097 .withMapVersionNumber((short) 1)
98 .withMaskLength((byte) 0x01)
99 .withAction(LispMapReplyAction.NativelyForward)
100 .withEidPrefixAfi(ipv4Locator1)
101 .build();
102 }
103
Jian Li18f3bce2016-08-04 17:36:41 +0900104 @Test
105 public void testEquality() {
106 new EqualsTester()
107 .addEqualityGroup(reply1, sameAsReply1)
108 .addEqualityGroup(reply2).testEquals();
109 }
110
111 @Test
112 public void testConstruction() {
113 DefaultLispMapReply reply = (DefaultLispMapReply) reply1;
114
115 assertThat(reply.isEtr(), is(true));
116 assertThat(reply.isProbe(), is(false));
117 assertThat(reply.isSecurity(), is(true));
118 assertThat(reply.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900119 assertThat(reply.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900120 }
121
122 @Test
123 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
124 ByteBuf byteBuf = Unpooled.buffer();
125 ReplyWriter writer = new ReplyWriter();
126 writer.writeTo(byteBuf, reply1);
127
128 ReplyReader reader = new ReplyReader();
129 LispMapReply deserialized = reader.readFrom(byteBuf);
130
Jian Li5e505c62016-12-05 02:44:24 +0900131 new EqualsTester().addEqualityGroup(reply1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900132 }
133}