blob: ff2c104a587b2a12849fff4dbba69bac0464ee7d [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.DefaultLispMapRegister.RegisterReader;
29import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterWriter;
Jian Lib26d3502016-08-31 01:43:24 +090030import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
31import org.onosproject.lisp.msg.protocols.LispMapRegister.RegisterBuilder;
32import 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;
39import static org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.DefaultRegisterBuilder;
Jian Li18f3bce2016-08-04 17:36:41 +090040
41/**
42 * Unit tests for DefaultLispMapRegister class.
43 */
Jian Li47671902016-08-11 01:18:18 +090044public final class DefaultLispMapRegisterTest {
Jian Li18f3bce2016-08-04 17:36:41 +090045
46 private LispMapRegister register1;
47 private LispMapRegister sameAsRegister1;
48 private LispMapRegister register2;
49
50 @Before
51 public void setup() {
52
Jian Lib26d3502016-08-31 01:43:24 +090053 RegisterBuilder builder1 = new DefaultRegisterBuilder();
54
55 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090056
57 register1 = builder1
58 .withIsProxyMapReply(true)
59 .withIsWantMapNotify(false)
60 .withKeyId((short) 1)
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 RegisterBuilder builder2 = new DefaultRegisterBuilder();
66
67 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090068
69 sameAsRegister1 = builder2
70 .withIsProxyMapReply(true)
71 .withIsWantMapNotify(false)
72 .withKeyId((short) 1)
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 RegisterBuilder builder3 = new DefaultRegisterBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090078
79 register2 = builder3
80 .withIsProxyMapReply(true)
81 .withIsWantMapNotify(false)
82 .withKeyId((short) 2)
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(register1, sameAsRegister1)
106 .addEqualityGroup(register2).testEquals();
107 }
108
109 @Test
110 public void testConstruction() {
111 DefaultLispMapRegister register = (DefaultLispMapRegister) register1;
112
113 assertThat(register.isProxyMapReply(), is(true));
114 assertThat(register.isWantMapNotify(), is(false));
115 assertThat(register.getKeyId(), is((short) 1));
116 assertThat(register.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900117 assertThat(register.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
124 RegisterWriter writer = new RegisterWriter();
125 writer.writeTo(byteBuf, register1);
126
127 RegisterReader reader = new RegisterReader();
128 LispMapRegister deserialized = reader.readFrom(byteBuf);
129
130 new EqualsTester()
131 .addEqualityGroup(register1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900132 }
133}