blob: 4b7fe12bf216f3779e2e853d3314e425a512e2a4 [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
46 private LispMapNotify notify1;
47 private LispMapNotify sameAsNotify1;
48 private LispMapNotify notify2;
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 Li84b75822016-10-04 23:10:35 +090054 NotifyBuilder builder1 = new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090055
Jian Lib26d3502016-08-31 01:43:24 +090056 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
57
Jian Li18f3bce2016-08-04 17:36:41 +090058 notify1 = builder1
59 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090060 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090061 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090062 .withMapRecords(records1)
Jian Li18f3bce2016-08-04 17:36:41 +090063 .build();
64
Jian Li84b75822016-10-04 23:10:35 +090065 NotifyBuilder builder2 = new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090066
Jian Lib26d3502016-08-31 01:43:24 +090067 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
68
Jian Li18f3bce2016-08-04 17:36:41 +090069 sameAsNotify1 = builder2
70 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090071 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090072 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090073 .withMapRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090074 .build();
75
Jian Li84b75822016-10-04 23:10:35 +090076 NotifyBuilder builder3 = new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090077
78 notify2 = builder3
79 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +090080 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090081 .withNonce(2L)
Jian Li18f3bce2016-08-04 17:36:41 +090082 .build();
83 }
84
Jian Lib26d3502016-08-31 01:43:24 +090085 private LispMapRecord getMapRecord() {
86 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
87
88 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
89
90 return builder1
91 .withRecordTtl(100)
92 .withAuthoritative(true)
93 .withMapVersionNumber((short) 1)
94 .withMaskLength((byte) 0x01)
95 .withAction(LispMapReplyAction.NativelyForward)
96 .withEidPrefixAfi(ipv4Locator1)
97 .build();
98 }
99
Jian Li18f3bce2016-08-04 17:36:41 +0900100 @Test
101 public void testEquality() {
102 new EqualsTester()
103 .addEqualityGroup(notify1, sameAsNotify1)
104 .addEqualityGroup(notify2).testEquals();
105 }
106
107 @Test
108 public void testConstruction() {
109 DefaultLispMapNotify notify = (DefaultLispMapNotify) notify1;
110
111 assertThat(notify.getKeyId(), is((short) 1));
112 assertThat(notify.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900113 assertThat(notify.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900114 }
115
116 @Test
117 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
118 ByteBuf byteBuf = Unpooled.buffer();
119
120 NotifyWriter writer = new NotifyWriter();
121 writer.writeTo(byteBuf, notify1);
122
123 NotifyReader reader = new NotifyReader();
124 LispMapNotify deserialized = reader.readFrom(byteBuf);
125
Jian Li5e505c62016-12-05 02:44:24 +0900126 new EqualsTester().addEqualityGroup(notify1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900127 }
128}