Jian Li | 2af9eaa | 2017-02-05 09:15:07 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.types.lcaf; |
| 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.types.LispIpv4Address; |
| 28 | import org.onosproject.lisp.msg.types.lcaf.LispMulticastLcafAddress.MulticastAddressBuilder; |
| 29 | import org.onosproject.lisp.msg.types.lcaf.LispMulticastLcafAddress.MulticastLcafAddressReader; |
| 30 | import org.onosproject.lisp.msg.types.lcaf.LispMulticastLcafAddress.MulticastLcafAddressWriter; |
| 31 | |
| 32 | import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | import static org.hamcrest.Matchers.is; |
| 34 | |
| 35 | /** |
| 36 | * Unit tests for LispMulticastLcafAddress class. |
| 37 | */ |
| 38 | public class LispMulticastLcafAddressTest { |
| 39 | |
| 40 | private static final String IP_ADDRESS_1 = "192.168.1.1"; |
| 41 | private static final String IP_ADDRESS_2 = "192.168.1.2"; |
| 42 | |
| 43 | private LispMulticastLcafAddress address1; |
| 44 | private LispMulticastLcafAddress sameAsAddress1; |
| 45 | private LispMulticastLcafAddress address2; |
| 46 | |
| 47 | @Before |
| 48 | public void setup() { |
| 49 | |
| 50 | MulticastAddressBuilder builder1 = new MulticastAddressBuilder(); |
| 51 | |
| 52 | LispIpv4Address ipv4Address1 = |
| 53 | new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1)); |
| 54 | |
| 55 | address1 = builder1 |
| 56 | .withInstanceId(1) |
| 57 | .withSrcMaskLength((byte) 0x24) |
| 58 | .withGrpMaskLength((byte) 0x24) |
| 59 | .withSrcAddress(ipv4Address1) |
| 60 | .withGrpAddress(ipv4Address1) |
| 61 | .build(); |
| 62 | |
| 63 | MulticastAddressBuilder builder2 = new MulticastAddressBuilder(); |
| 64 | |
| 65 | sameAsAddress1 = builder2 |
| 66 | .withInstanceId(1) |
| 67 | .withSrcMaskLength((byte) 0x24) |
| 68 | .withGrpMaskLength((byte) 0x24) |
| 69 | .withSrcAddress(ipv4Address1) |
| 70 | .withGrpAddress(ipv4Address1) |
| 71 | .build(); |
| 72 | |
| 73 | MulticastAddressBuilder builder3 = new MulticastAddressBuilder(); |
| 74 | |
| 75 | LispIpv4Address ipv4Address2 = |
| 76 | new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_2)); |
| 77 | |
| 78 | address2 = builder3 |
| 79 | .withInstanceId(2) |
| 80 | .withSrcMaskLength((byte) 0x24) |
| 81 | .withGrpMaskLength((byte) 0x24) |
| 82 | .withSrcAddress(ipv4Address2) |
| 83 | .withGrpAddress(ipv4Address2) |
| 84 | .build(); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testEquality() { |
| 89 | new EqualsTester() |
| 90 | .addEqualityGroup(address1, sameAsAddress1) |
| 91 | .addEqualityGroup(address2).testEquals(); |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testConstruction() { |
| 96 | LispMulticastLcafAddress multicastLcafAddress = address1; |
| 97 | |
| 98 | LispIpv4Address ipv4Address = |
| 99 | new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1)); |
| 100 | |
| 101 | assertThat(multicastLcafAddress.getInstanceId(), is(1)); |
| 102 | assertThat(multicastLcafAddress.getSrcMaskLenth(), is((byte) 0x24)); |
| 103 | assertThat(multicastLcafAddress.getGrpMaskLength(), is((byte) 0x24)); |
| 104 | assertThat(multicastLcafAddress.getSrcAddress(), is(ipv4Address)); |
| 105 | assertThat(multicastLcafAddress.getGrpAddress(), is(ipv4Address)); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testSerialization() throws LispWriterException, LispParseError, |
| 110 | LispReaderException { |
| 111 | ByteBuf byteBuf = Unpooled.buffer(); |
| 112 | |
| 113 | MulticastLcafAddressWriter writer = new MulticastLcafAddressWriter(); |
| 114 | writer.writeTo(byteBuf, address1); |
| 115 | |
| 116 | MulticastLcafAddressReader reader = new MulticastLcafAddressReader(); |
| 117 | LispMulticastLcafAddress deserialized = reader.readFrom(byteBuf); |
| 118 | |
| 119 | new EqualsTester().addEqualityGroup(address1, deserialized).testEquals(); |
| 120 | } |
| 121 | } |