Jian Li | f4bfbaa | 2017-02-08 14:59:58 +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.protocols; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import io.netty.buffer.ByteBuf; |
| 21 | import io.netty.buffer.Unpooled; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.DeserializationException; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 27 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 28 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 29 | import org.onosproject.lisp.msg.protocols.DefaultLispMapReferral.DefaultMapReferralBuilder; |
| 30 | import org.onosproject.lisp.msg.protocols.DefaultLispMapReferral.MapReferralReader; |
| 31 | import org.onosproject.lisp.msg.protocols.DefaultLispMapReferral.MapReferralWriter; |
| 32 | import org.onosproject.lisp.msg.protocols.DefaultLispReferralRecord.DefaultReferralRecordBuilder; |
| 33 | import org.onosproject.lisp.msg.protocols.LispMapReferral.MapReferralBuilder; |
| 34 | import org.onosproject.lisp.msg.protocols.LispReferralRecord.ReferralRecordBuilder; |
| 35 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 36 | |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static org.hamcrest.MatcherAssert.assertThat; |
| 40 | import static org.hamcrest.Matchers.is; |
| 41 | |
| 42 | /** |
| 43 | * Unit tests for DefaultLispMapReferral class. |
| 44 | */ |
| 45 | public final class DefaultLispMapReferralTest { |
| 46 | |
| 47 | private static final String IP_ADDRESS = "192.168.1.1"; |
| 48 | |
| 49 | private LispMapReferral referral1; |
| 50 | private LispMapReferral sameAsReferral1; |
| 51 | private LispMapReferral referral2; |
| 52 | |
| 53 | @Before |
| 54 | public void setup() { |
| 55 | |
| 56 | MapReferralBuilder builder1 = new DefaultMapReferralBuilder(); |
| 57 | |
| 58 | List<LispReferralRecord> records1 = |
| 59 | ImmutableList.of(getReferralRecord(), getReferralRecord()); |
| 60 | |
| 61 | referral1 = builder1 |
| 62 | .withNonce(1L) |
| 63 | .withReferralRecords(records1) |
| 64 | .build(); |
| 65 | |
| 66 | MapReferralBuilder builder2 = new DefaultMapReferralBuilder(); |
| 67 | |
| 68 | List<LispReferralRecord> records2 = |
| 69 | ImmutableList.of(getReferralRecord(), getReferralRecord()); |
| 70 | |
| 71 | sameAsReferral1 = builder2 |
| 72 | .withNonce(1L) |
| 73 | .withReferralRecords(records1) |
| 74 | .build(); |
| 75 | |
| 76 | MapReferralBuilder builder3 = new DefaultMapReferralBuilder(); |
| 77 | |
| 78 | referral2 = builder3 |
| 79 | .withNonce(2L) |
| 80 | .build(); |
| 81 | } |
| 82 | |
| 83 | private LispReferralRecord getReferralRecord() { |
| 84 | ReferralRecordBuilder builder = new DefaultReferralRecordBuilder(); |
| 85 | |
| 86 | LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS)); |
| 87 | |
| 88 | return builder |
| 89 | .withRecordTtl(100) |
| 90 | .withIsAuthoritative(true) |
| 91 | .withIsIncomplete(false) |
| 92 | .withMapVersionNumber((short) 1) |
| 93 | .withMaskLength((byte) 0x01) |
| 94 | .withAction(LispMapReplyAction.NativelyForward) |
| 95 | .withEidPrefixAfi(ipv4Locator) |
| 96 | .build(); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testEquality() { |
| 101 | new EqualsTester() |
| 102 | .addEqualityGroup(referral1, sameAsReferral1) |
| 103 | .addEqualityGroup(referral2).testEquals(); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testConstruction() { |
| 108 | LispMapReferral referral = referral1; |
| 109 | |
| 110 | assertThat(referral.getNonce(), is(1L)); |
| 111 | assertThat(referral.getRecordCount(), is(2)); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testSerialization() throws LispReaderException, LispWriterException, |
| 116 | LispParseError, DeserializationException { |
| 117 | ByteBuf byteBuf = Unpooled.buffer(); |
| 118 | |
| 119 | MapReferralWriter writer = new MapReferralWriter(); |
| 120 | writer.writeTo(byteBuf, referral1); |
| 121 | |
| 122 | MapReferralReader reader = new MapReferralReader(); |
| 123 | LispMapReferral deserialized = reader.readFrom(byteBuf); |
| 124 | |
| 125 | new EqualsTester().addEqualityGroup(referral1, deserialized).testEquals(); |
| 126 | } |
| 127 | } |