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