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