blob: 4e9529d8f4040f8a38887c7be4713d73d7c29cb3 [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
46 private LispMapRegister register1;
47 private LispMapRegister sameAsRegister1;
48 private LispMapRegister register2;
Jian Lid1a109e2016-11-12 09:00:42 +090049 private static final String AUTH_KEY = "onos";
Jian Li18f3bce2016-08-04 17:36:41 +090050
51 @Before
52 public void setup() {
53
Jian Lib26d3502016-08-31 01:43:24 +090054 RegisterBuilder builder1 = new DefaultRegisterBuilder();
55
56 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090057
58 register1 = builder1
59 .withIsProxyMapReply(true)
60 .withIsWantMapNotify(false)
61 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090062 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090063 .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 RegisterBuilder builder2 = new DefaultRegisterBuilder();
68
69 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
Jian Li18f3bce2016-08-04 17:36:41 +090070
71 sameAsRegister1 = builder2
72 .withIsProxyMapReply(true)
73 .withIsWantMapNotify(false)
74 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090075 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090076 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090077 .withMapRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090078 .build();
79
Jian Lib26d3502016-08-31 01:43:24 +090080 RegisterBuilder builder3 = new DefaultRegisterBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090081
82 register2 = builder3
83 .withIsProxyMapReply(true)
84 .withIsWantMapNotify(false)
85 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +090086 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090087 .withNonce(2L)
Jian Li18f3bce2016-08-04 17:36:41 +090088 .build();
89 }
90
Jian Lib26d3502016-08-31 01:43:24 +090091 private LispMapRecord getMapRecord() {
92 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
93
94 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
95
96 return builder1
97 .withRecordTtl(100)
98 .withAuthoritative(true)
99 .withMapVersionNumber((short) 1)
100 .withMaskLength((byte) 0x01)
101 .withAction(LispMapReplyAction.NativelyForward)
102 .withEidPrefixAfi(ipv4Locator1)
103 .build();
104 }
105
Jian Li18f3bce2016-08-04 17:36:41 +0900106 @Test
107 public void testEquality() {
108 new EqualsTester()
109 .addEqualityGroup(register1, sameAsRegister1)
110 .addEqualityGroup(register2).testEquals();
111 }
112
113 @Test
114 public void testConstruction() {
115 DefaultLispMapRegister register = (DefaultLispMapRegister) register1;
116
117 assertThat(register.isProxyMapReply(), is(true));
118 assertThat(register.isWantMapNotify(), is(false));
119 assertThat(register.getKeyId(), is((short) 1));
120 assertThat(register.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900121 assertThat(register.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900122 }
123
124 @Test
125 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
126 ByteBuf byteBuf = Unpooled.buffer();
127
128 RegisterWriter writer = new RegisterWriter();
129 writer.writeTo(byteBuf, register1);
130
131 RegisterReader reader = new RegisterReader();
132 LispMapRegister deserialized = reader.readFrom(byteBuf);
133
Jian Li5e505c62016-12-05 02:44:24 +0900134 new EqualsTester().addEqualityGroup(register1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900135 }
136}