blob: ca5b1f8d82bdc6a9051e2a89de737cdaf0cea785 [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;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080023import org.onlab.packet.Ip4Address;
24import org.onlab.packet.Ip4Prefix;
Jonathan Hart335ef462014-10-16 08:20:46 -070025
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() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080035 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
36 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070037
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() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080048 Ip4Prefix prefix = null;
49 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070050
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() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080059 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
60 Ip4Address nextHop = null;
Jonathan Hart335ef462014-10-16 08:20:46 -070061
62 new RouteEntry(prefix, nextHop);
63 }
64
65 /**
66 * Tests getting the fields of a route entry.
67 */
68 @Test
69 public void testGetFields() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080070 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
71 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070072
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() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080083 Ip4Prefix prefix;
Jonathan Hart335ef462014-10-16 08:20:46 -070084
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080085 prefix = Ip4Prefix.valueOf("0.0.0.0/0");
Jonathan Hart335ef462014-10-16 08:20:46 -070086 assertThat(RouteEntry.createBinaryString(prefix), is(""));
87
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080088 prefix = Ip4Prefix.valueOf("192.168.166.0/22");
Jonathan Hart335ef462014-10-16 08:20:46 -070089 assertThat(RouteEntry.createBinaryString(prefix),
90 is("1100000010101000101001"));
91
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080092 prefix = Ip4Prefix.valueOf("192.168.166.0/23");
Jonathan Hart335ef462014-10-16 08:20:46 -070093 assertThat(RouteEntry.createBinaryString(prefix),
94 is("11000000101010001010011"));
95
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080096 prefix = Ip4Prefix.valueOf("192.168.166.0/24");
Jonathan Hart335ef462014-10-16 08:20:46 -070097 assertThat(RouteEntry.createBinaryString(prefix),
98 is("110000001010100010100110"));
99
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800100 prefix = Ip4Prefix.valueOf("130.162.10.1/25");
Jonathan Hart335ef462014-10-16 08:20:46 -0700101 assertThat(RouteEntry.createBinaryString(prefix),
102 is("1000001010100010000010100"));
103
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800104 prefix = Ip4Prefix.valueOf("255.255.255.255/32");
Jonathan Hart335ef462014-10-16 08:20:46 -0700105 assertThat(RouteEntry.createBinaryString(prefix),
106 is("11111111111111111111111111111111"));
107 }
108
109 /**
110 * Tests equality of {@link RouteEntry}.
111 */
112 @Test
113 public void testEquality() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800114 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
115 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700116 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
117
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800118 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/24");
119 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700120 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() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800130 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
131 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700132 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
133
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800134 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25"); // Different
135 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700136 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
137
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800138 Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24");
139 Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9"); // Different
Jonathan Hart335ef462014-10-16 08:20:46 -0700140 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() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800151 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
152 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700153 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}