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