blob: 620496de21a5063a0fe5715ddca06f5433d54318 [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.DefaultLispMapNotify.DefaultNotifyBuilder;
29import org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.NotifyReader;
30import org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.NotifyWriter;
31import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
32import org.onosproject.lisp.msg.protocols.LispMapNotify.NotifyBuilder;
Jian Lib26d3502016-08-31 01:43:24 +090033import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
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 DefaultLispMapNotify class.
43 */
Jian Li47671902016-08-11 01:18:18 +090044public final class DefaultLispMapNotifyTest {
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 LispMapNotify notify1;
49 private LispMapNotify sameAsNotify1;
50 private LispMapNotify notify2;
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 Li84b75822016-10-04 23:10:35 +090056 NotifyBuilder builder1 = new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090057
Jian Lib26d3502016-08-31 01:43:24 +090058 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
59
Jian Li18f3bce2016-08-04 17:36:41 +090060 notify1 = builder1
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 Li84b75822016-10-04 23:10:35 +090067 NotifyBuilder builder2 = new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090068
Jian Lib26d3502016-08-31 01:43:24 +090069 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
70
Jian Li18f3bce2016-08-04 17:36:41 +090071 sameAsNotify1 = builder2
72 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090073 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090074 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090075 .withMapRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090076 .build();
77
Jian Li84b75822016-10-04 23:10:35 +090078 NotifyBuilder builder3 = new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090079
80 notify2 = builder3
81 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +090082 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090083 .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
Jian Li672ebda2017-02-06 20:21:04 +090090 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS));
Jian Lib26d3502016-08-31 01:43:24 +090091
92 return builder1
93 .withRecordTtl(100)
Jian Li672ebda2017-02-06 20:21:04 +090094 .withIsAuthoritative(true)
Jian Lib26d3502016-08-31 01:43:24 +090095 .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(notify1, sameAsNotify1)
106 .addEqualityGroup(notify2).testEquals();
107 }
108
109 @Test
110 public void testConstruction() {
111 DefaultLispMapNotify notify = (DefaultLispMapNotify) notify1;
112
113 assertThat(notify.getKeyId(), is((short) 1));
114 assertThat(notify.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900115 assertThat(notify.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900116 }
117
118 @Test
119 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
120 ByteBuf byteBuf = Unpooled.buffer();
121
122 NotifyWriter writer = new NotifyWriter();
123 writer.writeTo(byteBuf, notify1);
124
125 NotifyReader reader = new NotifyReader();
126 LispMapNotify deserialized = reader.readFrom(byteBuf);
127
Jian Li5e505c62016-12-05 02:44:24 +0900128 new EqualsTester().addEqualityGroup(notify1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900129 }
130}