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 | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 57 | |
| 58 | request1 = builder1 |
| 59 | .withIsAuthoritative(true) |
| 60 | .withIsMapDataPresent(true) |
| 61 | .withIsPitr(false) |
| 62 | .withIsProbe(false) |
| 63 | .withIsSmr(true) |
| 64 | .withIsSmrInvoked(false) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 65 | .withSourceEid(ipv4Eid1) |
| 66 | .withItrRlocs(rlocs1) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 67 | .withNonce(1L) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 68 | .withRecordCount((byte) 0) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 69 | .build(); |
| 70 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 71 | RequestBuilder builder2 = new DefaultRequestBuilder(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 72 | |
| 73 | sameAsRequest1 = builder2 |
| 74 | .withIsAuthoritative(true) |
| 75 | .withIsMapDataPresent(true) |
| 76 | .withIsPitr(false) |
| 77 | .withIsProbe(false) |
| 78 | .withIsSmr(true) |
| 79 | .withIsSmrInvoked(false) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 80 | .withSourceEid(ipv4Eid1) |
| 81 | .withItrRlocs(rlocs1) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 82 | .withNonce(1L) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 83 | .withRecordCount((byte) 0) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 84 | .build(); |
| 85 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 86 | RequestBuilder builder3 = new DefaultRequestBuilder(); |
| 87 | |
| 88 | LispIpv4Address ipv4Eid2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2")); |
| 89 | |
| 90 | LispIpv4Address ipv4Rloc3 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1")); |
| 91 | LispIpv4Address ipv4Rloc4 = new LispIpv4Address(IpAddress.valueOf("10.1.1.2")); |
| 92 | |
| 93 | List<LispAfiAddress> rlocs2 = ImmutableList.of(ipv4Rloc3, ipv4Rloc4); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 94 | |
| 95 | request2 = builder3 |
| 96 | .withIsAuthoritative(false) |
| 97 | .withIsMapDataPresent(false) |
| 98 | .withIsPitr(true) |
| 99 | .withIsProbe(true) |
| 100 | .withIsSmr(false) |
| 101 | .withIsSmrInvoked(true) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 102 | .withSourceEid(ipv4Eid2) |
| 103 | .withItrRlocs(rlocs2) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 104 | .withNonce(2L) |
| 105 | .withRecordCount((byte) 0x02) |
| 106 | .build(); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testEquality() { |
| 111 | new EqualsTester() |
| 112 | .addEqualityGroup(request1, sameAsRequest1) |
| 113 | .addEqualityGroup(request2).testEquals(); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testConstruction() { |
| 118 | DefaultLispMapRequest request = (DefaultLispMapRequest) request1; |
| 119 | |
| 120 | assertThat(request.isAuthoritative(), is(true)); |
| 121 | assertThat(request.isMapDataPresent(), is(true)); |
| 122 | assertThat(request.isPitr(), is(false)); |
| 123 | assertThat(request.isProbe(), is(false)); |
| 124 | assertThat(request.isSmr(), is(true)); |
| 125 | assertThat(request.isSmrInvoked(), is(false)); |
| 126 | assertThat(request.getNonce(), is(1L)); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 127 | assertThat(request.getRecordCount(), is((byte) 0)); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testSerialization() throws LispReaderException, LispWriterException, LispParseError { |
| 132 | ByteBuf byteBuf = Unpooled.buffer(); |
| 133 | RequestWriter writer = new RequestWriter(); |
| 134 | writer.writeTo(byteBuf, request1); |
| 135 | |
| 136 | RequestReader reader = new RequestReader(); |
| 137 | LispMapRequest deserialized = reader.readFrom(byteBuf); |
| 138 | |
| 139 | new EqualsTester() |
| 140 | .addEqualityGroup(request1, deserialized).testEquals(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 141 | } |
| 142 | } |