Added new constructor. May need to tighten down interfaces in the future.
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSIpv6AddressTest.java
1 package org.umu.cops.stack;
2
3 import org.junit.Assert;
4 import org.junit.Test;
5
6 import java.net.Inet4Address;
7 import java.net.Inet6Address;
8 import java.net.InetAddress;
9 import java.net.UnknownHostException;
10
11 /**
12  * Tests for the COPSIpv6Address class.
13  */
14 public class COPSIpv6AddressTest {
15
16     @Test(expected = UnknownHostException.class)
17     public void badHost() throws Exception {
18         new COPSIpv6Address("foo");
19     }
20
21     @Test
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());
27     }
28
29     @Test
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());
36     }
37
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();
43             }
44         }
45         throw new UnknownHostException("InetAddress could not be found");
46     }
47 }