blob: 25ce27f65faa40ee9b23711305c9baf36958ae61 [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
45 LispMapRecord.MapRecordBuilder builder1 =
Jian Lie4ba2a42016-08-29 20:24:15 +090046 new DefaultMapRecordBuilder();
47
48 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
Jian Li18f3bce2016-08-04 17:36:41 +090049
50 record1 = builder1
51 .withRecordTtl(100)
52 .withAuthoritative(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090053 .withLocatorCount(0)
Jian Li18f3bce2016-08-04 17:36:41 +090054 .withMapVersionNumber((short) 1)
55 .withMaskLength((byte) 0x01)
Jian Lie4ba2a42016-08-29 20:24:15 +090056 .withAction(LispMapReplyAction.NativelyForward)
57 .withEidPrefixAfi(ipv4Locator1)
Jian Li18f3bce2016-08-04 17:36:41 +090058 .build();
59
60 LispMapRecord.MapRecordBuilder builder2 =
Jian Lie4ba2a42016-08-29 20:24:15 +090061 new DefaultMapRecordBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090062
63 sameAsRecord1 = builder2
64 .withRecordTtl(100)
65 .withAuthoritative(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090066 .withLocatorCount(0)
Jian Li18f3bce2016-08-04 17:36:41 +090067 .withMapVersionNumber((short) 1)
68 .withMaskLength((byte) 0x01)
Jian Lie4ba2a42016-08-29 20:24:15 +090069 .withAction(LispMapReplyAction.NativelyForward)
70 .withEidPrefixAfi(ipv4Locator1)
Jian Li18f3bce2016-08-04 17:36:41 +090071 .build();
72
73 LispMapRecord.MapRecordBuilder builder3 =
Jian Lie4ba2a42016-08-29 20:24:15 +090074 new DefaultMapRecordBuilder();
75
76 LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
Jian Li18f3bce2016-08-04 17:36:41 +090077
78 record2 = builder3
79 .withRecordTtl(200)
80 .withAuthoritative(false)
81 .withLocatorCount(200)
82 .withMapVersionNumber((short) 2)
83 .withMaskLength((byte) 0x02)
Jian Lie4ba2a42016-08-29 20:24:15 +090084 .withAction(LispMapReplyAction.Drop)
85 .withEidPrefixAfi(ipv4Locator2)
Jian Li18f3bce2016-08-04 17:36:41 +090086 .build();
87 }
88
89 @Test
90 public void testEquality() {
91 new EqualsTester()
92 .addEqualityGroup(record1, sameAsRecord1)
93 .addEqualityGroup(record2).testEquals();
94 }
95
96 @Test
97 public void testConstruction() {
98 DefaultLispMapRecord record = (DefaultLispMapRecord) record1;
99
Jian Lie4ba2a42016-08-29 20:24:15 +0900100 LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
101
Jian Li18f3bce2016-08-04 17:36:41 +0900102 assertThat(record.getRecordTtl(), is(100));
103 assertThat(record.isAuthoritative(), is(true));
Jian Lie4ba2a42016-08-29 20:24:15 +0900104 assertThat(record.getLocatorCount(), is(0));
Jian Li18f3bce2016-08-04 17:36:41 +0900105 assertThat(record.getMapVersionNumber(), is((short) 1));
106 assertThat(record.getMaskLength(), is((byte) 0x01));
Jian Lie4ba2a42016-08-29 20:24:15 +0900107 assertThat(record.getAction(), is(LispMapReplyAction.NativelyForward));
108 assertThat(record.getEidPrefixAfi(), is(ipv4Locator));
109 }
110
111 @Test
112 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
113 ByteBuf byteBuf = Unpooled.buffer();
114
115 MapRecordWriter writer = new MapRecordWriter();
116 writer.writeTo(byteBuf, record1);
117
118 MapRecordReader reader = new MapRecordReader();
119 LispMapRecord deserialized = reader.readFrom(byteBuf);
120
121 new EqualsTester()
122 .addEqualityGroup(record1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900123 }
124}