summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/utils/misc/src/test/java/org
diff options
context:
space:
mode:
authorAshlee Young <ashlee@onosfw.com>2015-09-09 22:15:21 -0700
committerAshlee Young <ashlee@onosfw.com>2015-09-09 22:15:21 -0700
commit13d05bc8458758ee39cb829098241e89616717ee (patch)
tree22a4d1ce65f15952f07a3df5af4b462b4697cb3a /framework/src/onos/utils/misc/src/test/java/org
parent6139282e1e93c2322076de4b91b1c85d0bc4a8b3 (diff)
ONOS checkin based on commit tag e796610b1f721d02f9b0e213cf6f7790c10ecd60
Change-Id: Ife8810491034fe7becdba75dda20de4267bd15cd
Diffstat (limited to 'framework/src/onos/utils/misc/src/test/java/org')
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AbstractEdgeTest.java37
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AbstractGraphPathSearchTest.java61
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AdjacencyListsGraphTest.java72
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/BellmanFordGraphSearchTest.java77
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/BreadthFirstSearchTest.java100
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DefaultMutablePathTest.java110
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DefaultPathTest.java57
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DepthFirstSearchTest.java97
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DijkstraGraphSearchTest.java165
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/GraphTest.java66
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/HeapTest.java97
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/KshortestPathSearchTest.java197
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/SRLGGraphSearchTest.java183
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/SuurballeGraphSearchTest.java154
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TarjanGraphSearchTest.java125
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TestEdge.java73
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TestVertex.java53
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ArpTest.java88
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/DhcpTest.java137
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/EthernetTest.java103
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMP6Test.java110
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java70
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IGMPTest.java96
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv4Test.java99
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv6Test.java154
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4AddressTest.java432
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4PrefixTest.java534
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6AddressTest.java499
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6PrefixTest.java568
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java946
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java1080
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLCTest.java70
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLDPTest.java115
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/MplsTest.java74
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PacketTestUtils.java98
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/TCPTest.java163
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/UDPTest.java134
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/VlanIdTest.java53
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/AuthenticationTest.java121
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/BaseOptionsTest.java115
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/DestinationOptionsTest.java37
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/EncapSecurityPayloadTest.java104
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/FragmentTest.java113
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/HopByHopOptionsTest.java37
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/RoutingTest.java128
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborAdvertisementTest.java140
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborSolicitationTest.java134
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RedirectTest.java147
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterAdvertisementTest.java140
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterSolicitationTest.java114
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/AbstractAccumulatorTest.java184
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/BandwidthTest.java82
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/BlockingBooleanTest.java210
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/BoundedThreadPoolTest.java227
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/ByteArraySizeHashPrinterTest.java53
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/CounterTest.java86
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/FrequencyTest.java107
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/GroupedThreadFactoryTest.java53
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/HexStringTest.java85
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/PositionalParameterStringFormatterTest.java61
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/RetryingFunctionTest.java94
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/SharedExecutorsTest.java54
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/SlidingWindowCounterTest.java105
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/ToolsTest.java76
64 files changed, 10154 insertions, 0 deletions
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AbstractEdgeTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AbstractEdgeTest.java
new file mode 100644
index 00000000..6e8635dd
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AbstractEdgeTest.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+/**
+ * Test of the base edge implementation.
+ */
+public class AbstractEdgeTest {
+
+ @Test
+ public void equality() {
+ TestVertex v1 = new TestVertex("1");
+ TestVertex v2 = new TestVertex("2");
+ new EqualsTester()
+ .addEqualityGroup(new TestEdge(v1, v2, 1),
+ new TestEdge(v1, v2, 1))
+ .addEqualityGroup(new TestEdge(v2, v1, 1))
+ .testEquals();
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AbstractGraphPathSearchTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AbstractGraphPathSearchTest.java
new file mode 100644
index 00000000..b42cacf6
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AbstractGraphPathSearchTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import org.junit.Test;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Base for all graph search tests.
+ */
+public abstract class AbstractGraphPathSearchTest extends GraphTest {
+
+ /**
+ * Creates a test-specific graph search to exercise.
+ *
+ * @return graph search
+ */
+ protected abstract AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch();
+
+ @Test(expected = IllegalArgumentException.class)
+ public void noSuchSourceArgument() {
+ graphSearch().search(new AdjacencyListsGraph<>(of(B, C),
+ of(new TestEdge(B, C, 1))),
+ A, H, weight, 1);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void nullGraphArgument() {
+ graphSearch().search(null, A, H, weight, 1);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void nullSourceArgument() {
+ graphSearch().search(new AdjacencyListsGraph<>(of(B, C),
+ of(new TestEdge(B, C, 1))),
+ null, H, weight, 1);
+ }
+
+ @Test
+ public void samenessThreshold() {
+ AbstractGraphPathSearch<TestVertex, TestEdge> search = graphSearch();
+ search.setSamenessThreshold(0.3);
+ assertEquals("incorrect threshold", 0.3, search.samenessThreshold(), 0.01);
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AdjacencyListsGraphTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AdjacencyListsGraphTest.java
new file mode 100644
index 00000000..1f22b5c4
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/AdjacencyListsGraphTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests of the graph implementation.
+ */
+public class AdjacencyListsGraphTest {
+
+ private static final TestVertex A = new TestVertex("A");
+ private static final TestVertex B = new TestVertex("B");
+ private static final TestVertex C = new TestVertex("C");
+ private static final TestVertex D = new TestVertex("D");
+ private static final TestVertex E = new TestVertex("E");
+ private static final TestVertex F = new TestVertex("F");
+ private static final TestVertex G = new TestVertex("G");
+
+ private final Set<TestEdge> edges =
+ ImmutableSet.of(new TestEdge(A, B, 1), new TestEdge(B, C, 1),
+ new TestEdge(C, D, 1), new TestEdge(D, A, 1),
+ new TestEdge(B, D, 1));
+
+ @Test
+ public void equality() {
+ Set<TestVertex> vertexes = ImmutableSet.of(A, B, C, D, E, F);
+ Set<TestVertex> vertexes2 = ImmutableSet.of(A, B, C, D, E, F, G);
+
+ AdjacencyListsGraph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(vertexes, edges);
+ AdjacencyListsGraph<TestVertex, TestEdge> same = new AdjacencyListsGraph<>(vertexes, edges);
+ AdjacencyListsGraph<TestVertex, TestEdge> different = new AdjacencyListsGraph<>(vertexes2, edges);
+
+ new EqualsTester()
+ .addEqualityGroup(graph, same)
+ .addEqualityGroup(different)
+ .testEquals();
+ }
+
+ @Test
+ public void basics() {
+ Set<TestVertex> vertexes = ImmutableSet.of(A, B, C, D, E, F);
+ AdjacencyListsGraph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(vertexes, edges);
+ assertEquals("incorrect vertex count", 6, graph.getVertexes().size());
+ assertEquals("incorrect edge count", 5, graph.getEdges().size());
+
+ assertEquals("incorrect egress edge count", 1, graph.getEdgesFrom(A).size());
+ assertEquals("incorrect ingress edge count", 1, graph.getEdgesTo(A).size());
+ assertEquals("incorrect ingress edge count", 1, graph.getEdgesTo(C).size());
+ assertEquals("incorrect egress edge count", 2, graph.getEdgesFrom(B).size());
+ assertEquals("incorrect ingress edge count", 2, graph.getEdgesTo(D).size());
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/BellmanFordGraphSearchTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/BellmanFordGraphSearchTest.java
new file mode 100644
index 00000000..ff363bfb
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/BellmanFordGraphSearchTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import org.junit.Test;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
+
+/**
+ * Test of the Bellman-Ford algorithm.
+ */
+public class BellmanFordGraphSearchTest extends BreadthFirstSearchTest {
+
+ @Override
+ protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
+ return new BellmanFordGraphSearch<>();
+ }
+
+ @Test
+ @Override
+ public void defaultGraphTest() {
+ executeDefaultTest(7, 5, 5.0);
+ }
+
+ @Test
+ public void defaultHopCountWeight() {
+ weight = null;
+ executeDefaultTest(10, 3, 3.0);
+ }
+
+ @Test
+ public void searchGraphWithNegativeCycles() {
+ Set<TestVertex> vertexes = new HashSet<>(vertexes());
+ vertexes.add(Z);
+
+ Set<TestEdge> edges = new HashSet<>(edges());
+ edges.add(new TestEdge(G, Z, 1.0));
+ edges.add(new TestEdge(Z, G, -2.0));
+
+ graph = new AdjacencyListsGraph<>(vertexes, edges);
+
+ GraphPathSearch<TestVertex, TestEdge> search = graphSearch();
+ Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weight, ALL_PATHS).paths();
+ assertEquals("incorrect paths count", 1, paths.size());
+
+ Path p = paths.iterator().next();
+ assertEquals("incorrect src", A, p.src());
+ assertEquals("incorrect dst", H, p.dst());
+ assertEquals("incorrect path length", 5, p.edges().size());
+ assertEquals("incorrect path cost", 5.0, p.cost(), 0.1);
+
+ paths = search.search(graph, A, G, weight, ALL_PATHS).paths();
+ assertEquals("incorrect paths count", 0, paths.size());
+
+ paths = search.search(graph, A, null, weight, ALL_PATHS).paths();
+ printPaths(paths);
+ assertEquals("incorrect paths count", 6, paths.size());
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/BreadthFirstSearchTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/BreadthFirstSearchTest.java
new file mode 100644
index 00000000..0b574aff
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/BreadthFirstSearchTest.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import org.junit.Test;
+
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
+
+/**
+ * Test of the BFS and similar path search algorithms.
+ */
+public class BreadthFirstSearchTest extends AbstractGraphPathSearchTest {
+
+ @Override
+ protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
+ return new BreadthFirstSearch<>();
+ }
+
+ @Test
+ public void defaultGraphTest() {
+ executeDefaultTest(7, 3, 8.0);
+ }
+
+ @Test
+ public void defaultHopCountWeight() {
+ weight = null;
+ executeDefaultTest(7, 3, 3.0);
+ }
+
+ // Executes the default test
+ protected void executeDefaultTest(int pathCount, int pathLength, double pathCost) {
+ graph = new AdjacencyListsGraph<>(vertexes(), edges());
+
+ GraphPathSearch<TestVertex, TestEdge> search = graphSearch();
+ Set<Path<TestVertex, TestEdge>> paths =
+ search.search(graph, A, H, weight, ALL_PATHS).paths();
+ assertEquals("incorrect paths count", 1, paths.size());
+
+ Path p = paths.iterator().next();
+ assertEquals("incorrect src", A, p.src());
+ assertEquals("incorrect dst", H, p.dst());
+ assertEquals("incorrect path length", pathLength, p.edges().size());
+ assertEquals("incorrect path cost", pathCost, p.cost(), 0.1);
+
+ paths = search.search(graph, A, null, weight, ALL_PATHS).paths();
+ printPaths(paths);
+ assertEquals("incorrect paths count", pathCount, paths.size());
+ }
+
+ // Executes the search and validates its results.
+ protected void executeSearch(GraphPathSearch<TestVertex, TestEdge> search,
+ Graph<TestVertex, TestEdge> graph,
+ TestVertex src, TestVertex dst,
+ EdgeWeight<TestVertex, TestEdge> weight,
+ int pathCount, double pathCost) {
+ GraphPathSearch.Result<TestVertex, TestEdge> result =
+ search.search(graph, src, dst, weight, ALL_PATHS);
+ Set<Path<TestVertex, TestEdge>> paths = result.paths();
+ printPaths(paths);
+ assertEquals("incorrect paths count", pathCount, paths.size());
+ if (pathCount > 0) {
+ Path<TestVertex, TestEdge> path = paths.iterator().next();
+ assertEquals("incorrect path cost", pathCost, path.cost(), 0.1);
+ }
+ }
+
+ // Executes the single-path search and validates its results.
+ protected void executeSinglePathSearch(GraphPathSearch<TestVertex, TestEdge> search,
+ Graph<TestVertex, TestEdge> graph,
+ TestVertex src, TestVertex dst,
+ EdgeWeight<TestVertex, TestEdge> weight,
+ int pathCount, double pathCost) {
+ GraphPathSearch.Result<TestVertex, TestEdge> result =
+ search.search(graph, src, dst, weight, 1);
+ Set<Path<TestVertex, TestEdge>> paths = result.paths();
+ printPaths(paths);
+ assertEquals("incorrect paths count", Math.min(pathCount, 1), paths.size());
+ if (pathCount > 0) {
+ Path<TestVertex, TestEdge> path = paths.iterator().next();
+ assertEquals("incorrect path cost", pathCost, path.cost(), 0.1);
+ }
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DefaultMutablePathTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DefaultMutablePathTest.java
new file mode 100644
index 00000000..8eb09df6
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DefaultMutablePathTest.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import static com.google.common.collect.ImmutableList.of;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Test of the default mutable path.
+ */
+public class DefaultMutablePathTest extends DefaultPathTest {
+
+ @Test
+ public void equality() {
+ DefaultPath<TestVertex, TestEdge> p1 =
+ new DefaultPath<>(of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1)), 2.0);
+ DefaultPath<TestVertex, TestEdge> p2 =
+ new DefaultPath<>(of(new TestEdge(A, B, 1),
+ new TestEdge(B, D, 1)), 2.0);
+ new EqualsTester().addEqualityGroup(new DefaultMutablePath<>(p1),
+ new DefaultMutablePath<>(p1))
+ .addEqualityGroup(new DefaultMutablePath<>(p2))
+ .testEquals();
+ }
+
+ @Test
+ public void empty() {
+ MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
+ assertNull("src should be null", p.src());
+ assertNull("dst should be null", p.dst());
+ assertEquals("incorrect edge count", 0, p.edges().size());
+ assertEquals("incorrect path cost", 0.0, p.cost(), 0.1);
+ }
+
+ @Test
+ public void pathCost() {
+ MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
+ p.setCost(4);
+ assertEquals("incorrect path cost", 4.0, p.cost(), 0.1);
+ }
+
+ private void validatePath(Path<TestVertex, TestEdge> p,
+ TestVertex src, TestVertex dst, int length) {
+ validatePath(p, src, dst, length, 0.0);
+ }
+
+ @Test
+ public void insertEdge() {
+ MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
+ p.insertEdge(new TestEdge(B, C, 1));
+ p.insertEdge(new TestEdge(A, B, 1));
+ validatePath(p, A, C, 2);
+ }
+
+ @Test
+ public void appendEdge() {
+ MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
+ p.appendEdge(new TestEdge(A, B, 1));
+ p.appendEdge(new TestEdge(B, C, 1));
+ validatePath(p, A, C, 2);
+ }
+
+ @Test
+ public void removeEdge() {
+ MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
+ p.appendEdge(new TestEdge(A, B, 1));
+ p.appendEdge(new TestEdge(B, C, 1));
+ p.appendEdge(new TestEdge(C, C, 2));
+ p.appendEdge(new TestEdge(C, D, 1));
+ validatePath(p, A, D, 4);
+
+ p.removeEdge(new TestEdge(A, B, 1));
+ validatePath(p, B, D, 3);
+
+ p.removeEdge(new TestEdge(C, C, 2));
+ validatePath(p, B, D, 2);
+
+ p.removeEdge(new TestEdge(C, D, 1));
+ validatePath(p, B, C, 1);
+ }
+
+ @Test
+ public void toImmutable() {
+ MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
+ p.appendEdge(new TestEdge(A, B, 1));
+ p.appendEdge(new TestEdge(B, C, 1));
+ validatePath(p, A, C, 2);
+
+ assertEquals("immutables should equal", p.toImmutable(), p.toImmutable());
+ validatePath(p.toImmutable(), A, C, 2);
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DefaultPathTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DefaultPathTest.java
new file mode 100644
index 00000000..92befbfe
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DefaultPathTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import java.util.List;
+
+import static com.google.common.collect.ImmutableList.of;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test of the default path.
+ */
+public class DefaultPathTest extends GraphTest {
+
+ @Test
+ public void equality() {
+ List<TestEdge> edges = of(new TestEdge(A, B, 1), new TestEdge(B, C, 1));
+ new EqualsTester().addEqualityGroup(new DefaultPath<>(edges, 2.0),
+ new DefaultPath<>(edges, 2.0))
+ .addEqualityGroup(new DefaultPath<>(edges, 3.0))
+ .testEquals();
+ }
+
+ @Test
+ public void basics() {
+ Path<TestVertex, TestEdge> p = new DefaultPath<>(of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1)), 2.0);
+ validatePath(p, A, C, 2, 2.0);
+ }
+
+ // Validates the path against expected attributes
+ protected void validatePath(Path<TestVertex, TestEdge> p,
+ TestVertex src, TestVertex dst,
+ int length, double cost) {
+ assertEquals("incorrect path length", length, p.edges().size());
+ assertEquals("incorrect source", src, p.src());
+ assertEquals("incorrect destination", dst, p.dst());
+ assertEquals("incorrect path cost", cost, p.cost(), 0.1);
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DepthFirstSearchTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DepthFirstSearchTest.java
new file mode 100644
index 00000000..3977ebf1
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DepthFirstSearchTest.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import org.junit.Test;
+
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.graph.DepthFirstSearch.EdgeType;
+import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
+
+/**
+ * Test of the DFS algorithm.
+ */
+public class DepthFirstSearchTest extends AbstractGraphPathSearchTest {
+
+ @Override
+ protected DepthFirstSearch<TestVertex, TestEdge> graphSearch() {
+ return new DepthFirstSearch<>();
+ }
+
+ @Test
+ public void defaultGraphTest() {
+ executeDefaultTest(3, 6, 5.0, 12.0);
+ executeBroadSearch();
+ }
+
+ @Test
+ public void defaultHopCountWeight() {
+ weight = null;
+ executeDefaultTest(3, 6, 3.0, 6.0);
+ executeBroadSearch();
+ }
+
+ protected void executeDefaultTest(int minLength, int maxLength,
+ double minCost, double maxCost) {
+ graph = new AdjacencyListsGraph<>(vertexes(), edges());
+ DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
+
+ DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
+ search.search(graph, A, H, weight, 1);
+ Set<Path<TestVertex, TestEdge>> paths = result.paths();
+ assertEquals("incorrect path count", 1, paths.size());
+
+ Path path = paths.iterator().next();
+ System.out.println(path);
+ assertEquals("incorrect src", A, path.src());
+ assertEquals("incorrect dst", H, path.dst());
+
+ int l = path.edges().size();
+ assertTrue("incorrect path length " + l,
+ minLength <= l && l <= maxLength);
+ assertTrue("incorrect path cost " + path.cost(),
+ minCost <= path.cost() && path.cost() <= maxCost);
+
+ System.out.println(result.edges());
+ printPaths(paths);
+ }
+
+ public void executeBroadSearch() {
+ graph = new AdjacencyListsGraph<>(vertexes(), edges());
+ DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
+
+ // Perform narrow path search to a specific destination.
+ DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
+ search.search(graph, A, null, weight, ALL_PATHS);
+ assertEquals("incorrect paths count", 7, result.paths().size());
+
+ int[] types = new int[]{0, 0, 0, 0};
+ for (EdgeType t : result.edges().values()) {
+ types[t.ordinal()] += 1;
+ }
+ assertEquals("incorrect tree-edge count", 7,
+ types[EdgeType.TREE_EDGE.ordinal()]);
+ assertEquals("incorrect back-edge count", 1,
+ types[EdgeType.BACK_EDGE.ordinal()]);
+ assertEquals("incorrect cross-edge & forward-edge count", 4,
+ types[EdgeType.FORWARD_EDGE.ordinal()] +
+ types[EdgeType.CROSS_EDGE.ordinal()]);
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DijkstraGraphSearchTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DijkstraGraphSearchTest.java
new file mode 100644
index 00000000..17f08225
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/DijkstraGraphSearchTest.java
@@ -0,0 +1,165 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import org.junit.Test;
+
+import java.text.DecimalFormat;
+import java.util.HashSet;
+import java.util.Set;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test of the Dijkstra algorithm.
+ */
+public class DijkstraGraphSearchTest extends BreadthFirstSearchTest {
+
+ @Override
+ protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
+ return new DijkstraGraphSearch<>();
+ }
+
+ @Test
+ @Override
+ public void defaultGraphTest() {
+ executeDefaultTest(7, 5, 5.0);
+ }
+
+ @Test
+ @Override
+ public void defaultHopCountWeight() {
+ weight = null;
+ executeDefaultTest(10, 3, 3.0);
+ }
+
+ @Test
+ public void noPath() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, A, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, C, 1)));
+ GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
+ Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weight, 1).paths();
+ printPaths(paths);
+ assertEquals("incorrect paths count", 1, paths.size());
+ assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
+
+ paths = gs.search(graph, A, D, weight, 1).paths();
+ printPaths(paths);
+ assertEquals("incorrect paths count", 0, paths.size());
+
+ paths = gs.search(graph, A, null, weight, 1).paths();
+ printPaths(paths);
+ assertEquals("incorrect paths count", 1, paths.size());
+ assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
+ }
+
+ @Test
+ public void simpleMultiplePath() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(A, C, 1),
+ new TestEdge(B, D, 1),
+ new TestEdge(C, D, 1)));
+ executeSearch(graphSearch(), graph, A, D, weight, 2, 2.0);
+ executeSinglePathSearch(graphSearch(), graph, A, D, weight, 1, 2.0);
+ }
+
+ @Test
+ public void denseMultiplePath() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(A, C, 1),
+ new TestEdge(B, D, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, E, 1),
+ new TestEdge(D, F, 1),
+ new TestEdge(E, G, 1),
+ new TestEdge(F, G, 1),
+ new TestEdge(A, G, 4)));
+ executeSearch(graphSearch(), graph, A, G, weight, 5, 4.0);
+ executeSinglePathSearch(graphSearch(), graph, A, G, weight, 1, 4.0);
+ }
+
+ @Test
+ public void dualEdgeMultiplePath() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H),
+ of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
+ new TestEdge(B, D, 2), new TestEdge(B, C, 1),
+ new TestEdge(B, E, 4), new TestEdge(C, E, 1),
+ new TestEdge(D, H, 5), new TestEdge(D, E, 1),
+ new TestEdge(E, F, 1), new TestEdge(F, D, 1),
+ new TestEdge(F, G, 1), new TestEdge(F, H, 1),
+ new TestEdge(A, E, 3), new TestEdge(B, D, 1)));
+ executeSearch(graphSearch(), graph, A, E, weight, 3, 3.0);
+ executeSinglePathSearch(graphSearch(), graph, A, E, weight, 1, 3.0);
+ }
+
+ @Test
+ public void negativeWeights() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(A, C, -1),
+ new TestEdge(B, D, 1),
+ new TestEdge(D, A, -2),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, E, 1),
+ new TestEdge(D, F, 1),
+ new TestEdge(E, G, 1),
+ new TestEdge(F, G, 1),
+ new TestEdge(G, A, -5),
+ new TestEdge(A, G, 4)));
+ executeSearch(graphSearch(), graph, A, G, weight, 3, 4.0);
+ executeSinglePathSearch(graphSearch(), graph, A, G, weight, 1, 4.0);
+ }
+
+ @Test
+ public void disconnectedPerf() {
+ disconnected();
+ disconnected();
+ disconnected();
+ disconnected();
+ disconnected();
+ disconnected();
+ disconnected();
+ disconnected();
+ disconnected();
+ disconnected();
+ }
+
+
+ @Test
+ public void disconnected() {
+ Set<TestVertex> vertexes = new HashSet<>();
+ for (int i = 0; i < 200; i++) {
+ vertexes.add(new TestVertex("v" + i));
+ }
+
+ graph = new AdjacencyListsGraph<>(vertexes, of());
+
+ long start = System.nanoTime();
+ for (TestVertex src : vertexes) {
+ executeSearch(graphSearch(), graph, src, null, null, 0, 0);
+ }
+ long end = System.nanoTime();
+ DecimalFormat fmt = new DecimalFormat("#,###");
+ System.out.println("Compute cost is " + fmt.format(end - start) + " nanos");
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/GraphTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/GraphTest.java
new file mode 100644
index 00000000..d29282fc
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/GraphTest.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import java.util.Set;
+
+import static com.google.common.collect.ImmutableSet.of;
+
+/**
+ * Base class for various graph-related tests.
+ */
+public class GraphTest {
+
+ static final TestVertex A = new TestVertex("A");
+ static final TestVertex B = new TestVertex("B");
+ static final TestVertex C = new TestVertex("C");
+ static final TestVertex D = new TestVertex("D");
+ static final TestVertex E = new TestVertex("E");
+ static final TestVertex F = new TestVertex("F");
+ static final TestVertex G = new TestVertex("G");
+ static final TestVertex H = new TestVertex("H");
+ static final TestVertex Z = new TestVertex("Z");
+
+ protected Graph<TestVertex, TestEdge> graph;
+
+ protected EdgeWeight<TestVertex, TestEdge> weight =
+ new EdgeWeight<TestVertex, TestEdge>() {
+ @Override
+ public double weight(TestEdge edge) {
+ return edge.weight();
+ }
+ };
+
+ protected void printPaths(Set<Path<TestVertex, TestEdge>> paths) {
+ for (Path p : paths) {
+ System.out.println(p);
+ }
+ }
+
+ protected Set<TestVertex> vertexes() {
+ return of(A, B, C, D, E, F, G, H);
+ }
+
+ protected Set<TestEdge> edges() {
+ return of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
+ new TestEdge(B, D, 2), new TestEdge(B, C, 1),
+ new TestEdge(B, E, 4), new TestEdge(C, E, 1),
+ new TestEdge(D, H, 5), new TestEdge(D, E, 1),
+ new TestEdge(E, F, 1), new TestEdge(F, D, 1),
+ new TestEdge(F, G, 1), new TestEdge(F, H, 1));
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/HeapTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/HeapTest.java
new file mode 100644
index 00000000..f34185e2
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/HeapTest.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import com.google.common.collect.Ordering;
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+
+import static com.google.common.collect.ImmutableList.of;
+import static org.junit.Assert.*;
+
+/**
+ * Heap data structure tests.
+ */
+public class HeapTest {
+
+ private ArrayList<Integer> data =
+ new ArrayList<>(of(6, 4, 5, 9, 8, 3, 2, 1, 7, 0));
+
+ private static final Comparator<Integer> MIN = Ordering.natural().reverse();
+ private static final Comparator<Integer> MAX = Ordering.natural();
+
+ @Test
+ public void equality() {
+ new EqualsTester()
+ .addEqualityGroup(new Heap<>(data, MIN),
+ new Heap<>(data, MIN))
+ .addEqualityGroup(new Heap<>(data, MAX))
+ .testEquals();
+ }
+
+ @Test
+ public void empty() {
+ Heap<Integer> h = new Heap<>(new ArrayList<Integer>(), MIN);
+ assertTrue("should be empty", h.isEmpty());
+ assertEquals("incorrect size", 0, h.size());
+ assertNull("no item expected", h.extreme());
+ assertNull("no item expected", h.extractExtreme());
+ }
+
+ @Test
+ public void insert() {
+ Heap<Integer> h = new Heap<>(data, MIN);
+ assertEquals("incorrect size", 10, h.size());
+ h.insert(3);
+ assertEquals("incorrect size", 11, h.size());
+ }
+
+ @Test
+ public void minQueue() {
+ Heap<Integer> h = new Heap<>(data, MIN);
+ assertFalse("should not be empty", h.isEmpty());
+ assertEquals("incorrect size", 10, h.size());
+ assertEquals("incorrect extreme", (Integer) 0, h.extreme());
+
+ for (int i = 0, n = h.size(); i < n; i++) {
+ assertEquals("incorrect element", (Integer) i, h.extractExtreme());
+ }
+ assertTrue("should be empty", h.isEmpty());
+ }
+
+ @Test
+ public void maxQueue() {
+ Heap<Integer> h = new Heap<>(data, MAX);
+ assertFalse("should not be empty", h.isEmpty());
+ assertEquals("incorrect size", 10, h.size());
+ assertEquals("incorrect extreme", (Integer) 9, h.extreme());
+
+ for (int i = h.size(); i > 0; i--) {
+ assertEquals("incorrect element", (Integer) (i - 1), h.extractExtreme());
+ }
+ assertTrue("should be empty", h.isEmpty());
+ }
+
+ @Test
+ public void iterator() {
+ Heap<Integer> h = new Heap<>(data, MIN);
+ assertTrue("should have next element", h.iterator().hasNext());
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/KshortestPathSearchTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/KshortestPathSearchTest.java
new file mode 100644
index 00000000..3e8900b8
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/KshortestPathSearchTest.java
@@ -0,0 +1,197 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayOutputStream;
+//import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class KshortestPathSearchTest extends BreadthFirstSearchTest {
+
+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
+
+ @Test
+ public void noPath() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, A, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, C, 1)));
+ KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
+ List<List<TestEdge>> result = gs.search(A, D, weight, 1);
+ List<Path> paths = new ArrayList<>();
+ Iterator<List<TestEdge>> itr = result.iterator();
+ while (itr.hasNext()) {
+ System.out.println(itr.next().toString());
+ }
+ assertEquals("incorrect paths count", 0, result.size());
+ }
+
+ @Test
+ public void test2Path() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, A, 1),
+ new TestEdge(B, D, 1),
+ new TestEdge(D, B, 1),
+ new TestEdge(A, C, 1),
+ new TestEdge(C, A, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, C, 1)));
+ KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
+ List<List<TestEdge>> result = gs.search(A, D, weight, 2);
+ List<Path> paths = new ArrayList<>();
+ Iterator<List<TestEdge>> itr = result.iterator();
+ while (itr.hasNext()) {
+ System.out.println(itr.next().toString());
+ }
+ assertEquals("incorrect paths count", 2, result.size());
+ // assertEquals("printing the paths", outContent.toString());
+ }
+
+ @Test
+ public void test3Path() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, A, 1),
+ new TestEdge(A, D, 1),
+ new TestEdge(D, A, 1),
+ new TestEdge(B, D, 1),
+ new TestEdge(D, B, 1),
+ new TestEdge(A, C, 1),
+ new TestEdge(C, A, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, C, 1)));
+ KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
+ List<List<TestEdge>> result = gs.search(A, D, weight, 3);
+ List<Path> paths = new ArrayList<>();
+ Iterator<List<TestEdge>> itr = result.iterator();
+ while (itr.hasNext()) {
+ System.out.println(itr.next().toString());
+ }
+ assertEquals("incorrect paths count", 3, result.size());
+ // assertEquals("printing the paths", outContent.toString());
+ }
+
+ @Test
+ public void test4Path() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, A, 1),
+ new TestEdge(A, C, 1),
+ new TestEdge(C, A, 1),
+ new TestEdge(B, D, 1),
+ new TestEdge(D, B, 1),
+ new TestEdge(C, E, 1),
+ new TestEdge(E, C, 1),
+ new TestEdge(D, F, 1),
+ new TestEdge(F, D, 1),
+ new TestEdge(F, E, 1),
+ new TestEdge(E, F, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, C, 1)));
+ KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
+ List<List<TestEdge>> result = gs.search(A, F, weight, 4);
+ List<Path> paths = new ArrayList<>();
+ Iterator<List<TestEdge>> itr = result.iterator();
+ while (itr.hasNext()) {
+ System.out.println(itr.next().toString());
+ }
+ assertEquals("incorrect paths count", 4, result.size());
+ // assertEquals("printing the paths", outContent.toString());
+ }
+
+ @Test
+ public void test6Path() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, A, 1),
+ new TestEdge(A, C, 1),
+ new TestEdge(C, A, 1),
+ new TestEdge(B, D, 1),
+ new TestEdge(D, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(C, B, 1),
+ new TestEdge(D, E, 1),
+ new TestEdge(E, D, 1),
+ new TestEdge(C, E, 1),
+ new TestEdge(E, C, 1),
+ new TestEdge(D, F, 1),
+ new TestEdge(F, D, 1),
+ new TestEdge(E, F, 1),
+ new TestEdge(F, E, 1)));
+ KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
+ List<List<TestEdge>> result = gs.search(A, F, weight, 6);
+ List<Path> paths = new ArrayList<>();
+ Iterator<List<TestEdge>> itr = result.iterator();
+ while (itr.hasNext()) {
+ System.out.println(itr.next().toString());
+ }
+ assertEquals("incorrect paths count", 6, result.size());
+ // assertEquals("printing the paths", outContent.toString());
+ }
+
+ @Test
+ public void dualEdgePath() {
+ graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H),
+ of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
+ new TestEdge(B, D, 2), new TestEdge(B, C, 1),
+ new TestEdge(B, E, 4), new TestEdge(C, E, 1),
+ new TestEdge(D, H, 5), new TestEdge(D, E, 1),
+ new TestEdge(E, F, 1), new TestEdge(F, D, 1),
+ new TestEdge(F, G, 1), new TestEdge(F, H, 1),
+ new TestEdge(A, E, 3), new TestEdge(B, D, 1)));
+ KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
+ List<List<TestEdge>> result = gs.search(A, G, weight, 6);
+ List<Path> paths = new ArrayList<>();
+ Iterator<List<TestEdge>> itr = result.iterator();
+ while (itr.hasNext()) {
+ System.out.println(itr.next().toString());
+ }
+ assertEquals("incorrect paths count", 6, result.size());
+ // assertEquals("printing the paths", outContent.toString());
+ }
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ // System.setOut(new PrintStream(outContent));
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ // System.setOut(null);
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/SRLGGraphSearchTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/SRLGGraphSearchTest.java
new file mode 100644
index 00000000..885fbe5c
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/SRLGGraphSearchTest.java
@@ -0,0 +1,183 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.graph;
+
+import org.junit.Test;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.HashMap;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static org.junit.Assert.assertTrue;
+
+
+
+/**
+ * Test of the Suurballe backup path algorithm.
+ */
+public class SRLGGraphSearchTest extends BreadthFirstSearchTest {
+ @Override
+ protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
+ return new SRLGGraphSearch<TestVertex, TestEdge>(null);
+ }
+
+ public void setWeights() {
+ weight = new EdgeWeight<TestVertex, TestEdge>() {
+ @Override
+ public double weight(TestEdge edge) {
+ return edge.weight();
+ }
+ };
+ }
+ public void setDefaultWeights() {
+ weight = null;
+ }
+ @Override
+ public void defaultGraphTest() {
+
+ }
+
+ @Override
+ public void defaultHopCountWeight() {
+
+ }
+
+ @Test
+ public void onePathPair() {
+ setDefaultWeights();
+ TestEdge aB = new TestEdge(A, B, 1);
+ TestEdge bC = new TestEdge(B, C, 1);
+ TestEdge aD = new TestEdge(A, D, 1);
+ TestEdge dC = new TestEdge(D, C, 1);
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(aB, bC, aD, dC));
+ Map<TestEdge, Integer> riskProfile = new HashMap<TestEdge, Integer>();
+ riskProfile.put(aB, 0);
+ riskProfile.put(bC, 0);
+ riskProfile.put(aD, 1);
+ riskProfile.put(dC, 1);
+ SRLGGraphSearch<TestVertex, TestEdge> search =
+ new SRLGGraphSearch<TestVertex, TestEdge>(2, riskProfile);
+ Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, C, weight, GraphPathSearch.ALL_PATHS).paths();
+ System.out.println("\n\n\n" + paths + "\n\n\n");
+ assertTrue("one disjoint path pair found", paths.size() == 1);
+ checkIsDisjoint(paths.iterator().next(), riskProfile);
+ }
+ public void checkIsDisjoint(Path<TestVertex, TestEdge> p, Map<TestEdge, Integer> risks) {
+ assertTrue("The path is not a DisjointPathPair", (p instanceof DisjointPathPair));
+ DisjointPathPair<TestVertex, TestEdge> q = (DisjointPathPair) p;
+ Set<Integer> p1Risks = new HashSet<Integer>();
+ Set<Integer> p2Risks = new HashSet<Integer>();
+ for (TestEdge e: q.edges()) {
+ p1Risks.add(risks.get(e));
+ }
+ if (!q.hasBackup()) {
+ return;
+ }
+ Path<TestVertex, TestEdge> pq = q.path2;
+ for (TestEdge e: pq.edges()) {
+ assertTrue("The paths are not disjoint", !p1Risks.contains(risks.get(e)));
+ }
+ }
+ @Test
+ public void complexGraphTest() {
+ setDefaultWeights();
+ TestEdge aB = new TestEdge(A, B, 1);
+ TestEdge bC = new TestEdge(B, C, 1);
+ TestEdge aD = new TestEdge(A, D, 1);
+ TestEdge dC = new TestEdge(D, C, 1);
+ TestEdge cE = new TestEdge(C, E, 1);
+ TestEdge bE = new TestEdge(B, E, 1);
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
+ of(aB, bC, aD, dC, cE, bE));
+ Map<TestEdge, Integer> riskProfile = new HashMap<TestEdge, Integer>();
+ riskProfile.put(aB, 0);
+ riskProfile.put(bC, 0);
+ riskProfile.put(aD, 1);
+ riskProfile.put(dC, 1);
+ riskProfile.put(cE, 2);
+ riskProfile.put(bE, 3);
+ SRLGGraphSearch<TestVertex, TestEdge> search =
+ new SRLGGraphSearch<TestVertex, TestEdge>(4, riskProfile);
+ Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, E, weight, GraphPathSearch.ALL_PATHS).paths();
+ }
+
+ @Test
+ public void multiplePathGraphTest() {
+ setDefaultWeights();
+ TestEdge aB = new TestEdge(A, B, 1);
+ TestEdge bE = new TestEdge(B, E, 1);
+ TestEdge aD = new TestEdge(A, D, 1);
+ TestEdge dE = new TestEdge(D, E, 1);
+ TestEdge aC = new TestEdge(A, C, 1);
+ TestEdge cE = new TestEdge(C, E, 1);
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
+ of(aB, bE, aD, dE, aC, cE));
+ Map<TestEdge, Integer> riskProfile = new HashMap<TestEdge, Integer>();
+ riskProfile.put(aB, 0);
+ riskProfile.put(bE, 1);
+ riskProfile.put(aD, 2);
+ riskProfile.put(dE, 3);
+ riskProfile.put(aC, 4);
+ riskProfile.put(cE, 5);
+ SRLGGraphSearch<TestVertex, TestEdge> search =
+ new SRLGGraphSearch<TestVertex, TestEdge>(6, riskProfile);
+ Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, E, weight, GraphPathSearch.ALL_PATHS).paths();
+ assertTrue("> one disjoint path pair found", paths.size() >= 1);
+ checkIsDisjoint(paths.iterator().next(), riskProfile);
+ }
+ @Test
+ public void onePath() {
+ setDefaultWeights();
+ TestEdge aB = new TestEdge(A, B, 1);
+ TestEdge bC = new TestEdge(B, C, 1);
+ TestEdge aD = new TestEdge(A, D, 1);
+ TestEdge dC = new TestEdge(D, C, 1);
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(aB, bC, aD, dC));
+ Map<TestEdge, Integer> riskProfile = new HashMap<TestEdge, Integer>();
+ riskProfile.put(aB, 0);
+ riskProfile.put(bC, 0);
+ riskProfile.put(aD, 1);
+ riskProfile.put(dC, 0);
+ SRLGGraphSearch<TestVertex, TestEdge> search =
+ new SRLGGraphSearch<TestVertex, TestEdge>(2, riskProfile);
+ Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, C, weight, GraphPathSearch.ALL_PATHS).paths();
+ System.out.println(paths);
+ assertTrue("no disjoint path pairs found", paths.size() == 0);
+ }
+ @Test
+ public void noPath() {
+ setDefaultWeights();
+ TestEdge aB = new TestEdge(A, B, 1);
+ TestEdge bC = new TestEdge(B, C, 1);
+ TestEdge aD = new TestEdge(A, D, 1);
+ TestEdge dC = new TestEdge(D, C, 1);
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
+ of(aB, bC, aD, dC));
+ Map<TestEdge, Integer> riskProfile = new HashMap<>();
+ riskProfile.put(aB, 0);
+ riskProfile.put(bC, 0);
+ riskProfile.put(aD, 1);
+ riskProfile.put(dC, 0);
+ SRLGGraphSearch<TestVertex, TestEdge> search =
+ new SRLGGraphSearch<>(2, riskProfile);
+ Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, E, weight, GraphPathSearch.ALL_PATHS).paths();
+ assertTrue("no disjoint path pairs found", paths.size() == 0);
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/SuurballeGraphSearchTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/SuurballeGraphSearchTest.java
new file mode 100644
index 00000000..0d2d13b0
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/SuurballeGraphSearchTest.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import org.junit.Test;
+import java.util.Set;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static org.junit.Assert.assertEquals;
+//import static org.junit.Assert.assertTrue;
+
+
+
+/**
+ * Test of the Suurballe backup path algorithm.
+ */
+public class SuurballeGraphSearchTest extends BreadthFirstSearchTest {
+
+ @Override
+ protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
+ return new SuurballeGraphSearch<>();
+ }
+
+ public void setWeights() {
+ weight = new EdgeWeight<TestVertex, TestEdge>() {
+ @Override
+ public double weight(TestEdge edge) {
+ return edge.weight();
+ }
+ };
+ }
+ public void setDefaultWeights() {
+ weight = null;
+ }
+ @Override
+ public void defaultGraphTest() {
+
+ }
+
+ @Override
+ public void defaultHopCountWeight() {
+
+ }
+
+ @Test
+ public void basicGraphTest() {
+ setDefaultWeights();
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(A, D, 1),
+ new TestEdge(D, C, 1)));
+ executeSearch(graphSearch(), graph, A, C, weight, 1, 4.0);
+ }
+
+ @Test
+ public void multiplePathOnePairGraphTest() {
+ setWeights();
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(A, D, 1),
+ new TestEdge(D, C, 1),
+ new TestEdge(B, E, 2),
+ new TestEdge(C, E, 1)));
+ executeSearch(graphSearch(), graph, A, E, weight, 1, 6.0);
+ }
+
+ @Test
+ public void multiplePathsMultiplePairs() {
+ setWeights();
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, E, 1),
+ new TestEdge(A, C, 1),
+ new TestEdge(C, E, 1),
+ new TestEdge(A, D, 1),
+ new TestEdge(D, E, 1),
+ new TestEdge(A, E, 2)));
+ GraphPathSearch.Result<TestVertex, TestEdge> result =
+ graphSearch().search(graph, A, E, weight, GraphPathSearch.ALL_PATHS);
+ Set<Path<TestVertex, TestEdge>> paths = result.paths();
+ System.out.println("\n\n" + paths + "\n\n\ndone\n");
+ assertEquals("incorrect paths count", 3, paths.size());
+ DisjointPathPair<TestVertex, TestEdge> dpp = (DisjointPathPair<TestVertex, TestEdge>) paths.iterator().next();
+ assertEquals("incorrect disjoint paths per path", 2, dpp.size());
+ }
+
+ @Test
+ public void differingPrimaryAndBackupPathLengths() {
+ setWeights();
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(A, D, 1),
+ new TestEdge(D, C, 1),
+ new TestEdge(B, E, 1),
+ new TestEdge(C, E, 1)));
+ executeSearch(graphSearch(), graph, A, E, weight, 1, 5.0);
+ }
+
+ @Test
+ public void onePath() {
+ setWeights();
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(A, C, 4),
+ new TestEdge(C, D, 1)));
+ GraphPathSearch.Result<TestVertex, TestEdge> result =
+ graphSearch().search(graph, A, D, weight, GraphPathSearch.ALL_PATHS);
+ Set<Path<TestVertex, TestEdge>> paths = result.paths();
+ assertEquals("incorrect paths count", 1, paths.size());
+ DisjointPathPair<TestVertex, TestEdge> dpp = (DisjointPathPair<TestVertex, TestEdge>) paths.iterator().next();
+ assertEquals("incorrect disjoint paths count", 1, dpp.size());
+ }
+
+ @Test
+ public void noPath() {
+ setWeights();
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(A, C, 4)));
+ GraphPathSearch.Result<TestVertex, TestEdge> result =
+ graphSearch().search(graph, A, D, weight, GraphPathSearch.ALL_PATHS);
+ Set<Path<TestVertex, TestEdge>> paths = result.paths();
+ assertEquals("incorrect paths count", paths.size(), 0);
+ }
+
+ @Test
+ public void disconnected() {
+ setWeights();
+ Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
+ of());
+ GraphPathSearch.Result<TestVertex, TestEdge> result =
+ graphSearch().search(graph, A, D, weight, GraphPathSearch.ALL_PATHS);
+ Set<Path<TestVertex, TestEdge>> paths = result.paths();
+ assertEquals("incorrect paths count", 0, paths.size());
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TarjanGraphSearchTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TarjanGraphSearchTest.java
new file mode 100644
index 00000000..624c5781
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TarjanGraphSearchTest.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import org.junit.Test;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static org.junit.Assert.assertEquals;
+import static org.onlab.graph.TarjanGraphSearch.SCCResult;
+
+/**
+ * Tarjan graph search tests.
+ */
+public class TarjanGraphSearchTest extends GraphTest {
+
+ private void validate(SCCResult<TestVertex, TestEdge> result, int cc) {
+ System.out.println("Cluster count: " + result.clusterVertexes().size());
+ System.out.println("Clusters: " + result.clusterVertexes());
+ assertEquals("incorrect cluster count", cc, result.clusterCount());
+ }
+
+ private void validate(SCCResult<TestVertex, TestEdge> result,
+ int i, int vc, int ec) {
+ assertEquals("incorrect cluster count", vc, result.clusterVertexes().get(i).size());
+ assertEquals("incorrect edge count", ec, result.clusterEdges().get(i).size());
+ }
+
+ @Test
+ public void basic() {
+ graph = new AdjacencyListsGraph<>(vertexes(), edges());
+ TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
+ SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
+ validate(result, 6);
+ }
+
+ @Test
+ public void singleCluster() {
+ graph = new AdjacencyListsGraph<>(vertexes(),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, E, 1),
+ new TestEdge(E, F, 1),
+ new TestEdge(F, G, 1),
+ new TestEdge(G, H, 1),
+ new TestEdge(H, A, 1)));
+
+ TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
+ SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
+ validate(result, 1);
+ validate(result, 0, 8, 8);
+ }
+
+ @Test
+ public void twoUnconnectedCluster() {
+ graph = new AdjacencyListsGraph<>(vertexes(),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, A, 1),
+ new TestEdge(E, F, 1),
+ new TestEdge(F, G, 1),
+ new TestEdge(G, H, 1),
+ new TestEdge(H, E, 1)));
+ TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
+ SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
+ validate(result, 2);
+ validate(result, 0, 4, 4);
+ validate(result, 1, 4, 4);
+ }
+
+ @Test
+ public void twoWeaklyConnectedClusters() {
+ graph = new AdjacencyListsGraph<>(vertexes(),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, A, 1),
+ new TestEdge(E, F, 1),
+ new TestEdge(F, G, 1),
+ new TestEdge(G, H, 1),
+ new TestEdge(H, E, 1),
+ new TestEdge(B, E, 1)));
+ TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
+ SCCResult<TestVertex, TestEdge> result = gs.search(graph, null);
+ validate(result, 2);
+ validate(result, 0, 4, 4);
+ validate(result, 1, 4, 4);
+ }
+
+ @Test
+ public void twoClustersConnectedWithIgnoredEdges() {
+ graph = new AdjacencyListsGraph<>(vertexes(),
+ of(new TestEdge(A, B, 1),
+ new TestEdge(B, C, 1),
+ new TestEdge(C, D, 1),
+ new TestEdge(D, A, 1),
+ new TestEdge(E, F, 1),
+ new TestEdge(F, G, 1),
+ new TestEdge(G, H, 1),
+ new TestEdge(H, E, 1),
+ new TestEdge(B, E, -1),
+ new TestEdge(E, B, -1)));
+
+ TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
+ SCCResult<TestVertex, TestEdge> result = gs.search(graph, weight);
+ validate(result, 2);
+ validate(result, 0, 4, 4);
+ validate(result, 1, 4, 4);
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TestEdge.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TestEdge.java
new file mode 100644
index 00000000..951f655b
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TestEdge.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Test edge.
+ */
+public class TestEdge extends AbstractEdge<TestVertex> {
+
+ private final double weight;
+
+ /**
+ * Creates a new edge between the specified source and destination vertexes.
+ *
+ * @param src source vertex
+ * @param dst destination vertex
+ * @param weight edge weight
+ */
+ public TestEdge(TestVertex src, TestVertex dst, double weight) {
+ super(src, dst);
+ this.weight = weight;
+ }
+
+ /**
+ * Returns the edge weight.
+ *
+ * @return edge weight
+ */
+ public double weight() {
+ return weight;
+ }
+
+ @Override
+ public int hashCode() {
+ return 31 * super.hashCode() + Objects.hash(weight);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof TestEdge) {
+ final TestEdge other = (TestEdge) obj;
+ return super.equals(obj) && Objects.equals(this.weight, other.weight);
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this).add("src", src()).add("dst", dst()).
+ add("weight", weight).toString();
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TestVertex.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TestVertex.java
new file mode 100644
index 00000000..be92bd27
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/graph/TestVertex.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.graph;
+
+import java.util.Objects;
+
+/**
+ * Test vertex.
+ */
+public class TestVertex implements Vertex {
+
+ private final String name;
+
+ public TestVertex(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof TestVertex) {
+ final TestVertex other = (TestVertex) obj;
+ return Objects.equals(this.name, other.name);
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ArpTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ArpTest.java
new file mode 100644
index 00000000..62246245
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ArpTest.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit tests for the ARP class.
+ */
+public class ArpTest {
+
+ private Deserializer<ARP> deserializer = ARP.deserializer();
+
+ private final byte hwAddressLength = 6;
+ private final byte protoAddressLength = 4;
+
+ private MacAddress srcMac = MacAddress.valueOf(1);
+ private MacAddress targetMac = MacAddress.valueOf(2);
+ private Ip4Address srcIp = Ip4Address.valueOf(1);
+ private Ip4Address targetIp = Ip4Address.valueOf(2);
+
+ private byte[] byteHeader;
+
+ @Before
+ public void setUp() {
+ ByteBuffer bb = ByteBuffer.allocate(ARP.INITIAL_HEADER_LENGTH +
+ 2 * hwAddressLength + 2 * protoAddressLength);
+ bb.putShort(ARP.HW_TYPE_ETHERNET);
+ bb.putShort(ARP.PROTO_TYPE_IP);
+ bb.put(hwAddressLength);
+ bb.put(protoAddressLength);
+ bb.putShort(ARP.OP_REPLY);
+
+ bb.put(srcMac.toBytes());
+ bb.put(srcIp.toOctets());
+ bb.put(targetMac.toBytes());
+ bb.put(targetIp.toOctets());
+
+ byteHeader = bb.array();
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader);
+ }
+
+ @Test
+ public void testDeserialize() throws Exception {
+ ARP arp = deserializer.deserialize(byteHeader, 0, byteHeader.length);
+
+ assertEquals(ARP.HW_TYPE_ETHERNET, arp.getHardwareType());
+ assertEquals(ARP.PROTO_TYPE_IP, arp.getProtocolType());
+ assertEquals(hwAddressLength, arp.getHardwareAddressLength());
+ assertEquals(protoAddressLength, arp.getProtocolAddressLength());
+ assertEquals(ARP.OP_REPLY, arp.getOpCode());
+
+ assertTrue(Arrays.equals(srcMac.toBytes(), arp.getSenderHardwareAddress()));
+ assertTrue(Arrays.equals(srcIp.toOctets(), arp.getSenderProtocolAddress()));
+ assertTrue(Arrays.equals(targetMac.toBytes(), arp.getTargetHardwareAddress()));
+ assertTrue(Arrays.equals(targetIp.toOctets(), arp.getTargetProtocolAddress()));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/DhcpTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/DhcpTest.java
new file mode 100644
index 00000000..aac81412
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/DhcpTest.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import com.google.common.base.Charsets;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit tests for DHCP class.
+ */
+public class DhcpTest {
+
+ private Deserializer<DHCP> deserializer = DHCP.deserializer();
+
+ private byte opCode = 1;
+ private byte hardwareType = 1;
+ private byte hardwareAddressLength = Ethernet.DATALAYER_ADDRESS_LENGTH;
+ private byte hops = 0;
+ private int transactionId = 0x2ed4eb50;
+ private short seconds = 0;
+ private short flags = 0;
+ private int clientIpAddress = 1;
+ private int yourIpAddress = 2;
+ private int serverIpAddress = 3;
+ private int gatewayIpAddress = 4;
+ private byte[] clientHardwareAddress = MacAddress.valueOf(500).toBytes();
+ private String serverName = "test-server";
+ private String bootFileName = "test-file";
+
+ private String hostName = "test-host";
+ private DHCPOption hostNameOption = new DHCPOption();
+
+ private byte[] byteHeader;
+
+ @Before
+ public void setUp() {
+ hostNameOption.setCode((byte) 55);
+ hostNameOption.setLength((byte) hostName.length());
+ hostNameOption.setData(hostName.getBytes(Charsets.US_ASCII));
+
+ // Packet length is the fixed DHCP header plus option length plus an
+ // extra byte to indicate 'end of options'.
+ ByteBuffer bb = ByteBuffer.allocate(DHCP.MIN_HEADER_LENGTH +
+ 2 + hostNameOption.getLength() + 1);
+
+ bb.put(opCode);
+ bb.put(hardwareType);
+ bb.put(hardwareAddressLength);
+ bb.put(hops);
+ bb.putInt(transactionId);
+ bb.putShort(seconds);
+ bb.putShort(flags);
+ bb.putInt(clientIpAddress);
+ bb.putInt(yourIpAddress);
+ bb.putInt(serverIpAddress);
+ bb.putInt(gatewayIpAddress);
+ bb.put(clientHardwareAddress);
+
+ // need 16 bytes of zeros to pad out the client hardware address field
+ bb.put(new byte[16 - hardwareAddressLength]);
+
+ // Put server name and pad out to 64 bytes
+ bb.put(serverName.getBytes(Charsets.US_ASCII));
+ bb.put(new byte[64 - serverName.length()]);
+
+ // Put boot file name and pad out to 128 bytes
+ bb.put(bootFileName.getBytes(Charsets.US_ASCII));
+ bb.put(new byte[128 - bootFileName.length()]);
+
+ // Magic cookie
+ bb.put("DHCP".getBytes(Charsets.US_ASCII));
+
+ bb.put(hostNameOption.getCode());
+ bb.put(hostNameOption.getLength());
+ bb.put(hostNameOption.getData());
+
+ // End of options marker
+ bb.put((byte) (0xff & 255));
+
+ byteHeader = bb.array();
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader);
+ }
+
+ @Test
+ public void testDeserialize() throws Exception {
+ DHCP dhcp = deserializer.deserialize(byteHeader, 0, byteHeader.length);
+
+ assertEquals(opCode, dhcp.opCode);
+ assertEquals(hardwareType, dhcp.hardwareType);
+ assertEquals(hardwareAddressLength, dhcp.hardwareAddressLength);
+ assertEquals(hops, dhcp.hops);
+ assertEquals(transactionId, dhcp.transactionId);
+ assertEquals(seconds, dhcp.seconds);
+ assertEquals(flags, dhcp.flags);
+ assertEquals(clientIpAddress, dhcp.clientIPAddress);
+ assertEquals(yourIpAddress, dhcp.yourIPAddress);
+ assertEquals(serverIpAddress, dhcp.serverIPAddress);
+ assertEquals(gatewayIpAddress, dhcp.gatewayIPAddress);
+ assertTrue(Arrays.equals(clientHardwareAddress, dhcp.clientHardwareAddress));
+
+ assertEquals(serverName, dhcp.serverName);
+ assertEquals(bootFileName, dhcp.bootFileName);
+ assertEquals(2, dhcp.options.size());
+ assertEquals(hostNameOption, dhcp.options.get(0));
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/EthernetTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/EthernetTest.java
new file mode 100644
index 00000000..15a01fc3
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/EthernetTest.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for the Ethernet class.
+ */
+public class EthernetTest {
+
+ private MacAddress dstMac;
+ private MacAddress srcMac;
+ private short ethertype = 6;
+ private short vlan = 5;
+
+ private Deserializer<Ethernet> deserializer;
+
+ private byte[] byteHeader;
+ private byte[] vlanByteHeader;
+
+ @Before
+ public void setUp() {
+ deserializer = Ethernet.deserializer();
+
+ byte[] dstMacBytes = {
+ (byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88,
+ (byte) 0x88 };
+ dstMac = MacAddress.valueOf(dstMacBytes);
+ byte[] srcMacBytes = {
+ (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa,
+ (byte) 0xaa };
+ srcMac = MacAddress.valueOf(srcMacBytes);
+
+ // Create Ethernet byte array with no VLAN header
+ ByteBuffer bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH);
+ bb.put(dstMacBytes);
+ bb.put(srcMacBytes);
+ bb.putShort(ethertype);
+
+ byteHeader = bb.array();
+
+ // Create Ethernet byte array with a VLAN header
+ bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH + Ethernet.VLAN_HEADER_LENGTH);
+ bb.put(dstMacBytes);
+ bb.put(srcMacBytes);
+ bb.putShort(Ethernet.TYPE_VLAN);
+ bb.putShort(vlan);
+ bb.putShort(ethertype);
+
+ vlanByteHeader = bb.array();
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws DeserializationException {
+ PacketTestUtils.testDeserializeTruncated(deserializer, vlanByteHeader);
+ }
+
+ @Test
+ public void testDeserializeNoVlan() throws Exception {
+ Ethernet eth = deserializer.deserialize(byteHeader, 0, byteHeader.length);
+
+ assertEquals(dstMac, eth.getDestinationMAC());
+ assertEquals(srcMac, eth.getSourceMAC());
+ assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID());
+ assertEquals(ethertype, eth.getEtherType());
+ }
+
+ @Test
+ public void testDeserializeWithVlan() throws Exception {
+ Ethernet eth = deserializer.deserialize(vlanByteHeader, 0, vlanByteHeader.length);
+
+ assertEquals(dstMac, eth.getDestinationMAC());
+ assertEquals(srcMac, eth.getSourceMAC());
+ assertEquals(vlan, eth.getVlanID());
+ assertEquals(ethertype, eth.getEtherType());
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMP6Test.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMP6Test.java
new file mode 100644
index 00000000..39ddc24c
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMP6Test.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+
+package org.onlab.packet;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link ICMP6}.
+ */
+public class ICMP6Test {
+ private static final byte[] IPV6_SOURCE_ADDRESS = {
+ (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
+ };
+ private static final byte[] IPV6_DESTINATION_ADDRESS = {
+ (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
+ };
+
+ private static IPv6 ipv6 = new IPv6();
+ private static byte[] bytePacket = {
+ ICMP6.ECHO_REQUEST, // type
+ (byte) 0x00, // code
+ (byte) 0x82, (byte) 0xbc, // checksum
+ };
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
+ ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
+ ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ ICMP6 icmp6 = new ICMP6();
+ icmp6.setIcmpType(ICMP6.ECHO_REQUEST);
+ icmp6.setIcmpCode((byte) 0);
+ icmp6.setParent(ipv6);
+
+ assertArrayEquals(bytePacket, icmp6.serialize());
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(ICMP6.deserializer());
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(ICMP6.deserializer(), bytePacket);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws Exception {
+ ICMP6 icmp6 = ICMP6.deserializer().deserialize(bytePacket, 0, bytePacket.length);
+
+ assertThat(icmp6.getIcmpType(), is(ICMP6.ECHO_REQUEST));
+ assertThat(icmp6.getIcmpCode(), is((byte) 0x00));
+ assertThat(icmp6.getChecksum(), is((short) 0x82bc));
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ ICMP6 icmp61 = new ICMP6();
+ icmp61.setIcmpType(ICMP6.ECHO_REQUEST);
+ icmp61.setIcmpCode((byte) 0);
+ icmp61.setChecksum((short) 0);
+
+ ICMP6 icmp62 = new ICMP6();
+ icmp62.setIcmpType(ICMP6.ECHO_REPLY);
+ icmp62.setIcmpCode((byte) 0);
+ icmp62.setChecksum((short) 0);
+
+ assertTrue(icmp61.equals(icmp61));
+ assertFalse(icmp61.equals(icmp62));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java
new file mode 100644
index 00000000..8514aa59
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for the ICMP class.
+ */
+public class ICMPTest {
+
+ private Deserializer<ICMP> deserializer;
+
+ private byte icmpType = ICMP.TYPE_ECHO_REQUEST;
+ private byte icmpCode = 4;
+ private short checksum = 870;
+
+ private byte[] headerBytes;
+
+ @Before
+ public void setUp() throws Exception {
+ deserializer = ICMP.deserializer();
+
+ ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH);
+
+ bb.put(icmpType);
+ bb.put(icmpCode);
+ bb.putShort(checksum);
+
+ headerBytes = bb.array();
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes);
+ }
+
+ @Test
+ public void testDeserialize() throws Exception {
+ ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length);
+
+ assertEquals(icmpType, icmp.getIcmpType());
+ assertEquals(icmpCode, icmp.getIcmpCode());
+ assertEquals(checksum, icmp.getChecksum());
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IGMPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IGMPTest.java
new file mode 100644
index 00000000..a25f7217
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IGMPTest.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static junit.framework.Assert.assertTrue;
+
+/**
+ * Unit tests for IGMP class.
+ */
+public class IGMPTest {
+ private Deserializer<IGMP> deserializer;
+
+ private IGMP igmpQuery;
+ private IGMP igmpMembership;
+
+ private Ip4Address gaddr1;
+ private Ip4Address gaddr2;
+ private Ip4Address saddr1;
+ private Ip4Address saddr2;
+
+ @Before
+ public void setUp() throws Exception {
+ gaddr1 = Ip4Address.valueOf(0xe1010101);
+ gaddr2 = Ip4Address.valueOf(0xe2020202);
+ saddr1 = Ip4Address.valueOf(0x0a010101);
+ saddr2 = Ip4Address.valueOf(0x0b020202);
+
+ deserializer = IGMP.deserializer();
+
+ // Create an IGMP Query object
+ igmpQuery = new IGMP();
+ igmpQuery.setIgmpType(IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY);
+ igmpQuery.setMaxRespCode((byte) 0x7f);
+ IGMPQuery q = new IGMPQuery(gaddr1, (byte) 0x7f);
+ q.addSource(saddr1);
+ q.addSource(saddr2);
+ q.setSbit(false);
+ igmpQuery.groups.add(q);
+
+ // Create an IGMP Membership Object
+ igmpMembership = new IGMP();
+ igmpMembership.setIgmpType(IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT);
+ IGMPMembership g1 = new IGMPMembership(gaddr1);
+ g1.addSource(saddr1);
+ g1.addSource(saddr2);
+ igmpMembership.groups.add(g1);
+ IGMPMembership g2 = new IGMPMembership(gaddr2);
+ g2.addSource(saddr1);
+ g2.addSource(saddr2);
+ igmpMembership.groups.add(g2);
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ byte [] bits = igmpQuery.serialize();
+ PacketTestUtils.testDeserializeTruncated(deserializer, bits);
+
+ bits = igmpMembership.serialize();
+ PacketTestUtils.testDeserializeTruncated(deserializer, bits);
+ }
+
+ @Test
+ public void testDeserializeQuery() throws Exception {
+ byte [] data = igmpQuery.serialize();
+ IGMP igmp = deserializer.deserialize(data, 0, data.length);
+ assertTrue(igmp.equals(igmpQuery));
+ }
+
+ @Test
+ public void testDeserializeMembership() throws Exception {
+ byte [] data = igmpMembership.serialize();
+ IGMP igmp = deserializer.deserialize(data, 0, data.length);
+ assertTrue(igmp.equals(igmpMembership));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv4Test.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv4Test.java
new file mode 100644
index 00000000..1bacf2a2
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv4Test.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit tests for IPv4 class.
+ */
+public class IPv4Test {
+
+ private Deserializer<IPv4> deserializer;
+
+ private byte version = 4;
+ private byte headerLength = 6;
+ private byte diffServ = 2;
+ private short totalLength = 20;
+ private short identification = 1;
+ private byte flags = 1;
+ private short fragmentOffset = 1;
+ private byte ttl = 60;
+ private byte protocol = 4;
+ private short checksum = 4;
+ private int sourceAddress = 1;
+ private int destinationAddress = 2;
+ private byte[] options = new byte[] {0x1, 0x2, 0x3, 0x4};
+
+ private byte[] headerBytes;
+
+ @Before
+ public void setUp() throws Exception {
+ deserializer = IPv4.deserializer();
+
+ ByteBuffer bb = ByteBuffer.allocate(headerLength * 4);
+
+ bb.put((byte) ((version & 0xf) << 4 | headerLength & 0xf));
+ bb.put(diffServ);
+ bb.putShort(totalLength);
+ bb.putShort(identification);
+ bb.putShort((short) ((flags & 0x7) << 13 | fragmentOffset & 0x1fff));
+ bb.put(ttl);
+ bb.put(protocol);
+ bb.putShort(checksum);
+ bb.putInt(sourceAddress);
+ bb.putInt(destinationAddress);
+ bb.put(options);
+
+ headerBytes = bb.array();
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes);
+ }
+
+ @Test
+ public void testDeserialize() throws Exception {
+ IPv4 ipv4 = deserializer.deserialize(headerBytes, 0, headerBytes.length);
+
+ assertEquals(version, ipv4.getVersion());
+ assertEquals(headerLength, ipv4.getHeaderLength());
+ assertEquals(diffServ, ipv4.getDiffServ());
+ assertEquals(totalLength, ipv4.getTotalLength());
+ assertEquals(identification, ipv4.getIdentification());
+ assertEquals(flags, ipv4.getFlags());
+ assertEquals(fragmentOffset, ipv4.getFragmentOffset());
+ assertEquals(ttl, ipv4.getTtl());
+ assertEquals(protocol, ipv4.getProtocol());
+ assertEquals(checksum, ipv4.getChecksum());
+ assertEquals(sourceAddress, ipv4.getSourceAddress());
+ assertEquals(destinationAddress, ipv4.getDestinationAddress());
+ assertTrue(ipv4.isTruncated());
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv6Test.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv6Test.java
new file mode 100644
index 00000000..720a4d20
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv6Test.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link IPv6}.
+ */
+public class IPv6Test {
+ private static final byte[] SOURCE_ADDRESS = {
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
+ };
+ private static final byte[] DESTINATION_ADDRESS = {
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
+ };
+ private static Data data;
+ private static UDP udp;
+ private static byte[] bytePacket;
+
+ private Deserializer<IPv6> deserializer;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ data = new Data();
+ data.setData("testSerialize".getBytes());
+ udp = new UDP();
+ udp.setPayload(data);
+
+ byte[] bytePayload = udp.serialize();
+ byte[] byteHeader = {
+ (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79,
+ (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff),
+ (byte) 0x11, (byte) 0x20,
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8,
+ };
+ bytePacket = new byte[byteHeader.length + bytePayload.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
+ }
+
+ @Before
+ public void setUp() {
+ deserializer = IPv6.deserializer();
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ IPv6 ipv6 = new IPv6();
+ ipv6.setPayload(udp);
+ ipv6.setVersion((byte) 6);
+ ipv6.setTrafficClass((byte) 0x93);
+ ipv6.setFlowLabel(0x13579);
+ ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
+ ipv6.setHopLimit((byte) 32);
+ ipv6.setSourceAddress(SOURCE_ADDRESS);
+ ipv6.setDestinationAddress(DESTINATION_ADDRESS);
+
+ assertArrayEquals(ipv6.serialize(), bytePacket);
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ // Run the truncation test only on the IPv6 header
+ byte[] ipv6Header = new byte[IPv6.FIXED_HEADER_LENGTH];
+ ByteBuffer.wrap(bytePacket).get(ipv6Header);
+
+ PacketTestUtils.testDeserializeTruncated(deserializer, ipv6Header);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws DeserializationException {
+ IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertThat(ipv6.getVersion(), is((byte) 6));
+ assertThat(ipv6.getTrafficClass(), is((byte) 0x93));
+ assertThat(ipv6.getFlowLabel(), is(0x13579));
+ assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP));
+ assertThat(ipv6.getHopLimit(), is((byte) 32));
+ assertArrayEquals(ipv6.getSourceAddress(), SOURCE_ADDRESS);
+ assertArrayEquals(ipv6.getDestinationAddress(), DESTINATION_ADDRESS);
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ IPv6 packet1 = new IPv6();
+ packet1.setPayload(udp);
+ packet1.setVersion((byte) 6);
+ packet1.setTrafficClass((byte) 0x93);
+ packet1.setFlowLabel(0x13579);
+ packet1.setNextHeader(IPv6.PROTOCOL_UDP);
+ packet1.setHopLimit((byte) 32);
+ packet1.setSourceAddress(SOURCE_ADDRESS);
+ packet1.setDestinationAddress(DESTINATION_ADDRESS);
+
+ IPv6 packet2 = new IPv6();
+ packet2.setPayload(udp);
+ packet2.setVersion((byte) 6);
+ packet2.setTrafficClass((byte) 0x93);
+ packet2.setFlowLabel(0x13579);
+ packet2.setNextHeader(IPv6.PROTOCOL_UDP);
+ packet2.setHopLimit((byte) 32);
+ packet2.setSourceAddress(DESTINATION_ADDRESS);
+ packet2.setDestinationAddress(SOURCE_ADDRESS);
+
+ assertTrue(packet1.equals(packet1));
+ assertFalse(packet1.equals(packet2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4AddressTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4AddressTest.java
new file mode 100644
index 00000000..3bbf0009
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4AddressTest.java
@@ -0,0 +1,432 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import com.google.common.net.InetAddresses;
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import java.net.InetAddress;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Tests for class {@link Ip4Address}.
+ */
+public class Ip4AddressTest {
+ /**
+ * Tests the immutability of {@link Ip4Address}.
+ */
+ @Test
+ public void testImmutable() {
+ assertThatClassIsImmutable(Ip4Address.class);
+ }
+
+ /**
+ * Tests the IPv4 address version constant.
+ */
+ @Test
+ public void testAddressVersion() {
+ assertThat(Ip4Address.VERSION, is(IpAddress.Version.INET));
+ }
+
+ /**
+ * Tests the length of the address in bytes (octets).
+ */
+ @Test
+ public void testAddrByteLength() {
+ assertThat(Ip4Address.BYTE_LENGTH, is(4));
+ }
+
+ /**
+ * Tests the length of the address in bits.
+ */
+ @Test
+ public void testAddrBitLength() {
+ assertThat(Ip4Address.BIT_LENGTH, is(32));
+ }
+
+ /**
+ * Tests returning the IP address version.
+ */
+ @Test
+ public void testVersion() {
+ Ip4Address ipAddress;
+
+ // IPv4
+ ipAddress = Ip4Address.valueOf("0.0.0.0");
+ assertThat(ipAddress.version(), is(IpAddress.Version.INET));
+ }
+
+ /**
+ * Tests returning an IPv4 address as a byte array.
+ */
+ @Test
+ public void testAddressToOctetsIPv4() {
+ Ip4Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipAddress = Ip4Address.valueOf("1.2.3.4");
+ assertThat(ipAddress.toOctets(), is(value));
+
+ value = new byte[] {0, 0, 0, 0};
+ ipAddress = Ip4Address.valueOf("0.0.0.0");
+ assertThat(ipAddress.toOctets(), is(value));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipAddress = Ip4Address.valueOf("255.255.255.255");
+ assertThat(ipAddress.toOctets(), is(value));
+ }
+
+ /**
+ * Tests returning an IPv4 address as an integer.
+ */
+ @Test
+ public void testToInt() {
+ Ip4Address ipAddress;
+
+ ipAddress = Ip4Address.valueOf("1.2.3.4");
+ assertThat(ipAddress.toInt(), is(0x01020304));
+
+ ipAddress = Ip4Address.valueOf("0.0.0.0");
+ assertThat(ipAddress.toInt(), is(0));
+
+ ipAddress = Ip4Address.valueOf("255.255.255.255");
+ assertThat(ipAddress.toInt(), is(-1));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 integer value.
+ */
+ @Test
+ public void testValueOfForIntegerIPv4() {
+ Ip4Address ipAddress;
+
+ ipAddress = Ip4Address.valueOf(0x01020304);
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ ipAddress = Ip4Address.valueOf(0);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ ipAddress = Ip4Address.valueOf(0xffffffff);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 byte array.
+ */
+ @Test
+ public void testValueOfByteArrayIPv4() {
+ Ip4Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipAddress = Ip4Address.valueOf(value);
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ value = new byte[] {0, 0, 0, 0};
+ ipAddress = Ip4Address.valueOf(value);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipAddress = Ip4Address.valueOf(value);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null array for IPv4.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullArrayIPv4() {
+ Ip4Address ipAddress;
+ byte[] value;
+
+ value = null;
+ ipAddress = Ip4Address.valueOf(value);
+ }
+
+ /**
+ * Tests invalid valueOf() converger for an array that is too short for
+ * IPv4.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfShortArrayIPv4() {
+ Ip4Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3};
+ ipAddress = Ip4Address.valueOf(value);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 byte array and an offset.
+ */
+ @Test
+ public void testValueOfByteArrayOffsetIPv4() {
+ Ip4Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {11, 22, 33, // Preamble
+ 1, 2, 3, 4,
+ 44, 55}; // Extra bytes
+ ipAddress = Ip4Address.valueOf(value, 3);
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ value = new byte[] {11, 22, // Preamble
+ 0, 0, 0, 0,
+ 33}; // Extra bytes
+ ipAddress = Ip4Address.valueOf(value, 2);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ value = new byte[] {11, 22, // Preamble
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ 33}; // Extra bytes
+ ipAddress = Ip4Address.valueOf(value, 2);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests invalid valueOf() converger for an array and an invalid offset
+ * for IPv4.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfArrayInvalidOffsetIPv4() {
+ Ip4Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {11, 22, 33, // Preamble
+ 1, 2, 3, 4,
+ 44, 55}; // Extra bytes
+ ipAddress = Ip4Address.valueOf(value, 6);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 InetAddress.
+ */
+ @Test
+ public void testValueOfInetAddressIPv4() {
+ Ip4Address ipAddress;
+ InetAddress inetAddress;
+
+ inetAddress = InetAddresses.forString("1.2.3.4");
+ ipAddress = Ip4Address.valueOf(inetAddress);
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ inetAddress = InetAddresses.forString("0.0.0.0");
+ ipAddress = Ip4Address.valueOf(inetAddress);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ inetAddress = InetAddresses.forString("255.255.255.255");
+ ipAddress = Ip4Address.valueOf(inetAddress);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 string.
+ */
+ @Test
+ public void testValueOfStringIPv4() {
+ Ip4Address ipAddress;
+
+ ipAddress = Ip4Address.valueOf("1.2.3.4");
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ ipAddress = Ip4Address.valueOf("0.0.0.0");
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ ipAddress = Ip4Address.valueOf("255.255.255.255");
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null string.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullString() {
+ Ip4Address ipAddress;
+
+ String fromString = null;
+ ipAddress = Ip4Address.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an empty string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfEmptyString() {
+ Ip4Address ipAddress;
+
+ String fromString = "";
+ ipAddress = Ip4Address.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an incorrect string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIncorrectString() {
+ Ip4Address ipAddress;
+
+ String fromString = "NoSuchIpAddress";
+ ipAddress = Ip4Address.valueOf(fromString);
+ }
+
+ /**
+ * Tests making a mask prefix for a given prefix length for IPv4.
+ */
+ @Test
+ public void testMakeMaskPrefixIPv4() {
+ Ip4Address ipAddress;
+
+ ipAddress = Ip4Address.makeMaskPrefix(25);
+ assertThat(ipAddress.toString(), is("255.255.255.128"));
+
+ ipAddress = Ip4Address.makeMaskPrefix(0);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ ipAddress = Ip4Address.makeMaskPrefix(32);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests making a mask prefix for an invalid prefix length for IPv4:
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeNegativeMaskPrefixIPv4() {
+ Ip4Address ipAddress;
+
+ ipAddress = Ip4Address.makeMaskPrefix(-1);
+ }
+
+ /**
+ * Tests making a mask prefix for an invalid prefix length for IPv4:
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeTooLongMaskPrefixIPv4() {
+ Ip4Address ipAddress;
+
+ ipAddress = Ip4Address.makeMaskPrefix(33);
+ }
+
+ /**
+ * Tests making of a masked address for IPv4.
+ */
+ @Test
+ public void testMakeMaskedAddressIPv4() {
+ Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5");
+ Ip4Address ipAddressMasked;
+
+ ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 24);
+ assertThat(ipAddressMasked.toString(), is("1.2.3.0"));
+
+ ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 0);
+ assertThat(ipAddressMasked.toString(), is("0.0.0.0"));
+
+ ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 32);
+ assertThat(ipAddressMasked.toString(), is("1.2.3.5"));
+ }
+
+ /**
+ * Tests making of a masked address for invalid prefix length for IPv4:
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeNegativeMaskedAddressIPv4() {
+ Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5");
+ Ip4Address ipAddressMasked;
+
+ ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, -1);
+ }
+
+ /**
+ * Tests making of a masked address for an invalid prefix length for IPv4:
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeTooLongMaskedAddressIPv4() {
+ Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5");
+ Ip4Address ipAddressMasked;
+
+ ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 33);
+ }
+
+ /**
+ * Tests comparison of {@link Ip4Address} for IPv4.
+ */
+ @Test
+ public void testComparisonIPv4() {
+ Ip4Address addr1, addr2, addr3, addr4;
+
+ addr1 = Ip4Address.valueOf("1.2.3.4");
+ addr2 = Ip4Address.valueOf("1.2.3.4");
+ addr3 = Ip4Address.valueOf("1.2.3.3");
+ addr4 = Ip4Address.valueOf("1.2.3.5");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+
+ addr1 = Ip4Address.valueOf("255.2.3.4");
+ addr2 = Ip4Address.valueOf("255.2.3.4");
+ addr3 = Ip4Address.valueOf("255.2.3.3");
+ addr4 = Ip4Address.valueOf("255.2.3.5");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+ }
+
+ /**
+ * Tests equality of {@link Ip4Address} for IPv4.
+ */
+ @Test
+ public void testEqualityIPv4() {
+ new EqualsTester()
+ .addEqualityGroup(Ip4Address.valueOf("1.2.3.4"),
+ Ip4Address.valueOf("1.2.3.4"))
+ .addEqualityGroup(Ip4Address.valueOf("1.2.3.5"),
+ Ip4Address.valueOf("1.2.3.5"))
+ .addEqualityGroup(Ip4Address.valueOf("0.0.0.0"),
+ Ip4Address.valueOf("0.0.0.0"))
+ .addEqualityGroup(Ip4Address.valueOf("255.255.255.255"),
+ Ip4Address.valueOf("255.255.255.255"))
+ .testEquals();
+ }
+
+ /**
+ * Tests object string representation for IPv4.
+ */
+ @Test
+ public void testToStringIPv4() {
+ Ip4Address ipAddress;
+
+ ipAddress = Ip4Address.valueOf("1.2.3.4");
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ ipAddress = Ip4Address.valueOf("0.0.0.0");
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ ipAddress = Ip4Address.valueOf("255.255.255.255");
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4PrefixTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4PrefixTest.java
new file mode 100644
index 00000000..c731ed66
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4PrefixTest.java
@@ -0,0 +1,534 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Tests for class {@link Ip4Prefix}.
+ */
+public class Ip4PrefixTest {
+ /**
+ * Tests the immutability of {@link Ip4Prefix}.
+ */
+ @Test
+ public void testImmutable() {
+ assertThatClassIsImmutable(Ip4Prefix.class);
+ }
+
+ /**
+ * Tests the IPv4 prefix address version constant.
+ */
+ @Test
+ public void testAddressVersion() {
+ assertThat(Ip4Prefix.VERSION, is(IpAddress.Version.INET));
+ }
+
+ /**
+ * Tests the maximum mask length.
+ */
+ @Test
+ public void testMaxMaskLength() {
+ assertThat(Ip4Prefix.MAX_MASK_LENGTH, is(32));
+ }
+
+ /**
+ * Tests returning the IP version of the prefix.
+ */
+ @Test
+ public void testVersion() {
+ Ip4Prefix ipPrefix;
+
+ // IPv4
+ ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
+ assertThat(ipPrefix.version(), is(IpAddress.Version.INET));
+ }
+
+ /**
+ * Tests returning the IP address value and IP address prefix length of
+ * an IPv4 prefix.
+ */
+ @Test
+ public void testAddressAndPrefixLengthIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24");
+ assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0")));
+ assertThat(ipPrefix.prefixLength(), is(24));
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24");
+ assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0")));
+ assertThat(ipPrefix.prefixLength(), is(24));
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32");
+ assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.4")));
+ assertThat(ipPrefix.prefixLength(), is(32));
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32");
+ assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.5")));
+ assertThat(ipPrefix.prefixLength(), is(32));
+
+ ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
+ assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("0.0.0.0")));
+ assertThat(ipPrefix.prefixLength(), is(0));
+
+ ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
+ assertThat(ipPrefix.address(),
+ equalTo(Ip4Address.valueOf("255.255.255.255")));
+ assertThat(ipPrefix.prefixLength(), is(32));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 integer value.
+ */
+ @Test
+ public void testValueOfForIntegerIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf(0x01020304, 24);
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = Ip4Prefix.valueOf(0x01020304, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
+
+ ipPrefix = Ip4Prefix.valueOf(0x01020305, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
+
+ ipPrefix = Ip4Prefix.valueOf(0, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = Ip4Prefix.valueOf(0, 32);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
+
+ ipPrefix = Ip4Prefix.valueOf(0xffffffff, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = Ip4Prefix.valueOf(0xffffffff, 16);
+ assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
+
+ ipPrefix = Ip4Prefix.valueOf(0xffffffff, 32);
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 integer value and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf(0x01020304, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 integer value and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf(0x01020304, 33);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 byte array.
+ */
+ @Test
+ public void testValueOfByteArrayIPv4() {
+ Ip4Prefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipPrefix = Ip4Prefix.valueOf(value, 24);
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = Ip4Prefix.valueOf(value, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
+
+ value = new byte[] {1, 2, 3, 5};
+ ipPrefix = Ip4Prefix.valueOf(value, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
+
+ value = new byte[] {0, 0, 0, 0};
+ ipPrefix = Ip4Prefix.valueOf(value, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = Ip4Prefix.valueOf(value, 32);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipPrefix = Ip4Prefix.valueOf(value, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = Ip4Prefix.valueOf(value, 16);
+ assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
+
+ ipPrefix = Ip4Prefix.valueOf(value, 32);
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null array for IPv4.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullArrayIPv4() {
+ Ip4Prefix ipPrefix;
+ byte[] value;
+
+ value = null;
+ ipPrefix = Ip4Prefix.valueOf(value, 24);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a short array for IPv4.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfShortArrayIPv4() {
+ Ip4Prefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3};
+ ipPrefix = Ip4Prefix.valueOf(value, 24);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 byte array and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() {
+ Ip4Prefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipPrefix = Ip4Prefix.valueOf(value, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 byte array and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() {
+ Ip4Prefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipPrefix = Ip4Prefix.valueOf(value, 33);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 address.
+ */
+ @Test
+ public void testValueOfAddressIPv4() {
+ Ip4Address ipAddress;
+ Ip4Prefix ipPrefix;
+
+ ipAddress = Ip4Address.valueOf("1.2.3.4");
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 24);
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
+
+ ipAddress = Ip4Address.valueOf("1.2.3.5");
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
+
+ ipAddress = Ip4Address.valueOf("0.0.0.0");
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
+
+ ipAddress = Ip4Address.valueOf("255.255.255.255");
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 16);
+ assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
+
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null IP address.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullAddress() {
+ Ip4Address ipAddress;
+ Ip4Prefix ipPrefix;
+
+ ipAddress = null;
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 24);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 address and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfAddressNegativePrefixLengthIPv4() {
+ Ip4Address ipAddress;
+ Ip4Prefix ipPrefix;
+
+ ipAddress = Ip4Address.valueOf("1.2.3.4");
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 address and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() {
+ Ip4Address ipAddress;
+ Ip4Prefix ipPrefix;
+
+ ipAddress = Ip4Address.valueOf("1.2.3.4");
+ ipPrefix = Ip4Prefix.valueOf(ipAddress, 33);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 string.
+ */
+ @Test
+ public void testValueOfStringIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24");
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32");
+ assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32");
+ assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
+
+ ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = Ip4Prefix.valueOf("0.0.0.0/32");
+ assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
+
+ ipPrefix = Ip4Prefix.valueOf("255.255.255.255/0");
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = Ip4Prefix.valueOf("255.255.255.255/16");
+ assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
+
+ ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null string.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullString() {
+ Ip4Prefix ipPrefix;
+ String fromString;
+
+ fromString = null;
+ ipPrefix = Ip4Prefix.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an empty string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfEmptyString() {
+ Ip4Prefix ipPrefix;
+ String fromString;
+
+ fromString = "";
+ ipPrefix = Ip4Prefix.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an incorrect string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIncorrectString() {
+ Ip4Prefix ipPrefix;
+ String fromString;
+
+ fromString = "NoSuchIpPrefix";
+ ipPrefix = Ip4Prefix.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 string and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfStringNegativePrefixLengthIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.4/-1");
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 string and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfStringTooLongPrefixLengthIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.4/33");
+ }
+
+ /**
+ * Tests IP prefix contains another IP prefix for IPv4.
+ */
+ @Test
+ public void testContainsIpPrefixIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24");
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32");
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
+
+ ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
+
+ ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
+ assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
+ assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
+ }
+
+ /**
+ * Tests IP prefix contains IP address for IPv4.
+ */
+ @Test
+ public void testContainsIpAddressIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24");
+ assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
+ assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32");
+ assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
+
+ ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
+ assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
+ assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
+ assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
+ assertTrue(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
+ assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
+
+ ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
+ assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
+ assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
+ }
+
+ /**
+ * Tests equality of {@link Ip4Prefix} for IPv4.
+ */
+ @Test
+ public void testEqualityIPv4() {
+ new EqualsTester()
+ .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/24"),
+ Ip4Prefix.valueOf("1.2.0.0/24"),
+ Ip4Prefix.valueOf("1.2.0.4/24"))
+ .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/16"),
+ Ip4Prefix.valueOf("1.2.0.0/16"))
+ .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/32"),
+ Ip4Prefix.valueOf("1.2.0.0/32"))
+ .addEqualityGroup(Ip4Prefix.valueOf("1.3.0.0/24"),
+ Ip4Prefix.valueOf("1.3.0.0/24"))
+ .addEqualityGroup(Ip4Prefix.valueOf("0.0.0.0/0"),
+ Ip4Prefix.valueOf("0.0.0.0/0"))
+ .addEqualityGroup(Ip4Prefix.valueOf("255.255.255.255/32"),
+ Ip4Prefix.valueOf("255.255.255.255/32"))
+ .testEquals();
+ }
+
+ /**
+ * Tests object string representation for IPv4.
+ */
+ @Test
+ public void testToStringIPv4() {
+ Ip4Prefix ipPrefix;
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24");
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24");
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6AddressTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6AddressTest.java
new file mode 100644
index 00000000..e7d017d1
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6AddressTest.java
@@ -0,0 +1,499 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import com.google.common.net.InetAddresses;
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import java.net.InetAddress;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Tests for class {@link Ip6Address}.
+ */
+public class Ip6AddressTest {
+ /**
+ * Tests the immutability of {@link Ip6Address}.
+ */
+ @Test
+ public void testImmutable() {
+ assertThatClassIsImmutable(Ip6Address.class);
+ }
+
+ /**
+ * Tests the IPv4 address version constant.
+ */
+ @Test
+ public void testAddressVersion() {
+ assertThat(Ip6Address.VERSION, is(IpAddress.Version.INET6));
+ }
+
+ /**
+ * Tests the length of the address in bytes (octets).
+ */
+ @Test
+ public void testAddrByteLength() {
+ assertThat(Ip6Address.BYTE_LENGTH, is(16));
+ }
+
+ /**
+ * Tests the length of the address in bits.
+ */
+ @Test
+ public void testAddrBitLength() {
+ assertThat(Ip6Address.BIT_LENGTH, is(128));
+ }
+
+ /**
+ * Tests returning the IP address version.
+ */
+ @Test
+ public void testVersion() {
+ IpAddress ipAddress;
+
+ // IPv6
+ ipAddress = IpAddress.valueOf("::");
+ assertThat(ipAddress.version(), is(IpAddress.Version.INET6));
+ }
+
+ /**
+ * Tests returning an IPv6 address as a byte array.
+ */
+ @Test
+ public void testAddressToOctetsIPv6() {
+ Ip6Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77,
+ (byte) 0x88, (byte) 0x88};
+ ipAddress =
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ assertThat(ipAddress.toOctets(), is(value));
+
+ value = new byte[] {0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00};
+ ipAddress = Ip6Address.valueOf("::");
+ assertThat(ipAddress.toOctets(), is(value));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipAddress =
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ assertThat(ipAddress.toOctets(), is(value));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 byte array.
+ */
+ @Test
+ public void testValueOfByteArrayIPv6() {
+ Ip6Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77,
+ (byte) 0x88, (byte) 0x88};
+ ipAddress = Ip6Address.valueOf(value);
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ value = new byte[] {0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00};
+ ipAddress = Ip6Address.valueOf(value);
+ assertThat(ipAddress.toString(), is("::"));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipAddress = Ip6Address.valueOf(value);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null array for IPv6.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullArrayIPv6() {
+ Ip6Address ipAddress;
+ byte[] value;
+
+ value = null;
+ ipAddress = Ip6Address.valueOf(value);
+ }
+
+ /**
+ * Tests invalid valueOf() converger for an array that is too short for
+ * IPv6.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfShortArrayIPv6() {
+ Ip6Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
+ ipAddress = Ip6Address.valueOf(value);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 byte array and an offset.
+ */
+ @Test
+ public void testValueOfByteArrayOffsetIPv6() {
+ Ip6Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {11, 22, 33, // Preamble
+ 0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77,
+ (byte) 0x88, (byte) 0x88,
+ 44, 55}; // Extra bytes
+ ipAddress = Ip6Address.valueOf(value, 3);
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ value = new byte[] {11, 22, // Preamble
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 33}; // Extra bytes
+ ipAddress = Ip6Address.valueOf(value, 2);
+ assertThat(ipAddress.toString(), is("::"));
+
+ value = new byte[] {11, 22, // Preamble
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ 33}; // Extra bytes
+ ipAddress = Ip6Address.valueOf(value, 2);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+ }
+
+ /**
+ * Tests invalid valueOf() converger for an array and an invalid offset
+ * for IPv6.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfArrayInvalidOffsetIPv6() {
+ Ip6Address ipAddress;
+ byte[] value;
+
+ value = new byte[] {11, 22, 33, // Preamble
+ 0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77,
+ (byte) 0x88, (byte) 0x88,
+ 44, 55}; // Extra bytes
+ ipAddress = Ip6Address.valueOf(value, 6);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 InetAddress.
+ */
+ @Test
+ public void testValueOfInetAddressIPv6() {
+ Ip6Address ipAddress;
+ InetAddress inetAddress;
+
+ inetAddress =
+ InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
+ ipAddress = Ip6Address.valueOf(inetAddress);
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ inetAddress = InetAddresses.forString("::");
+ ipAddress = Ip6Address.valueOf(inetAddress);
+ assertThat(ipAddress.toString(), is("::"));
+
+ inetAddress =
+ InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ ipAddress = Ip6Address.valueOf(inetAddress);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 string.
+ */
+ @Test
+ public void testValueOfStringIPv6() {
+ Ip6Address ipAddress;
+
+ ipAddress =
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ ipAddress = Ip6Address.valueOf("::");
+ assertThat(ipAddress.toString(), is("::"));
+
+ ipAddress =
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null string.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullString() {
+ Ip6Address ipAddress;
+
+ String fromString = null;
+ ipAddress = Ip6Address.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an empty string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfEmptyString() {
+ Ip6Address ipAddress;
+
+ String fromString = "";
+ ipAddress = Ip6Address.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an incorrect string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIncorrectString() {
+ Ip6Address ipAddress;
+
+ String fromString = "NoSuchIpAddress";
+ ipAddress = Ip6Address.valueOf(fromString);
+ }
+
+ /**
+ * Tests making a mask prefix for a given prefix length for IPv6.
+ */
+ @Test
+ public void testMakeMaskPrefixIPv6() {
+ Ip6Address ipAddress;
+
+ ipAddress = Ip6Address.makeMaskPrefix(8);
+ assertThat(ipAddress.toString(), is("ff00::"));
+
+ ipAddress = Ip6Address.makeMaskPrefix(120);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00"));
+
+ ipAddress = Ip6Address.makeMaskPrefix(0);
+ assertThat(ipAddress.toString(), is("::"));
+
+ ipAddress = Ip6Address.makeMaskPrefix(128);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+
+ ipAddress = Ip6Address.makeMaskPrefix(64);
+ assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::"));
+ }
+
+ /**
+ * Tests making a mask prefix for an invalid prefix length for IPv6:
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeNegativeMaskPrefixIPv6() {
+ Ip6Address ipAddress;
+
+ ipAddress = Ip6Address.makeMaskPrefix(-1);
+ }
+
+ /**
+ * Tests making a mask prefix for an invalid prefix length for IPv6:
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeTooLongMaskPrefixIPv6() {
+ Ip6Address ipAddress;
+
+ ipAddress = Ip6Address.makeMaskPrefix(129);
+ }
+
+ /**
+ * Tests making of a masked address for IPv6.
+ */
+ @Test
+ public void testMakeMaskedAddressIPv6() {
+ Ip6Address ipAddress =
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
+ Ip6Address ipAddressMasked;
+
+ ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 8);
+ assertThat(ipAddressMasked.toString(), is("1100::"));
+
+ ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 120);
+ assertThat(ipAddressMasked.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8800"));
+
+ ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 0);
+ assertThat(ipAddressMasked.toString(), is("::"));
+
+ ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 128);
+ assertThat(ipAddressMasked.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8885"));
+
+ ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 64);
+ assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::"));
+ }
+
+ /**
+ * Tests making of a masked address for invalid prefix length for IPv6:
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeNegativeMaskedAddressIPv6() {
+ Ip6Address ipAddress =
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
+ Ip6Address ipAddressMasked;
+
+ ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, -1);
+ }
+
+ /**
+ * Tests making of a masked address for an invalid prefix length for IPv6:
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeTooLongMaskedAddressIPv6() {
+ Ip6Address ipAddress =
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
+ Ip6Address ipAddressMasked;
+
+ ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 129);
+ }
+
+ /**
+ * Tests comparison of {@link Ip6Address} for IPv6.
+ */
+ @Test
+ public void testComparisonIPv6() {
+ Ip6Address addr1, addr2, addr3, addr4;
+
+ addr1 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ addr2 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ addr3 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8887");
+ addr4 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8889");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+
+ addr1 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
+ addr2 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
+ addr3 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887");
+ addr4 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+
+ addr1 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
+ addr2 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
+ addr3 = Ip6Address.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888");
+ addr4 = Ip6Address.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+ }
+
+ /**
+ * Tests equality of {@link Ip6Address} for IPv6.
+ */
+ @Test
+ public void testEqualityIPv6() {
+ new EqualsTester()
+ .addEqualityGroup(
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"),
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"))
+ .addEqualityGroup(
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"),
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"))
+ .addEqualityGroup(
+ Ip6Address.valueOf("::"),
+ Ip6Address.valueOf("::"))
+ .addEqualityGroup(
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))
+ .testEquals();
+ }
+
+ /**
+ * Tests object string representation for IPv6.
+ */
+ @Test
+ public void testToStringIPv6() {
+ Ip6Address ipAddress;
+
+ ipAddress =
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ ipAddress = Ip6Address.valueOf("1111::8888");
+ assertThat(ipAddress.toString(), is("1111::8888"));
+
+ ipAddress = Ip6Address.valueOf("1111::");
+ assertThat(ipAddress.toString(), is("1111::"));
+
+ ipAddress = Ip6Address.valueOf("::8888");
+ assertThat(ipAddress.toString(), is("::8888"));
+
+ ipAddress = Ip6Address.valueOf("::");
+ assertThat(ipAddress.toString(), is("::"));
+
+ ipAddress =
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6PrefixTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6PrefixTest.java
new file mode 100644
index 00000000..dceeb840
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6PrefixTest.java
@@ -0,0 +1,568 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Tests for class {@link Ip6Prefix}.
+ */
+public class Ip6PrefixTest {
+ /**
+ * Tests the immutability of {@link Ip6Prefix}.
+ */
+ @Test
+ public void testImmutable() {
+ assertThatClassIsImmutable(Ip6Prefix.class);
+ }
+
+ /**
+ * Tests the IPv4 prefix address version constant.
+ */
+ @Test
+ public void testAddressVersion() {
+ assertThat(Ip6Prefix.VERSION, is(IpAddress.Version.INET6));
+ }
+
+ /**
+ * Tests the maximum mask length.
+ */
+ @Test
+ public void testMaxMaskLength() {
+ assertThat(Ip6Prefix.MAX_MASK_LENGTH, is(128));
+ }
+
+ /**
+ * Tests returning the IP version of the prefix.
+ */
+ @Test
+ public void testVersion() {
+ Ip6Prefix ipPrefix;
+
+ // IPv6
+ ipPrefix = Ip6Prefix.valueOf("::/0");
+ assertThat(ipPrefix.version(), is(IpAddress.Version.INET6));
+ }
+
+ /**
+ * Tests returning the IP address value and IP address prefix length of
+ * an IPv6 prefix.
+ */
+ @Test
+ public void testAddressAndPrefixLengthIPv6() {
+ Ip6Prefix ipPrefix;
+
+ ipPrefix = Ip6Prefix.valueOf("1100::/8");
+ assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("1100::")));
+ assertThat(ipPrefix.prefixLength(), is(8));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
+ assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("1100::")));
+ assertThat(ipPrefix.prefixLength(), is(8));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8800/120");
+ assertThat(ipPrefix.address(),
+ equalTo(Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8800")));
+ assertThat(ipPrefix.prefixLength(), is(120));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128");
+ assertThat(ipPrefix.address(),
+ equalTo(Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885")));
+ assertThat(ipPrefix.prefixLength(), is(128));
+
+ ipPrefix = Ip6Prefix.valueOf("::/0");
+ assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("::")));
+ assertThat(ipPrefix.prefixLength(), is(0));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertThat(ipPrefix.address(),
+ equalTo(Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+ assertThat(ipPrefix.prefixLength(), is(128));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/64");
+ assertThat(ipPrefix.address(),
+ equalTo(Ip6Address.valueOf("1111:2222:3333:4444::")));
+ assertThat(ipPrefix.prefixLength(), is(64));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 byte array.
+ */
+ @Test
+ public void testValueOfByteArrayIPv6() {
+ Ip6Prefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77, (byte) 0x88, (byte) 0x88};
+ ipPrefix = Ip6Prefix.valueOf(value, 120);
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
+
+ ipPrefix = Ip6Prefix.valueOf(value, 128);
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
+
+ value = new byte[] {0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00};
+ ipPrefix = Ip6Prefix.valueOf(value, 0);
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = Ip6Prefix.valueOf(value, 128);
+ assertThat(ipPrefix.toString(), is("::/128"));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipPrefix = Ip6Prefix.valueOf(value, 0);
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = Ip6Prefix.valueOf(value, 64);
+ assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
+
+ ipPrefix = Ip6Prefix.valueOf(value, 128);
+ assertThat(ipPrefix.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null array for IPv6.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullArrayIPv6() {
+ Ip6Prefix ipPrefix;
+ byte[] value;
+
+ value = null;
+ ipPrefix = Ip6Prefix.valueOf(value, 120);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a short array for IPv6.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfShortArrayIPv6() {
+ Ip6Prefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
+ ipPrefix = Ip6Prefix.valueOf(value, 120);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 byte array and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() {
+ Ip6Prefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77, (byte) 0x88, (byte) 0x88};
+ ipPrefix = Ip6Prefix.valueOf(value, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 byte array and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() {
+ Ip6Prefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77, (byte) 0x88, (byte) 0x88};
+ ipPrefix = Ip6Prefix.valueOf(value, 129);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 address.
+ */
+ @Test
+ public void testValueOfAddressIPv6() {
+ Ip6Address ipAddress;
+ Ip6Prefix ipPrefix;
+
+ ipAddress =
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, 120);
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
+
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, 128);
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
+
+ ipAddress = Ip6Address.valueOf("::");
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, 0);
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, 128);
+ assertThat(ipPrefix.toString(), is("::/128"));
+
+ ipAddress =
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, 0);
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, 64);
+ assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
+
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, 128);
+ assertThat(ipPrefix.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null IP address.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullAddress() {
+ Ip6Address ipAddress;
+ Ip6Prefix ipPrefix;
+
+ ipAddress = null;
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, 24);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 address and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfAddressNegativePrefixLengthIPv6() {
+ Ip6Address ipAddress;
+ Ip6Prefix ipPrefix;
+
+ ipAddress =
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 address and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfAddressTooLongPrefixLengthIPv6() {
+ Ip6Address ipAddress;
+ Ip6Prefix ipPrefix;
+
+ ipAddress =
+ Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ ipPrefix = Ip6Prefix.valueOf(ipAddress, 129);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 string.
+ */
+ @Test
+ public void testValueOfStringIPv6() {
+ Ip6Prefix ipPrefix;
+
+ ipPrefix =
+ Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/120");
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/128");
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
+
+ ipPrefix = Ip6Prefix.valueOf("::/0");
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = Ip6Prefix.valueOf("::/128");
+ assertThat(ipPrefix.toString(), is("::/128"));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0");
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64");
+ assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertThat(ipPrefix.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null string.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullString() {
+ Ip6Prefix ipPrefix;
+ String fromString;
+
+ fromString = null;
+ ipPrefix = Ip6Prefix.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an empty string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfEmptyString() {
+ Ip6Prefix ipPrefix;
+ String fromString;
+
+ fromString = "";
+ ipPrefix = Ip6Prefix.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an incorrect string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIncorrectString() {
+ Ip6Prefix ipPrefix;
+ String fromString;
+
+ fromString = "NoSuchIpPrefix";
+ ipPrefix = Ip6Prefix.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 string and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfStringNegativePrefixLengthIPv6() {
+ Ip6Prefix ipPrefix;
+
+ ipPrefix =
+ Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/-1");
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 string and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfStringTooLongPrefixLengthIPv6() {
+ Ip6Prefix ipPrefix;
+
+ ipPrefix =
+ Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/129");
+ }
+
+ /**
+ * Tests IP prefix contains another IP prefix for IPv6.
+ */
+ @Test
+ public void testContainsIpPrefixIPv6() {
+ Ip6Prefix ipPrefix;
+
+ ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/120");
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/120")));
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/128")));
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::1/128")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/64")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4445::/120")));
+ assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64")));
+ assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
+
+ ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/128");
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/120")));
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/128")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::1/128")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/64")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4445::/120")));
+ assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64")));
+ assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
+
+ ipPrefix = Ip6Prefix.valueOf("::/0");
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/120")));
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/128")));
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::1/128")));
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/64")));
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4445::/120")));
+ assertTrue(ipPrefix.contains(Ip6Prefix.valueOf("::/64")));
+ assertTrue(ipPrefix.contains(Ip6Prefix.valueOf("::/0")));
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/120")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/128")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::1/128")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/64")));
+ assertFalse(ipPrefix.contains(
+ Ip6Prefix.valueOf("1111:2222:3333:4445::/120")));
+ assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64")));
+ assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0")));
+ assertTrue(ipPrefix.contains(
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
+ }
+
+ /**
+ * Tests IP prefix contains IP address for IPv6.
+ */
+ @Test
+ public void testContainsIpAddressIPv6() {
+ Ip6Prefix ipPrefix;
+
+ ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/120");
+ assertTrue(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4444::")));
+ assertTrue(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4444::1")));
+ assertFalse(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4445::")));
+ assertFalse(ipPrefix.contains(Ip6Address.valueOf("::")));
+ assertFalse(ipPrefix.contains(
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+
+ ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/128");
+ assertTrue(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4444::")));
+ assertFalse(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4444::1")));
+ assertFalse(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4445::")));
+ assertFalse(ipPrefix.contains(Ip6Address.valueOf("::")));
+ assertFalse(ipPrefix.contains(
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+
+ ipPrefix = Ip6Prefix.valueOf("::/0");
+ assertTrue(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4444::")));
+ assertTrue(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4444::1")));
+ assertTrue(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4445::")));
+ assertTrue(ipPrefix.contains(Ip6Address.valueOf("::")));
+ assertTrue(ipPrefix.contains(
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+
+ ipPrefix =
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertFalse(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4444::")));
+ assertFalse(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4444::1")));
+ assertFalse(ipPrefix.contains(
+ Ip6Address.valueOf("1111:2222:3333:4445::")));
+ assertFalse(ipPrefix.contains(Ip6Address.valueOf("::")));
+ assertTrue(ipPrefix.contains(
+ Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+ }
+
+ /**
+ * Tests equality of {@link Ip6Prefix} for IPv6.
+ */
+ @Test
+ public void testEqualityIPv6() {
+ new EqualsTester()
+ .addEqualityGroup(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/120"),
+ Ip6Prefix.valueOf("1111:2222:3333:4444::1/120"),
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))
+ .addEqualityGroup(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/64"),
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))
+ .addEqualityGroup(
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/128"),
+ Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))
+ .addEqualityGroup(
+ Ip6Prefix.valueOf("1111:2222:3333:4445::/64"),
+ Ip6Prefix.valueOf("1111:2222:3333:4445::/64"))
+ .addEqualityGroup(
+ Ip6Prefix.valueOf("::/0"),
+ Ip6Prefix.valueOf("::/0"))
+ .addEqualityGroup(
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"),
+ Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))
+ .testEquals();
+ }
+
+ /**
+ * Tests object string representation for IPv6.
+ */
+ @Test
+ public void testToStringIPv6() {
+ Ip6Prefix ipPrefix;
+
+ ipPrefix = Ip6Prefix.valueOf("1100::/8");
+ assertThat(ipPrefix.toString(), is("1100::/8"));
+
+ ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
+ assertThat(ipPrefix.toString(), is("1100::/8"));
+
+ ipPrefix = Ip6Prefix.valueOf("::/0");
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertThat(ipPrefix.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java
new file mode 100644
index 00000000..8618c466
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java
@@ -0,0 +1,946 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import java.net.InetAddress;
+
+import org.junit.Test;
+
+import com.google.common.net.InetAddresses;
+import com.google.common.testing.EqualsTester;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
+
+/**
+ * Tests for class {@link IpAddress}.
+ */
+public class IpAddressTest {
+ /**
+ * Tests the immutability of {@link IpAddress}.
+ */
+ @Test
+ public void testImmutable() {
+ assertThatClassIsImmutableBaseClass(IpAddress.class);
+ }
+
+ /**
+ * Tests the length of the address in bytes (octets).
+ */
+ @Test
+ public void testAddrByteLength() {
+ assertThat(IpAddress.INET_BYTE_LENGTH, is(4));
+ assertThat(IpAddress.INET6_BYTE_LENGTH, is(16));
+ assertThat(IpAddress.byteLength(IpAddress.Version.INET), is(4));
+ assertThat(IpAddress.byteLength(IpAddress.Version.INET6), is(16));
+ }
+
+ /**
+ * Tests the length of the address in bits.
+ */
+ @Test
+ public void testAddrBitLength() {
+ assertThat(IpAddress.INET_BIT_LENGTH, is(32));
+ assertThat(IpAddress.INET6_BIT_LENGTH, is(128));
+ }
+
+ /**
+ * Tests returning the IP address version.
+ */
+ @Test
+ public void testVersion() {
+ IpAddress ipAddress;
+
+ // IPv4
+ ipAddress = IpAddress.valueOf("0.0.0.0");
+ assertThat(ipAddress.version(), is(IpAddress.Version.INET));
+
+ // IPv6
+ ipAddress = IpAddress.valueOf("::");
+ assertThat(ipAddress.version(), is(IpAddress.Version.INET6));
+ }
+
+ /**
+ * Tests whether the IP version of an address is IPv4.
+ */
+ @Test
+ public void testIsIp4() {
+ IpAddress ipAddress;
+
+ // IPv4
+ ipAddress = IpAddress.valueOf("0.0.0.0");
+ assertTrue(ipAddress.isIp4());
+
+ // IPv6
+ ipAddress = IpAddress.valueOf("::");
+ assertFalse(ipAddress.isIp4());
+ }
+
+ /**
+ * Tests whether the IP version of an address is IPv6.
+ */
+ @Test
+ public void testIsIp6() {
+ IpAddress ipAddress;
+
+ // IPv4
+ ipAddress = IpAddress.valueOf("0.0.0.0");
+ assertFalse(ipAddress.isIp6());
+
+ // IPv6
+ ipAddress = IpAddress.valueOf("::");
+ assertTrue(ipAddress.isIp6());
+ }
+
+ /**
+ * Tests getting the Ip4Address and Ip6Address view of the IP address.
+ */
+ @Test
+ public void testGetIp4AndIp6AddressView() {
+ IpAddress ipAddress;
+ Ip4Address ip4Address;
+ Ip6Address ip6Address;
+
+ // Pure IPv4 IpAddress
+ ipAddress = IpAddress.valueOf("1.2.3.4");
+ ip4Address = ipAddress.getIp4Address();
+ ip6Address = ipAddress.getIp6Address();
+ assertThat(ip4Address.toString(), is("1.2.3.4"));
+ assertNull(ip6Address);
+
+ // IPv4 IpAddress that is Ip4Address
+ ipAddress = Ip4Address.valueOf("1.2.3.4");
+ ip4Address = ipAddress.getIp4Address();
+ ip6Address = ipAddress.getIp6Address();
+ assertThat(ip4Address.toString(), is("1.2.3.4"));
+ assertNull(ip6Address);
+
+ // Pure IPv6 IpAddress
+ ipAddress = IpAddress.valueOf("1111:2222::");
+ ip4Address = ipAddress.getIp4Address();
+ ip6Address = ipAddress.getIp6Address();
+ assertNull(ip4Address);
+ assertThat(ip6Address.toString(), is("1111:2222::"));
+
+ // IPv6 IpAddress that is Ip6Address
+ ipAddress = Ip6Address.valueOf("1111:2222::");
+ ip4Address = ipAddress.getIp4Address();
+ ip6Address = ipAddress.getIp6Address();
+ assertNull(ip4Address);
+ assertThat(ip6Address.toString(), is("1111:2222::"));
+ }
+
+ /**
+ * Tests returning an IPv4 address as a byte array.
+ */
+ @Test
+ public void testAddressToOctetsIPv4() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipAddress = IpAddress.valueOf("1.2.3.4");
+ assertThat(ipAddress.toOctets(), is(value));
+
+ value = new byte[] {0, 0, 0, 0};
+ ipAddress = IpAddress.valueOf("0.0.0.0");
+ assertThat(ipAddress.toOctets(), is(value));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipAddress = IpAddress.valueOf("255.255.255.255");
+ assertThat(ipAddress.toOctets(), is(value));
+ }
+
+ /**
+ * Tests returning an IPv6 address as a byte array.
+ */
+ @Test
+ public void testAddressToOctetsIPv6() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77,
+ (byte) 0x88, (byte) 0x88};
+ ipAddress =
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ assertThat(ipAddress.toOctets(), is(value));
+
+ value = new byte[] {0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00};
+ ipAddress = IpAddress.valueOf("::");
+ assertThat(ipAddress.toOctets(), is(value));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipAddress =
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ assertThat(ipAddress.toOctets(), is(value));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 integer value.
+ */
+ @Test
+ public void testValueOfForIntegerIPv4() {
+ IpAddress ipAddress;
+
+ ipAddress = IpAddress.valueOf(0x01020304);
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ ipAddress = IpAddress.valueOf(0);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ ipAddress = IpAddress.valueOf(0xffffffff);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 byte array.
+ */
+ @Test
+ public void testValueOfByteArrayIPv4() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ value = new byte[] {0, 0, 0, 0};
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 byte array.
+ */
+ @Test
+ public void testValueOfByteArrayIPv6() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77,
+ (byte) 0x88, (byte) 0x88};
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ value = new byte[] {0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00};
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
+ assertThat(ipAddress.toString(), is("::"));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null array for IPv4.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullArrayIPv4() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = null;
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null array for IPv6.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullArrayIPv6() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = null;
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
+ }
+
+ /**
+ * Tests invalid valueOf() converger for an array that is too short for
+ * IPv4.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfShortArrayIPv4() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3};
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
+ }
+
+ /**
+ * Tests invalid valueOf() converger for an array that is too short for
+ * IPv6.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfShortArrayIPv6() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 byte array and an offset.
+ */
+ @Test
+ public void testValueOfByteArrayOffsetIPv4() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {11, 22, 33, // Preamble
+ 1, 2, 3, 4,
+ 44, 55}; // Extra bytes
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 3);
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ value = new byte[] {11, 22, // Preamble
+ 0, 0, 0, 0,
+ 33}; // Extra bytes
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ value = new byte[] {11, 22, // Preamble
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ 33}; // Extra bytes
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 byte array and an offset.
+ */
+ @Test
+ public void testValueOfByteArrayOffsetIPv6() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {11, 22, 33, // Preamble
+ 0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77,
+ (byte) 0x88, (byte) 0x88,
+ 44, 55}; // Extra bytes
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 3);
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ value = new byte[] {11, 22, // Preamble
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 33}; // Extra bytes
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
+ assertThat(ipAddress.toString(), is("::"));
+
+ value = new byte[] {11, 22, // Preamble
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ 33}; // Extra bytes
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+ }
+
+ /**
+ * Tests invalid valueOf() converger for an array and an invalid offset
+ * for IPv4.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfArrayInvalidOffsetIPv4() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {11, 22, 33, // Preamble
+ 1, 2, 3, 4,
+ 44, 55}; // Extra bytes
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 6);
+ }
+
+ /**
+ * Tests invalid valueOf() converger for an array and an invalid offset
+ * for IPv6.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfArrayInvalidOffsetIPv6() {
+ IpAddress ipAddress;
+ byte[] value;
+
+ value = new byte[] {11, 22, 33, // Preamble
+ 0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77,
+ (byte) 0x88, (byte) 0x88,
+ 44, 55}; // Extra bytes
+ ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 6);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 InetAddress.
+ */
+ @Test
+ public void testValueOfInetAddressIPv4() {
+ IpAddress ipAddress;
+ InetAddress inetAddress;
+
+ inetAddress = InetAddresses.forString("1.2.3.4");
+ ipAddress = IpAddress.valueOf(inetAddress);
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ inetAddress = InetAddresses.forString("0.0.0.0");
+ ipAddress = IpAddress.valueOf(inetAddress);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ inetAddress = InetAddresses.forString("255.255.255.255");
+ ipAddress = IpAddress.valueOf(inetAddress);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 InetAddress.
+ */
+ @Test
+ public void testValueOfInetAddressIPv6() {
+ IpAddress ipAddress;
+ InetAddress inetAddress;
+
+ inetAddress =
+ InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
+ ipAddress = IpAddress.valueOf(inetAddress);
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ inetAddress = InetAddresses.forString("::");
+ ipAddress = IpAddress.valueOf(inetAddress);
+ assertThat(ipAddress.toString(), is("::"));
+
+ inetAddress =
+ InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ ipAddress = IpAddress.valueOf(inetAddress);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 string.
+ */
+ @Test
+ public void testValueOfStringIPv4() {
+ IpAddress ipAddress;
+
+ ipAddress = IpAddress.valueOf("1.2.3.4");
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ ipAddress = IpAddress.valueOf("0.0.0.0");
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ ipAddress = IpAddress.valueOf("255.255.255.255");
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 string.
+ */
+ @Test
+ public void testValueOfStringIPv6() {
+ IpAddress ipAddress;
+
+ ipAddress =
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ ipAddress = IpAddress.valueOf("::");
+ assertThat(ipAddress.toString(), is("::"));
+
+ ipAddress =
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null string.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullString() {
+ IpAddress ipAddress;
+
+ String fromString = null;
+ ipAddress = IpAddress.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an empty string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfEmptyString() {
+ IpAddress ipAddress;
+
+ String fromString = "";
+ ipAddress = IpAddress.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an incorrect string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIncorrectString() {
+ IpAddress ipAddress;
+
+ String fromString = "NoSuchIpAddress";
+ ipAddress = IpAddress.valueOf(fromString);
+ }
+
+ /**
+ * Tests making a mask prefix for a given prefix length for IPv4.
+ */
+ @Test
+ public void testMakeMaskPrefixIPv4() {
+ IpAddress ipAddress;
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 25);
+ assertThat(ipAddress.toString(), is("255.255.255.128"));
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 0);
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 32);
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests making a mask prefix for a given prefix length for IPv6.
+ */
+ @Test
+ public void testMakeMaskPrefixIPv6() {
+ IpAddress ipAddress;
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 8);
+ assertThat(ipAddress.toString(), is("ff00::"));
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 120);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00"));
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 0);
+ assertThat(ipAddress.toString(), is("::"));
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 128);
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 64);
+ assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::"));
+ }
+
+ /**
+ * Tests making a mask prefix for an invalid prefix length for IPv4:
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeNegativeMaskPrefixIPv4() {
+ IpAddress ipAddress;
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, -1);
+ }
+
+ /**
+ * Tests making a mask prefix for an invalid prefix length for IPv6:
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeNegativeMaskPrefixIPv6() {
+ IpAddress ipAddress;
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, -1);
+ }
+
+ /**
+ * Tests making a mask prefix for an invalid prefix length for IPv4:
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeTooLongMaskPrefixIPv4() {
+ IpAddress ipAddress;
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 33);
+ }
+
+ /**
+ * Tests making a mask prefix for an invalid prefix length for IPv6:
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeTooLongMaskPrefixIPv6() {
+ IpAddress ipAddress;
+
+ ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 129);
+ }
+
+ /**
+ * Tests making of a masked address for IPv4.
+ */
+ @Test
+ public void testMakeMaskedAddressIPv4() {
+ IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
+ IpAddress ipAddressMasked;
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 24);
+ assertThat(ipAddressMasked.toString(), is("1.2.3.0"));
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
+ assertThat(ipAddressMasked.toString(), is("0.0.0.0"));
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 32);
+ assertThat(ipAddressMasked.toString(), is("1.2.3.5"));
+ }
+
+ /**
+ * Tests making of a masked address for IPv6.
+ */
+ @Test
+ public void testMakeMaskedAddressIPv6() {
+ IpAddress ipAddress =
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
+ IpAddress ipAddressMasked;
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 8);
+ assertThat(ipAddressMasked.toString(), is("1100::"));
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 120);
+ assertThat(ipAddressMasked.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8800"));
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
+ assertThat(ipAddressMasked.toString(), is("::"));
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 128);
+ assertThat(ipAddressMasked.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8885"));
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 64);
+ assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::"));
+ }
+
+ /**
+ * Tests making of a masked address for invalid prefix length for IPv4:
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeNegativeMaskedAddressIPv4() {
+ IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
+ IpAddress ipAddressMasked;
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
+ }
+
+ /**
+ * Tests making of a masked address for invalid prefix length for IPv6:
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeNegativeMaskedAddressIPv6() {
+ IpAddress ipAddress =
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
+ IpAddress ipAddressMasked;
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
+ }
+
+ /**
+ * Tests making of a masked address for an invalid prefix length for IPv4:
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeTooLongMaskedAddressIPv4() {
+ IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
+ IpAddress ipAddressMasked;
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 33);
+ }
+
+ /**
+ * Tests making of a masked address for an invalid prefix length for IPv6:
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidMakeTooLongMaskedAddressIPv6() {
+ IpAddress ipAddress =
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
+ IpAddress ipAddressMasked;
+
+ ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 129);
+ }
+
+ /**
+ * Tests if address is zero for IPv4.
+ */
+ @Test
+ public void testIsZeroIPv4() {
+ IpAddress normalIP = IpAddress.valueOf("10.0.0.1");
+ IpAddress zeroIP = IpAddress.valueOf("0.0.0.0");
+ assertFalse(normalIP.isZero());
+ assertTrue(zeroIP.isZero());
+ }
+
+ /**
+ * Tests if address is zero for IPv6.
+ */
+ @Test
+ public void testIsZeroIPv6() {
+ IpAddress normalIP = IpAddress.valueOf("fe80::1");
+ IpAddress zeroIP = IpAddress.valueOf("::");
+ assertFalse(normalIP.isZero());
+ assertTrue(zeroIP.isZero());
+ }
+
+ /**
+ * Tests if address is self-assigned for IPv4.
+ */
+ @Test
+ public void testIsSelfAssignedIpv4() {
+ IpAddress normalIP = IpAddress.valueOf("10.0.0.1");
+ IpAddress selfAssignedIP = IpAddress.valueOf("169.1.2.3");
+ assertFalse(normalIP.isSelfAssigned());
+ assertTrue(selfAssignedIP.isSelfAssigned());
+ }
+
+ /**
+ * Tests comparison of {@link IpAddress} for IPv4.
+ */
+ @Test
+ public void testComparisonIPv4() {
+ IpAddress addr1, addr2, addr3, addr4;
+
+ addr1 = IpAddress.valueOf("1.2.3.4");
+ addr2 = IpAddress.valueOf("1.2.3.4");
+ addr3 = IpAddress.valueOf("1.2.3.3");
+ addr4 = IpAddress.valueOf("1.2.3.5");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+
+ addr1 = IpAddress.valueOf("255.2.3.4");
+ addr2 = IpAddress.valueOf("255.2.3.4");
+ addr3 = IpAddress.valueOf("255.2.3.3");
+ addr4 = IpAddress.valueOf("255.2.3.5");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+ }
+
+ /**
+ * Tests comparison of {@link IpAddress} for IPv6.
+ */
+ @Test
+ public void testComparisonIPv6() {
+ IpAddress addr1, addr2, addr3, addr4;
+
+ addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ addr3 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8887");
+ addr4 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8889");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+
+ addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
+ addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
+ addr3 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887");
+ addr4 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+
+ addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
+ addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
+ addr3 = IpAddress.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888");
+ addr4 = IpAddress.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888");
+ assertTrue(addr1.compareTo(addr2) == 0);
+ assertTrue(addr1.compareTo(addr3) > 0);
+ assertTrue(addr1.compareTo(addr4) < 0);
+ }
+
+ /**
+ * Tests equality of {@link IpAddress} for IPv4.
+ */
+ @Test
+ public void testEqualityIPv4() {
+ new EqualsTester()
+ .addEqualityGroup(IpAddress.valueOf("1.2.3.4"),
+ IpAddress.valueOf("1.2.3.4"))
+ .addEqualityGroup(IpAddress.valueOf("1.2.3.5"),
+ IpAddress.valueOf("1.2.3.5"))
+ .addEqualityGroup(IpAddress.valueOf("0.0.0.0"),
+ IpAddress.valueOf("0.0.0.0"))
+ .addEqualityGroup(IpAddress.valueOf("255.255.255.255"),
+ IpAddress.valueOf("255.255.255.255"))
+ .testEquals();
+ }
+
+ /**
+ * Tests equality of {@link IpAddress} for IPv6.
+ */
+ @Test
+ public void testEqualityIPv6() {
+ new EqualsTester()
+ .addEqualityGroup(
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"),
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"))
+ .addEqualityGroup(
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"),
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"))
+ .addEqualityGroup(
+ IpAddress.valueOf("::"),
+ IpAddress.valueOf("::"))
+ .addEqualityGroup(
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))
+ .testEquals();
+ }
+
+ /**
+ * Tests object string representation for IPv4.
+ */
+ @Test
+ public void testToStringIPv4() {
+ IpAddress ipAddress;
+
+ ipAddress = IpAddress.valueOf("1.2.3.4");
+ assertThat(ipAddress.toString(), is("1.2.3.4"));
+
+ ipAddress = IpAddress.valueOf("0.0.0.0");
+ assertThat(ipAddress.toString(), is("0.0.0.0"));
+
+ ipAddress = IpAddress.valueOf("255.255.255.255");
+ assertThat(ipAddress.toString(), is("255.255.255.255"));
+ }
+
+ /**
+ * Tests object string representation for IPv6.
+ */
+ @Test
+ public void testToStringIPv6() {
+ IpAddress ipAddress;
+
+ ipAddress =
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ assertThat(ipAddress.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888"));
+
+ ipAddress = IpAddress.valueOf("1111::8888");
+ assertThat(ipAddress.toString(), is("1111::8888"));
+
+ ipAddress = IpAddress.valueOf("1111::");
+ assertThat(ipAddress.toString(), is("1111::"));
+
+ ipAddress = IpAddress.valueOf("::8888");
+ assertThat(ipAddress.toString(), is("::8888"));
+
+ ipAddress = IpAddress.valueOf("::");
+ assertThat(ipAddress.toString(), is("::"));
+
+ ipAddress =
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ assertThat(ipAddress.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
+
+ ipAddress =
+ IpAddress.valueOf("::1111:2222");
+ assertThat(ipAddress.toString(),
+ is("::1111:2222"));
+
+ ipAddress =
+ IpAddress.valueOf("1:0:0:1:0:0:2:3");
+ assertThat(ipAddress.toString(),
+ is("1::1:0:0:2:3"));
+
+ ipAddress =
+ IpAddress.valueOf("::0123:0004");
+ assertThat(ipAddress.toString(),
+ is("::123:4"));
+
+ ipAddress =
+ IpAddress.valueOf("0:0:1:1:0:0:1:1");
+ assertThat(ipAddress.toString(),
+ is("::1:1:0:0:1:1"));
+
+ ipAddress =
+ IpAddress.valueOf("1:1a2b::");
+ assertThat(ipAddress.toString(),
+ is("1:1a2b::"));
+
+ ipAddress =
+ IpAddress.valueOf("0:0:00:00:0000:00:00:000");
+ assertThat(ipAddress.toString(),
+ is("::"));
+
+ ipAddress =
+ IpAddress.valueOf("0:0:0:1:0:0:0:0");
+ assertThat(ipAddress.toString(),
+ is("0:0:0:1::"));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java
new file mode 100644
index 00000000..052a4cbb
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java
@@ -0,0 +1,1080 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import org.junit.Test;
+
+import com.google.common.testing.EqualsTester;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
+
+/**
+ * Tests for class {@link IpPrefix}.
+ */
+public class IpPrefixTest {
+ /**
+ * Tests the immutability of {@link IpPrefix}.
+ */
+ @Test
+ public void testImmutable() {
+ assertThatClassIsImmutableBaseClass(IpPrefix.class);
+ }
+
+ /**
+ * Tests the maximum mask length.
+ */
+ @Test
+ public void testMaxMaskLength() {
+ assertThat(IpPrefix.MAX_INET_MASK_LENGTH, is(32));
+ assertThat(IpPrefix.MAX_INET6_MASK_LENGTH, is(128));
+ }
+
+ /**
+ * Tests returning the IP version of the prefix.
+ */
+ @Test
+ public void testVersion() {
+ IpPrefix ipPrefix;
+
+ // IPv4
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertThat(ipPrefix.version(), is(IpAddress.Version.INET));
+
+ // IPv6
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertThat(ipPrefix.version(), is(IpAddress.Version.INET6));
+ }
+
+ /**
+ * Tests whether the IP version of a prefix is IPv4.
+ */
+ @Test
+ public void testIsIp4() {
+ IpPrefix ipPrefix;
+
+ // IPv4
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertTrue(ipPrefix.isIp4());
+
+ // IPv6
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertFalse(ipPrefix.isIp4());
+ }
+
+ /**
+ * Tests whether the IP version of a prefix is IPv6.
+ */
+ @Test
+ public void testIsIp6() {
+ IpPrefix ipPrefix;
+
+ // IPv4
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertFalse(ipPrefix.isIp6());
+
+ // IPv6
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertTrue(ipPrefix.isIp6());
+ }
+
+ /**
+ * Tests returning the IP address value and IP address prefix length of
+ * an IPv4 prefix.
+ */
+ @Test
+ public void testAddressAndPrefixLengthIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.0/24");
+ assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0")));
+ assertThat(ipPrefix.prefixLength(), is(24));
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.4/24");
+ assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0")));
+ assertThat(ipPrefix.prefixLength(), is(24));
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.4/32");
+ assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.4")));
+ assertThat(ipPrefix.prefixLength(), is(32));
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.5/32");
+ assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.5")));
+ assertThat(ipPrefix.prefixLength(), is(32));
+
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("0.0.0.0")));
+ assertThat(ipPrefix.prefixLength(), is(0));
+
+ ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
+ assertThat(ipPrefix.address(),
+ equalTo(IpAddress.valueOf("255.255.255.255")));
+ assertThat(ipPrefix.prefixLength(), is(32));
+ }
+
+ /**
+ * Tests returning the IP address value and IP address prefix length of
+ * an IPv6 prefix.
+ */
+ @Test
+ public void testAddressAndPrefixLengthIPv6() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1100::/8");
+ assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::")));
+ assertThat(ipPrefix.prefixLength(), is(8));
+
+ ipPrefix =
+ IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
+ assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::")));
+ assertThat(ipPrefix.prefixLength(), is(8));
+
+ ipPrefix =
+ IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8800/120");
+ assertThat(ipPrefix.address(),
+ equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8800")));
+ assertThat(ipPrefix.prefixLength(), is(120));
+
+ ipPrefix =
+ IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128");
+ assertThat(ipPrefix.address(),
+ equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885")));
+ assertThat(ipPrefix.prefixLength(), is(128));
+
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("::")));
+ assertThat(ipPrefix.prefixLength(), is(0));
+
+ ipPrefix =
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertThat(ipPrefix.address(),
+ equalTo(IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+ assertThat(ipPrefix.prefixLength(), is(128));
+
+ ipPrefix =
+ IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/64");
+ assertThat(ipPrefix.address(),
+ equalTo(IpAddress.valueOf("1111:2222:3333:4444::")));
+ assertThat(ipPrefix.prefixLength(), is(64));
+ }
+
+ /**
+ * Tests getting the Ip4Prefix and Ip6Prefix view of the IP prefix.
+ */
+ @Test
+ public void testGetIp4AndIp6PrefixView() {
+ IpPrefix ipPrefix;
+ Ip4Prefix ip4Prefix;
+ Ip6Prefix ip6Prefix;
+
+ // Pure IPv4 IpPrefix
+ ipPrefix = IpPrefix.valueOf("1.2.3.0/24");
+ ip4Prefix = ipPrefix.getIp4Prefix();
+ ip6Prefix = ipPrefix.getIp6Prefix();
+ assertThat(ip4Prefix.toString(), is("1.2.3.0/24"));
+ assertNull(ip6Prefix);
+
+ // IPv4 IpPrefix that is Ip4Prefix
+ ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24");
+ ip4Prefix = ipPrefix.getIp4Prefix();
+ ip6Prefix = ipPrefix.getIp6Prefix();
+ assertThat(ip4Prefix.toString(), is("1.2.3.0/24"));
+ assertNull(ip6Prefix);
+
+ // Pure IPv6 IpPrefix
+ ipPrefix = IpPrefix.valueOf("1111:2222::/64");
+ ip4Prefix = ipPrefix.getIp4Prefix();
+ ip6Prefix = ipPrefix.getIp6Prefix();
+ assertNull(ip4Prefix);
+ assertThat(ip6Prefix.toString(), is("1111:2222::/64"));
+
+ // IPv6 IpPrefix that is Ip6Prefix
+ ipPrefix = Ip6Prefix.valueOf("1111:2222::/64");
+ ip4Prefix = ipPrefix.getIp4Prefix();
+ ip6Prefix = ipPrefix.getIp6Prefix();
+ assertNull(ip4Prefix);
+ assertThat(ip6Prefix.toString(), is("1111:2222::/64"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 integer value.
+ */
+ @Test
+ public void testValueOfForIntegerIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf(0x01020304, 24);
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = IpPrefix.valueOf(0x01020304, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
+
+ ipPrefix = IpPrefix.valueOf(0x01020305, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
+
+ ipPrefix = IpPrefix.valueOf(0, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = IpPrefix.valueOf(0, 32);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
+
+ ipPrefix = IpPrefix.valueOf(0xffffffff, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = IpPrefix.valueOf(0xffffffff, 16);
+ assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
+
+ ipPrefix = IpPrefix.valueOf(0xffffffff, 32);
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 integer value and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf(0x01020304, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 integer value and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf(0x01020304, 33);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 byte array.
+ */
+ @Test
+ public void testValueOfByteArrayIPv4() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24);
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
+
+ value = new byte[] {1, 2, 3, 5};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
+
+ value = new byte[] {0, 0, 0, 0};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 16);
+ assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
+
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 byte array.
+ */
+ @Test
+ public void testValueOfByteArrayIPv6() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77, (byte) 0x88, (byte) 0x88};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120);
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
+
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128);
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
+
+ value = new byte[] {0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0);
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128);
+ assertThat(ipPrefix.toString(), is("::/128"));
+
+ value = new byte[] {(byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0);
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 64);
+ assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
+
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128);
+ assertThat(ipPrefix.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null array for IPv4.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullArrayIPv4() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = null;
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null array for IPv6.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullArrayIPv6() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = null;
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a short array for IPv4.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfShortArrayIPv4() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a short array for IPv6.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfShortArrayIPv6() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 byte array and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 byte array and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77, (byte) 0x88, (byte) 0x88};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 byte array and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {1, 2, 3, 4};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 33);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 byte array and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() {
+ IpPrefix ipPrefix;
+ byte[] value;
+
+ value = new byte[] {0x11, 0x11, 0x22, 0x22,
+ 0x33, 0x33, 0x44, 0x44,
+ 0x55, 0x55, 0x66, 0x66,
+ 0x77, 0x77, (byte) 0x88, (byte) 0x88};
+ ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 129);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 address.
+ */
+ @Test
+ public void testValueOfAddressIPv4() {
+ IpAddress ipAddress;
+ IpPrefix ipPrefix;
+
+ ipAddress = IpAddress.valueOf("1.2.3.4");
+ ipPrefix = IpPrefix.valueOf(ipAddress, 24);
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = IpPrefix.valueOf(ipAddress, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
+
+ ipAddress = IpAddress.valueOf("1.2.3.5");
+ ipPrefix = IpPrefix.valueOf(ipAddress, 32);
+ assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
+
+ ipAddress = IpAddress.valueOf("0.0.0.0");
+ ipPrefix = IpPrefix.valueOf(ipAddress, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = IpPrefix.valueOf(ipAddress, 32);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
+
+ ipAddress = IpAddress.valueOf("255.255.255.255");
+ ipPrefix = IpPrefix.valueOf(ipAddress, 0);
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = IpPrefix.valueOf(ipAddress, 16);
+ assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
+
+ ipPrefix = IpPrefix.valueOf(ipAddress, 32);
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 address.
+ */
+ @Test
+ public void testValueOfAddressIPv6() {
+ IpAddress ipAddress;
+ IpPrefix ipPrefix;
+
+ ipAddress =
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ ipPrefix = IpPrefix.valueOf(ipAddress, 120);
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
+
+ ipPrefix = IpPrefix.valueOf(ipAddress, 128);
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
+
+ ipAddress = IpAddress.valueOf("::");
+ ipPrefix = IpPrefix.valueOf(ipAddress, 0);
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = IpPrefix.valueOf(ipAddress, 128);
+ assertThat(ipPrefix.toString(), is("::/128"));
+
+ ipAddress =
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
+ ipPrefix = IpPrefix.valueOf(ipAddress, 0);
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = IpPrefix.valueOf(ipAddress, 64);
+ assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
+
+ ipPrefix = IpPrefix.valueOf(ipAddress, 128);
+ assertThat(ipPrefix.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null IP address.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullAddress() {
+ IpAddress ipAddress;
+ IpPrefix ipPrefix;
+
+ ipAddress = null;
+ ipPrefix = IpPrefix.valueOf(ipAddress, 24);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 address and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfAddressNegativePrefixLengthIPv4() {
+ IpAddress ipAddress;
+ IpPrefix ipPrefix;
+
+ ipAddress = IpAddress.valueOf("1.2.3.4");
+ ipPrefix = IpPrefix.valueOf(ipAddress, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 address and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfAddressNegativePrefixLengthIPv6() {
+ IpAddress ipAddress;
+ IpPrefix ipPrefix;
+
+ ipAddress =
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ ipPrefix = IpPrefix.valueOf(ipAddress, -1);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 address and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() {
+ IpAddress ipAddress;
+ IpPrefix ipPrefix;
+
+ ipAddress = IpAddress.valueOf("1.2.3.4");
+ ipPrefix = IpPrefix.valueOf(ipAddress, 33);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 address and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfAddressTooLongPrefixLengthIPv6() {
+ IpAddress ipAddress;
+ IpPrefix ipPrefix;
+
+ ipAddress =
+ IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
+ ipPrefix = IpPrefix.valueOf(ipAddress, 129);
+ }
+
+ /**
+ * Tests valueOf() converter for IPv4 string.
+ */
+ @Test
+ public void testValueOfStringIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.4/24");
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.4/32");
+ assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.5/32");
+ assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
+
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/32");
+ assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
+
+ ipPrefix = IpPrefix.valueOf("255.255.255.255/0");
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = IpPrefix.valueOf("255.255.255.255/16");
+ assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
+
+ ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+
+ /**
+ * Tests valueOf() converter for IPv6 string.
+ */
+ @Test
+ public void testValueOfStringIPv6() {
+ IpPrefix ipPrefix;
+
+ ipPrefix =
+ IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/120");
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
+
+ ipPrefix =
+ IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/128");
+ assertThat(ipPrefix.toString(),
+ is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
+
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = IpPrefix.valueOf("::/128");
+ assertThat(ipPrefix.toString(), is("::/128"));
+
+ ipPrefix =
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0");
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix =
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64");
+ assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
+
+ ipPrefix =
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertThat(ipPrefix.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
+ }
+
+ /**
+ * Tests invalid valueOf() converter for a null string.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testInvalidValueOfNullString() {
+ IpPrefix ipPrefix;
+ String fromString;
+
+ fromString = null;
+ ipPrefix = IpPrefix.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an empty string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfEmptyString() {
+ IpPrefix ipPrefix;
+ String fromString;
+
+ fromString = "";
+ ipPrefix = IpPrefix.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for an incorrect string.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfIncorrectString() {
+ IpPrefix ipPrefix;
+ String fromString;
+
+ fromString = "NoSuchIpPrefix";
+ ipPrefix = IpPrefix.valueOf(fromString);
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 string and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfStringNegativePrefixLengthIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.4/-1");
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 string and
+ * negative prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfStringNegativePrefixLengthIPv6() {
+ IpPrefix ipPrefix;
+
+ ipPrefix =
+ IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/-1");
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv4 string and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfStringTooLongPrefixLengthIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.4/33");
+ }
+
+ /**
+ * Tests invalid valueOf() converter for IPv6 string and
+ * too long prefix length.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidValueOfStringTooLongPrefixLengthIPv6() {
+ IpPrefix ipPrefix;
+
+ ipPrefix =
+ IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/129");
+ }
+
+ /**
+ * Tests IP prefix contains another IP prefix for IPv4.
+ */
+ @Test
+ public void testContainsIpPrefixIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1.2.0.0/24");
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
+
+ ipPrefix = IpPrefix.valueOf("1.2.0.0/32");
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
+
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
+
+ ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
+
+ // Test when there is a mistmatch in the compared IP address families
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1111:2222:3333:4444::/120")));
+ ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
+ }
+
+ /**
+ * Tests IP prefix contains another IP prefix for IPv6.
+ */
+ @Test
+ public void testContainsIpPrefixIPv6() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120");
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/120")));
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/128")));
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/64")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4445::/120")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
+
+ ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128");
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/120")));
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/128")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/64")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4445::/120")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
+
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/120")));
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/128")));
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/64")));
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4445::/120")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/64")));
+ assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/0")));
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
+
+ ipPrefix =
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/120")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/128")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4444::/64")));
+ assertFalse(ipPrefix.contains(
+ IpPrefix.valueOf("1111:2222:3333:4445::/120")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64")));
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
+ assertTrue(ipPrefix.contains(
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
+
+ // Test when there is a mistmatch in the compared IP address families
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
+ ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
+ }
+
+ /**
+ * Tests IP prefix contains IP address for IPv4.
+ */
+ @Test
+ public void testContainsIpAddressIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1.2.0.0/24");
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
+
+ ipPrefix = IpPrefix.valueOf("1.2.0.0/32");
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
+
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
+
+ ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
+
+ // Test when there is a mistmatch in the compared IP address families
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("1111:2222:3333:4444::")));
+ ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+ }
+
+ /**
+ * Tests IP prefix contains IP address for IPv6.
+ */
+ @Test
+ public void testContainsIpAddressIPv6() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120");
+ assertTrue(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4444::")));
+ assertTrue(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4444::1")));
+ assertFalse(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4445::")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
+ assertFalse(ipPrefix.contains(
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+
+ ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128");
+ assertTrue(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4444::")));
+ assertFalse(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4444::1")));
+ assertFalse(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4445::")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
+ assertFalse(ipPrefix.contains(
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertTrue(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4444::")));
+ assertTrue(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4444::1")));
+ assertTrue(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4445::")));
+ assertTrue(ipPrefix.contains(IpAddress.valueOf("::")));
+ assertTrue(ipPrefix.contains(
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+
+ ipPrefix =
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertFalse(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4444::")));
+ assertFalse(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4444::1")));
+ assertFalse(ipPrefix.contains(
+ IpAddress.valueOf("1111:2222:3333:4445::")));
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
+ assertTrue(ipPrefix.contains(
+ IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
+
+ // Test when there is a mistmatch in the compared IP address families
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
+ ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
+ }
+
+ /**
+ * Tests equality of {@link IpPrefix} for IPv4.
+ */
+ @Test
+ public void testEqualityIPv4() {
+ new EqualsTester()
+ .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/24"),
+ IpPrefix.valueOf("1.2.0.0/24"),
+ IpPrefix.valueOf("1.2.0.4/24"))
+ .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/16"),
+ IpPrefix.valueOf("1.2.0.0/16"))
+ .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/32"),
+ IpPrefix.valueOf("1.2.0.0/32"))
+ .addEqualityGroup(IpPrefix.valueOf("1.3.0.0/24"),
+ IpPrefix.valueOf("1.3.0.0/24"))
+ .addEqualityGroup(IpPrefix.valueOf("0.0.0.0/0"),
+ IpPrefix.valueOf("0.0.0.0/0"))
+ .addEqualityGroup(IpPrefix.valueOf("255.255.255.255/32"),
+ IpPrefix.valueOf("255.255.255.255/32"))
+ .testEquals();
+ }
+
+ /**
+ * Tests equality of {@link IpPrefix} for IPv6.
+ */
+ @Test
+ public void testEqualityIPv6() {
+ new EqualsTester()
+ .addEqualityGroup(
+ IpPrefix.valueOf("1111:2222:3333:4444::/120"),
+ IpPrefix.valueOf("1111:2222:3333:4444::1/120"),
+ IpPrefix.valueOf("1111:2222:3333:4444::/120"))
+ .addEqualityGroup(
+ IpPrefix.valueOf("1111:2222:3333:4444::/64"),
+ IpPrefix.valueOf("1111:2222:3333:4444::/64"))
+ .addEqualityGroup(
+ IpPrefix.valueOf("1111:2222:3333:4444::/128"),
+ IpPrefix.valueOf("1111:2222:3333:4444::/128"))
+ .addEqualityGroup(
+ IpPrefix.valueOf("1111:2222:3333:4445::/64"),
+ IpPrefix.valueOf("1111:2222:3333:4445::/64"))
+ .addEqualityGroup(
+ IpPrefix.valueOf("::/0"),
+ IpPrefix.valueOf("::/0"))
+ .addEqualityGroup(
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"),
+ IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))
+ .testEquals();
+ }
+
+ /**
+ * Tests object string representation for IPv4.
+ */
+ @Test
+ public void testToStringIPv4() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.0/24");
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = IpPrefix.valueOf("1.2.3.4/24");
+ assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
+
+ ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
+ assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
+
+ ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
+ assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
+ }
+
+ /**
+ * Tests object string representation for IPv6.
+ */
+ @Test
+ public void testToStringIPv6() {
+ IpPrefix ipPrefix;
+
+ ipPrefix = IpPrefix.valueOf("1100::/8");
+ assertThat(ipPrefix.toString(), is("1100::/8"));
+
+ ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
+ assertThat(ipPrefix.toString(), is("1100::/8"));
+
+ ipPrefix = IpPrefix.valueOf("::/0");
+ assertThat(ipPrefix.toString(), is("::/0"));
+
+ ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
+ assertThat(ipPrefix.toString(),
+ is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLCTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLCTest.java
new file mode 100644
index 00000000..39bb72f4
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLCTest.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for LLC class.
+ */
+public class LLCTest {
+
+ private Deserializer<LLC> deserializer;
+
+ private byte dsap = 10;
+ private byte ssap = 20;
+ private byte ctrl = 30;
+
+ private byte[] bytes;
+
+ @Before
+ public void setUp() throws Exception {
+ deserializer = LLC.deserializer();
+
+ ByteBuffer bb = ByteBuffer.allocate(LLC.LLC_HEADER_LENGTH);
+
+ bb.put(dsap);
+ bb.put(ssap);
+ bb.put(ctrl);
+
+ bytes = bb.array();
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(deserializer, bytes);
+ }
+
+ @Test
+ public void testDeserialize() throws Exception {
+ LLC llc = deserializer.deserialize(bytes, 0, bytes.length);
+
+ assertEquals(dsap, llc.getDsap());
+ assertEquals(ssap, llc.getSsap());
+ assertEquals(ctrl, llc.getCtrl());
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLDPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLDPTest.java
new file mode 100644
index 00000000..95b0b04a
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLDPTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit tests for the LLDP class.
+ */
+public class LLDPTest {
+
+ private Deserializer<LLDP> deserializer;
+
+ private byte[] chassisValue = new byte[] {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7};
+ private byte[] portValue = new byte[] {0x1, 0x2, 0x3, 0x4, 0x5};
+ private byte[] ttlValue = new byte[] {0x0, 0x20};
+
+ private short optionalTlvSize = 6;
+ private byte[] optionalTlvValue = new byte[] {0x6, 0x5, 0x4, 0x3, 0x2, 0x1};
+
+ private byte[] bytes;
+
+ @Before
+ public void setUp() throws Exception {
+ deserializer = LLDP.deserializer();
+
+ // Each TLV is 2 bytes for the type+length, plus the size of the value
+ // There are 2 zero-bytes at the end
+ ByteBuffer bb = ByteBuffer.allocate(2 + LLDP.CHASSIS_TLV_SIZE +
+ 2 + LLDP.PORT_TLV_SIZE +
+ 2 + LLDP.TTL_TLV_SIZE +
+ 2 + optionalTlvSize +
+ 2);
+
+ // Chassis TLV
+ bb.putShort(getTypeLength(LLDP.CHASSIS_TLV_TYPE, LLDP.CHASSIS_TLV_SIZE));
+ bb.put(chassisValue);
+
+ // Port TLV
+ bb.putShort(getTypeLength(LLDP.PORT_TLV_TYPE, LLDP.PORT_TLV_SIZE));
+ bb.put(portValue);
+
+ // TTL TLV
+ bb.putShort(getTypeLength(LLDP.TTL_TLV_TYPE, LLDP.TTL_TLV_SIZE));
+ bb.put(ttlValue);
+
+ // Optional TLV
+ bb.putShort(getTypeLength(LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE, optionalTlvSize));
+ bb.put(optionalTlvValue);
+
+ bb.putShort((short) 0);
+
+ bytes = bb.array();
+
+ }
+
+ private short getTypeLength(byte type, short length) {
+ return (short) ((0x7f & type) << 9 | 0x1ff & length);
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(deserializer, bytes);
+ }
+
+ @Test
+ public void testDeserialize() throws Exception {
+ LLDP lldp = deserializer.deserialize(bytes, 0, bytes.length);
+
+ assertEquals(LLDP.CHASSIS_TLV_TYPE, lldp.getChassisId().getType());
+ assertEquals(LLDP.CHASSIS_TLV_SIZE, lldp.getChassisId().getLength());
+ assertTrue(Arrays.equals(chassisValue, lldp.getChassisId().getValue()));
+
+ assertEquals(LLDP.PORT_TLV_TYPE, lldp.getPortId().getType());
+ assertEquals(LLDP.PORT_TLV_SIZE, lldp.getPortId().getLength());
+ assertTrue(Arrays.equals(portValue, lldp.getPortId().getValue()));
+
+ assertEquals(LLDP.TTL_TLV_TYPE, lldp.getTtl().getType());
+ assertEquals(LLDP.TTL_TLV_SIZE, lldp.getTtl().getLength());
+ assertTrue(Arrays.equals(ttlValue, lldp.getTtl().getValue()));
+
+ assertEquals(1, lldp.getOptionalTLVList().size());
+ LLDPTLV optionalTlv = lldp.getOptionalTLVList().get(0);
+
+ assertEquals(LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE, optionalTlv.getType());
+ assertEquals(optionalTlvSize, optionalTlv.getLength());
+ assertTrue(Arrays.equals(optionalTlvValue, optionalTlv.getValue()));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/MplsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/MplsTest.java
new file mode 100644
index 00000000..2ab8ff98
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/MplsTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for MPLS class.
+ */
+public class MplsTest {
+
+ private Deserializer<MPLS> deserializer;
+
+ private int label = 1048575;
+ private byte bos = 1;
+ private byte ttl = 20;
+ private byte protocol = MPLS.PROTOCOL_IPV4;
+
+ private byte[] bytes;
+
+ @Before
+ public void setUp() throws Exception {
+ // Replace normal deserializer map with an empty map. This will cause
+ // the DataDeserializer to be used which will silently handle 0-byte input.
+ MPLS.protocolDeserializerMap = new HashMap<>();
+
+ deserializer = MPLS.deserializer();
+
+ ByteBuffer bb = ByteBuffer.allocate(MPLS.HEADER_LENGTH);
+ bb.putInt(((label & 0x000fffff) << 12) | ((bos & 0x1) << 8 | (ttl & 0xff)));
+
+ bytes = bb.array();
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(deserializer, bytes);
+ }
+
+ @Test
+ public void testDeserialize() throws Exception {
+ MPLS mpls = deserializer.deserialize(bytes, 0, bytes.length);
+
+ assertEquals(label, mpls.label);
+ assertEquals(bos, mpls.bos);
+ assertEquals(ttl, mpls.ttl);
+ assertEquals(protocol, mpls.protocol);
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PacketTestUtils.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PacketTestUtils.java
new file mode 100644
index 00000000..209b1d27
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PacketTestUtils.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import java.nio.ByteBuffer;
+
+import static junit.framework.TestCase.fail;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Utilities for testing packet methods.
+ */
+public final class PacketTestUtils {
+
+ private PacketTestUtils() {
+ }
+
+ /**
+ * Tests that the Deserializer function is resilient to bad input parameters
+ * such as null input, negative offset and length, etc.
+ *
+ * @param deserializer deserializer function to test
+ */
+ public static void testDeserializeBadInput(Deserializer deserializer) {
+ byte[] bytes = ByteBuffer.allocate(4).array();
+
+ try {
+ deserializer.deserialize(null, 0, 4);
+ fail("NullPointerException was not thrown");
+ } catch (NullPointerException e) {
+ assertTrue(true);
+ } catch (DeserializationException e) {
+ fail("NullPointerException was not thrown");
+ }
+
+ // input byte array length, offset and length don't make sense
+ expectDeserializationException(deserializer, bytes, -1, 0);
+ expectDeserializationException(deserializer, bytes, 0, -1);
+ expectDeserializationException(deserializer, bytes, 0, 5);
+ expectDeserializationException(deserializer, bytes, 2, 3);
+ expectDeserializationException(deserializer, bytes, 5, 0);
+ }
+
+ /**
+ * Tests that the Deserializer function is resilient to truncated input, or
+ * cases where the input byte array does not contain enough bytes to
+ * deserialize the packet.
+ *
+ * @param deserializer deserializer function to test
+ * @param header byte array of a full-size packet
+ */
+ public static void testDeserializeTruncated(Deserializer deserializer,
+ byte[] header) {
+ byte[] truncated;
+
+ for (int i = 0; i < header.length; i++) {
+ truncated = new byte[i];
+
+ ByteBuffer.wrap(header).get(truncated);
+
+ expectDeserializationException(deserializer, truncated, 0, truncated.length);
+ }
+ }
+
+ /**
+ * Run the given deserializer function against the given inputs and verify
+ * that a DeserializationException is thrown. The the test will fail if a
+ * DeserializationException is not thrown by the deserializer function.
+ *
+ * @param deserializer deserializer function to test
+ * @param bytes input byte array
+ * @param offset input offset
+ * @param length input length
+ */
+ public static void expectDeserializationException(Deserializer deserializer,
+ byte[] bytes, int offset, int length) {
+ try {
+ deserializer.deserialize(bytes, offset, length);
+ fail("DeserializationException was not thrown");
+ } catch (DeserializationException e) {
+ assertTrue(true);
+ }
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/TCPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/TCPTest.java
new file mode 100644
index 00000000..faab5c2c
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/TCPTest.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+
+package org.onlab.packet;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link TCP}.
+ */
+public class TCPTest {
+ private static final byte[] IPV4_SOURCE_ADDRESS = {
+ (byte) 192, (byte) 168, (byte) 1, (byte) 1
+ };
+ private static final byte[] IPV4_DESTINATION_ADDRESS = {
+ (byte) 192, (byte) 168, (byte) 1, (byte) 2
+ };
+ private static final byte[] IPV6_SOURCE_ADDRESS = {
+ (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
+ };
+ private static final byte[] IPV6_DESTINATION_ADDRESS = {
+ (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
+ };
+
+ private static IPv4 ipv4 = new IPv4();
+ private static IPv6 ipv6 = new IPv6();
+ private static byte[] bytePacketTCP4 = {
+ (byte) 0x00, (byte) 0x50, (byte) 0x00, (byte) 0x60, // src,dst port
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, // seq
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, // ack
+ (byte) 0x50, (byte) 0x02, // offset,flag
+ (byte) 0x10, (byte) 0x00, // window
+ (byte) 0x1b, (byte) 0xae, // checksum
+ (byte) 0x00, (byte) 0x01 // urgent
+ };
+ private static byte[] bytePacketTCP6 = {
+ (byte) 0x00, (byte) 0x50, (byte) 0x00, (byte) 0x60, // src,dst port
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, // seq
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, // ack
+ (byte) 0x50, (byte) 0x02, // offset,flag
+ (byte) 0x10, (byte) 0x00, // window
+ (byte) 0xa1, (byte) 0xfd, // checksum
+ (byte) 0x00, (byte) 0x01 // urgent
+ };
+
+ private static Deserializer<TCP> deserializer;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ deserializer = TCP.deserializer();
+
+ ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS));
+ ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS));
+ ipv4.setProtocol(IPv4.PROTOCOL_TCP);
+
+ ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
+ ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
+ ipv6.setNextHeader(IPv6.PROTOCOL_TCP);
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ TCP tcp = new TCP();
+ tcp.setSourcePort(0x50);
+ tcp.setDestinationPort(0x60);
+ tcp.setSequence(0x10);
+ tcp.setAcknowledge(0x20);
+ tcp.setDataOffset((byte) 0x5);
+ tcp.setFlags((short) 0x2);
+ tcp.setWindowSize((short) 0x1000);
+ tcp.setUrgentPointer((short) 0x1);
+
+ tcp.setParent(ipv4);
+ assertArrayEquals(bytePacketTCP4, tcp.serialize());
+ tcp.resetChecksum();
+ tcp.setParent(ipv6);
+ assertArrayEquals(bytePacketTCP6, tcp.serialize());
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(deserializer, bytePacketTCP4);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws Exception {
+ TCP tcp = deserializer.deserialize(bytePacketTCP4, 0, bytePacketTCP4.length);
+
+ assertThat(tcp.getSourcePort(), is(0x50));
+ assertThat(tcp.getDestinationPort(), is(0x60));
+ assertThat(tcp.getSequence(), is(0x10));
+ assertThat(tcp.getAcknowledge(), is(0x20));
+ assertThat(tcp.getDataOffset(), is((byte) 0x5));
+ assertThat(tcp.getFlags(), is((short) 0x2));
+ assertThat(tcp.getWindowSize(), is((short) 0x1000));
+ assertThat(tcp.getUrgentPointer(), is((short) 0x1));
+ assertThat(tcp.getChecksum(), is((short) 0x1bae));
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ TCP tcp1 = new TCP();
+ tcp1.setSourcePort(0x50);
+ tcp1.setDestinationPort(0x60);
+ tcp1.setSequence(0x10);
+ tcp1.setAcknowledge(0x20);
+ tcp1.setDataOffset((byte) 0x5);
+ tcp1.setFlags((short) 0x2);
+ tcp1.setWindowSize((short) 0x1000);
+ tcp1.setUrgentPointer((short) 0x1);
+
+ TCP tcp2 = new TCP();
+ tcp2.setSourcePort(0x70);
+ tcp2.setDestinationPort(0x60);
+ tcp2.setSequence(0x10);
+ tcp2.setAcknowledge(0x20);
+ tcp2.setDataOffset((byte) 0x5);
+ tcp2.setFlags((short) 0x2);
+ tcp2.setWindowSize((short) 0x1000);
+ tcp2.setUrgentPointer((short) 0x1);
+
+ assertTrue(tcp1.equals(tcp1));
+ assertFalse(tcp1.equals(tcp2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/UDPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/UDPTest.java
new file mode 100644
index 00000000..ba453f61
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/UDPTest.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+
+package org.onlab.packet;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link UDP}.
+ */
+public class UDPTest {
+ private static final byte[] IPV4_SOURCE_ADDRESS = {
+ (byte) 192, (byte) 168, (byte) 1, (byte) 1
+ };
+ private static final byte[] IPV4_DESTINATION_ADDRESS = {
+ (byte) 192, (byte) 168, (byte) 1, (byte) 2
+ };
+ private static final byte[] IPV6_SOURCE_ADDRESS = {
+ (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
+ };
+ private static final byte[] IPV6_DESTINATION_ADDRESS = {
+ (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
+ };
+
+ private static IPv4 ipv4 = new IPv4();
+ private static IPv6 ipv6 = new IPv6();
+ private static byte[] bytePacketUDP4 = {
+ (byte) 0x00, (byte) 0x50, // src port
+ (byte) 0x00, (byte) 0x60, // dst port
+ (byte) 0x00, (byte) 0x08, // length
+ (byte) 0x7b, (byte) 0xda, // checksum
+ };
+ private static byte[] bytePacketUDP6 = {
+ (byte) 0x00, (byte) 0x50, // src port
+ (byte) 0x00, (byte) 0x60, // dst port
+ (byte) 0x00, (byte) 0x08, // length
+ (byte) 0x02, (byte) 0x2a, // checksum
+ };
+
+ private static Deserializer<UDP> deserializer;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ deserializer = UDP.deserializer();
+
+ ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS));
+ ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS));
+ ipv4.setProtocol(IPv4.PROTOCOL_UDP);
+
+ ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
+ ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
+ ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ UDP udp = new UDP();
+ udp.setSourcePort(0x50);
+ udp.setDestinationPort(0x60);
+
+ udp.setParent(ipv4);
+ assertArrayEquals(bytePacketUDP4, udp.serialize());
+ udp.resetChecksum();
+ udp.setParent(ipv6);
+ assertArrayEquals(bytePacketUDP6, udp.serialize());
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(deserializer);
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ PacketTestUtils.testDeserializeTruncated(deserializer, bytePacketUDP4);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws Exception {
+ UDP udp = deserializer.deserialize(bytePacketUDP4, 0, bytePacketUDP4.length);
+
+ assertThat(udp.getSourcePort(), is(0x50));
+ assertThat(udp.getDestinationPort(), is(0x60));
+ assertThat(udp.getLength(), is((short) 8));
+ assertThat(udp.getChecksum(), is((short) 0x7bda));
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ UDP udp1 = new UDP();
+ udp1.setSourcePort(0x50);
+ udp1.setDestinationPort(0x60);
+
+ UDP udp2 = new UDP();
+ udp2.setSourcePort(0x70);
+ udp2.setDestinationPort(0x60);
+
+ assertTrue(udp1.equals(udp1));
+ assertFalse(udp1.equals(udp2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/VlanIdTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/VlanIdTest.java
new file mode 100644
index 00000000..9ec8ddda
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/VlanIdTest.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import com.google.common.testing.EqualsTester;
+
+public class VlanIdTest {
+
+ @Test
+ public void testEquality() {
+
+ VlanId vlan1 = VlanId.vlanId((short) -1);
+ VlanId vlan2 = VlanId.vlanId((short) 100);
+ VlanId vlan3 = VlanId.vlanId((short) 100);
+
+ new EqualsTester().addEqualityGroup(VlanId.vlanId(), vlan1)
+ .addEqualityGroup(vlan2, vlan3)
+ .addEqualityGroup(VlanId.vlanId((short) 10));
+
+ }
+
+ @Test
+ public void basics() {
+ // purposefully create UNTAGGED VLAN
+ VlanId vlan1 = VlanId.vlanId((short) 10);
+ VlanId vlan2 = VlanId.vlanId((short) -1);
+
+ assertEquals("incorrect VLAN value", 10, vlan1.toShort());
+ assertEquals("invalid untagged value", VlanId.UNTAGGED, vlan2.toShort());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testIllicitVLAN() {
+ VlanId.vlanId((short) 5000);
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/AuthenticationTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/AuthenticationTest.java
new file mode 100644
index 00000000..39ab9104
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/AuthenticationTest.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet.ipv6;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.Data;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.UDP;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link Authentication}.
+ */
+public class AuthenticationTest {
+ private static Data data;
+ private static UDP udp;
+ private static byte[] icv = {
+ (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
+ };
+ private static byte[] bytePacket;
+
+ private Deserializer<Authentication> deserializer;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ data = new Data();
+ data.setData("testSerialize".getBytes());
+ udp = new UDP();
+ udp.setPayload(data);
+
+ byte[] bytePayload = udp.serialize();
+ byte[] byteHeader = {
+ (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x00,
+ (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68,
+ (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00,
+ (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
+ };
+ bytePacket = new byte[byteHeader.length + bytePayload.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
+ }
+
+ @Before
+ public void setUp() {
+ deserializer = Authentication.deserializer();
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ Authentication auth = new Authentication();
+ auth.setNextHeader((byte) 0x11);
+ auth.setPayloadLength((byte) 0x02);
+ auth.setSecurityParamIndex(0x13572468);
+ auth.setSequence(0xffff00);
+ auth.setIngegrityCheck(icv);
+ auth.setPayload(udp);
+
+ assertArrayEquals(auth.serialize(), bytePacket);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws Exception {
+ Authentication auth = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertThat(auth.getNextHeader(), is((byte) 0x11));
+ assertThat(auth.getPayloadLength(), is((byte) 0x02));
+ assertThat(auth.getSecurityParamIndex(), is(0x13572468));
+ assertThat(auth.getSequence(), is(0xffff00));
+ assertArrayEquals(auth.getIntegrityCheck(), icv);
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ Authentication auth1 = new Authentication();
+ auth1.setNextHeader((byte) 0x11);
+ auth1.setPayloadLength((byte) 0x02);
+ auth1.setSecurityParamIndex(0x13572468);
+ auth1.setSequence(0xffff00);
+ auth1.setIngegrityCheck(icv);
+
+ Authentication auth2 = new Authentication();
+ auth2.setNextHeader((byte) 0x11);
+ auth2.setPayloadLength((byte) 0x02);
+ auth2.setSecurityParamIndex(0x13572467);
+ auth2.setSequence(0xffff00);
+ auth2.setIngegrityCheck(icv);
+
+ assertTrue(auth1.equals(auth1));
+ assertFalse(auth1.equals(auth2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/BaseOptionsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/BaseOptionsTest.java
new file mode 100644
index 00000000..bb91e9e7
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/BaseOptionsTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet.ipv6;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.Data;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.IPv6;
+import org.onlab.packet.UDP;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link BaseOptions}.
+ */
+public class BaseOptionsTest {
+ private static Data data;
+ private static UDP udp;
+ private static byte[] options = {
+ (byte) 0x00, (byte) 0x03,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00
+ };
+ private static byte[] bytePacket;
+
+ private Deserializer<BaseOptions> deserializer;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ data = new Data();
+ data.setData("testSerialize".getBytes());
+ udp = new UDP();
+ udp.setPayload(data);
+
+ byte[] bytePayload = udp.serialize();
+ byte[] byteHeader = {
+ (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x03,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00
+ };
+ bytePacket = new byte[byteHeader.length + bytePayload.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
+ }
+
+ @Before
+ public void setUp() {
+ deserializer = BaseOptions.deserializer();
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ BaseOptions baseopt = new BaseOptions();
+ baseopt.setNextHeader((byte) 0x11);
+ baseopt.setHeaderExtLength((byte) 0x00);
+ baseopt.setOptions(options);
+ baseopt.setPayload(udp);
+
+ assertArrayEquals(baseopt.serialize(), bytePacket);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws Exception {
+ BaseOptions baseopt = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertThat(baseopt.getNextHeader(), is((byte) 0x11));
+ assertThat(baseopt.getHeaderExtLength(), is((byte) 0x00));
+ assertArrayEquals(baseopt.getOptions(), options);
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ BaseOptions baseopt1 = new BaseOptions();
+ baseopt1.setNextHeader((byte) 0x11);
+ baseopt1.setHeaderExtLength((byte) 0x00);
+ baseopt1.setOptions(options);
+ baseopt1.setType(IPv6.PROTOCOL_HOPOPT);
+
+ BaseOptions baseopt2 = new BaseOptions();
+ baseopt2.setNextHeader((byte) 0x11);
+ baseopt2.setHeaderExtLength((byte) 0x00);
+ baseopt2.setOptions(options);
+ baseopt1.setType(IPv6.PROTOCOL_DSTOPT);
+
+ assertTrue(baseopt1.equals(baseopt1));
+ assertFalse(baseopt1.equals(baseopt2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/DestinationOptionsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/DestinationOptionsTest.java
new file mode 100644
index 00000000..29dd1266
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/DestinationOptionsTest.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet.ipv6;
+
+import org.junit.Test;
+import org.onlab.packet.IPv6;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for class {@link DestinationOptions}.
+ */
+public class DestinationOptionsTest {
+ /**
+ * Tests constructor.
+ */
+ @Test
+ public void testConstructor() {
+ DestinationOptions dstopt = new DestinationOptions();
+ assertThat(dstopt.getType(), is(IPv6.PROTOCOL_DSTOPT));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/EncapSecurityPayloadTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/EncapSecurityPayloadTest.java
new file mode 100644
index 00000000..e0e99191
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/EncapSecurityPayloadTest.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet.ipv6;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.Data;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.Deserializer;
+
+import java.util.Arrays;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link EncapSecurityPayload}.
+ */
+public class EncapSecurityPayloadTest {
+ private static Data data;
+ private static byte[] dataByte = new byte[32];
+ private static byte[] bytePacket;
+
+ private Deserializer<EncapSecurityPayload> deserializer;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ Arrays.fill(dataByte, (byte) 0xff);
+ data = new Data().setData(dataByte);
+
+ byte[] bytePayload = data.serialize();
+ byte[] byteHeader = {
+ (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68,
+ (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00
+ };
+ bytePacket = new byte[byteHeader.length + bytePayload.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
+ }
+
+ @Before
+ public void setUp() {
+ deserializer = EncapSecurityPayload.deserializer();
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ EncapSecurityPayload esp = new EncapSecurityPayload();
+ esp.setSecurityParamIndex(0x13572468);
+ esp.setSequence(0xffff00);
+ esp.setPayload(data);
+
+ assertArrayEquals(esp.serialize(), bytePacket);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws DeserializationException {
+ EncapSecurityPayload esp = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertThat(esp.getSecurityParamIndex(), is(0x13572468));
+ assertThat(esp.getSequence(), is(0xffff00));
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ EncapSecurityPayload esp1 = new EncapSecurityPayload();
+ esp1.setSecurityParamIndex(0x13572468);
+ esp1.setSequence(0xffff00);
+
+ EncapSecurityPayload esp2 = new EncapSecurityPayload();
+ esp2.setSecurityParamIndex(0x13572468);
+ esp2.setSequence(0xfffff0);
+
+ assertTrue(esp1.equals(esp1));
+ assertFalse(esp1.equals(esp2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/FragmentTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/FragmentTest.java
new file mode 100644
index 00000000..f2d5e48e
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/FragmentTest.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet.ipv6;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.Data;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.UDP;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link Fragment}.
+ */
+public class FragmentTest {
+ private static Data data;
+ private static UDP udp;
+ private static byte[] bytePacket;
+
+ private Deserializer<Fragment> deserializer;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ data = new Data();
+ data.setData("testSerialize".getBytes());
+ udp = new UDP();
+ udp.setPayload(data);
+
+ byte[] bytePayload = udp.serialize();
+ byte[] byteHeader = {
+ (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0xf9,
+ (byte) 0x00, (byte) 0x00, (byte) 0x13, (byte) 0x57
+ };
+ bytePacket = new byte[byteHeader.length + bytePayload.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
+ }
+
+ @Before
+ public void setUp() {
+ deserializer = Fragment.deserializer();
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ Fragment frag = new Fragment();
+ frag.setNextHeader((byte) 0x11);
+ frag.setFragmentOffset((short) 0x1f);
+ frag.setMoreFragment((byte) 1);
+ frag.setIdentification(0x1357);
+ frag.setPayload(udp);
+
+ assertArrayEquals(frag.serialize(), bytePacket);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws DeserializationException {
+ Fragment frag = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertThat(frag.getNextHeader(), is((byte) 0x11));
+ assertThat(frag.getFragmentOffset(), is((short) 0x1f));
+ assertThat(frag.getMoreFragment(), is((byte) 1));
+ assertThat(frag.getIdentification(), is(0x1357));
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ Fragment frag1 = new Fragment();
+ frag1.setNextHeader((byte) 0x11);
+ frag1.setFragmentOffset((short) 0x1f);
+ frag1.setMoreFragment((byte) 1);
+ frag1.setIdentification(0x1357);
+
+ Fragment frag2 = new Fragment();
+ frag2.setNextHeader((byte) 0x11);
+ frag2.setFragmentOffset((short) 0x1f);
+ frag2.setMoreFragment((byte) 1);
+ frag2.setIdentification(0x1358);
+
+ assertTrue(frag1.equals(frag1));
+ assertFalse(frag1.equals(frag2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/HopByHopOptionsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/HopByHopOptionsTest.java
new file mode 100644
index 00000000..1e9be2f6
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/HopByHopOptionsTest.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet.ipv6;
+
+import org.junit.Test;
+import org.onlab.packet.IPv6;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for class {@link HopByHopOptions}.
+ */
+public class HopByHopOptionsTest {
+ /**
+ * Tests constructor.
+ */
+ @Test
+ public void testConstructor() {
+ HopByHopOptions hopopt = new HopByHopOptions();
+ assertThat(hopopt.getType(), is(IPv6.PROTOCOL_HOPOPT));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/RoutingTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/RoutingTest.java
new file mode 100644
index 00000000..a03bacc9
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/RoutingTest.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet.ipv6;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.Data;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.UDP;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link Routing}.
+ */
+public class RoutingTest {
+ private static Data data;
+ private static UDP udp;
+ private static byte[] routingData = {
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
+ };
+ private static byte[] bytePacket;
+
+ private Deserializer<Routing> deserializer;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ data = new Data();
+ data.setData("testSerialize".getBytes());
+ udp = new UDP();
+ udp.setPayload(data);
+
+ byte[] bytePayload = udp.serialize();
+ byte[] byteHeader = {
+ (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x03,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
+ };
+ bytePacket = new byte[byteHeader.length + bytePayload.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
+ }
+
+ @Before
+ public void setUp() {
+ deserializer = Routing.deserializer();
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ Routing routing = new Routing();
+ routing.setNextHeader((byte) 0x11);
+ routing.setHeaderExtLength((byte) 0x02);
+ routing.setRoutingType((byte) 0x00);
+ routing.setSegmntsLeft((byte) 0x03);
+ routing.setRoutingData(routingData);
+ routing.setPayload(udp);
+
+ assertArrayEquals(routing.serialize(), bytePacket);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws DeserializationException {
+ Routing routing = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertThat(routing.getNextHeader(), is((byte) 0x11));
+ assertThat(routing.getHeaderExtLength(), is((byte) 0x02));
+ assertThat(routing.getRoutingType(), is((byte) 0x00));
+ assertThat(routing.getSegmentsLeft(), is((byte) 0x03));
+ assertArrayEquals(routing.getRoutingData(), routingData);
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ Routing routing1 = new Routing();
+ routing1.setNextHeader((byte) 0x11);
+ routing1.setHeaderExtLength((byte) 0x02);
+ routing1.setRoutingType((byte) 0x00);
+ routing1.setSegmntsLeft((byte) 0x03);
+ routing1.setRoutingData(routingData);
+
+ Routing routing2 = new Routing();
+ routing2.setNextHeader((byte) 0x11);
+ routing2.setHeaderExtLength((byte) 0x02);
+ routing2.setRoutingType((byte) 0x00);
+ routing2.setSegmntsLeft((byte) 0x02);
+ routing2.setRoutingData(routingData);
+
+ assertTrue(routing1.equals(routing1));
+ assertFalse(routing1.equals(routing2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborAdvertisementTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborAdvertisementTest.java
new file mode 100644
index 00000000..d537251c
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborAdvertisementTest.java
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet.ndp;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.PacketTestUtils;
+
+import java.nio.ByteBuffer;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link NeighborAdvertisement}.
+ */
+public class NeighborAdvertisementTest {
+ private static final byte[] TARGET_ADDRESS = {
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
+ };
+ private static final MacAddress MAC_ADDRESS =
+ MacAddress.valueOf("11:22:33:44:55:66");
+
+ private static byte[] bytePacket;
+
+ private Deserializer<NeighborAdvertisement> deserializer
+ = NeighborAdvertisement.deserializer();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ byte[] byteHeader = {
+ (byte) 0xe0, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
+ (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
+ (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
+ };
+ bytePacket = new byte[byteHeader.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ NeighborAdvertisement na = new NeighborAdvertisement();
+ na.setRouterFlag((byte) 1);
+ na.setSolicitedFlag((byte) 1);
+ na.setOverrideFlag((byte) 1);
+ na.setTargetAddress(TARGET_ADDRESS);
+ na.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ assertArrayEquals(na.serialize(), bytePacket);
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(NeighborAdvertisement.deserializer());
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ // Run the truncation test only on the NeighborAdvertisement header
+ byte[] naHeader = new byte[NeighborAdvertisement.HEADER_LENGTH];
+ ByteBuffer.wrap(bytePacket).get(naHeader);
+
+ PacketTestUtils.testDeserializeTruncated(NeighborAdvertisement.deserializer(), naHeader);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws DeserializationException {
+ NeighborAdvertisement na = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertThat(na.getRouterFlag(), is((byte) 1));
+ assertThat(na.getSolicitedFlag(), is((byte) 1));
+ assertThat(na.getOverrideFlag(), is((byte) 1));
+ assertArrayEquals(na.getTargetAddress(), TARGET_ADDRESS);
+
+ // Check the option(s)
+ assertThat(na.getOptions().size(), is(1));
+ NeighborDiscoveryOptions.Option option = na.getOptions().get(0);
+ assertThat(option.type(),
+ is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
+ assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ NeighborAdvertisement na1 = new NeighborAdvertisement();
+ na1.setRouterFlag((byte) 1);
+ na1.setSolicitedFlag((byte) 1);
+ na1.setOverrideFlag((byte) 1);
+ na1.setTargetAddress(TARGET_ADDRESS);
+ na1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ NeighborAdvertisement na2 = new NeighborAdvertisement();
+ na2.setRouterFlag((byte) 1);
+ na2.setSolicitedFlag((byte) 1);
+ na2.setOverrideFlag((byte) 0);
+ na2.setTargetAddress(TARGET_ADDRESS);
+ na2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ assertTrue(na1.equals(na1));
+ assertFalse(na1.equals(na2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborSolicitationTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborSolicitationTest.java
new file mode 100644
index 00000000..03e57f5e
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborSolicitationTest.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet.ndp;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.PacketTestUtils;
+
+import java.nio.ByteBuffer;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link NeighborSolicitation}.
+ */
+public class NeighborSolicitationTest {
+ private static final byte[] TARGET_ADDRESS = {
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
+ };
+ private static final byte[] TARGET_ADDRESS2 = {
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
+ };
+ private static final MacAddress MAC_ADDRESS =
+ MacAddress.valueOf("11:22:33:44:55:66");
+
+ private static byte[] bytePacket;
+
+ private Deserializer<NeighborSolicitation> deserializer
+ = NeighborSolicitation.deserializer();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ byte[] byteHeader = {
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
+ (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
+ (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
+ };
+ bytePacket = new byte[byteHeader.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ NeighborSolicitation ns = new NeighborSolicitation();
+ ns.setTargetAddress(TARGET_ADDRESS);
+ ns.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ assertArrayEquals(ns.serialize(), bytePacket);
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(NeighborSolicitation.deserializer());
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ // Run the truncation test only on the NeighborSolicitation header
+ byte[] nsHeader = new byte[NeighborSolicitation.HEADER_LENGTH];
+ ByteBuffer.wrap(bytePacket).get(nsHeader);
+
+ PacketTestUtils.testDeserializeTruncated(NeighborSolicitation.deserializer(), nsHeader);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws DeserializationException {
+ NeighborSolicitation ns = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertArrayEquals(ns.getTargetAddress(), TARGET_ADDRESS);
+
+ // Check the option(s)
+ assertThat(ns.getOptions().size(), is(1));
+ NeighborDiscoveryOptions.Option option = ns.getOptions().get(0);
+ assertThat(option.type(),
+ is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
+ assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ NeighborSolicitation ns1 = new NeighborSolicitation();
+ ns1.setTargetAddress(TARGET_ADDRESS);
+ ns1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ NeighborSolicitation ns2 = new NeighborSolicitation();
+ ns2.setTargetAddress(TARGET_ADDRESS2);
+ ns2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ assertTrue(ns1.equals(ns1));
+ assertFalse(ns1.equals(ns2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RedirectTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RedirectTest.java
new file mode 100644
index 00000000..31157145
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RedirectTest.java
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet.ndp;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.PacketTestUtils;
+
+import java.nio.ByteBuffer;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link Redirect}.
+ */
+public class RedirectTest {
+ private static final byte[] TARGET_ADDRESS = {
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
+ };
+ private static final byte[] DESTINATION_ADDRESS = {
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
+ };
+ private static final byte[] DESTINATION_ADDRESS2 = {
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
+ };
+ private static final MacAddress MAC_ADDRESS =
+ MacAddress.valueOf("11:22:33:44:55:66");
+
+ private static byte[] bytePacket;
+
+ private Deserializer<Redirect> deserializer = Redirect.deserializer();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ byte[] byteHeader = {
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
+ (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
+ (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
+ (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
+ (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
+ (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
+ };
+ bytePacket = new byte[byteHeader.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ Redirect rd = new Redirect();
+ rd.setTargetAddress(TARGET_ADDRESS);
+ rd.setDestinationAddress(DESTINATION_ADDRESS);
+ rd.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ assertArrayEquals(rd.serialize(), bytePacket);
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(Redirect.deserializer());
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ // Run the truncation test only on the Redirect header
+ byte[] rdHeader = new byte[Redirect.HEADER_LENGTH];
+ ByteBuffer.wrap(bytePacket).get(rdHeader);
+
+ PacketTestUtils.testDeserializeTruncated(Redirect.deserializer(), rdHeader);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws DeserializationException {
+ Redirect rd = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertArrayEquals(rd.getTargetAddress(), TARGET_ADDRESS);
+ assertArrayEquals(rd.getDestinationAddress(), DESTINATION_ADDRESS);
+
+ // Check the option(s)
+ assertThat(rd.getOptions().size(), is(1));
+ NeighborDiscoveryOptions.Option option = rd.getOptions().get(0);
+ assertThat(option.type(),
+ is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
+ assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ Redirect rd1 = new Redirect();
+ rd1.setTargetAddress(TARGET_ADDRESS);
+ rd1.setDestinationAddress(DESTINATION_ADDRESS);
+ rd1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ Redirect rd2 = new Redirect();
+ rd2.setTargetAddress(TARGET_ADDRESS);
+ rd2.setDestinationAddress(DESTINATION_ADDRESS2);
+ rd2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ assertTrue(rd1.equals(rd1));
+ assertFalse(rd1.equals(rd2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterAdvertisementTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterAdvertisementTest.java
new file mode 100644
index 00000000..ede15747
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterAdvertisementTest.java
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet.ndp;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.PacketTestUtils;
+
+import java.nio.ByteBuffer;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link RouterAdvertisement}.
+ */
+public class RouterAdvertisementTest {
+ private static final MacAddress MAC_ADDRESS =
+ MacAddress.valueOf("11:22:33:44:55:66");
+
+ private static byte[] bytePacket;
+
+ private Deserializer<RouterAdvertisement> deserializer
+ = RouterAdvertisement.deserializer();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ byte[] byteHeader = {
+ (byte) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58,
+ (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8,
+ (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4,
+ (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
+ (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
+ };
+ bytePacket = new byte[byteHeader.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ RouterAdvertisement ra = new RouterAdvertisement();
+ ra.setCurrentHopLimit((byte) 3);
+ ra.setMFlag((byte) 1);
+ ra.setOFlag((byte) 1);
+ ra.setRouterLifetime((short) 0x258);
+ ra.setReachableTime(0x3e8);
+ ra.setRetransmitTimer(0x1f4);
+ ra.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ assertArrayEquals(ra.serialize(), bytePacket);
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(RouterAdvertisement.deserializer());
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ // Run the truncation test only on the RouterAdvertisement header
+ byte[] raHeader = new byte[RouterAdvertisement.HEADER_LENGTH];
+ ByteBuffer.wrap(bytePacket).get(raHeader);
+
+ PacketTestUtils.testDeserializeTruncated(RouterAdvertisement.deserializer(), raHeader);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws DeserializationException {
+ RouterAdvertisement ra = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ assertThat(ra.getCurrentHopLimit(), is((byte) 3));
+ assertThat(ra.getMFlag(), is((byte) 1));
+ assertThat(ra.getOFlag(), is((byte) 1));
+ assertThat(ra.getRouterLifetime(), is((short) 0x258));
+ assertThat(ra.getReachableTime(), is(0x3e8));
+ assertThat(ra.getRetransmitTimer(), is(0x1f4));
+
+ // Check the option(s)
+ assertThat(ra.getOptions().size(), is(1));
+ NeighborDiscoveryOptions.Option option = ra.getOptions().get(0);
+ assertThat(option.type(),
+ is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
+ assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ RouterAdvertisement ra1 = new RouterAdvertisement();
+ ra1.setCurrentHopLimit((byte) 3);
+ ra1.setMFlag((byte) 1);
+ ra1.setOFlag((byte) 1);
+ ra1.setRouterLifetime((short) 0x258);
+ ra1.setReachableTime(0x3e8);
+ ra1.setRetransmitTimer(0x1f4);
+ ra1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ RouterAdvertisement ra2 = new RouterAdvertisement();
+ ra2.setCurrentHopLimit((byte) 3);
+ ra2.setMFlag((byte) 0);
+ ra2.setOFlag((byte) 0);
+ ra2.setRouterLifetime((short) 0x1f4);
+ ra2.setReachableTime(0x3e8);
+ ra2.setRetransmitTimer(0x1f4);
+ ra2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS.toBytes());
+
+ assertTrue(ra1.equals(ra1));
+ assertFalse(ra1.equals(ra2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterSolicitationTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterSolicitationTest.java
new file mode 100644
index 00000000..e3b5686c
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterSolicitationTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet.ndp;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.PacketTestUtils;
+
+import java.nio.ByteBuffer;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link RouterSolicitation}.
+ */
+public class RouterSolicitationTest {
+ private static final MacAddress MAC_ADDRESS1 =
+ MacAddress.valueOf("11:22:33:44:55:66");
+ private static final MacAddress MAC_ADDRESS2 =
+ MacAddress.valueOf("11:22:33:44:55:00");
+
+ private static byte[] bytePacket;
+
+ private Deserializer<RouterSolicitation> deserializer
+ = RouterSolicitation.deserializer();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ byte[] byteHeader = {
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
+ (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
+ };
+ bytePacket = new byte[byteHeader.length];
+ System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
+ }
+
+ /**
+ * Tests serialize and setters.
+ */
+ @Test
+ public void testSerialize() {
+ RouterSolicitation rs = new RouterSolicitation();
+ rs.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS1.toBytes());
+
+ assertArrayEquals(rs.serialize(), bytePacket);
+ }
+
+ @Test
+ public void testDeserializeBadInput() throws Exception {
+ PacketTestUtils.testDeserializeBadInput(RouterSolicitation.deserializer());
+ }
+
+ @Test
+ public void testDeserializeTruncated() throws Exception {
+ // Run the truncation test only on the RouterSolicitation header
+ byte[] rsHeader = new byte[RouterSolicitation.HEADER_LENGTH];
+ ByteBuffer.wrap(bytePacket).get(rsHeader);
+
+ PacketTestUtils.testDeserializeTruncated(RouterSolicitation.deserializer(), rsHeader);
+ }
+
+ /**
+ * Tests deserialize and getters.
+ */
+ @Test
+ public void testDeserialize() throws Exception {
+ RouterSolicitation rs = deserializer.deserialize(bytePacket, 0, bytePacket.length);
+
+ // Check the option(s)
+ assertThat(rs.getOptions().size(), is(1));
+ NeighborDiscoveryOptions.Option option = rs.getOptions().get(0);
+ assertThat(option.type(),
+ is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
+ assertArrayEquals(option.data(), MAC_ADDRESS1.toBytes());
+ }
+
+ /**
+ * Tests comparator.
+ */
+ @Test
+ public void testEqual() {
+ RouterSolicitation rs1 = new RouterSolicitation();
+ rs1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS1.toBytes());
+
+ RouterSolicitation rs2 = new RouterSolicitation();
+ rs2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
+ MAC_ADDRESS2.toBytes());
+
+ assertTrue(rs1.equals(rs1));
+ assertFalse(rs1.equals(rs2));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/AbstractAccumulatorTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/AbstractAccumulatorTest.java
new file mode 100644
index 00000000..02f0deb1
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/AbstractAccumulatorTest.java
@@ -0,0 +1,184 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.Timer;
+import java.util.stream.IntStream;
+
+import static org.junit.Assert.*;
+import static org.onlab.junit.TestTools.assertAfter;
+import static org.onlab.junit.TestTools.delay;
+
+/**
+ * Tests the operation of the accumulator.
+ */
+public class AbstractAccumulatorTest {
+
+ private final Timer timer = new Timer();
+
+ @Test
+ public void basics() throws Exception {
+ TestAccumulator accumulator = new TestAccumulator();
+ assertEquals("incorrect timer", timer, accumulator.timer());
+ assertEquals("incorrect max events", 5, accumulator.maxItems());
+ assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis());
+ assertEquals("incorrect idle ms", 70, accumulator.maxIdleMillis());
+ }
+
+ @Ignore("FIXME: timing sensitive test failing randomly.")
+ @Test
+ public void eventTrigger() {
+ TestAccumulator accumulator = new TestAccumulator();
+ accumulator.add(new TestItem("a"));
+ accumulator.add(new TestItem("b"));
+ accumulator.add(new TestItem("c"));
+ accumulator.add(new TestItem("d"));
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.add(new TestItem("e"));
+ delay(20);
+ assertFalse("should have fired", accumulator.batch.isEmpty());
+ assertEquals("incorrect batch", "abcde", accumulator.batch);
+ }
+
+ @Ignore("FIXME: timing sensitive test failing randomly.")
+ @Test
+ public void timeTrigger() {
+ TestAccumulator accumulator = new TestAccumulator();
+ accumulator.add(new TestItem("a"));
+ delay(30);
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.add(new TestItem("b"));
+ delay(30);
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.add(new TestItem("c"));
+ delay(30);
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.add(new TestItem("d"));
+ delay(60);
+ assertFalse("should have fired", accumulator.batch.isEmpty());
+ assertEquals("incorrect batch", "abcd", accumulator.batch);
+ }
+
+ @Ignore("FIXME: timing sensitive test failing randomly.")
+ @Test
+ public void idleTrigger() {
+ TestAccumulator accumulator = new TestAccumulator();
+ accumulator.add(new TestItem("a"));
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.add(new TestItem("b"));
+ delay(80);
+ assertFalse("should have fired", accumulator.batch.isEmpty());
+ assertEquals("incorrect batch", "ab", accumulator.batch);
+ }
+
+ @Ignore("FIXME: timing sensitive test failing randomly.")
+ @Test
+ public void readyIdleTrigger() {
+ TestAccumulator accumulator = new TestAccumulator();
+ accumulator.ready = false;
+ accumulator.add(new TestItem("a"));
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.add(new TestItem("b"));
+ delay(80);
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.ready = true;
+ delay(80);
+ assertFalse("should have fired", accumulator.batch.isEmpty());
+ assertEquals("incorrect batch", "ab", accumulator.batch);
+ }
+
+ @Ignore("FIXME: timing sensitive test failing randomly.")
+ @Test
+ public void readyLongTrigger() {
+ TestAccumulator accumulator = new TestAccumulator();
+ accumulator.ready = false;
+ delay(120);
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.add(new TestItem("a"));
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.ready = true;
+ delay(80);
+ assertFalse("should have fired", accumulator.batch.isEmpty());
+ assertEquals("incorrect batch", "a", accumulator.batch);
+ }
+
+ @Ignore("FIXME: timing sensitive test failing randomly.")
+ @Test
+ public void readyMaxTrigger() {
+ TestAccumulator accumulator = new TestAccumulator();
+ accumulator.ready = false;
+ accumulator.add(new TestItem("a"));
+ accumulator.add(new TestItem("b"));
+ accumulator.add(new TestItem("c"));
+ accumulator.add(new TestItem("d"));
+ accumulator.add(new TestItem("e"));
+ accumulator.add(new TestItem("f"));
+ assertTrue("should not have fired yet", accumulator.batch.isEmpty());
+ accumulator.ready = true;
+ accumulator.add(new TestItem("g"));
+ delay(5);
+ assertFalse("should have fired", accumulator.batch.isEmpty());
+ assertEquals("incorrect batch", "abcdefg", accumulator.batch);
+ }
+
+ @Ignore("FIXME: timing sensitive test failing randomly.")
+ @Test
+ public void stormTest() {
+ TestAccumulator accumulator = new TestAccumulator();
+ IntStream.range(0, 1000).forEach(i -> accumulator.add(new TestItem("#" + i)));
+ assertAfter(100, () -> assertEquals("wrong item count", 1000, accumulator.itemCount));
+ assertEquals("wrong batch count", 200, accumulator.batchCount);
+ }
+
+ private class TestItem {
+ private final String s;
+
+ public TestItem(String s) {
+ this.s = s;
+ }
+ }
+
+ private class TestAccumulator extends AbstractAccumulator<TestItem> {
+
+ String batch = "";
+ boolean ready = true;
+ int batchCount = 0;
+ int itemCount = 0;
+
+ protected TestAccumulator() {
+ super(timer, 5, 100, 70);
+ }
+
+ @Override
+ public void processItems(List<TestItem> items) {
+ batchCount++;
+ itemCount += items.size();
+ for (TestItem item : items) {
+ batch += item.s;
+ }
+ }
+
+ @Override
+ public boolean isReady() {
+ return ready;
+ }
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/BandwidthTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/BandwidthTest.java
new file mode 100644
index 00000000..a3baf06c
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/BandwidthTest.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.lessThan;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit tests for Bandwidth.
+ */
+public class BandwidthTest {
+
+ private final Bandwidth small = Bandwidth.kbps(100.0);
+ private final Bandwidth large = Bandwidth.mbps(1.0);
+
+ /**
+ * Tests equality of Bandwidth instances.
+ */
+ @Test
+ public void testEquality() {
+ new EqualsTester()
+ .addEqualityGroup(Bandwidth.kbps(1000.0), Bandwidth.kbps(1000.0), Bandwidth.mbps(1.0))
+ .addEqualityGroup(Bandwidth.gbps(1.0))
+ .testEquals();
+ }
+
+ /**
+ * Tests add operation of two Bandwidths.
+ */
+ @Test
+ public void testAdd() {
+ Bandwidth expected = Bandwidth.kbps(1100.0);
+
+ assertThat(small.add(large), is(expected));
+ }
+
+ /**
+ * Tests subtract operation of two Bandwidths.
+ */
+ @Test
+ public void testSubtract() {
+ Bandwidth expected = Bandwidth.kbps(900.0);
+
+ assertThat(large.subtract(small), is(expected));
+ }
+
+ /**
+ * Tests if the first object is less than the second object.
+ */
+ @Test
+ public void testLessThan() {
+ assertThat(small, is(lessThan(large)));
+ assertThat(small.isLessThan(large), is(true));
+ }
+
+ /**
+ * Tests if the first object is greater than the second object.
+ */
+ @Test
+ public void testGreaterThan() {
+ assertThat(large, is(greaterThan(small)));
+ assertThat(large.isGreaterThan(small), is(true));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/BlockingBooleanTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/BlockingBooleanTest.java
new file mode 100644
index 00000000..2d8b688e
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/BlockingBooleanTest.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import org.apache.commons.lang.mutable.MutableBoolean;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests of the BlockingBoolean utility.
+ */
+public class BlockingBooleanTest {
+
+ private static final int TIMEOUT = 100; //ms
+
+ @Test
+ public void basics() {
+ BlockingBoolean b = new BlockingBoolean(false);
+ assertEquals(false, b.get());
+ b.set(true);
+ assertEquals(true, b.get());
+ b.set(true);
+ assertEquals(true, b.get());
+ b.set(false);
+ assertEquals(false, b.get());
+ }
+
+ private void waitChange(boolean value, int numThreads) {
+ BlockingBoolean b = new BlockingBoolean(!value);
+
+ CountDownLatch latch = new CountDownLatch(numThreads);
+ ExecutorService exec = Executors.newFixedThreadPool(numThreads);
+ for (int i = 0; i < numThreads; i++) {
+ exec.submit(() -> {
+ try {
+ b.await(value);
+ latch.countDown();
+ } catch (InterruptedException e) {
+ fail();
+ }
+ });
+ }
+ b.set(value);
+ try {
+ assertTrue(latch.await(TIMEOUT, TimeUnit.MILLISECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+ exec.shutdown();
+ }
+
+ @Test
+ public void waitTrueChange() {
+ waitChange(true, 4);
+ }
+
+ @Test
+ public void waitFalseChange() {
+ waitChange(false, 4);
+ }
+
+ @Test
+ public void waitSame() {
+ BlockingBoolean b = new BlockingBoolean(true);
+
+ CountDownLatch latch = new CountDownLatch(1);
+ ExecutorService exec = Executors.newSingleThreadExecutor();
+ exec.submit(() -> {
+ try {
+ b.await(true);
+ latch.countDown();
+ } catch (InterruptedException e) {
+ fail();
+ }
+ });
+ try {
+ assertTrue(latch.await(TIMEOUT, TimeUnit.MILLISECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+ exec.shutdown();
+ }
+
+ @Test
+ public void someWait() {
+ BlockingBoolean b = new BlockingBoolean(false);
+
+ int numThreads = 4;
+ CountDownLatch sameLatch = new CountDownLatch(numThreads / 2);
+ CountDownLatch waitLatch = new CountDownLatch(numThreads / 2);
+
+ ExecutorService exec = Executors.newFixedThreadPool(numThreads);
+ for (int i = 0; i < numThreads; i++) {
+ final boolean value = (i % 2 == 1);
+ exec.submit(() -> {
+ try {
+ b.await(value);
+ if (value) {
+ waitLatch.countDown();
+ } else {
+ sameLatch.countDown();
+ }
+ } catch (InterruptedException e) {
+ fail();
+ }
+ });
+ }
+ try {
+ assertTrue(sameLatch.await(TIMEOUT, TimeUnit.MILLISECONDS));
+ assertEquals(waitLatch.getCount(), numThreads / 2);
+ } catch (InterruptedException e) {
+ fail();
+ }
+ b.set(true);
+ try {
+ assertTrue(waitLatch.await(TIMEOUT, TimeUnit.MILLISECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+ exec.shutdown();
+ }
+
+ @Test
+ public void waitTimeout() {
+ BlockingBoolean b = new BlockingBoolean(true);
+
+ CountDownLatch latch = new CountDownLatch(1);
+ ExecutorService exec = Executors.newSingleThreadExecutor();
+ exec.submit(() -> {
+ try {
+ if (!b.await(false, 1, TimeUnit.NANOSECONDS)) {
+ latch.countDown();
+ } else {
+ fail();
+ }
+ } catch (InterruptedException e) {
+ fail();
+ }
+ });
+ try {
+ assertTrue(latch.await(TIMEOUT, TimeUnit.MILLISECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+ exec.shutdown();
+
+ }
+
+ @Test
+ @Ignore
+ public void samePerf() {
+ int iters = 10_000;
+
+ BlockingBoolean b1 = new BlockingBoolean(false);
+ long t1 = System.nanoTime();
+ for (int i = 0; i < iters; i++) {
+ b1.set(false);
+ }
+ long t2 = System.nanoTime();
+ MutableBoolean b2 = new MutableBoolean(false);
+ for (int i = 0; i < iters; i++) {
+ b2.setValue(false);
+ }
+ long t3 = System.nanoTime();
+ System.out.println((t2 - t1) + " " + (t3 - t2) + " " + ((t2 - t1) <= (t3 - t2)));
+ }
+
+ @Test
+ @Ignore
+ public void changePerf() {
+ int iters = 10_000;
+
+ BlockingBoolean b1 = new BlockingBoolean(false);
+ boolean v = true;
+ long t1 = System.nanoTime();
+ for (int i = 0; i < iters; i++) {
+ b1.set(v);
+ v = !v;
+ }
+ long t2 = System.nanoTime();
+ MutableBoolean b2 = new MutableBoolean(false);
+ for (int i = 0; i < iters; i++) {
+ b2.setValue(v);
+ v = !v;
+ }
+ long t3 = System.nanoTime();
+ System.out.println((t2 - t1) + " " + (t3 - t2) + " " + ((t2 - t1) <= (t3 - t2)));
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/BoundedThreadPoolTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/BoundedThreadPoolTest.java
new file mode 100644
index 00000000..c6132de1
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/BoundedThreadPoolTest.java
@@ -0,0 +1,227 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import com.google.common.collect.Lists;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.junit.Assert.*;
+import static org.onlab.util.BoundedThreadPool.*;
+import static org.onlab.util.Tools.namedThreads;
+
+/**
+ * Test of BoundedThreadPool.
+ */
+public final class BoundedThreadPoolTest {
+
+ @Test
+ public void simpleJob() {
+ final Thread myThread = Thread.currentThread();
+ final AtomicBoolean sameThread = new AtomicBoolean(true);
+ final CountDownLatch latch = new CountDownLatch(1);
+
+ BoundedThreadPool exec = newSingleThreadExecutor(namedThreads("test"));
+ exec.submit(() -> {
+ sameThread.set(myThread.equals(Thread.currentThread()));
+ latch.countDown();
+ });
+
+ try {
+ assertTrue("Job not run", latch.await(100, TimeUnit.MILLISECONDS));
+ assertFalse("Runnable used caller thread", sameThread.get());
+ } catch (InterruptedException e) {
+ fail();
+ } finally {
+ exec.shutdown();
+ }
+
+ // TODO perhaps move to tearDown
+ try {
+ assertTrue(exec.awaitTermination(1, TimeUnit.SECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+ }
+
+ private List<CountDownLatch> fillExecutor(BoundedThreadPool exec) {
+ int numThreads = exec.getMaximumPoolSize();
+ List<CountDownLatch> latches = Lists.newArrayList();
+ final CountDownLatch started = new CountDownLatch(numThreads);
+ List<CountDownLatch> finished = Lists.newArrayList();
+
+ // seed the executor's threads
+ for (int i = 0; i < numThreads; i++) {
+ final CountDownLatch latch = new CountDownLatch(1);
+ final CountDownLatch fin = new CountDownLatch(1);
+ latches.add(latch);
+ finished.add(fin);
+ exec.submit(() -> {
+ try {
+ started.countDown();
+ latch.await();
+ fin.countDown();
+ } catch (InterruptedException e) {
+ fail();
+ }
+ });
+ }
+ try {
+ assertTrue(started.await(100, TimeUnit.MILLISECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+ // fill the queue
+ CountDownLatch startedBlocked = new CountDownLatch(1);
+ while (exec.getQueue().remainingCapacity() > 0) {
+ final CountDownLatch latch = new CountDownLatch(1);
+ latches.add(latch);
+ exec.submit(() -> {
+ try {
+ startedBlocked.countDown();
+ latch.await();
+ } catch (InterruptedException e) {
+ fail();
+ }
+ });
+ }
+
+ latches.remove(0).countDown(); // release one of the executors
+ // ... we need to do this because load is recomputed when jobs are taken
+ // Note: For this to work, 1 / numThreads must be less than the load threshold (0.2)
+
+ // verify that the old job has terminated
+ try {
+ assertTrue("Job didn't finish",
+ finished.remove(0).await(100, TimeUnit.MILLISECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+
+ // verify that a previously blocked thread has started
+ try {
+ assertTrue(startedBlocked.await(10, TimeUnit.MILLISECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+
+
+ // add another job to fill the queue
+ final CountDownLatch latch = new CountDownLatch(1);
+ latches.add(latch);
+ exec.submit(() -> {
+ try {
+ latch.await();
+ } catch (InterruptedException e) {
+ fail();
+ }
+ });
+ assertEquals(exec.getQueue().size(), maxQueueSize);
+
+ return latches;
+ }
+
+ @Test
+ public void releaseOneThread() {
+ maxQueueSize = 10;
+ BoundedThreadPool exec = newFixedThreadPool(4, namedThreads("test"));
+ List<CountDownLatch> latches = fillExecutor(exec);
+
+ CountDownLatch myLatch = new CountDownLatch(1);
+ ExecutorService myExec = Executors.newSingleThreadExecutor();
+ Future<Thread> expected = myExec.submit(Thread::currentThread);
+
+ assertEquals(exec.getQueue().size(), maxQueueSize);
+ long start = System.nanoTime();
+ Future<Thread> actual = myExec.submit(() -> {
+ return exec.submit(() -> {
+ myLatch.countDown();
+ return Thread.currentThread();
+ }).get();
+ });
+
+ try {
+ assertFalse("Thread should still be blocked",
+ myLatch.await(10, TimeUnit.MILLISECONDS));
+
+ latches.remove(0).countDown(); // release the first thread
+ assertFalse("Thread should still be blocked",
+ myLatch.await(10, TimeUnit.MILLISECONDS));
+ latches.remove(0).countDown(); // release the second thread
+
+ assertTrue("Thread should be unblocked",
+ myLatch.await(10, TimeUnit.MILLISECONDS));
+ long delta = System.nanoTime() - start;
+ double load = exec.getQueue().size() / (double) maxQueueSize;
+ assertTrue("Load is greater than threshold", load <= 0.8);
+ assertTrue("Load is less than threshold", load >= 0.6);
+ assertEquals("Work done on wrong thread", expected.get(), actual.get());
+ assertTrue("Took more than one second", delta < Math.pow(10, 9));
+ } catch (InterruptedException | ExecutionException e) {
+ fail();
+ } finally {
+ latches.forEach(CountDownLatch::countDown);
+ exec.shutdown();
+ }
+
+ // TODO perhaps move to tearDown
+ try {
+ assertTrue(exec.awaitTermination(1, TimeUnit.SECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+
+ }
+
+ @Test
+ public void highLoadTimeout() {
+ maxQueueSize = 10;
+ BoundedThreadPool exec = newFixedThreadPool(2, namedThreads("test"));
+ List<CountDownLatch> latches = fillExecutor(exec);
+
+ // true if the job is executed and it is done on the test thread
+ final AtomicBoolean sameThread = new AtomicBoolean(false);
+ final Thread myThread = Thread.currentThread();
+ long start = System.nanoTime();
+ exec.submit(() -> {
+ sameThread.set(myThread.equals(Thread.currentThread()));
+ });
+
+ long delta = System.nanoTime() - start;
+ assertEquals(maxQueueSize, exec.getQueue().size());
+ assertTrue("Work done on wrong thread (or didn't happen)", sameThread.get());
+ assertTrue("Took less than one second. Actual: " + delta / 1_000_000.0 + "ms",
+ delta > Math.pow(10, 9));
+ assertTrue("Took more than two seconds", delta < 2 * Math.pow(10, 9));
+ latches.forEach(CountDownLatch::countDown);
+ exec.shutdown();
+
+ // TODO perhaps move to tearDown
+ try {
+ assertTrue(exec.awaitTermination(1, TimeUnit.SECONDS));
+ } catch (InterruptedException e) {
+ fail();
+ }
+ }
+} \ No newline at end of file
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ByteArraySizeHashPrinterTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ByteArraySizeHashPrinterTest.java
new file mode 100644
index 00000000..e3a5e945
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ByteArraySizeHashPrinterTest.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.util;
+
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+
+/**
+ * Test cases for byte[] pretty printer.
+ */
+public class ByteArraySizeHashPrinterTest {
+
+ /**
+ * Test method for {@link org.onlab.util.ByteArraySizeHashPrinter#toString()}.
+ */
+ @Test
+ public void testToStringNull() {
+ final byte[] none = null;
+
+ assertEquals("byte[]{null}", String.valueOf(ByteArraySizeHashPrinter.of(none)));
+ assertNull(ByteArraySizeHashPrinter.orNull(none));
+ }
+
+ /**
+ * Test method for {@link org.onlab.util.ByteArraySizeHashPrinter#toString()}.
+ */
+ @Test
+ public void testToString() {
+ final byte[] some = new byte[] {2, 5, 0, 1 };
+ final String expected = "byte[]{length=" + some.length + ", hash=" + Arrays.hashCode(some) + "}";
+
+ assertEquals(expected, String.valueOf(ByteArraySizeHashPrinter.of(some)));
+ assertNotNull(ByteArraySizeHashPrinter.orNull(some));
+ }
+
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/CounterTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/CounterTest.java
new file mode 100644
index 00000000..d30e1b59
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/CounterTest.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.junit.TestTools.delay;
+
+/**
+ * Tests of the Counter utility.
+ */
+public class CounterTest {
+
+ @Test
+ public void basics() {
+ Counter tt = new Counter();
+ assertEquals("incorrect number of bytes", 0L, tt.total());
+ assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
+ tt.add(1234567890L);
+ assertEquals("incorrect number of bytes", 1234567890L, tt.total());
+ assertTrue("incorrect throughput", 1234567890.0 < tt.throughput());
+ delay(1500);
+ tt.add(1L);
+ assertEquals("incorrect number of bytes", 1234567891L, tt.total());
+ assertTrue("incorrect throughput", 1234567891.0 > tt.throughput());
+ tt.reset();
+ assertEquals("incorrect number of bytes", 0L, tt.total());
+ assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
+ }
+
+ @Test
+ public void freeze() {
+ Counter tt = new Counter();
+ tt.add(123L);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+ delay(1000);
+ tt.freeze();
+ tt.add(123L);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+
+ double d = tt.duration();
+ double t = tt.throughput();
+ assertEquals("incorrect duration", d, tt.duration(), 0.0001);
+ assertEquals("incorrect throughput", t, tt.throughput(), 0.0001);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+ }
+
+ @Test
+ public void reset() {
+ Counter tt = new Counter();
+ tt.add(123L);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+
+ double d = tt.duration();
+ double t = tt.throughput();
+ assertEquals("incorrect duration", d, tt.duration(), 0.0001);
+ assertEquals("incorrect throughput", t, tt.throughput(), 0.0001);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+
+ tt.reset();
+ assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
+ assertEquals("incorrect number of bytes", 0, tt.total());
+ }
+
+ @Test
+ public void syntheticTracker() {
+ Counter tt = new Counter(5000, 1000, 6000);
+ assertEquals("incorrect duration", 1, tt.duration(), 0.1);
+ assertEquals("incorrect throughput", 1000, tt.throughput(), 1.0);
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/FrequencyTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/FrequencyTest.java
new file mode 100644
index 00000000..727c0f73
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/FrequencyTest.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.onlab.junit.ImmutableClassChecker;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.lessThan;
+
+public class FrequencyTest {
+
+ private final Frequency frequency1 = Frequency.ofMHz(1000);
+ private final Frequency sameFrequency1 = Frequency.ofMHz(1000);
+ private final Frequency frequency2 = Frequency.ofGHz(1000);
+ private final Frequency sameFrequency2 = Frequency.ofGHz(1000);
+ private final Frequency moreSameFrequency2 = Frequency.ofTHz(1);
+ private final Frequency frequency3 = Frequency.ofTHz(193.1);
+ private final Frequency sameFrequency3 = Frequency.ofGHz(193100);
+
+ /**
+ * Tests immutability of Frequency.
+ */
+ @Test
+ public void testImmutability() {
+ ImmutableClassChecker.assertThatClassIsImmutable(Frequency.class);
+ }
+
+ /**
+ * Tests equality of Frequency instances.
+ */
+ @Test
+ public void testEquality() {
+ new EqualsTester()
+ .addEqualityGroup(frequency1, sameFrequency1)
+ .addEqualityGroup(frequency2, sameFrequency2, moreSameFrequency2)
+ .addEqualityGroup(frequency3, sameFrequency3)
+ .testEquals();
+ }
+
+ /**
+ * Tests the first object is less than the second object.
+ */
+ @Test
+ public void testLessThan() {
+ assertThat(frequency1, is(lessThan(frequency2)));
+ assertThat(frequency1.isLessThan(frequency2), is(true));
+ }
+
+ @Test
+ public void testGreaterThan() {
+ assertThat(frequency2, is(greaterThan(frequency1)));
+ assertThat(frequency2.isGreaterThan(frequency1), is(true));
+ }
+
+ /**
+ * Tests add operation of two Frequencies.
+ */
+ @Test
+ public void testAdd() {
+ Frequency low = Frequency.ofMHz(100);
+ Frequency high = Frequency.ofGHz(1);
+ Frequency expected = Frequency.ofMHz(1100);
+
+ assertThat(low.add(high), is(expected));
+ }
+
+ /**
+ * Tests subtract operation of two Frequencies.
+ */
+ @Test
+ public void testSubtract() {
+ Frequency high = Frequency.ofGHz(1);
+ Frequency low = Frequency.ofMHz(100);
+ Frequency expected = Frequency.ofMHz(900);
+
+ assertThat(high.subtract(low), is(expected));
+ }
+
+ /**
+ * Tests multiply operation of Frequency.
+ */
+ @Test
+ public void testMultiply() {
+ Frequency frequency = Frequency.ofMHz(1000);
+ long factor = 5;
+ Frequency expected = Frequency.ofGHz(5);
+
+ assertThat(frequency.multiply(5), is(expected));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/GroupedThreadFactoryTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/GroupedThreadFactoryTest.java
new file mode 100644
index 00000000..5be1cda4
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/GroupedThreadFactoryTest.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import org.junit.Test;
+import org.onlab.junit.TestTools;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests of the group thread factory.
+ */
+public class GroupedThreadFactoryTest {
+
+ @Test
+ public void basics() {
+ GroupedThreadFactory a = GroupedThreadFactory.groupedThreadFactory("foo");
+ GroupedThreadFactory b = GroupedThreadFactory.groupedThreadFactory("foo");
+ assertSame("factories should be same", a, b);
+
+ assertTrue("wrong toString", a.toString().contains("foo"));
+ Thread t = a.newThread(() -> TestTools.print("yo"));
+ assertSame("wrong group", a.threadGroup(), t.getThreadGroup());
+ }
+
+ @Test
+ public void hierarchical() {
+ GroupedThreadFactory a = GroupedThreadFactory.groupedThreadFactory("foo/bar");
+ GroupedThreadFactory b = GroupedThreadFactory.groupedThreadFactory("foo/goo");
+ GroupedThreadFactory p = GroupedThreadFactory.groupedThreadFactory("foo");
+
+ assertSame("groups should be same", p.threadGroup(), a.threadGroup().getParent());
+ assertSame("groups should be same", p.threadGroup(), b.threadGroup().getParent());
+
+ assertEquals("wrong name", "foo/bar", a.threadGroup().getName());
+ assertEquals("wrong name", "foo/goo", b.threadGroup().getName());
+ assertEquals("wrong name", "foo", p.threadGroup().getName());
+ }
+
+} \ No newline at end of file
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/HexStringTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/HexStringTest.java
new file mode 100644
index 00000000..27652123
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/HexStringTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import org.junit.Test;
+
+import com.esotericsoftware.minlog.Log;
+
+import junit.framework.TestCase;
+
+/**
+ * Test of the Hexstring.
+ *
+ */
+
+public class HexStringTest extends TestCase {
+
+ @Test
+ public void testMarshalling() throws Exception {
+ String dpidStr = "00:00:00:23:20:2d:16:71";
+ long dpid = HexString.toLong(dpidStr);
+ String testStr = HexString.toHexString(dpid);
+ TestCase.assertEquals(dpidStr, testStr);
+ }
+
+ @Test
+ public void testToLong() {
+ String dpidStr = "3e:1f:01:fc:72:8c:63:31";
+ long valid = 0x3e1f01fc728c6331L;
+ long testLong = HexString.toLong(dpidStr);
+ TestCase.assertEquals(valid, testLong);
+ }
+
+ @Test
+ public void testToLongMSB() {
+ String dpidStr = "ca:7c:5e:d1:64:7a:95:9b";
+ long valid = -3856102927509056101L;
+ long testLong = HexString.toLong(dpidStr);
+ TestCase.assertEquals(valid, testLong);
+ }
+
+ @Test
+ public void testToLongError() {
+ String dpidStr = "09:08:07:06:05:04:03:02:01";
+ try {
+ HexString.toLong(dpidStr);
+ fail("HexString.toLong() should have thrown a NumberFormatException");
+ } catch (NumberFormatException expected) {
+ Log.info("HexString.toLong() have thrown a NumberFormatException");
+ }
+ }
+
+ @Test
+ public void testToStringBytes() {
+ byte[] dpid = {0, 0, 0, 0, 0, 0, 0, -1 };
+ String valid = "00:00:00:00:00:00:00:ff";
+ String testString = HexString.toHexString(dpid);
+ TestCase.assertEquals(valid, testString);
+ }
+
+ @Test
+ public void testFromHexStringError() {
+ String invalidStr = "00:00:00:00:00:00:ffff";
+ try {
+ HexString.fromHexString(invalidStr);
+ fail("HexString.fromHexString() should have thrown a NumberFormatException");
+ } catch (NumberFormatException expected) {
+ Log.info("HexString.toLong() have thrown a NumberFormatException");
+ }
+ }
+}
+
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/PositionalParameterStringFormatterTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/PositionalParameterStringFormatterTest.java
new file mode 100644
index 00000000..9758511f
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/PositionalParameterStringFormatterTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.util;
+
+import static org.junit.Assert.*;
+import static org.onlab.util.PositionalParameterStringFormatter.format;
+
+import org.junit.Test;
+
+public class PositionalParameterStringFormatterTest {
+
+ @Test
+ public void testFormat0() {
+ String fmt = "Some string 1 2 3";
+ assertEquals("Some string 1 2 3", format(fmt));
+ }
+
+ @Test
+ public void testFormat1() {
+ String fmt = "Some string {} 2 3";
+ assertEquals("Some string 1 2 3", format(fmt, 1));
+ }
+
+ @Test
+ public void testFormat2() {
+ String fmt = "Some string {} 2 {}";
+ assertEquals("Some string 1 2 3", format(fmt, 1, "3"));
+ }
+
+ @Test
+ public void testFormatNull() {
+ String fmt = "Some string {} 2 {}";
+ assertEquals("Some string 1 2 null", format(fmt, 1, null));
+ }
+
+ @Test
+ public void testFormatExtraBracket() {
+ String fmt = "Some string {} 2 {}";
+ assertEquals("Some string 1 2 {}", format(fmt, 1));
+ }
+
+ @Test
+ public void testFormatMissingBracket() {
+ String fmt = "Some string 1 2 3";
+ assertEquals("Some string 1 2 3", format(fmt, 7));
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/RetryingFunctionTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/RetryingFunctionTest.java
new file mode 100644
index 00000000..4b08d2fc
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/RetryingFunctionTest.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit tests for RetryingFunction.
+ *
+ */
+public class RetryingFunctionTest {
+
+ private int round;
+
+ @Before
+ public void setUp() {
+ round = 1;
+ }
+
+ @After
+ public void tearDown() {
+ round = 0;
+ }
+
+ @Test(expected = RetryableException.class)
+ public void testNoRetries() {
+ new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 0, 10).apply(null);
+ }
+
+ @Test
+ public void testSuccessAfterOneRetry() {
+ new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 1, 10).apply(null);
+ }
+
+ @Test(expected = RetryableException.class)
+ public void testFailureAfterOneRetry() {
+ new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 1, 10).apply(null);
+ }
+
+ @Test
+ public void testFailureAfterTwoRetries() {
+ new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 2, 10).apply(null);
+ }
+
+ @Test(expected = NonRetryableException.class)
+ public void testFailureWithNonRetryableFailure() {
+ new RetryingFunction<>(this::failCompletely, RetryableException.class, 2, 10).apply(null);
+ }
+
+ private String succeedAfterOneFailure(String input) {
+ if (round++ <= 1) {
+ throw new RetryableException();
+ } else {
+ return "pass";
+ }
+ }
+
+ private String succeedAfterTwoFailures(String input) {
+ if (round++ <= 2) {
+ throw new RetryableException();
+ } else {
+ return "pass";
+ }
+ }
+
+ private String failCompletely(String input) {
+ if (round++ <= 1) {
+ throw new NonRetryableException();
+ } else {
+ return "pass";
+ }
+ }
+
+ private class RetryableException extends RuntimeException {
+ }
+
+ private class NonRetryableException extends RuntimeException {
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/SharedExecutorsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/SharedExecutorsTest.java
new file mode 100644
index 00000000..1730ca1f
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/SharedExecutorsTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import org.junit.Test;
+
+import java.util.concurrent.ExecutorService;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests of the SharedExecutors Test.
+ */
+public class SharedExecutorsTest {
+
+ @Test
+ public void singleThread() {
+ ExecutorService a = SharedExecutors.getSingleThreadExecutor();
+ assertNotNull("ExecutorService must not be null", a);
+ ExecutorService b = SharedExecutors.getSingleThreadExecutor();
+ assertSame("factories should be same", a, b);
+
+ }
+
+ @Test
+ public void poolThread() {
+ ExecutorService a = SharedExecutors.getPoolThreadExecutor();
+ assertNotNull("ExecutorService must not be null", a);
+ ExecutorService b = SharedExecutors.getPoolThreadExecutor();
+ assertSame("factories should be same", a, b);
+
+ }
+
+ @Test
+ public void timer() {
+ java.util.Timer a = SharedExecutors.getTimer();
+ assertNotNull("Timer must not be null", a);
+ java.util.Timer b = SharedExecutors.getTimer();
+ assertSame("factories should be same", a, b);
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/SlidingWindowCounterTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/SlidingWindowCounterTest.java
new file mode 100644
index 00000000..c15cc8a6
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/SlidingWindowCounterTest.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static junit.framework.TestCase.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit tests for the sliding window counter.
+ */
+
+@Ignore("Disable these for now because of intermittent load related failures on Jenkins runs.")
+public class SlidingWindowCounterTest {
+
+ private SlidingWindowCounter counter;
+
+ @Before
+ public void setUp() {
+ counter = new SlidingWindowCounter(2);
+ }
+
+ @After
+ public void tearDown() {
+ counter.destroy();
+ }
+
+ @Test
+ public void testIncrementCount() {
+ assertEquals(0, counter.get(1));
+ assertEquals(0, counter.get(2));
+ counter.incrementCount();
+ assertEquals(1, counter.get(1));
+ assertEquals(1, counter.get(2));
+ counter.incrementCount(2);
+ assertEquals(3, counter.get(2));
+ }
+
+ @Test
+ public void testSlide() {
+ counter.incrementCount();
+ counter.advanceHead();
+ assertEquals(0, counter.get(1));
+ assertEquals(1, counter.get(2));
+ counter.incrementCount(2);
+ assertEquals(2, counter.get(1));
+ assertEquals(3, counter.get(2));
+ }
+
+ @Test
+ public void testWrap() {
+ counter.incrementCount();
+ counter.advanceHead();
+ counter.incrementCount(2);
+ counter.advanceHead();
+ assertEquals(0, counter.get(1));
+ assertEquals(2, counter.get(2));
+ counter.advanceHead();
+ assertEquals(0, counter.get(1));
+ assertEquals(0, counter.get(2));
+
+ }
+
+ @Test
+ public void testCornerCases() {
+ try {
+ counter.get(3);
+ fail("Exception should have been thrown");
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
+ try {
+ new SlidingWindowCounter(0);
+ fail("Exception should have been thrown");
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+
+ try {
+ new SlidingWindowCounter(-1);
+ fail("Exception should have been thrown");
+ } catch (IllegalArgumentException e) {
+ assertTrue(true);
+ }
+ }
+}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ToolsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ToolsTest.java
new file mode 100644
index 00000000..56f0f957
--- /dev/null
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ToolsTest.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.util;
+
+import org.junit.Test;
+import org.onlab.junit.TestTools;
+
+import java.util.concurrent.ThreadFactory;
+
+import static org.junit.Assert.*;
+import static org.onlab.junit.TestTools.assertAfter;
+
+/**
+ * Test of the miscellaneous tools.
+ */
+public class ToolsTest {
+
+ @Test
+ public void fromHex() throws Exception {
+ assertEquals(15, Tools.fromHex("0f"));
+ assertEquals(16, Tools.fromHex("10"));
+ assertEquals(65535, Tools.fromHex("ffff"));
+ assertEquals(4096, Tools.fromHex("1000"));
+ assertEquals(0xffffffffffffffffL, Tools.fromHex("ffffffffffffffff"));
+ }
+
+ @Test
+ public void toHex() throws Exception {
+ assertEquals("0f", Tools.toHex(15, 2));
+ assertEquals("ffff", Tools.toHex(65535, 4));
+ assertEquals("1000", Tools.toHex(4096, 4));
+ assertEquals("000000000000000f", Tools.toHex(15));
+ assertEquals("ffffffffffffffff", Tools.toHex(0xffffffffffffffffL));
+
+ }
+
+ @Test
+ public void namedThreads() {
+ ThreadFactory f = Tools.namedThreads("foo-%d");
+ Thread t = f.newThread(() -> TestTools.print("yo"));
+ assertTrue("wrong pattern", t.getName().startsWith("foo-"));
+ }
+
+ @Test
+ public void groupedThreads() {
+ ThreadFactory f = Tools.groupedThreads("foo/bar-me", "foo-%d");
+ Thread t = f.newThread(() -> TestTools.print("yo"));
+ assertTrue("wrong pattern", t.getName().startsWith("foo-bar-me-foo-"));
+ assertTrue("wrong group", t.getThreadGroup().getName().equals("foo/bar-me"));
+ }
+
+ @Test
+ public void exceptionHandler() throws InterruptedException {
+ ThreadFactory f = Tools.namedThreads("foo");
+ Thread t = f.newThread(() -> {
+ throw new IllegalStateException("BOOM!");
+ });
+ assertNotNull("thread should have exception handler", t.getUncaughtExceptionHandler());
+ t.start();
+ assertAfter(100, () -> assertEquals("incorrect thread state", Thread.State.TERMINATED, t.getState()));
+ }
+
+}