blob: 1cecd06dc8bb1d8e7b06aa9628a81171a8803db0 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Jonathan Hart335ef462014-10-16 08:20:46 -070019package org.onlab.onos.sdnip;
20
21import static org.hamcrest.Matchers.is;
22import static org.hamcrest.Matchers.not;
23import static org.junit.Assert.assertThat;
24
25import org.junit.Test;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.IpPrefix;
28
29/**
30 * Unit tests for the RouteEntry class.
31 */
32public class RouteEntryTest {
33 /**
34 * Tests valid class constructor.
35 */
36 @Test
37 public void testConstructor() {
38 IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24");
39 IpAddress nextHop = IpAddress.valueOf("5.6.7.8");
40
41 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
42 assertThat(routeEntry.toString(),
43 is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
44 }
45
46 /**
47 * Tests invalid class constructor for null IPv4 prefix.
48 */
49 @Test(expected = NullPointerException.class)
50 public void testInvalidConstructorNullPrefix() {
51 IpPrefix prefix = null;
52 IpAddress nextHop = IpAddress.valueOf("5.6.7.8");
53
54 new RouteEntry(prefix, nextHop);
55 }
56
57 /**
58 * Tests invalid class constructor for null IPv4 next-hop.
59 */
60 @Test(expected = NullPointerException.class)
61 public void testInvalidConstructorNullNextHop() {
62 IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24");
63 IpAddress nextHop = null;
64
65 new RouteEntry(prefix, nextHop);
66 }
67
68 /**
69 * Tests getting the fields of a route entry.
70 */
71 @Test
72 public void testGetFields() {
73 IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24");
74 IpAddress nextHop = IpAddress.valueOf("5.6.7.8");
75
76 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
77 assertThat(routeEntry.prefix(), is(prefix));
78 assertThat(routeEntry.nextHop(), is(nextHop));
79 }
80
81 /**
82 * Tests creating a binary string from IPv4 prefix.
83 */
84 @Test
85 public void testCreateBinaryString() {
86 IpPrefix prefix;
87
88 prefix = IpPrefix.valueOf("0.0.0.0/0");
89 assertThat(RouteEntry.createBinaryString(prefix), is(""));
90
91 prefix = IpPrefix.valueOf("192.168.166.0/22");
92 assertThat(RouteEntry.createBinaryString(prefix),
93 is("1100000010101000101001"));
94
95 prefix = IpPrefix.valueOf("192.168.166.0/23");
96 assertThat(RouteEntry.createBinaryString(prefix),
97 is("11000000101010001010011"));
98
99 prefix = IpPrefix.valueOf("192.168.166.0/24");
100 assertThat(RouteEntry.createBinaryString(prefix),
101 is("110000001010100010100110"));
102
103 prefix = IpPrefix.valueOf("130.162.10.1/25");
104 assertThat(RouteEntry.createBinaryString(prefix),
105 is("1000001010100010000010100"));
106
107 prefix = IpPrefix.valueOf("255.255.255.255/32");
108 assertThat(RouteEntry.createBinaryString(prefix),
109 is("11111111111111111111111111111111"));
110 }
111
112 /**
113 * Tests equality of {@link RouteEntry}.
114 */
115 @Test
116 public void testEquality() {
117 IpPrefix prefix1 = IpPrefix.valueOf("1.2.3.0/24");
118 IpAddress nextHop1 = IpAddress.valueOf("5.6.7.8");
119 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
120
121 IpPrefix prefix2 = IpPrefix.valueOf("1.2.3.0/24");
122 IpAddress nextHop2 = IpAddress.valueOf("5.6.7.8");
123 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
124
125 assertThat(routeEntry1, is(routeEntry2));
126 }
127
128 /**
129 * Tests non-equality of {@link RouteEntry}.
130 */
131 @Test
132 public void testNonEquality() {
133 IpPrefix prefix1 = IpPrefix.valueOf("1.2.3.0/24");
134 IpAddress nextHop1 = IpAddress.valueOf("5.6.7.8");
135 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
136
137 IpPrefix prefix2 = IpPrefix.valueOf("1.2.3.0/25"); // Different
138 IpAddress nextHop2 = IpAddress.valueOf("5.6.7.8");
139 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
140
141 IpPrefix prefix3 = IpPrefix.valueOf("1.2.3.0/24");
142 IpAddress nextHop3 = IpAddress.valueOf("5.6.7.9"); // Different
143 RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3);
144
145 assertThat(routeEntry1, is(not(routeEntry2)));
146 assertThat(routeEntry1, is(not(routeEntry3)));
147 }
148
149 /**
150 * Tests object string representation.
151 */
152 @Test
153 public void testToString() {
154 IpPrefix prefix = IpPrefix.valueOf("1.2.3.0/24");
155 IpAddress nextHop = IpAddress.valueOf("5.6.7.8");
156 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
157
158 assertThat(routeEntry.toString(),
159 is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
160 }
161}