Jian Li | 2775935 | 2016-10-04 20:14:42 +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; |
| 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.buffer.Unpooled; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 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; |
| 27 | import org.onosproject.lisp.msg.protocols.DefaultLispInfoRequest.DefaultInfoRequestBuilder; |
| 28 | import org.onosproject.lisp.msg.protocols.DefaultLispInfoRequest.InfoRequestReader; |
| 29 | import org.onosproject.lisp.msg.protocols.DefaultLispInfoRequest.InfoRequestWriter; |
| 30 | import org.onosproject.lisp.msg.protocols.LispInfoRequest.InfoRequestBuilder; |
| 31 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 32 | |
| 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.Matchers.is; |
| 35 | |
| 36 | /** |
| 37 | * Unit tests for DefaultLispInfoRequest class. |
| 38 | */ |
| 39 | public final class DefaultLispInfoRequestTest { |
| 40 | |
| 41 | private LispInfoRequest request1; |
| 42 | private LispInfoRequest sameAsRequest1; |
| 43 | private LispInfoRequest request2; |
| 44 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 45 | private static final String AUTH_KEY = "onos"; |
| 46 | |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 47 | @Before |
| 48 | public void setup() { |
| 49 | |
| 50 | InfoRequestBuilder builder1 = new DefaultInfoRequestBuilder(); |
| 51 | |
| 52 | LispIpv4Address address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 53 | |
| 54 | request1 = builder1 |
| 55 | .withNonce(1L) |
| 56 | .withKeyId((short) 1) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 57 | .withAuthKey(AUTH_KEY) |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 58 | .withIsInfoReply(false) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 59 | .withMaskLength((byte) 1) |
| 60 | .withEidPrefix(address1).build(); |
| 61 | |
| 62 | InfoRequestBuilder builder2 = new DefaultInfoRequestBuilder(); |
| 63 | |
| 64 | sameAsRequest1 = builder2 |
| 65 | .withNonce(1L) |
| 66 | .withKeyId((short) 1) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 67 | .withAuthKey(AUTH_KEY) |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 68 | .withIsInfoReply(false) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 69 | .withMaskLength((byte) 1) |
| 70 | .withEidPrefix(address1).build(); |
| 71 | |
| 72 | InfoRequestBuilder builder3 = new DefaultInfoRequestBuilder(); |
| 73 | |
| 74 | LispIpv4Address address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1")); |
| 75 | |
| 76 | request2 = builder3 |
| 77 | .withNonce(2L) |
| 78 | .withKeyId((short) 2) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 79 | .withAuthKey(AUTH_KEY) |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 80 | .withIsInfoReply(true) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 81 | .withMaskLength((byte) 1) |
| 82 | .withEidPrefix(address2).build(); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testEquality() { |
| 87 | new EqualsTester() |
| 88 | .addEqualityGroup(request1, sameAsRequest1) |
| 89 | .addEqualityGroup(request2).testEquals(); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testConstruction() { |
| 94 | DefaultLispInfoRequest request = (DefaultLispInfoRequest) request1; |
| 95 | |
| 96 | LispIpv4Address address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 97 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 98 | assertThat(request.isInfoReply(), is(false)); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 99 | assertThat(request.getNonce(), is(1L)); |
| 100 | assertThat(request.getKeyId(), is((short) 1)); |
| 101 | assertThat(request.getMaskLength(), is((byte) 1)); |
| 102 | assertThat(request.getPrefix(), is(address)); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | @Test |
| 107 | public void testSerialization() throws LispReaderException, LispWriterException, LispParseError { |
| 108 | ByteBuf byteBuf = Unpooled.buffer(); |
| 109 | |
| 110 | InfoRequestWriter writer = new InfoRequestWriter(); |
| 111 | writer.writeTo(byteBuf, request1); |
| 112 | |
| 113 | InfoRequestReader reader = new InfoRequestReader(); |
| 114 | LispInfoRequest deserialized = reader.readFrom(byteBuf); |
| 115 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 116 | new EqualsTester().addEqualityGroup(request1, deserialized).testEquals(); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 117 | } |
| 118 | } |