Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.lisp.msg.types; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame^] | 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.buffer.Unpooled; |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame^] | 24 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 25 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 26 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 27 | |
| 28 | import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | import static org.hamcrest.Matchers.is; |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame^] | 30 | import static org.onosproject.lisp.msg.types.LispSourceDestLcafAddress.SourceDestLcafAddressReader; |
| 31 | import static org.onosproject.lisp.msg.types.LispSourceDestLcafAddress.SourceDestLcafAddressWriter; |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Unit tests for LispSourceDestLcafAddress class. |
| 35 | */ |
| 36 | public class LispSourceDestLcafAddressTest { |
| 37 | |
| 38 | private LispSourceDestLcafAddress address1; |
| 39 | private LispSourceDestLcafAddress sameAsAddress1; |
| 40 | private LispSourceDestLcafAddress address2; |
| 41 | |
| 42 | @Before |
| 43 | public void setup() { |
| 44 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 45 | LispSourceDestLcafAddress.SourceDestAddressBuilder builder1 = |
| 46 | new LispSourceDestLcafAddress.SourceDestAddressBuilder(); |
| 47 | |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 48 | LispIpv4Address srcAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 49 | LispIpv4Address dstAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2")); |
| 50 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 51 | address1 = builder1 |
| 52 | .withReserved((short) 1) |
| 53 | .withSrcMaskLength((byte) 0x01) |
| 54 | .withDstMaskLength((byte) 0x01) |
| 55 | .withSrcPrefix(srcAddress1) |
| 56 | .withDstPrefix(dstAddress1) |
| 57 | .build(); |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 58 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 59 | 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 Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 72 | |
| 73 | LispIpv4Address srcAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1")); |
| 74 | LispIpv4Address dstAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2")); |
| 75 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 76 | address2 = builder3 |
| 77 | .withReserved((short) 2) |
| 78 | .withSrcMaskLength((byte) 0x02) |
| 79 | .withDstMaskLength((byte) 0x02) |
| 80 | .withSrcPrefix(srcAddress2) |
| 81 | .withDstPrefix(dstAddress2) |
| 82 | .build(); |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 83 | } |
| 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 Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame^] | 105 | |
| 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 Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 119 | } |