blob: dea24924a1bf383e49374936bc735e9ed09f638b [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 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;
Jian Lib26d3502016-08-31 01:43:24 +090030import org.onosproject.lisp.msg.types.LispIpv4Address;
31
32import java.util.List;
Jian Li18f3bce2016-08-04 17:36:41 +090033
34import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.is;
Jian Lib26d3502016-08-31 01:43:24 +090036import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
37import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordBuilder;
38import static org.onosproject.lisp.msg.protocols.DefaultLispMapReply.DefaultReplyBuilder;
39import static org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyBuilder;
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
46 private LispMapReply reply1;
47 private LispMapReply sameAsReply1;
48 private LispMapReply reply2;
49
50 @Before
51 public void setup() {
52
Jian Lib26d3502016-08-31 01:43:24 +090053 ReplyBuilder builder1 = new DefaultReplyBuilder();
54
55 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090056
57 reply1 = builder1
58 .withIsEtr(true)
59 .withIsProbe(false)
60 .withIsSecurity(true)
61 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090062 .withMapRecords(records1)
Jian Li18f3bce2016-08-04 17:36:41 +090063 .build();
64
Jian Lib26d3502016-08-31 01:43:24 +090065 ReplyBuilder builder2 = new DefaultReplyBuilder();
66
67 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090068
69 sameAsReply1 = builder2
70 .withIsEtr(true)
71 .withIsProbe(false)
72 .withIsSecurity(true)
73 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090074 .withMapRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090075 .build();
76
Jian Lib26d3502016-08-31 01:43:24 +090077 ReplyBuilder builder3 = new DefaultReplyBuilder();
78
Jian Li18f3bce2016-08-04 17:36:41 +090079 reply2 = builder3
80 .withIsEtr(false)
81 .withIsProbe(true)
82 .withIsSecurity(false)
83 .withNonce(2L)
Jian Li18f3bce2016-08-04 17:36:41 +090084 .build();
85 }
86
Jian Lib26d3502016-08-31 01:43:24 +090087 private LispMapRecord getMapRecord() {
88 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
89
90 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
91
92 return builder1
93 .withRecordTtl(100)
94 .withAuthoritative(true)
95 .withMapVersionNumber((short) 1)
96 .withMaskLength((byte) 0x01)
97 .withAction(LispMapReplyAction.NativelyForward)
98 .withEidPrefixAfi(ipv4Locator1)
99 .build();
100 }
101
Jian Li18f3bce2016-08-04 17:36:41 +0900102 @Test
103 public void testEquality() {
104 new EqualsTester()
105 .addEqualityGroup(reply1, sameAsReply1)
106 .addEqualityGroup(reply2).testEquals();
107 }
108
109 @Test
110 public void testConstruction() {
111 DefaultLispMapReply reply = (DefaultLispMapReply) reply1;
112
113 assertThat(reply.isEtr(), is(true));
114 assertThat(reply.isProbe(), is(false));
115 assertThat(reply.isSecurity(), is(true));
116 assertThat(reply.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900117 assertThat(reply.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900118 }
119
120 @Test
121 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
122 ByteBuf byteBuf = Unpooled.buffer();
123 ReplyWriter writer = new ReplyWriter();
124 writer.writeTo(byteBuf, reply1);
125
126 ReplyReader reader = new ReplyReader();
127 LispMapReply deserialized = reader.readFrom(byteBuf);
128
Jian Li5e505c62016-12-05 02:44:24 +0900129 new EqualsTester().addEqualityGroup(reply1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900130 }
131}