blob: 00af10802a2900d9a5c7dbb74ac78887a49ed3cb [file] [log] [blame]
Jian Lie9af3b42016-08-08 15:50:01 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Lie9af3b42016-08-08 15:50:01 +09003 *
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 */
Jian Lif31019a2017-02-05 07:57:46 +090016package org.onosproject.lisp.msg.types.lcaf;
Jian Lie9af3b42016-08-08 15:50:01 +090017
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 Lif31019a2017-02-05 07:57:46 +090027import org.onosproject.lisp.msg.types.LispIpv4Address;
28import org.onosproject.lisp.msg.types.lcaf.LispSourceDestLcafAddress.SourceDestAddressBuilder;
29import org.onosproject.lisp.msg.types.lcaf.LispSourceDestLcafAddress.SourceDestLcafAddressReader;
30import org.onosproject.lisp.msg.types.lcaf.LispSourceDestLcafAddress.SourceDestLcafAddressWriter;
Jian Lie9af3b42016-08-08 15:50:01 +090031
32import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.Matchers.is;
34
35/**
36 * Unit tests for LispSourceDestLcafAddress class.
37 */
38public class LispSourceDestLcafAddressTest {
39
40 private LispSourceDestLcafAddress address1;
41 private LispSourceDestLcafAddress sameAsAddress1;
42 private LispSourceDestLcafAddress address2;
43
44 @Before
45 public void setup() {
46
Jian Li84b75822016-10-04 23:10:35 +090047 SourceDestAddressBuilder builder1 = new SourceDestAddressBuilder();
Jian Li115d8602016-08-15 20:21:53 +090048
Jian Lie9af3b42016-08-08 15:50:01 +090049 LispIpv4Address srcAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
50 LispIpv4Address dstAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
51
Jian Li115d8602016-08-15 20:21:53 +090052 address1 = builder1
53 .withReserved((short) 1)
54 .withSrcMaskLength((byte) 0x01)
55 .withDstMaskLength((byte) 0x01)
56 .withSrcPrefix(srcAddress1)
57 .withDstPrefix(dstAddress1)
58 .build();
Jian Lie9af3b42016-08-08 15:50:01 +090059
Jian Li84b75822016-10-04 23:10:35 +090060 SourceDestAddressBuilder builder2 = new SourceDestAddressBuilder();
Jian Li115d8602016-08-15 20:21:53 +090061
62 sameAsAddress1 = builder2
63 .withReserved((short) 1)
64 .withSrcMaskLength((byte) 0x01)
65 .withDstMaskLength((byte) 0x01)
66 .withSrcPrefix(srcAddress1)
67 .withDstPrefix(dstAddress1)
68 .build();
69
Jian Li84b75822016-10-04 23:10:35 +090070 SourceDestAddressBuilder builder3 = new SourceDestAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090071
72 LispIpv4Address srcAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
73 LispIpv4Address dstAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2"));
74
Jian Li115d8602016-08-15 20:21:53 +090075 address2 = builder3
76 .withReserved((short) 2)
77 .withSrcMaskLength((byte) 0x02)
78 .withDstMaskLength((byte) 0x02)
79 .withSrcPrefix(srcAddress2)
80 .withDstPrefix(dstAddress2)
81 .build();
Jian Lie9af3b42016-08-08 15:50:01 +090082 }
83
84 @Test
85 public void testEquality() {
86 new EqualsTester()
87 .addEqualityGroup(address1, sameAsAddress1)
88 .addEqualityGroup(address2).testEquals();
89 }
90
91 @Test
92 public void testConstruction() {
93 LispSourceDestLcafAddress sourceDestLcafAddress = address1;
94
95 LispIpv4Address srcAddress = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
96 LispIpv4Address dstAddress = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
97
98 assertThat(sourceDestLcafAddress.getReserved(), is((short) 1));
99 assertThat(sourceDestLcafAddress.getSrcMaskLength(), is((byte) 0x01));
100 assertThat(sourceDestLcafAddress.getDstMaskLength(), is((byte) 0x01));
101 assertThat(sourceDestLcafAddress.getSrcPrefix(), is(srcAddress));
102 assertThat(sourceDestLcafAddress.getDstPrefix(), is(dstAddress));
103 }
Jian Li76ea0572016-08-29 12:41:16 +0900104
105 @Test
106 public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
107 ByteBuf byteBuf = Unpooled.buffer();
108
109 SourceDestLcafAddressWriter writer = new SourceDestLcafAddressWriter();
110 writer.writeTo(byteBuf, address1);
111
112 SourceDestLcafAddressReader reader = new SourceDestLcafAddressReader();
113 LispSourceDestLcafAddress deserialized = reader.readFrom(byteBuf);
114
Jian Li5e505c62016-12-05 02:44:24 +0900115 new EqualsTester().addEqualityGroup(address1, deserialized).testEquals();
Jian Li76ea0572016-08-29 12:41:16 +0900116 }
Jian Lie9af3b42016-08-08 15:50:01 +0900117}