blob: f9dd47f9c7566f6ec57dda1b8f07a27ef8304962 [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 Lib26d3502016-08-31 01:43:24 +090028import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
29import org.onosproject.lisp.msg.types.LispIpv4Address;
30
31import java.util.List;
Jian Li18f3bce2016-08-04 17:36:41 +090032
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.is;
Jian Lie4ba2a42016-08-29 20:24:15 +090035import static org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.*;
Jian Lib26d3502016-08-31 01:43:24 +090036import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
Jian Li18f3bce2016-08-04 17:36:41 +090037
38/**
39 * Unit tests for DefaultLispMapNotify class.
40 */
Jian Li47671902016-08-11 01:18:18 +090041public final class DefaultLispMapNotifyTest {
Jian Li18f3bce2016-08-04 17:36:41 +090042
43 private LispMapNotify notify1;
44 private LispMapNotify sameAsNotify1;
45 private LispMapNotify notify2;
Jian Lid1a109e2016-11-12 09:00:42 +090046 private static final String AUTH_KEY = "onos";
Jian Li18f3bce2016-08-04 17:36:41 +090047
48 @Before
49 public void setup() {
50
Jian Li84b75822016-10-04 23:10:35 +090051 NotifyBuilder builder1 = new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090052
Jian Lib26d3502016-08-31 01:43:24 +090053 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
54
Jian Li18f3bce2016-08-04 17:36:41 +090055 notify1 = builder1
56 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090057 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090058 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090059 .withMapRecords(records1)
Jian Li18f3bce2016-08-04 17:36:41 +090060 .build();
61
Jian Li84b75822016-10-04 23:10:35 +090062 NotifyBuilder builder2 = new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090063
Jian Lib26d3502016-08-31 01:43:24 +090064 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
65
Jian Li18f3bce2016-08-04 17:36:41 +090066 sameAsNotify1 = builder2
67 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090068 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090069 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090070 .withMapRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090071 .build();
72
Jian Li84b75822016-10-04 23:10:35 +090073 NotifyBuilder builder3 = new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090074
75 notify2 = builder3
76 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +090077 .withAuthKey(AUTH_KEY)
Jian Li18f3bce2016-08-04 17:36:41 +090078 .withNonce(2L)
Jian Li18f3bce2016-08-04 17:36:41 +090079 .build();
80 }
81
Jian Lib26d3502016-08-31 01:43:24 +090082 private LispMapRecord getMapRecord() {
83 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
84
85 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
86
87 return builder1
88 .withRecordTtl(100)
89 .withAuthoritative(true)
90 .withMapVersionNumber((short) 1)
91 .withMaskLength((byte) 0x01)
92 .withAction(LispMapReplyAction.NativelyForward)
93 .withEidPrefixAfi(ipv4Locator1)
94 .build();
95 }
96
Jian Li18f3bce2016-08-04 17:36:41 +090097 @Test
98 public void testEquality() {
99 new EqualsTester()
100 .addEqualityGroup(notify1, sameAsNotify1)
101 .addEqualityGroup(notify2).testEquals();
102 }
103
104 @Test
105 public void testConstruction() {
106 DefaultLispMapNotify notify = (DefaultLispMapNotify) notify1;
107
108 assertThat(notify.getKeyId(), is((short) 1));
109 assertThat(notify.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900110 assertThat(notify.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900111 }
112
113 @Test
114 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
115 ByteBuf byteBuf = Unpooled.buffer();
116
117 NotifyWriter writer = new NotifyWriter();
118 writer.writeTo(byteBuf, notify1);
119
120 NotifyReader reader = new NotifyReader();
121 LispMapNotify deserialized = reader.readFrom(byteBuf);
122
123 new EqualsTester()
124 .addEqualityGroup(notify1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900125 }
126}