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.DefaultLispInfoReply.DefaultInfoReplyBuilder; |
| 28 | import org.onosproject.lisp.msg.protocols.DefaultLispInfoReply.InfoReplyReader; |
| 29 | import org.onosproject.lisp.msg.protocols.DefaultLispInfoReply.InfoReplyWriter; |
| 30 | import org.onosproject.lisp.msg.protocols.LispInfoReply.InfoReplyBuilder; |
| 31 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 32 | import org.onosproject.lisp.msg.types.LispNatLcafAddress; |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 33 | import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatAddressBuilder; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 34 | |
| 35 | import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | import static org.hamcrest.Matchers.is; |
| 37 | |
| 38 | /** |
| 39 | * Unit tests for DefaultLispInfoReply class. |
| 40 | */ |
| 41 | public final class DefaultLispInfoReplyTest { |
| 42 | |
| 43 | private LispInfoReply reply1; |
| 44 | private LispInfoReply sameAsReply1; |
| 45 | private LispInfoReply reply2; |
| 46 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 47 | private static final String AUTH_KEY = "onos"; |
| 48 | |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 49 | @Before |
| 50 | public void setup() { |
| 51 | |
| 52 | InfoReplyBuilder builder1 = new DefaultInfoReplyBuilder(); |
| 53 | |
| 54 | short msUdpPortNumber1 = 80; |
| 55 | short etrUdpPortNumber1 = 100; |
| 56 | LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 57 | LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2")); |
| 58 | LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3")); |
| 59 | |
| 60 | LispIpv4Address address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.4")); |
| 61 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 62 | LispNatLcafAddress natLcafAddress1 = new NatAddressBuilder() |
| 63 | .withLength((short) 0) |
| 64 | .withMsUdpPortNumber(msUdpPortNumber1) |
| 65 | .withEtrUdpPortNumber(etrUdpPortNumber1) |
| 66 | .withGlobalEtrRlocAddress(globalEtrRlocAddress1) |
| 67 | .withMsRlocAddress(msRlocAddress1) |
| 68 | .withPrivateEtrRlocAddress(privateEtrRlocAddress1) |
| 69 | .build(); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 70 | |
| 71 | reply1 = builder1 |
| 72 | .withNonce(1L) |
| 73 | .withKeyId((short) 1) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 74 | .withAuthKey(AUTH_KEY) |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 75 | .withIsInfoReply(false) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 76 | .withMaskLength((byte) 1) |
| 77 | .withEidPrefix(address1) |
| 78 | .withNatLcafAddress(natLcafAddress1).build(); |
| 79 | |
| 80 | InfoReplyBuilder builder2 = new DefaultInfoReplyBuilder(); |
| 81 | |
| 82 | sameAsReply1 = builder2 |
| 83 | .withNonce(1L) |
| 84 | .withKeyId((short) 1) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 85 | .withAuthKey(AUTH_KEY) |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 86 | .withIsInfoReply(false) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 87 | .withMaskLength((byte) 1) |
| 88 | .withEidPrefix(address1) |
| 89 | .withNatLcafAddress(natLcafAddress1).build(); |
| 90 | |
| 91 | InfoReplyBuilder builder3 = new DefaultInfoReplyBuilder(); |
| 92 | |
| 93 | short msUdpPortNumber2 = 81; |
| 94 | short etrUdpPortNumber2 = 101; |
| 95 | LispIpv4Address globalEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1")); |
| 96 | LispIpv4Address msRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2")); |
| 97 | LispIpv4Address privateEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.3")); |
| 98 | |
| 99 | LispIpv4Address address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.4")); |
| 100 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 101 | LispNatLcafAddress natLcafAddress2 = new NatAddressBuilder() |
| 102 | .withLength((short) 0) |
| 103 | .withMsUdpPortNumber(msUdpPortNumber2) |
| 104 | .withEtrUdpPortNumber(etrUdpPortNumber2) |
| 105 | .withGlobalEtrRlocAddress(globalEtrRlocAddress2) |
| 106 | .withMsRlocAddress(msRlocAddress2) |
| 107 | .withPrivateEtrRlocAddress(privateEtrRlocAddress2) |
| 108 | .build(); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 109 | |
| 110 | reply2 = builder3 |
| 111 | .withNonce(2L) |
| 112 | .withKeyId((short) 2) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 113 | .withAuthKey(AUTH_KEY) |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 114 | .withIsInfoReply(true) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 115 | .withMaskLength((byte) 1) |
| 116 | .withEidPrefix(address2) |
| 117 | .withNatLcafAddress(natLcafAddress2).build(); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testEquality() { |
| 122 | new EqualsTester() |
| 123 | .addEqualityGroup(reply1, sameAsReply1) |
| 124 | .addEqualityGroup(reply2).testEquals(); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testConstruction() { |
| 129 | DefaultLispInfoReply reply = (DefaultLispInfoReply) reply1; |
| 130 | |
| 131 | LispIpv4Address address = new LispIpv4Address(IpAddress.valueOf("192.168.1.4")); |
| 132 | |
| 133 | short msUdpPortNumber1 = 80; |
| 134 | short etrUdpPortNumber1 = 100; |
| 135 | LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 136 | LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2")); |
| 137 | LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3")); |
| 138 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 139 | LispNatLcafAddress natLcafAddress = new NatAddressBuilder() |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 140 | .withLength((short) 0) |
| 141 | .withMsUdpPortNumber(msUdpPortNumber1) |
| 142 | .withEtrUdpPortNumber(etrUdpPortNumber1) |
| 143 | .withGlobalEtrRlocAddress(globalEtrRlocAddress1) |
| 144 | .withMsRlocAddress(msRlocAddress1) |
| 145 | .withPrivateEtrRlocAddress(privateEtrRlocAddress1) |
| 146 | .build(); |
| 147 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 148 | assertThat(reply.isInfoReply(), is(false)); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 149 | assertThat(reply.getNonce(), is(1L)); |
| 150 | assertThat(reply.getKeyId(), is((short) 1)); |
| 151 | assertThat(reply.getMaskLength(), is((byte) 1)); |
| 152 | assertThat(reply.getPrefix(), is(address)); |
| 153 | assertThat(reply.getNatLcafAddress(), is(natLcafAddress)); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void testSerialization() throws LispReaderException, LispWriterException, LispParseError { |
| 158 | ByteBuf byteBuf = Unpooled.buffer(); |
| 159 | |
| 160 | InfoReplyWriter writer = new InfoReplyWriter(); |
| 161 | writer.writeTo(byteBuf, reply1); |
| 162 | |
| 163 | InfoReplyReader reader = new InfoReplyReader(); |
| 164 | LispInfoReply deserialized = reader.readFrom(byteBuf); |
| 165 | |
| 166 | new EqualsTester() |
| 167 | .addEqualityGroup(reply1, deserialized).testEquals(); |
| 168 | } |
| 169 | } |