blob: 2ab8002e45b4d47014b4de3e474f9d550ebe1a4f [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.DefaultLispInfoReply.DefaultInfoReplyBuilder;
28import org.onosproject.lisp.msg.protocols.DefaultLispInfoReply.InfoReplyReader;
29import org.onosproject.lisp.msg.protocols.DefaultLispInfoReply.InfoReplyWriter;
30import org.onosproject.lisp.msg.protocols.LispInfoReply.InfoReplyBuilder;
31import org.onosproject.lisp.msg.types.LispIpv4Address;
32import org.onosproject.lisp.msg.types.LispNatLcafAddress;
33
34import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.is;
36
37/**
38 * Unit tests for DefaultLispInfoReply class.
39 */
40public final class DefaultLispInfoReplyTest {
41
42 private LispInfoReply reply1;
43 private LispInfoReply sameAsReply1;
44 private LispInfoReply reply2;
45
Jian Lid1a109e2016-11-12 09:00:42 +090046 private static final String AUTH_KEY = "onos";
47
Jian Li27759352016-10-04 20:14:42 +090048 @Before
49 public void setup() {
50
51 InfoReplyBuilder builder1 = new DefaultInfoReplyBuilder();
52
53 short msUdpPortNumber1 = 80;
54 short etrUdpPortNumber1 = 100;
55 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
56 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
57 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
58
59 LispIpv4Address address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.4"));
60
61 LispNatLcafAddress natLcafAddress1 = new LispNatLcafAddress.NatAddressBuilder()
62 .withLength((short) 0)
63 .withMsUdpPortNumber(msUdpPortNumber1)
64 .withEtrUdpPortNumber(etrUdpPortNumber1)
65 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
66 .withMsRlocAddress(msRlocAddress1)
67 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
68 .build();
69
70 reply1 = builder1
71 .withNonce(1L)
72 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090073 .withAuthKey(AUTH_KEY)
Jian Li27759352016-10-04 20:14:42 +090074 .withInfoReply(false)
75 .withMaskLength((byte) 1)
76 .withEidPrefix(address1)
77 .withNatLcafAddress(natLcafAddress1).build();
78
79 InfoReplyBuilder builder2 = new DefaultInfoReplyBuilder();
80
81 sameAsReply1 = builder2
82 .withNonce(1L)
83 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090084 .withAuthKey(AUTH_KEY)
Jian Li27759352016-10-04 20:14:42 +090085 .withInfoReply(false)
86 .withMaskLength((byte) 1)
87 .withEidPrefix(address1)
88 .withNatLcafAddress(natLcafAddress1).build();
89
90 InfoReplyBuilder builder3 = new DefaultInfoReplyBuilder();
91
92 short msUdpPortNumber2 = 81;
93 short etrUdpPortNumber2 = 101;
94 LispIpv4Address globalEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
95 LispIpv4Address msRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2"));
96 LispIpv4Address privateEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.3"));
97
98 LispIpv4Address address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.4"));
99
100 LispNatLcafAddress natLcafAddress2 = new LispNatLcafAddress.NatAddressBuilder()
101 .withLength((short) 0)
102 .withMsUdpPortNumber(msUdpPortNumber2)
103 .withEtrUdpPortNumber(etrUdpPortNumber2)
104 .withGlobalEtrRlocAddress(globalEtrRlocAddress2)
105 .withMsRlocAddress(msRlocAddress2)
106 .withPrivateEtrRlocAddress(privateEtrRlocAddress2)
107 .build();
108
109 reply2 = builder3
110 .withNonce(2L)
111 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +0900112 .withAuthKey(AUTH_KEY)
Jian Li27759352016-10-04 20:14:42 +0900113 .withInfoReply(true)
114 .withMaskLength((byte) 1)
115 .withEidPrefix(address2)
116 .withNatLcafAddress(natLcafAddress2).build();
117 }
118
119 @Test
120 public void testEquality() {
121 new EqualsTester()
122 .addEqualityGroup(reply1, sameAsReply1)
123 .addEqualityGroup(reply2).testEquals();
124 }
125
126 @Test
127 public void testConstruction() {
128 DefaultLispInfoReply reply = (DefaultLispInfoReply) reply1;
129
130 LispIpv4Address address = new LispIpv4Address(IpAddress.valueOf("192.168.1.4"));
131
132 short msUdpPortNumber1 = 80;
133 short etrUdpPortNumber1 = 100;
134 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
135 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
136 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
137
138 LispNatLcafAddress natLcafAddress = new LispNatLcafAddress.NatAddressBuilder()
139 .withLength((short) 0)
140 .withMsUdpPortNumber(msUdpPortNumber1)
141 .withEtrUdpPortNumber(etrUdpPortNumber1)
142 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
143 .withMsRlocAddress(msRlocAddress1)
144 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
145 .build();
146
Jian Lid1a109e2016-11-12 09:00:42 +0900147 assertThat(reply.isInfoReply(), is(false));
Jian Li27759352016-10-04 20:14:42 +0900148 assertThat(reply.getNonce(), is(1L));
149 assertThat(reply.getKeyId(), is((short) 1));
150 assertThat(reply.getMaskLength(), is((byte) 1));
151 assertThat(reply.getPrefix(), is(address));
152 assertThat(reply.getNatLcafAddress(), is(natLcafAddress));
153 }
154
155 @Test
156 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
157 ByteBuf byteBuf = Unpooled.buffer();
158
159 InfoReplyWriter writer = new InfoReplyWriter();
160 writer.writeTo(byteBuf, reply1);
161
162 InfoReplyReader reader = new InfoReplyReader();
163 LispInfoReply deserialized = reader.readFrom(byteBuf);
164
165 new EqualsTester()
166 .addEqualityGroup(reply1, deserialized).testEquals();
167 }
168}