blob: aa1c7df91ac27f1aa22dbbff2c1ddb9cf3b9d7fd [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
18import com.google.common.testing.EqualsTester;
Jian Lie4ba2a42016-08-29 20:24:15 +090019import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
Jian Li18f3bce2016-08-04 17:36:41 +090021import org.junit.Before;
22import org.junit.Test;
Jian Lie4ba2a42016-08-29 20:24:15 +090023import org.onlab.packet.IpAddress;
24import org.onosproject.lisp.msg.exceptions.LispParseError;
25import org.onosproject.lisp.msg.exceptions.LispReaderException;
26import org.onosproject.lisp.msg.exceptions.LispWriterException;
27import org.onosproject.lisp.msg.types.LispIpv4Address;
Jian Li18f3bce2016-08-04 17:36:41 +090028
29import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.is;
Jian Lie4ba2a42016-08-29 20:24:15 +090031import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.*;
Jian Li18f3bce2016-08-04 17:36:41 +090032
33/**
34 * Unit tests for DefaultLispMapRecord class.
35 */
Jian Li47671902016-08-11 01:18:18 +090036public final class DefaultLispMapRecordTest {
Jian Li18f3bce2016-08-04 17:36:41 +090037
38 private LispMapRecord record1;
39 private LispMapRecord sameAsRecord1;
40 private LispMapRecord record2;
41
42 @Before
43 public void setup() {
44
Jian Lib26d3502016-08-31 01:43:24 +090045 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090046
47 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
Jian Li18f3bce2016-08-04 17:36:41 +090048
49 record1 = builder1
50 .withRecordTtl(100)
51 .withAuthoritative(true)
Jian Li18f3bce2016-08-04 17:36:41 +090052 .withMapVersionNumber((short) 1)
53 .withMaskLength((byte) 0x01)
Jian Lie4ba2a42016-08-29 20:24:15 +090054 .withAction(LispMapReplyAction.NativelyForward)
55 .withEidPrefixAfi(ipv4Locator1)
Jian Li18f3bce2016-08-04 17:36:41 +090056 .build();
57
Jian Lib26d3502016-08-31 01:43:24 +090058 MapRecordBuilder builder2 = new DefaultMapRecordBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090059
60 sameAsRecord1 = builder2
61 .withRecordTtl(100)
62 .withAuthoritative(true)
Jian Li18f3bce2016-08-04 17:36:41 +090063 .withMapVersionNumber((short) 1)
64 .withMaskLength((byte) 0x01)
Jian Lie4ba2a42016-08-29 20:24:15 +090065 .withAction(LispMapReplyAction.NativelyForward)
66 .withEidPrefixAfi(ipv4Locator1)
Jian Li18f3bce2016-08-04 17:36:41 +090067 .build();
68
Jian Lib26d3502016-08-31 01:43:24 +090069 MapRecordBuilder builder3 = new DefaultMapRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090070
71 LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
Jian Li18f3bce2016-08-04 17:36:41 +090072
73 record2 = builder3
74 .withRecordTtl(200)
75 .withAuthoritative(false)
Jian Li18f3bce2016-08-04 17:36:41 +090076 .withMapVersionNumber((short) 2)
77 .withMaskLength((byte) 0x02)
Jian Lie4ba2a42016-08-29 20:24:15 +090078 .withAction(LispMapReplyAction.Drop)
79 .withEidPrefixAfi(ipv4Locator2)
Jian Li18f3bce2016-08-04 17:36:41 +090080 .build();
81 }
82
83 @Test
84 public void testEquality() {
85 new EqualsTester()
86 .addEqualityGroup(record1, sameAsRecord1)
87 .addEqualityGroup(record2).testEquals();
88 }
89
90 @Test
91 public void testConstruction() {
92 DefaultLispMapRecord record = (DefaultLispMapRecord) record1;
93
Jian Lie4ba2a42016-08-29 20:24:15 +090094 LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
95
Jian Li18f3bce2016-08-04 17:36:41 +090096 assertThat(record.getRecordTtl(), is(100));
97 assertThat(record.isAuthoritative(), is(true));
Jian Li18f3bce2016-08-04 17:36:41 +090098 assertThat(record.getMapVersionNumber(), is((short) 1));
99 assertThat(record.getMaskLength(), is((byte) 0x01));
Jian Lie4ba2a42016-08-29 20:24:15 +0900100 assertThat(record.getAction(), is(LispMapReplyAction.NativelyForward));
101 assertThat(record.getEidPrefixAfi(), is(ipv4Locator));
102 }
103
104 @Test
105 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
106 ByteBuf byteBuf = Unpooled.buffer();
107
108 MapRecordWriter writer = new MapRecordWriter();
109 writer.writeTo(byteBuf, record1);
110
111 MapRecordReader reader = new MapRecordReader();
112 LispMapRecord deserialized = reader.readFrom(byteBuf);
113
114 new EqualsTester()
115 .addEqualityGroup(record1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900116 }
117}