Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip; |
| 17 | |
| 18 | import static org.hamcrest.Matchers.is; |
| 19 | import static org.hamcrest.Matchers.not; |
| 20 | import static org.junit.Assert.assertThat; |
| 21 | |
| 22 | import org.junit.Test; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.packet.Ip4Prefix; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Unit tests for the RouteEntry class. |
| 28 | */ |
| 29 | public class RouteEntryTest { |
| 30 | /** |
| 31 | * Tests valid class constructor. |
| 32 | */ |
| 33 | @Test |
| 34 | public void testConstructor() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 35 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 36 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 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() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 48 | Ip4Prefix prefix = null; |
| 49 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 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() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 59 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 60 | Ip4Address nextHop = null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 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() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 70 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 71 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 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() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 83 | Ip4Prefix prefix; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 84 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 85 | prefix = Ip4Prefix.valueOf("0.0.0.0/0"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 86 | assertThat(RouteEntry.createBinaryString(prefix), is("")); |
| 87 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 88 | prefix = Ip4Prefix.valueOf("192.168.166.0/22"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 89 | assertThat(RouteEntry.createBinaryString(prefix), |
| 90 | is("1100000010101000101001")); |
| 91 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 92 | prefix = Ip4Prefix.valueOf("192.168.166.0/23"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 93 | assertThat(RouteEntry.createBinaryString(prefix), |
| 94 | is("11000000101010001010011")); |
| 95 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 96 | prefix = Ip4Prefix.valueOf("192.168.166.0/24"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 97 | assertThat(RouteEntry.createBinaryString(prefix), |
| 98 | is("110000001010100010100110")); |
| 99 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 100 | prefix = Ip4Prefix.valueOf("130.162.10.1/25"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 101 | assertThat(RouteEntry.createBinaryString(prefix), |
| 102 | is("1000001010100010000010100")); |
| 103 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 104 | prefix = Ip4Prefix.valueOf("255.255.255.255/32"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 105 | assertThat(RouteEntry.createBinaryString(prefix), |
| 106 | is("11111111111111111111111111111111")); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Tests equality of {@link RouteEntry}. |
| 111 | */ |
| 112 | @Test |
| 113 | public void testEquality() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 114 | Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 115 | Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 116 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 117 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 118 | Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 119 | Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 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() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 130 | Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 131 | Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 132 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 133 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 134 | Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25"); // Different |
| 135 | Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 136 | RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2); |
| 137 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 138 | Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 139 | Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9"); // Different |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 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() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 151 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 152 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 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 | } |