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 | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 16 | package org.onosproject.routing; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 17 | |
| 18 | import org.hamcrest.Matchers; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.Ip4Prefix; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 22 | |
| 23 | import static org.hamcrest.Matchers.is; |
| 24 | import static org.hamcrest.Matchers.not; |
| 25 | import static org.junit.Assert.assertThat; |
| 26 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Unit tests for the RouteEntry class. |
| 29 | */ |
| 30 | public class RouteEntryTest { |
| 31 | /** |
| 32 | * Tests valid class constructor. |
| 33 | */ |
| 34 | @Test |
| 35 | public void testConstructor() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 36 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 37 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 38 | |
| 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 Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 49 | Ip4Prefix prefix = null; |
| 50 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 51 | |
| 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 Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 60 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 61 | Ip4Address nextHop = null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 62 | |
| 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 Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 71 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 72 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 73 | |
| 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 Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 84 | Ip4Prefix prefix; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 85 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 86 | prefix = Ip4Prefix.valueOf("0.0.0.0/0"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 87 | assertThat(RouteEntry.createBinaryString(prefix), is("")); |
| 88 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 89 | prefix = Ip4Prefix.valueOf("192.168.166.0/22"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 90 | assertThat(RouteEntry.createBinaryString(prefix), |
| 91 | is("1100000010101000101001")); |
| 92 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 93 | prefix = Ip4Prefix.valueOf("192.168.166.0/23"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 94 | assertThat(RouteEntry.createBinaryString(prefix), |
| 95 | is("11000000101010001010011")); |
| 96 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 97 | prefix = Ip4Prefix.valueOf("192.168.166.0/24"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 98 | assertThat(RouteEntry.createBinaryString(prefix), |
| 99 | is("110000001010100010100110")); |
| 100 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 101 | prefix = Ip4Prefix.valueOf("130.162.10.1/25"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 102 | assertThat(RouteEntry.createBinaryString(prefix), |
| 103 | is("1000001010100010000010100")); |
| 104 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 105 | prefix = Ip4Prefix.valueOf("255.255.255.255/32"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 106 | assertThat(RouteEntry.createBinaryString(prefix), |
| 107 | is("11111111111111111111111111111111")); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests equality of {@link RouteEntry}. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testEquality() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 115 | Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 116 | Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 117 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 118 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 119 | Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 120 | Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 121 | 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 Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 131 | Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 132 | Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 133 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 134 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 135 | Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25"); // Different |
| 136 | Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 137 | RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2); |
| 138 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 139 | Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 140 | Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9"); // Different |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 141 | RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3); |
| 142 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 143 | assertThat(routeEntry1, Matchers.is(not(routeEntry2))); |
| 144 | assertThat(routeEntry1, Matchers.is(not(routeEntry3))); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Tests object string representation. |
| 149 | */ |
| 150 | @Test |
| 151 | public void testToString() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 152 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 153 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 154 | 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 | } |