blob: 1952a03ea40373d9f0d468195a537620e0bb7e02 [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
46 @Before
47 public void setup() {
48
49 InfoReplyBuilder builder1 = new DefaultInfoReplyBuilder();
50
51 short msUdpPortNumber1 = 80;
52 short etrUdpPortNumber1 = 100;
53 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
54 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
55 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
56
57 LispIpv4Address address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.4"));
58
59 LispNatLcafAddress natLcafAddress1 = new LispNatLcafAddress.NatAddressBuilder()
60 .withLength((short) 0)
61 .withMsUdpPortNumber(msUdpPortNumber1)
62 .withEtrUdpPortNumber(etrUdpPortNumber1)
63 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
64 .withMsRlocAddress(msRlocAddress1)
65 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
66 .build();
67
68 reply1 = builder1
69 .withNonce(1L)
70 .withKeyId((short) 1)
71 .withInfoReply(false)
72 .withMaskLength((byte) 1)
73 .withEidPrefix(address1)
74 .withNatLcafAddress(natLcafAddress1).build();
75
76 InfoReplyBuilder builder2 = new DefaultInfoReplyBuilder();
77
78 sameAsReply1 = builder2
79 .withNonce(1L)
80 .withKeyId((short) 1)
81 .withInfoReply(false)
82 .withMaskLength((byte) 1)
83 .withEidPrefix(address1)
84 .withNatLcafAddress(natLcafAddress1).build();
85
86 InfoReplyBuilder builder3 = new DefaultInfoReplyBuilder();
87
88 short msUdpPortNumber2 = 81;
89 short etrUdpPortNumber2 = 101;
90 LispIpv4Address globalEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
91 LispIpv4Address msRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2"));
92 LispIpv4Address privateEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.3"));
93
94 LispIpv4Address address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.4"));
95
96 LispNatLcafAddress natLcafAddress2 = new LispNatLcafAddress.NatAddressBuilder()
97 .withLength((short) 0)
98 .withMsUdpPortNumber(msUdpPortNumber2)
99 .withEtrUdpPortNumber(etrUdpPortNumber2)
100 .withGlobalEtrRlocAddress(globalEtrRlocAddress2)
101 .withMsRlocAddress(msRlocAddress2)
102 .withPrivateEtrRlocAddress(privateEtrRlocAddress2)
103 .build();
104
105 reply2 = builder3
106 .withNonce(2L)
107 .withKeyId((short) 2)
108 .withInfoReply(true)
109 .withMaskLength((byte) 1)
110 .withEidPrefix(address2)
111 .withNatLcafAddress(natLcafAddress2).build();
112 }
113
114 @Test
115 public void testEquality() {
116 new EqualsTester()
117 .addEqualityGroup(reply1, sameAsReply1)
118 .addEqualityGroup(reply2).testEquals();
119 }
120
121 @Test
122 public void testConstruction() {
123 DefaultLispInfoReply reply = (DefaultLispInfoReply) reply1;
124
125 LispIpv4Address address = new LispIpv4Address(IpAddress.valueOf("192.168.1.4"));
126
127 short msUdpPortNumber1 = 80;
128 short etrUdpPortNumber1 = 100;
129 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
130 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
131 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
132
133 LispNatLcafAddress natLcafAddress = new LispNatLcafAddress.NatAddressBuilder()
134 .withLength((short) 0)
135 .withMsUdpPortNumber(msUdpPortNumber1)
136 .withEtrUdpPortNumber(etrUdpPortNumber1)
137 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
138 .withMsRlocAddress(msRlocAddress1)
139 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
140 .build();
141
142 assertThat(reply.hasInfoReply(), is(false));
143 assertThat(reply.getNonce(), is(1L));
144 assertThat(reply.getKeyId(), is((short) 1));
145 assertThat(reply.getMaskLength(), is((byte) 1));
146 assertThat(reply.getPrefix(), is(address));
147 assertThat(reply.getNatLcafAddress(), is(natLcafAddress));
148 }
149
150 @Test
151 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
152 ByteBuf byteBuf = Unpooled.buffer();
153
154 InfoReplyWriter writer = new InfoReplyWriter();
155 writer.writeTo(byteBuf, reply1);
156
157 InfoReplyReader reader = new InfoReplyReader();
158 LispInfoReply deserialized = reader.readFrom(byteBuf);
159
160 new EqualsTester()
161 .addEqualityGroup(reply1, deserialized).testEquals();
162 }
163}