Jian Li | 18f3bce | 2016-08-04 17:36:41 +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 | 18f3bce | 2016-08-04 17:36:41 +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.DefaultLispMapRecord.DefaultMapRecordBuilder; |
| 28 | import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 29 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 30 | |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.is; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 33 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.*; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Unit tests for DefaultLispMapRecord class. |
| 37 | */ |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 38 | public final class DefaultLispMapRecordTest { |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 39 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 40 | private static final String IP_ADDRESS_1 = "192.168.1.1"; |
| 41 | private static final String IP_ADDRESS_2 = "192.168.1.2"; |
| 42 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 43 | private LispMapRecord record1; |
| 44 | private LispMapRecord sameAsRecord1; |
| 45 | private LispMapRecord record2; |
| 46 | |
| 47 | @Before |
| 48 | public void setup() { |
| 49 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 50 | MapRecordBuilder builder1 = new DefaultMapRecordBuilder(); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 51 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 52 | LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1)); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 53 | |
| 54 | record1 = builder1 |
| 55 | .withRecordTtl(100) |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 56 | .withIsAuthoritative(true) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 57 | .withMapVersionNumber((short) 1) |
| 58 | .withMaskLength((byte) 0x01) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 59 | .withAction(LispMapReplyAction.NativelyForward) |
| 60 | .withEidPrefixAfi(ipv4Locator1) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 61 | .build(); |
| 62 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 63 | MapRecordBuilder builder2 = new DefaultMapRecordBuilder(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 64 | |
| 65 | sameAsRecord1 = builder2 |
| 66 | .withRecordTtl(100) |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 67 | .withIsAuthoritative(true) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 68 | .withMapVersionNumber((short) 1) |
| 69 | .withMaskLength((byte) 0x01) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 70 | .withAction(LispMapReplyAction.NativelyForward) |
| 71 | .withEidPrefixAfi(ipv4Locator1) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 72 | .build(); |
| 73 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 74 | MapRecordBuilder builder3 = new DefaultMapRecordBuilder(); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 75 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 76 | LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_2)); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 77 | |
| 78 | record2 = builder3 |
| 79 | .withRecordTtl(200) |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 80 | .withIsAuthoritative(false) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 81 | .withMapVersionNumber((short) 2) |
| 82 | .withMaskLength((byte) 0x02) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 83 | .withAction(LispMapReplyAction.Drop) |
| 84 | .withEidPrefixAfi(ipv4Locator2) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 85 | .build(); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testEquality() { |
| 90 | new EqualsTester() |
| 91 | .addEqualityGroup(record1, sameAsRecord1) |
| 92 | .addEqualityGroup(record2).testEquals(); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testConstruction() { |
| 97 | DefaultLispMapRecord record = (DefaultLispMapRecord) record1; |
| 98 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 99 | LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1)); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 100 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 101 | assertThat(record.getRecordTtl(), is(100)); |
| 102 | assertThat(record.isAuthoritative(), is(true)); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 103 | assertThat(record.getMapVersionNumber(), is((short) 1)); |
| 104 | assertThat(record.getMaskLength(), is((byte) 0x01)); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 105 | assertThat(record.getAction(), is(LispMapReplyAction.NativelyForward)); |
| 106 | assertThat(record.getEidPrefixAfi(), is(ipv4Locator)); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testSerialization() throws LispReaderException, LispWriterException, LispParseError { |
| 111 | ByteBuf byteBuf = Unpooled.buffer(); |
| 112 | |
| 113 | MapRecordWriter writer = new MapRecordWriter(); |
| 114 | writer.writeTo(byteBuf, record1); |
| 115 | |
| 116 | MapRecordReader reader = new MapRecordReader(); |
| 117 | LispMapRecord deserialized = reader.readFrom(byteBuf); |
| 118 | |
| 119 | new EqualsTester() |
| 120 | .addEqualityGroup(record1, deserialized).testEquals(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 121 | } |
| 122 | } |