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 | b26d350 | 2016-08-31 01:43:24 +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 | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 26 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 27 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 28 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder; |
| 29 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.DefaultRegisterBuilder; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 30 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterReader; |
| 31 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterWriter; |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 32 | import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder; |
| 33 | import org.onosproject.lisp.msg.protocols.LispMapRegister.RegisterBuilder; |
| 34 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 35 | |
| 36 | import java.util.List; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 37 | |
| 38 | import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | import static org.hamcrest.Matchers.is; |
| 40 | |
| 41 | /** |
| 42 | * Unit tests for DefaultLispMapRegister class. |
| 43 | */ |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 44 | public final class DefaultLispMapRegisterTest { |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 45 | |
| 46 | private LispMapRegister register1; |
| 47 | private LispMapRegister sameAsRegister1; |
| 48 | private LispMapRegister register2; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 49 | private static final String AUTH_KEY = "onos"; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 50 | |
| 51 | @Before |
| 52 | public void setup() { |
| 53 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 54 | RegisterBuilder builder1 = new DefaultRegisterBuilder(); |
| 55 | |
| 56 | List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord()); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 57 | |
| 58 | register1 = builder1 |
| 59 | .withIsProxyMapReply(true) |
| 60 | .withIsWantMapNotify(false) |
| 61 | .withKeyId((short) 1) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 62 | .withAuthKey(AUTH_KEY) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 63 | .withNonce(1L) |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 64 | .withMapRecords(records1) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 65 | .build(); |
| 66 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 67 | RegisterBuilder builder2 = new DefaultRegisterBuilder(); |
| 68 | |
| 69 | List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord()); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 70 | |
| 71 | sameAsRegister1 = builder2 |
| 72 | .withIsProxyMapReply(true) |
| 73 | .withIsWantMapNotify(false) |
| 74 | .withKeyId((short) 1) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 75 | .withAuthKey(AUTH_KEY) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 76 | .withNonce(1L) |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 77 | .withMapRecords(records2) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 78 | .build(); |
| 79 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 80 | RegisterBuilder builder3 = new DefaultRegisterBuilder(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 81 | |
| 82 | register2 = builder3 |
| 83 | .withIsProxyMapReply(true) |
| 84 | .withIsWantMapNotify(false) |
| 85 | .withKeyId((short) 2) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 86 | .withAuthKey(AUTH_KEY) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 87 | .withNonce(2L) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 88 | .build(); |
| 89 | } |
| 90 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 91 | private LispMapRecord getMapRecord() { |
| 92 | MapRecordBuilder builder1 = new DefaultMapRecordBuilder(); |
| 93 | |
| 94 | LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 95 | |
| 96 | return builder1 |
| 97 | .withRecordTtl(100) |
| 98 | .withAuthoritative(true) |
| 99 | .withMapVersionNumber((short) 1) |
| 100 | .withMaskLength((byte) 0x01) |
| 101 | .withAction(LispMapReplyAction.NativelyForward) |
| 102 | .withEidPrefixAfi(ipv4Locator1) |
| 103 | .build(); |
| 104 | } |
| 105 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 106 | @Test |
| 107 | public void testEquality() { |
| 108 | new EqualsTester() |
| 109 | .addEqualityGroup(register1, sameAsRegister1) |
| 110 | .addEqualityGroup(register2).testEquals(); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testConstruction() { |
| 115 | DefaultLispMapRegister register = (DefaultLispMapRegister) register1; |
| 116 | |
| 117 | assertThat(register.isProxyMapReply(), is(true)); |
| 118 | assertThat(register.isWantMapNotify(), is(false)); |
| 119 | assertThat(register.getKeyId(), is((short) 1)); |
| 120 | assertThat(register.getNonce(), is(1L)); |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 121 | assertThat(register.getRecordCount(), is(2)); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testSerialization() throws LispReaderException, LispWriterException, LispParseError { |
| 126 | ByteBuf byteBuf = Unpooled.buffer(); |
| 127 | |
| 128 | RegisterWriter writer = new RegisterWriter(); |
| 129 | writer.writeTo(byteBuf, register1); |
| 130 | |
| 131 | RegisterReader reader = new RegisterReader(); |
| 132 | LispMapRegister deserialized = reader.readFrom(byteBuf); |
| 133 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 134 | new EqualsTester().addEqualityGroup(register1, deserialized).testEquals(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 135 | } |
| 136 | } |