1 package org.umu.cops.stack;
3 import org.junit.Assert;
6 import java.net.Inet4Address;
7 import java.net.Inet6Address;
8 import java.net.InetAddress;
9 import java.net.UnknownHostException;
12 * Tests for the COPSIpv6Address class.
14 public class COPSIpv6AddressTest {
16 @Test(expected = UnknownHostException.class)
17 public void badHost() throws Exception {
18 new COPSIpv6Address("foo");
22 public void localhost() throws Exception {
23 final COPSIpv6Address address = new COPSIpv6Address("localhost");
24 Assert.assertEquals(16, address.getDataLength());
25 Assert.assertArrayEquals(getLocalhostIpv6Address(), address.getAddressBytes());
26 Assert.assertEquals("localhost", address.getIpName());
30 public void addrBytes() throws Exception {
31 final byte[] addr = getLocalhostIpv6Address();
32 final COPSIpv6Address address = new COPSIpv6Address(addr);
33 Assert.assertEquals(16, address.getDataLength());
34 Assert.assertArrayEquals(addr, address.getAddressBytes());
35 Assert.assertEquals("localhost", address.getIpName());
38 private byte[] getLocalhostIpv6Address() throws UnknownHostException {
39 final InetAddress[] addrs = Inet4Address.getAllByName("localhost");
40 for (final InetAddress addr : addrs) {
41 if (addr instanceof Inet6Address) {
42 return addr.getAddress();
45 throw new UnknownHostException("InetAddress could not be found");