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 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 19 | import com.google.common.testing.EqualsTester; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 20 | import io.netty.buffer.ByteBuf; |
| 21 | import io.netty.buffer.Unpooled; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 26 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 27 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.DefaultRequestBuilder; |
| 28 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.RequestReader; |
| 29 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.RequestWriter; |
| 30 | import org.onosproject.lisp.msg.protocols.LispMapRequest.RequestBuilder; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 31 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 32 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 33 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 34 | |
| 35 | import java.util.List; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 36 | |
| 37 | import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | import static org.hamcrest.Matchers.is; |
| 39 | |
| 40 | /** |
| 41 | * Unit tests for DefaultLispMapRequest class. |
| 42 | */ |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 43 | public final class DefaultLispMapRequestTest { |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 44 | |
| 45 | private LispMapRequest request1; |
| 46 | private LispMapRequest sameAsRequest1; |
| 47 | private LispMapRequest request2; |
| 48 | |
| 49 | @Before |
| 50 | public void setup() { |
| 51 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 52 | RequestBuilder builder1 = new DefaultRequestBuilder(); |
| 53 | |
| 54 | LispIpv4Address ipv4Eid1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 55 | |
| 56 | LispIpv4Address ipv4Rloc1 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1")); |
| 57 | LispIpv4Address ipv4Rloc2 = new LispIpv4Address(IpAddress.valueOf("10.1.1.2")); |
| 58 | |
| 59 | List<LispAfiAddress> rlocs1 = ImmutableList.of(ipv4Rloc1, ipv4Rloc2); |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 60 | List<LispEidRecord> records1 = ImmutableList.of(getEidRecord(), getEidRecord()); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 61 | |
| 62 | request1 = builder1 |
| 63 | .withIsAuthoritative(true) |
| 64 | .withIsMapDataPresent(true) |
| 65 | .withIsPitr(false) |
| 66 | .withIsProbe(false) |
| 67 | .withIsSmr(true) |
| 68 | .withIsSmrInvoked(false) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 69 | .withSourceEid(ipv4Eid1) |
| 70 | .withItrRlocs(rlocs1) |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 71 | .withEidRecords(records1) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 72 | .withNonce(1L) |
Jian Li | 3e1bac2 | 2016-12-06 02:08:12 +0900 | [diff] [blame^] | 73 | .withReplyRecord(1) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 74 | .build(); |
| 75 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 76 | RequestBuilder builder2 = new DefaultRequestBuilder(); |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 77 | List<LispEidRecord> records2 = ImmutableList.of(getEidRecord(), getEidRecord()); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 78 | |
| 79 | sameAsRequest1 = builder2 |
| 80 | .withIsAuthoritative(true) |
| 81 | .withIsMapDataPresent(true) |
| 82 | .withIsPitr(false) |
| 83 | .withIsProbe(false) |
| 84 | .withIsSmr(true) |
| 85 | .withIsSmrInvoked(false) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 86 | .withSourceEid(ipv4Eid1) |
| 87 | .withItrRlocs(rlocs1) |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 88 | .withEidRecords(records2) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 89 | .withNonce(1L) |
Jian Li | 3e1bac2 | 2016-12-06 02:08:12 +0900 | [diff] [blame^] | 90 | .withReplyRecord(1) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 91 | .build(); |
| 92 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 93 | RequestBuilder builder3 = new DefaultRequestBuilder(); |
| 94 | |
| 95 | LispIpv4Address ipv4Eid2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2")); |
| 96 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 97 | LispIpv4Address ipv4Rloc3 = new LispIpv4Address(IpAddress.valueOf("20.1.1.1")); |
| 98 | LispIpv4Address ipv4Rloc4 = new LispIpv4Address(IpAddress.valueOf("20.1.1.2")); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 99 | |
| 100 | List<LispAfiAddress> rlocs2 = ImmutableList.of(ipv4Rloc3, ipv4Rloc4); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 101 | |
| 102 | request2 = builder3 |
| 103 | .withIsAuthoritative(false) |
| 104 | .withIsMapDataPresent(false) |
| 105 | .withIsPitr(true) |
| 106 | .withIsProbe(true) |
| 107 | .withIsSmr(false) |
| 108 | .withIsSmrInvoked(true) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 109 | .withSourceEid(ipv4Eid2) |
| 110 | .withItrRlocs(rlocs2) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 111 | .withNonce(2L) |
Jian Li | 3e1bac2 | 2016-12-06 02:08:12 +0900 | [diff] [blame^] | 112 | .withReplyRecord(2) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 113 | .build(); |
| 114 | } |
| 115 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 116 | private LispEidRecord getEidRecord() { |
| 117 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf("20.1.1.1")); |
| 118 | return new LispEidRecord((byte) 24, eid); |
| 119 | } |
| 120 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 121 | @Test |
| 122 | public void testEquality() { |
| 123 | new EqualsTester() |
| 124 | .addEqualityGroup(request1, sameAsRequest1) |
| 125 | .addEqualityGroup(request2).testEquals(); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testConstruction() { |
| 130 | DefaultLispMapRequest request = (DefaultLispMapRequest) request1; |
| 131 | |
| 132 | assertThat(request.isAuthoritative(), is(true)); |
| 133 | assertThat(request.isMapDataPresent(), is(true)); |
| 134 | assertThat(request.isPitr(), is(false)); |
| 135 | assertThat(request.isProbe(), is(false)); |
| 136 | assertThat(request.isSmr(), is(true)); |
| 137 | assertThat(request.isSmrInvoked(), is(false)); |
| 138 | assertThat(request.getNonce(), is(1L)); |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 139 | assertThat(request.getRecordCount(), is(2)); |
Jian Li | 3e1bac2 | 2016-12-06 02:08:12 +0900 | [diff] [blame^] | 140 | assertThat(request.getReplyRecord(), is(1)); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testSerialization() throws LispReaderException, LispWriterException, LispParseError { |
| 145 | ByteBuf byteBuf = Unpooled.buffer(); |
| 146 | RequestWriter writer = new RequestWriter(); |
| 147 | writer.writeTo(byteBuf, request1); |
| 148 | |
| 149 | RequestReader reader = new RequestReader(); |
| 150 | LispMapRequest deserialized = reader.readFrom(byteBuf); |
| 151 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 152 | new EqualsTester().addEqualityGroup(request1, deserialized).testEquals(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 153 | } |
| 154 | } |