blob: dca108797b20cf8cb2b0eb48ede4cf90da9b97a4 [file] [log] [blame]
Jian Lid55111b2017-02-07 10:36:40 +09001/*
2 * Copyright 2017-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.DeserializationException;
24import org.onlab.packet.IpAddress;
25import org.onosproject.lisp.msg.exceptions.LispParseError;
26import org.onosproject.lisp.msg.exceptions.LispReaderException;
27import org.onosproject.lisp.msg.exceptions.LispWriterException;
28import org.onosproject.lisp.msg.protocols.DefaultLispReferral.DefaultReferralBuilder;
29import org.onosproject.lisp.msg.protocols.DefaultLispReferral.ReferralReader;
30import org.onosproject.lisp.msg.protocols.DefaultLispReferral.ReferralWriter;
31import org.onosproject.lisp.msg.protocols.LispReferral.ReferralBuilder;
32import org.onosproject.lisp.msg.types.LispIpv4Address;
33
34import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.is;
36
37/**
38 * Unit tests for DefaultLispReferral class.
39 */
40public final class DefaultLispReferralTest {
41
42 private static final String IP_ADDRESS1 = "192.168.1.1";
43 private static final String IP_ADDRESS2 = "192.168.1.2";
44
45 private LispReferral referral1;
46 private LispReferral sameAsReferral1;
47 private LispReferral referral2;
48
49 @Before
50 public void setup() {
51
52 ReferralBuilder builder1 = new DefaultReferralBuilder();
53
54 LispIpv4Address ipv4Address1 =
55 new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS1));
56
57 referral1 = builder1
58 .withPriority((byte) 0x01)
59 .withWeight((byte) 0x01)
60 .withMulticastPriority((byte) 0x01)
61 .withMulticastWeight((byte) 0x01)
62 .withLocalLocator(true)
63 .withRlocProbed(false)
64 .withRouted(true)
65 .withLocatorAfi(ipv4Address1)
66 .build();
67
68 ReferralBuilder builder2 = new DefaultReferralBuilder();
69
70 sameAsReferral1 = builder2
71 .withPriority((byte) 0x01)
72 .withWeight((byte) 0x01)
73 .withMulticastPriority((byte) 0x01)
74 .withMulticastWeight((byte) 0x01)
75 .withLocalLocator(true)
76 .withRlocProbed(false)
77 .withRouted(true)
78 .withLocatorAfi(ipv4Address1)
79 .build();
80
81 ReferralBuilder builder3 = new DefaultReferralBuilder();
82
83 LispIpv4Address ipv4Address2 =
84 new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS2));
85
86 referral2 = builder3
87 .withPriority((byte) 0x02)
88 .withWeight((byte) 0x02)
89 .withMulticastPriority((byte) 0x02)
90 .withMulticastWeight((byte) 0x02)
91 .withLocalLocator(false)
92 .withRlocProbed(true)
93 .withRouted(false)
94 .withLocatorAfi(ipv4Address2)
95 .build();
96 }
97
98 @Test
99 public void testEquality() {
100 new EqualsTester()
101 .addEqualityGroup(referral1, sameAsReferral1)
102 .addEqualityGroup(referral2).testEquals();
103 }
104
105 @Test
106 public void testConstruction() {
107 LispReferral referral = referral1;
108
109 LispIpv4Address ipv4Address =
110 new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS1));
111
112 assertThat(referral.getPriority(), is((byte) 0x01));
113 assertThat(referral.getWeight(), is((byte) 0x01));
114 assertThat(referral.getMulticastPriority(), is((byte) 0x01));
115 assertThat(referral.getMulticastWeight(), is((byte) 0x01));
116 assertThat(referral.isLocalLocator(), is(true));
117 assertThat(referral.isRlocProbed(), is(false));
118 assertThat(referral.isRouted(), is(true));
119 assertThat(referral.getLocatorAfi(), is(ipv4Address));
120 }
121
122 @Test
123 public void testSerialization() throws LispReaderException, LispWriterException,
124 LispParseError, DeserializationException {
125 ByteBuf byteBuf = Unpooled.buffer();
126
127 ReferralWriter writer = new ReferralWriter();
128 writer.writeTo(byteBuf, referral1);
129
130 ReferralReader reader = new ReferralReader();
131 LispReferral deserialized = reader.readFrom(byteBuf);
132
133 new EqualsTester()
134 .addEqualityGroup(referral1, deserialized).testEquals();
135 }
136}