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 | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 27 | import org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataAddressBuilder; |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 28 | import org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataLcafAddressWriter; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 29 | import org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataLcafAddressReader; |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 30 | |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.is; |
| 33 | |
| 34 | /** |
| 35 | * Unit tests for LispAppDataLcafAddress class. |
| 36 | */ |
| 37 | public class LispAppDataLcafAddressTest { |
| 38 | |
| 39 | private LispAppDataLcafAddress address1; |
| 40 | private LispAppDataLcafAddress sameAsAddress1; |
| 41 | private LispAppDataLcafAddress address2; |
| 42 | |
| 43 | @Before |
| 44 | public void setup() { |
| 45 | |
Jian Li | 84b7582 | 2016-10-04 23:10:35 +0900 | [diff] [blame] | 46 | AppDataAddressBuilder builder1 = new AppDataAddressBuilder(); |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 47 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 48 | LispAfiAddress ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 49 | |
| 50 | address1 = builder1 |
| 51 | .withProtocol((byte) 0x01) |
| 52 | .withIpTos((short) 10) |
| 53 | .withLocalPortLow((short) 1) |
| 54 | .withLocalPortHigh((short) 255) |
| 55 | .withRemotePortLow((short) 2) |
| 56 | .withRemotePortHigh((short) 254) |
| 57 | .withAddress(ipv4Address1) |
| 58 | .build(); |
| 59 | |
Jian Li | 84b7582 | 2016-10-04 23:10:35 +0900 | [diff] [blame] | 60 | AppDataAddressBuilder builder2 = new AppDataAddressBuilder(); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 61 | |
| 62 | sameAsAddress1 = builder2 |
| 63 | .withProtocol((byte) 0x01) |
| 64 | .withIpTos((short) 10) |
| 65 | .withLocalPortLow((short) 1) |
| 66 | .withLocalPortHigh((short) 255) |
| 67 | .withRemotePortLow((short) 2) |
| 68 | .withRemotePortHigh((short) 254) |
| 69 | .withAddress(ipv4Address1) |
| 70 | .build(); |
| 71 | |
Jian Li | 84b7582 | 2016-10-04 23:10:35 +0900 | [diff] [blame] | 72 | AppDataAddressBuilder builder3 = new AppDataAddressBuilder(); |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 73 | |
| 74 | LispAfiAddress ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1")); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 75 | |
| 76 | address2 = builder3 |
| 77 | .withProtocol((byte) 0x02) |
| 78 | .withIpTos((short) 20) |
| 79 | .withLocalPortLow((short) 1) |
| 80 | .withLocalPortHigh((short) 255) |
| 81 | .withRemotePortLow((short) 2) |
| 82 | .withRemotePortHigh((short) 254) |
| 83 | .withAddress(ipv4Address2) |
| 84 | .build(); |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testEquality() { |
| 89 | new EqualsTester() |
| 90 | .addEqualityGroup(address1, sameAsAddress1) |
| 91 | .addEqualityGroup(address2).testEquals(); |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testConstruction() { |
| 96 | LispAppDataLcafAddress appDataLcafAddress = address1; |
| 97 | |
| 98 | LispAfiAddress ipv4Address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 99 | |
| 100 | assertThat(appDataLcafAddress.getProtocol(), is((byte) 0x01)); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 101 | assertThat(appDataLcafAddress.getIpTos(), is(10)); |
| 102 | assertThat(appDataLcafAddress.getLocalPortLow(), is((short) 1)); |
| 103 | assertThat(appDataLcafAddress.getLocalPortHigh(), is((short) 255)); |
| 104 | assertThat(appDataLcafAddress.getRemotePortLow(), is((short) 2)); |
| 105 | assertThat(appDataLcafAddress.getRemotePortHigh(), is((short) 254)); |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 106 | assertThat(appDataLcafAddress.getAddress(), is(ipv4Address)); |
| 107 | } |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 108 | |
| 109 | @Test |
| 110 | public void testSerialization() throws LispWriterException, LispParseError, LispReaderException { |
| 111 | ByteBuf byteBuf = Unpooled.buffer(); |
| 112 | |
| 113 | AppDataLcafAddressWriter writer = new AppDataLcafAddressWriter(); |
| 114 | writer.writeTo(byteBuf, address1); |
| 115 | |
| 116 | AppDataLcafAddressReader reader = new AppDataLcafAddressReader(); |
| 117 | LispAppDataLcafAddress deserialized = reader.readFrom(byteBuf); |
| 118 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 119 | new EqualsTester().addEqualityGroup(address1, deserialized).testEquals(); |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 120 | } |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 121 | } |