blob: f50c52ace2302c82022b2f51c34e871cf186f8d8 [file] [log] [blame]
Jian Lie9af3b42016-08-08 15:50:01 +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.types;
17
18import com.google.common.testing.EqualsTester;
Jian Li76ea0572016-08-29 12:41:16 +090019import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
Jian Lie9af3b42016-08-08 15:50:01 +090021import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
Jian Li76ea0572016-08-29 12:41:16 +090024import org.onosproject.lisp.msg.exceptions.LispParseError;
25import org.onosproject.lisp.msg.exceptions.LispReaderException;
26import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li84b75822016-10-04 23:10:35 +090027import org.onosproject.lisp.msg.types.LispSourceDestLcafAddress.SourceDestAddressBuilder;
Jian Li5e505c62016-12-05 02:44:24 +090028import org.onosproject.lisp.msg.types.LispSourceDestLcafAddress.SourceDestLcafAddressReader;
29import org.onosproject.lisp.msg.types.LispSourceDestLcafAddress.SourceDestLcafAddressWriter;
Jian Lie9af3b42016-08-08 15:50:01 +090030
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.is;
33
34/**
35 * Unit tests for LispSourceDestLcafAddress class.
36 */
37public class LispSourceDestLcafAddressTest {
38
39 private LispSourceDestLcafAddress address1;
40 private LispSourceDestLcafAddress sameAsAddress1;
41 private LispSourceDestLcafAddress address2;
42
43 @Before
44 public void setup() {
45
Jian Li84b75822016-10-04 23:10:35 +090046 SourceDestAddressBuilder builder1 = new SourceDestAddressBuilder();
Jian Li115d8602016-08-15 20:21:53 +090047
Jian Lie9af3b42016-08-08 15:50:01 +090048 LispIpv4Address srcAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
49 LispIpv4Address dstAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
50
Jian Li115d8602016-08-15 20:21:53 +090051 address1 = builder1
52 .withReserved((short) 1)
53 .withSrcMaskLength((byte) 0x01)
54 .withDstMaskLength((byte) 0x01)
55 .withSrcPrefix(srcAddress1)
56 .withDstPrefix(dstAddress1)
57 .build();
Jian Lie9af3b42016-08-08 15:50:01 +090058
Jian Li84b75822016-10-04 23:10:35 +090059 SourceDestAddressBuilder builder2 = new SourceDestAddressBuilder();
Jian Li115d8602016-08-15 20:21:53 +090060
61 sameAsAddress1 = builder2
62 .withReserved((short) 1)
63 .withSrcMaskLength((byte) 0x01)
64 .withDstMaskLength((byte) 0x01)
65 .withSrcPrefix(srcAddress1)
66 .withDstPrefix(dstAddress1)
67 .build();
68
Jian Li84b75822016-10-04 23:10:35 +090069 SourceDestAddressBuilder builder3 = new SourceDestAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090070
71 LispIpv4Address srcAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
72 LispIpv4Address dstAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2"));
73
Jian Li115d8602016-08-15 20:21:53 +090074 address2 = builder3
75 .withReserved((short) 2)
76 .withSrcMaskLength((byte) 0x02)
77 .withDstMaskLength((byte) 0x02)
78 .withSrcPrefix(srcAddress2)
79 .withDstPrefix(dstAddress2)
80 .build();
Jian Lie9af3b42016-08-08 15:50:01 +090081 }
82
83 @Test
84 public void testEquality() {
85 new EqualsTester()
86 .addEqualityGroup(address1, sameAsAddress1)
87 .addEqualityGroup(address2).testEquals();
88 }
89
90 @Test
91 public void testConstruction() {
92 LispSourceDestLcafAddress sourceDestLcafAddress = address1;
93
94 LispIpv4Address srcAddress = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
95 LispIpv4Address dstAddress = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
96
97 assertThat(sourceDestLcafAddress.getReserved(), is((short) 1));
98 assertThat(sourceDestLcafAddress.getSrcMaskLength(), is((byte) 0x01));
99 assertThat(sourceDestLcafAddress.getDstMaskLength(), is((byte) 0x01));
100 assertThat(sourceDestLcafAddress.getSrcPrefix(), is(srcAddress));
101 assertThat(sourceDestLcafAddress.getDstPrefix(), is(dstAddress));
102 }
Jian Li76ea0572016-08-29 12:41:16 +0900103
104 @Test
105 public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
106 ByteBuf byteBuf = Unpooled.buffer();
107
108 SourceDestLcafAddressWriter writer = new SourceDestLcafAddressWriter();
109 writer.writeTo(byteBuf, address1);
110
111 SourceDestLcafAddressReader reader = new SourceDestLcafAddressReader();
112 LispSourceDestLcafAddress deserialized = reader.readFrom(byteBuf);
113
Jian Li5e505c62016-12-05 02:44:24 +0900114 new EqualsTester().addEqualityGroup(address1, deserialized).testEquals();
Jian Li76ea0572016-08-29 12:41:16 +0900115 }
Jian Lie9af3b42016-08-08 15:50:01 +0900116}