Jian Li | 4767190 | 2016-08-11 01:18:18 +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.protocols; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.buffer.Unpooled; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 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.protocols.DefaultLispLocatorRecord.DefaultLocatorRecordBuilder; |
| 28 | import org.onosproject.lisp.msg.protocols.LispLocatorRecord.LocatorRecordBuilder; |
| 29 | import org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.LocatorRecordWriter; |
| 30 | import org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.LocatorRecordReader; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 31 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 32 | |
| 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.Matchers.is; |
| 35 | |
| 36 | /** |
| 37 | * Unit tests for DefaultLispLocatorRecord class. |
| 38 | */ |
| 39 | public final class DefaultLispLocatorRecordTest { |
| 40 | |
| 41 | private LispLocatorRecord record1; |
| 42 | private LispLocatorRecord sameAsRecord1; |
| 43 | private LispLocatorRecord record2; |
| 44 | |
| 45 | @Before |
| 46 | public void setup() { |
| 47 | |
Jian Li | 84b7582 | 2016-10-04 23:10:35 +0900 | [diff] [blame] | 48 | LocatorRecordBuilder builder1 = new DefaultLocatorRecordBuilder(); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 49 | |
| 50 | LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 51 | |
| 52 | record1 = builder1 |
| 53 | .withPriority((byte) 0x01) |
| 54 | .withWeight((byte) 0x01) |
| 55 | .withMulticastPriority((byte) 0x01) |
| 56 | .withMulticastWeight((byte) 0x01) |
| 57 | .withLocalLocator(true) |
| 58 | .withRlocProbed(false) |
| 59 | .withRouted(true) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 60 | .withLocatorAfi(ipv4Locator1) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 61 | .build(); |
| 62 | |
Jian Li | 84b7582 | 2016-10-04 23:10:35 +0900 | [diff] [blame] | 63 | LocatorRecordBuilder builder2 = new DefaultLocatorRecordBuilder(); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 64 | |
| 65 | sameAsRecord1 = builder2 |
| 66 | .withPriority((byte) 0x01) |
| 67 | .withWeight((byte) 0x01) |
| 68 | .withMulticastPriority((byte) 0x01) |
| 69 | .withMulticastWeight((byte) 0x01) |
| 70 | .withLocalLocator(true) |
| 71 | .withRlocProbed(false) |
| 72 | .withRouted(true) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 73 | .withLocatorAfi(ipv4Locator1) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 74 | .build(); |
| 75 | |
Jian Li | 84b7582 | 2016-10-04 23:10:35 +0900 | [diff] [blame] | 76 | LocatorRecordBuilder builder3 = new DefaultLocatorRecordBuilder(); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 77 | |
| 78 | LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2")); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 79 | |
| 80 | record2 = builder3 |
| 81 | .withPriority((byte) 0x02) |
| 82 | .withWeight((byte) 0x02) |
| 83 | .withMulticastPriority((byte) 0x02) |
| 84 | .withMulticastWeight((byte) 0x02) |
| 85 | .withLocalLocator(false) |
| 86 | .withRlocProbed(true) |
| 87 | .withRouted(false) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 88 | .withLocatorAfi(ipv4Locator2) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 89 | .build(); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testEquality() { |
| 94 | new EqualsTester() |
| 95 | .addEqualityGroup(record1, sameAsRecord1) |
| 96 | .addEqualityGroup(record2).testEquals(); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testConstruction() { |
| 101 | DefaultLispLocatorRecord record = (DefaultLispLocatorRecord) record1; |
| 102 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 103 | LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 104 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 105 | assertThat(record.getPriority(), is((byte) 0x01)); |
| 106 | assertThat(record.getWeight(), is((byte) 0x01)); |
| 107 | assertThat(record.getMulticastPriority(), is((byte) 0x01)); |
| 108 | assertThat(record.getMulticastWeight(), is((byte) 0x01)); |
| 109 | assertThat(record.isLocalLocator(), is(true)); |
| 110 | assertThat(record.isRlocProbed(), is(false)); |
| 111 | assertThat(record.isRouted(), is(true)); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 112 | assertThat(record.getLocatorAfi(), is(ipv4Locator)); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testSerialization() throws LispReaderException, LispWriterException, LispParseError { |
| 117 | ByteBuf byteBuf = Unpooled.buffer(); |
| 118 | |
| 119 | LocatorRecordWriter writer = new LocatorRecordWriter(); |
| 120 | writer.writeTo(byteBuf, record1); |
| 121 | |
| 122 | LocatorRecordReader reader = new LocatorRecordReader(); |
| 123 | LispLocatorRecord deserialized = reader.readFrom(byteBuf); |
| 124 | |
| 125 | new EqualsTester() |
| 126 | .addEqualityGroup(record1, deserialized).testEquals(); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 127 | } |
| 128 | } |