blob: 84ef0b37b9fc2534c08291d8107cddfc8b4ce60c [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070016package org.onlab.packet;
17
18import org.junit.Test;
19
20import static org.hamcrest.Matchers.equalTo;
21import static org.hamcrest.Matchers.is;
22import static org.hamcrest.Matchers.not;
23import static org.junit.Assert.assertThat;
24import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
25
26/**
27 * Tests for class {@link Ip4Prefix}.
28 */
29public class Ip4PrefixTest {
30 /**
31 * Tests the immutability of {@link Ip4Prefix}.
32 */
33 @Test
34 public void testImmutable() {
35 assertThatClassIsImmutable(Ip4Prefix.class);
36 }
37
38 /**
39 * Tests default class constructor.
40 */
41 @Test
42 public void testDefaultConstructor() {
43 Ip4Prefix ip4prefix = new Ip4Prefix();
44 assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
45 }
46
47 /**
48 * Tests valid class copy constructor.
49 */
50 @Test
51 public void testCopyConstructor() {
52 Ip4Prefix fromAddr = new Ip4Prefix("1.2.3.0/24");
53 Ip4Prefix ip4prefix = new Ip4Prefix(fromAddr);
54 assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
55
56 fromAddr = new Ip4Prefix("0.0.0.0/0");
57 ip4prefix = new Ip4Prefix(fromAddr);
58 assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
59
60 fromAddr = new Ip4Prefix("255.255.255.255/32");
61 ip4prefix = new Ip4Prefix(fromAddr);
62 assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
63 }
64
65 /**
66 * Tests invalid class copy constructor for a null object to copy from.
67 */
68 @Test(expected = NullPointerException.class)
69 public void testInvalidConstructorNullObject() {
70 Ip4Prefix fromAddr = null;
71 Ip4Prefix ip4prefix = new Ip4Prefix(fromAddr);
72 }
73
74 /**
75 * Tests valid class constructor for an address and prefix length.
76 */
77 @Test
78 public void testConstructorForAddressAndPrefixLength() {
79 Ip4Prefix ip4prefix =
80 new Ip4Prefix(new Ip4Address("1.2.3.0"), (short) 24);
81 assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
82
83 ip4prefix = new Ip4Prefix(new Ip4Address("1.2.3.4"), (short) 24);
84 assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
85
86 ip4prefix = new Ip4Prefix(new Ip4Address("1.2.3.5"), (short) 32);
87 assertThat(ip4prefix.toString(), is("1.2.3.5/32"));
88
89 ip4prefix = new Ip4Prefix(new Ip4Address("0.0.0.0"), (short) 0);
90 assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
91
92 ip4prefix =
93 new Ip4Prefix(new Ip4Address("255.255.255.255"), (short) 32);
94 assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
95 }
96
97 /**
98 * Tests valid class constructor for a string.
99 */
100 @Test
101 public void testConstructorForString() {
102 Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24");
103 assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
104
105 ip4prefix = new Ip4Prefix("1.2.3.4/24");
106 assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
107
108 ip4prefix = new Ip4Prefix("1.2.3.5/32");
109 assertThat(ip4prefix.toString(), is("1.2.3.5/32"));
110
111 ip4prefix = new Ip4Prefix("0.0.0.0/0");
112 assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
113
114 ip4prefix = new Ip4Prefix("255.255.255.255/32");
115 assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
116 }
117
118 /**
119 * Tests invalid class constructor for a null string.
120 */
121 @Test(expected = NullPointerException.class)
122 public void testInvalidConstructorNullString() {
123 String fromString = null;
124 Ip4Prefix ip4prefix = new Ip4Prefix(fromString);
125 }
126
127 /**
128 * Tests invalid class constructor for an empty string.
129 */
130 @Test(expected = IllegalArgumentException.class)
131 public void testInvalidConstructors() {
132 // Check constructor for invalid ID: empty string
133 Ip4Prefix ip4prefix = new Ip4Prefix("");
134 }
135
136 /**
137 * Tests getting the value of an address.
138 */
139 @Test
140 public void testGetValue() {
141 Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24");
142 assertThat(ip4prefix.getAddress(), equalTo(new Ip4Address("1.2.3.0")));
143 assertThat(ip4prefix.getPrefixLen(), is((short) 24));
144
145 ip4prefix = new Ip4Prefix("0.0.0.0/0");
146 assertThat(ip4prefix.getAddress(), equalTo(new Ip4Address("0.0.0.0")));
147 assertThat(ip4prefix.getPrefixLen(), is((short) 0));
148
149 ip4prefix = new Ip4Prefix("255.255.255.255/32");
150 assertThat(ip4prefix.getAddress(),
151 equalTo(new Ip4Address("255.255.255.255")));
152 assertThat(ip4prefix.getPrefixLen(), is((short) 32));
153 }
154
155 /**
156 * Tests equality of {@link Ip4Address}.
157 */
158 @Test
159 public void testEquality() {
160 Ip4Prefix addr1net = new Ip4Prefix("1.2.3.0/24");
161 Ip4Prefix addr2net = new Ip4Prefix("1.2.3.0/24");
162 assertThat(addr1net, is(addr2net));
163
164 addr1net = new Ip4Prefix("1.2.3.0/24");
165 addr2net = new Ip4Prefix("1.2.3.4/24");
166 assertThat(addr1net, is(addr2net));
167
168 addr1net = new Ip4Prefix("0.0.0.0/0");
169 addr2net = new Ip4Prefix("0.0.0.0/0");
170 assertThat(addr1net, is(addr2net));
171
172 addr1net = new Ip4Prefix("255.255.255.255/32");
173 addr2net = new Ip4Prefix("255.255.255.255/32");
174 assertThat(addr1net, is(addr2net));
175 }
176
177 /**
178 * Tests non-equality of {@link Ip4Address}.
179 */
180 @Test
181 public void testNonEquality() {
182 Ip4Prefix addr1net = new Ip4Prefix("1.2.0.0/16");
183 Ip4Prefix addr2net = new Ip4Prefix("1.3.0.0/16");
184 Ip4Prefix addr3net = new Ip4Prefix("1.3.0.0/24");
185 Ip4Prefix addr4net = new Ip4Prefix("0.0.0.0/0");
186 Ip4Prefix addr5net = new Ip4Prefix("255.255.255.255/32");
187 assertThat(addr1net, is(not(addr2net)));
188 assertThat(addr3net, is(not(addr2net)));
189 assertThat(addr4net, is(not(addr2net)));
190 assertThat(addr5net, is(not(addr2net)));
191 }
192
193 /**
194 * Tests object string representation.
195 */
196 @Test
197 public void testToString() {
198 Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24");
199 assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
200
201 ip4prefix = new Ip4Prefix("1.2.3.4/24");
202 assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
203
204 ip4prefix = new Ip4Prefix("0.0.0.0/0");
205 assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
206
207 ip4prefix = new Ip4Prefix("255.255.255.255/32");
208 assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
209 }
210}