blob: 9af90a75d6c21be7f3a7141009a74d4c80c7c763 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart335ef462014-10-16 08:20:46 -070016package org.onlab.onos.sdnip;
17
18import static org.hamcrest.Matchers.is;
19import static org.hamcrest.Matchers.not;
20import static org.junit.Assert.assertThat;
21
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.IpPrefix;
25
26/**
27 * Unit tests for the RouteEntry class.
28 */
29public class RouteEntryTest {
30 /**
31 * Tests valid class constructor.
32 */
33 @Test
34 public void testConstructor() {
35 IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24");
36 IpAddress nextHop = IpAddress.valueOf("5.6.7.8");
37
38 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
39 assertThat(routeEntry.toString(),
40 is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
41 }
42
43 /**
44 * Tests invalid class constructor for null IPv4 prefix.
45 */
46 @Test(expected = NullPointerException.class)
47 public void testInvalidConstructorNullPrefix() {
48 IpPrefix prefix = null;
49 IpAddress nextHop = IpAddress.valueOf("5.6.7.8");
50
51 new RouteEntry(prefix, nextHop);
52 }
53
54 /**
55 * Tests invalid class constructor for null IPv4 next-hop.
56 */
57 @Test(expected = NullPointerException.class)
58 public void testInvalidConstructorNullNextHop() {
59 IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24");
60 IpAddress nextHop = null;
61
62 new RouteEntry(prefix, nextHop);
63 }
64
65 /**
66 * Tests getting the fields of a route entry.
67 */
68 @Test
69 public void testGetFields() {
70 IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24");
71 IpAddress nextHop = IpAddress.valueOf("5.6.7.8");
72
73 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
74 assertThat(routeEntry.prefix(), is(prefix));
75 assertThat(routeEntry.nextHop(), is(nextHop));
76 }
77
78 /**
79 * Tests creating a binary string from IPv4 prefix.
80 */
81 @Test
82 public void testCreateBinaryString() {
83 IpPrefix prefix;
84
85 prefix = IpPrefix.valueOf("0.0.0.0/0");
86 assertThat(RouteEntry.createBinaryString(prefix), is(""));
87
88 prefix = IpPrefix.valueOf("192.168.166.0/22");
89 assertThat(RouteEntry.createBinaryString(prefix),
90 is("1100000010101000101001"));
91
92 prefix = IpPrefix.valueOf("192.168.166.0/23");
93 assertThat(RouteEntry.createBinaryString(prefix),
94 is("11000000101010001010011"));
95
96 prefix = IpPrefix.valueOf("192.168.166.0/24");
97 assertThat(RouteEntry.createBinaryString(prefix),
98 is("110000001010100010100110"));
99
100 prefix = IpPrefix.valueOf("130.162.10.1/25");
101 assertThat(RouteEntry.createBinaryString(prefix),
102 is("1000001010100010000010100"));
103
104 prefix = IpPrefix.valueOf("255.255.255.255/32");
105 assertThat(RouteEntry.createBinaryString(prefix),
106 is("11111111111111111111111111111111"));
107 }
108
109 /**
110 * Tests equality of {@link RouteEntry}.
111 */
112 @Test
113 public void testEquality() {
114 IpPrefix prefix1 = IpPrefix.valueOf("1.2.3.0/24");
115 IpAddress nextHop1 = IpAddress.valueOf("5.6.7.8");
116 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
117
118 IpPrefix prefix2 = IpPrefix.valueOf("1.2.3.0/24");
119 IpAddress nextHop2 = IpAddress.valueOf("5.6.7.8");
120 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
121
122 assertThat(routeEntry1, is(routeEntry2));
123 }
124
125 /**
126 * Tests non-equality of {@link RouteEntry}.
127 */
128 @Test
129 public void testNonEquality() {
130 IpPrefix prefix1 = IpPrefix.valueOf("1.2.3.0/24");
131 IpAddress nextHop1 = IpAddress.valueOf("5.6.7.8");
132 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
133
134 IpPrefix prefix2 = IpPrefix.valueOf("1.2.3.0/25"); // Different
135 IpAddress nextHop2 = IpAddress.valueOf("5.6.7.8");
136 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
137
138 IpPrefix prefix3 = IpPrefix.valueOf("1.2.3.0/24");
139 IpAddress nextHop3 = IpAddress.valueOf("5.6.7.9"); // Different
140 RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3);
141
142 assertThat(routeEntry1, is(not(routeEntry2)));
143 assertThat(routeEntry1, is(not(routeEntry3)));
144 }
145
146 /**
147 * Tests object string representation.
148 */
149 @Test
150 public void testToString() {
151 IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24");
152 IpAddress nextHop = IpAddress.valueOf("5.6.7.8");
153 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
154
155 assertThat(routeEntry.toString(),
156 is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
157 }
158}