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