blob: 83b6f891d6c9ef6549bad1c1c56dc23c0402d093 [file] [log] [blame]
Jian Li27759352016-10-04 20:14:42 +09001/*
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 */
16package org.onosproject.lisp.msg.protocols;
17
18import com.google.common.testing.EqualsTester;
19import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onosproject.lisp.msg.exceptions.LispParseError;
25import org.onosproject.lisp.msg.exceptions.LispReaderException;
26import org.onosproject.lisp.msg.exceptions.LispWriterException;
27import org.onosproject.lisp.msg.protocols.DefaultLispInfoRequest.DefaultInfoRequestBuilder;
28import org.onosproject.lisp.msg.protocols.DefaultLispInfoRequest.InfoRequestReader;
29import org.onosproject.lisp.msg.protocols.DefaultLispInfoRequest.InfoRequestWriter;
30import org.onosproject.lisp.msg.protocols.LispInfoRequest.InfoRequestBuilder;
31import org.onosproject.lisp.msg.types.LispIpv4Address;
32
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.is;
35
36/**
37 * Unit tests for DefaultLispInfoRequest class.
38 */
39public final class DefaultLispInfoRequestTest {
40
41 private LispInfoRequest request1;
42 private LispInfoRequest sameAsRequest1;
43 private LispInfoRequest request2;
44
Jian Lid1a109e2016-11-12 09:00:42 +090045 private static final String AUTH_KEY = "onos";
46
Jian Li27759352016-10-04 20:14:42 +090047 @Before
48 public void setup() {
49
50 InfoRequestBuilder builder1 = new DefaultInfoRequestBuilder();
51
52 LispIpv4Address address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
53
54 request1 = builder1
55 .withNonce(1L)
56 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090057 .withAuthKey(AUTH_KEY)
Jian Li27759352016-10-04 20:14:42 +090058 .withInfoReply(false)
59 .withMaskLength((byte) 1)
60 .withEidPrefix(address1).build();
61
62 InfoRequestBuilder builder2 = new DefaultInfoRequestBuilder();
63
64 sameAsRequest1 = builder2
65 .withNonce(1L)
66 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090067 .withAuthKey(AUTH_KEY)
Jian Li27759352016-10-04 20:14:42 +090068 .withInfoReply(false)
69 .withMaskLength((byte) 1)
70 .withEidPrefix(address1).build();
71
72 InfoRequestBuilder builder3 = new DefaultInfoRequestBuilder();
73
74 LispIpv4Address address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
75
76 request2 = builder3
77 .withNonce(2L)
78 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +090079 .withAuthKey(AUTH_KEY)
Jian Li27759352016-10-04 20:14:42 +090080 .withInfoReply(true)
81 .withMaskLength((byte) 1)
82 .withEidPrefix(address2).build();
83 }
84
85 @Test
86 public void testEquality() {
87 new EqualsTester()
88 .addEqualityGroup(request1, sameAsRequest1)
89 .addEqualityGroup(request2).testEquals();
90 }
91
92 @Test
93 public void testConstruction() {
94 DefaultLispInfoRequest request = (DefaultLispInfoRequest) request1;
95
96 LispIpv4Address address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
97
Jian Lid1a109e2016-11-12 09:00:42 +090098 assertThat(request.isInfoReply(), is(false));
Jian Li27759352016-10-04 20:14:42 +090099 assertThat(request.getNonce(), is(1L));
100 assertThat(request.getKeyId(), is((short) 1));
101 assertThat(request.getMaskLength(), is((byte) 1));
102 assertThat(request.getPrefix(), is(address));
103 }
104
105
106 @Test
107 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
108 ByteBuf byteBuf = Unpooled.buffer();
109
110 InfoRequestWriter writer = new InfoRequestWriter();
111 writer.writeTo(byteBuf, request1);
112
113 InfoRequestReader reader = new InfoRequestReader();
114 LispInfoRequest deserialized = reader.readFrom(byteBuf);
115
116 new EqualsTester()
117 .addEqualityGroup(request1, deserialized).testEquals();
118 }
119}