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