blob: c3d74e483ea32615d8f2e13b0ee411b5940b468d [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 Lie9af3b42016-08-08 15:50:01 +090027
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.hamcrest.Matchers.is;
Jian Li76ea0572016-08-29 12:41:16 +090030import static org.onosproject.lisp.msg.types.LispSourceDestLcafAddress.SourceDestLcafAddressReader;
31import static org.onosproject.lisp.msg.types.LispSourceDestLcafAddress.SourceDestLcafAddressWriter;
Jian Lie9af3b42016-08-08 15:50:01 +090032
33/**
34 * Unit tests for LispSourceDestLcafAddress class.
35 */
36public class LispSourceDestLcafAddressTest {
37
38 private LispSourceDestLcafAddress address1;
39 private LispSourceDestLcafAddress sameAsAddress1;
40 private LispSourceDestLcafAddress address2;
41
42 @Before
43 public void setup() {
44
Jian Li115d8602016-08-15 20:21:53 +090045 LispSourceDestLcafAddress.SourceDestAddressBuilder builder1 =
46 new LispSourceDestLcafAddress.SourceDestAddressBuilder();
47
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 Li115d8602016-08-15 20:21:53 +090059 LispSourceDestLcafAddress.SourceDestAddressBuilder builder2 =
60 new LispSourceDestLcafAddress.SourceDestAddressBuilder();
61
62 sameAsAddress1 = builder2
63 .withReserved((short) 1)
64 .withSrcMaskLength((byte) 0x01)
65 .withDstMaskLength((byte) 0x01)
66 .withSrcPrefix(srcAddress1)
67 .withDstPrefix(dstAddress1)
68 .build();
69
70 LispSourceDestLcafAddress.SourceDestAddressBuilder builder3 =
71 new LispSourceDestLcafAddress.SourceDestAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090072
73 LispIpv4Address srcAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
74 LispIpv4Address dstAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2"));
75
Jian Li115d8602016-08-15 20:21:53 +090076 address2 = builder3
77 .withReserved((short) 2)
78 .withSrcMaskLength((byte) 0x02)
79 .withDstMaskLength((byte) 0x02)
80 .withSrcPrefix(srcAddress2)
81 .withDstPrefix(dstAddress2)
82 .build();
Jian Lie9af3b42016-08-08 15:50:01 +090083 }
84
85 @Test
86 public void testEquality() {
87 new EqualsTester()
88 .addEqualityGroup(address1, sameAsAddress1)
89 .addEqualityGroup(address2).testEquals();
90 }
91
92 @Test
93 public void testConstruction() {
94 LispSourceDestLcafAddress sourceDestLcafAddress = address1;
95
96 LispIpv4Address srcAddress = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
97 LispIpv4Address dstAddress = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
98
99 assertThat(sourceDestLcafAddress.getReserved(), is((short) 1));
100 assertThat(sourceDestLcafAddress.getSrcMaskLength(), is((byte) 0x01));
101 assertThat(sourceDestLcafAddress.getDstMaskLength(), is((byte) 0x01));
102 assertThat(sourceDestLcafAddress.getSrcPrefix(), is(srcAddress));
103 assertThat(sourceDestLcafAddress.getDstPrefix(), is(dstAddress));
104 }
Jian Li76ea0572016-08-29 12:41:16 +0900105
106 @Test
107 public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
108 ByteBuf byteBuf = Unpooled.buffer();
109
110 SourceDestLcafAddressWriter writer = new SourceDestLcafAddressWriter();
111 writer.writeTo(byteBuf, address1);
112
113 SourceDestLcafAddressReader reader = new SourceDestLcafAddressReader();
114 LispSourceDestLcafAddress deserialized = reader.readFrom(byteBuf);
115
116 new EqualsTester()
117 .addEqualityGroup(address1, deserialized).testEquals();
118 }
Jian Lie9af3b42016-08-08 15:50:01 +0900119}