blob: c47d276847e684d10062dd1c173dde30b1f327f3 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 Hart2da1e602015-02-18 19:09:24 -080016package org.onosproject.routing;
Jonathan Hart41349e92015-02-09 14:14:02 -080017
18import org.hamcrest.Matchers;
19import org.junit.Test;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.Ip4Prefix;
Jonathan Hart335ef462014-10-16 08:20:46 -070022
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.not;
25import static org.junit.Assert.assertThat;
26
Jonathan Hart335ef462014-10-16 08:20:46 -070027/**
28 * Unit tests for the RouteEntry class.
29 */
30public class RouteEntryTest {
31 /**
32 * Tests valid class constructor.
33 */
34 @Test
35 public void testConstructor() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080036 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
37 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070038
39 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
40 assertThat(routeEntry.toString(),
41 is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
42 }
43
44 /**
45 * Tests invalid class constructor for null IPv4 prefix.
46 */
47 @Test(expected = NullPointerException.class)
48 public void testInvalidConstructorNullPrefix() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080049 Ip4Prefix prefix = null;
50 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070051
52 new RouteEntry(prefix, nextHop);
53 }
54
55 /**
56 * Tests invalid class constructor for null IPv4 next-hop.
57 */
58 @Test(expected = NullPointerException.class)
59 public void testInvalidConstructorNullNextHop() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080060 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
61 Ip4Address nextHop = null;
Jonathan Hart335ef462014-10-16 08:20:46 -070062
63 new RouteEntry(prefix, nextHop);
64 }
65
66 /**
67 * Tests getting the fields of a route entry.
68 */
69 @Test
70 public void testGetFields() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080071 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
72 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070073
74 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
75 assertThat(routeEntry.prefix(), is(prefix));
76 assertThat(routeEntry.nextHop(), is(nextHop));
77 }
78
79 /**
80 * Tests creating a binary string from IPv4 prefix.
81 */
82 @Test
83 public void testCreateBinaryString() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080084 Ip4Prefix prefix;
Jonathan Hart335ef462014-10-16 08:20:46 -070085
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080086 prefix = Ip4Prefix.valueOf("0.0.0.0/0");
Jonathan Hart335ef462014-10-16 08:20:46 -070087 assertThat(RouteEntry.createBinaryString(prefix), is(""));
88
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080089 prefix = Ip4Prefix.valueOf("192.168.166.0/22");
Jonathan Hart335ef462014-10-16 08:20:46 -070090 assertThat(RouteEntry.createBinaryString(prefix),
91 is("1100000010101000101001"));
92
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080093 prefix = Ip4Prefix.valueOf("192.168.166.0/23");
Jonathan Hart335ef462014-10-16 08:20:46 -070094 assertThat(RouteEntry.createBinaryString(prefix),
95 is("11000000101010001010011"));
96
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080097 prefix = Ip4Prefix.valueOf("192.168.166.0/24");
Jonathan Hart335ef462014-10-16 08:20:46 -070098 assertThat(RouteEntry.createBinaryString(prefix),
99 is("110000001010100010100110"));
100
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800101 prefix = Ip4Prefix.valueOf("130.162.10.1/25");
Jonathan Hart335ef462014-10-16 08:20:46 -0700102 assertThat(RouteEntry.createBinaryString(prefix),
103 is("1000001010100010000010100"));
104
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800105 prefix = Ip4Prefix.valueOf("255.255.255.255/32");
Jonathan Hart335ef462014-10-16 08:20:46 -0700106 assertThat(RouteEntry.createBinaryString(prefix),
107 is("11111111111111111111111111111111"));
108 }
109
110 /**
111 * Tests equality of {@link RouteEntry}.
112 */
113 @Test
114 public void testEquality() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800115 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
116 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700117 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
118
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800119 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/24");
120 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700121 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
122
123 assertThat(routeEntry1, is(routeEntry2));
124 }
125
126 /**
127 * Tests non-equality of {@link RouteEntry}.
128 */
129 @Test
130 public void testNonEquality() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800131 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
132 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700133 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
134
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800135 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25"); // Different
136 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700137 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
138
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800139 Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24");
140 Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9"); // Different
Jonathan Hart335ef462014-10-16 08:20:46 -0700141 RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3);
142
Jonathan Hart41349e92015-02-09 14:14:02 -0800143 assertThat(routeEntry1, Matchers.is(not(routeEntry2)));
144 assertThat(routeEntry1, Matchers.is(not(routeEntry3)));
Jonathan Hart335ef462014-10-16 08:20:46 -0700145 }
146
147 /**
148 * Tests object string representation.
149 */
150 @Test
151 public void testToString() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800152 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
153 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700154 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
155
156 assertThat(routeEntry.toString(),
157 is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
158 }
159}