blob: 80d7d251d681523e5f1b652d7209c841df79d530 [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;
Jian Li5e505c62016-12-05 02:44:24 +090028import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
29import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.DefaultRegisterBuilder;
Jian Lie4ba2a42016-08-29 20:24:15 +090030import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterReader;
31import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterWriter;
Jian Lib26d3502016-08-31 01:43:24 +090032import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
33import org.onosproject.lisp.msg.protocols.LispMapRegister.RegisterBuilder;
34import org.onosproject.lisp.msg.types.LispIpv4Address;
35
36import java.util.List;
Jian Li18f3bce2016-08-04 17:36:41 +090037
38import static org.hamcrest.MatcherAssert.assertThat;
39import static org.hamcrest.Matchers.is;
40
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
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 LispMapRegister register1;
49 private LispMapRegister sameAsRegister1;
50 private LispMapRegister register2;
Jian Lid1a109e2016-11-12 09:00:42 +090051 private static final String AUTH_KEY = "onos";
Jian Li18f3bce2016-08-04 17:36:41 +090052
53 @Before
54 public void setup() {
55
Jian Lib26d3502016-08-31 01:43:24 +090056 RegisterBuilder builder1 = new DefaultRegisterBuilder();
57
58 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090059
60 register1 = builder1
61 .withIsProxyMapReply(true)
62 .withIsWantMapNotify(false)
63 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090064 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090065 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090066 .withMapRecords(records1)
Jian Li18f3bce2016-08-04 17:36:41 +090067 .build();
68
Jian Lib26d3502016-08-31 01:43:24 +090069 RegisterBuilder builder2 = new DefaultRegisterBuilder();
70
71 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090072
73 sameAsRegister1 = builder2
74 .withIsProxyMapReply(true)
75 .withIsWantMapNotify(false)
76 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090077 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090078 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090079 .withMapRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090080 .build();
81
Jian Lib26d3502016-08-31 01:43:24 +090082 RegisterBuilder builder3 = new DefaultRegisterBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090083
84 register2 = builder3
85 .withIsProxyMapReply(true)
86 .withIsWantMapNotify(false)
87 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +090088 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090089 .withNonce(2L)
Jian Li18f3bce2016-08-04 17:36:41 +090090 .build();
91 }
92
Jian Lib26d3502016-08-31 01:43:24 +090093 private LispMapRecord getMapRecord() {
94 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
95
Jian Li672ebda2017-02-06 20:21:04 +090096 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS));
Jian Lib26d3502016-08-31 01:43:24 +090097
98 return builder1
99 .withRecordTtl(100)
Jian Li672ebda2017-02-06 20:21:04 +0900100 .withIsAuthoritative(true)
Jian Lib26d3502016-08-31 01:43:24 +0900101 .withMapVersionNumber((short) 1)
102 .withMaskLength((byte) 0x01)
103 .withAction(LispMapReplyAction.NativelyForward)
104 .withEidPrefixAfi(ipv4Locator1)
105 .build();
106 }
107
Jian Li18f3bce2016-08-04 17:36:41 +0900108 @Test
109 public void testEquality() {
110 new EqualsTester()
111 .addEqualityGroup(register1, sameAsRegister1)
112 .addEqualityGroup(register2).testEquals();
113 }
114
115 @Test
116 public void testConstruction() {
117 DefaultLispMapRegister register = (DefaultLispMapRegister) register1;
118
119 assertThat(register.isProxyMapReply(), is(true));
120 assertThat(register.isWantMapNotify(), is(false));
121 assertThat(register.getKeyId(), is((short) 1));
122 assertThat(register.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900123 assertThat(register.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900124 }
125
126 @Test
127 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
128 ByteBuf byteBuf = Unpooled.buffer();
129
130 RegisterWriter writer = new RegisterWriter();
131 writer.writeTo(byteBuf, register1);
132
133 RegisterReader reader = new RegisterReader();
134 LispMapRegister deserialized = reader.readFrom(byteBuf);
135
Jian Li5e505c62016-12-05 02:44:24 +0900136 new EqualsTester().addEqualityGroup(register1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900137 }
138}