1 package org.umu.cops.stack;
3 import org.junit.Assert;
5 import org.umu.cops.stack.COPSObjHeader.CNum;
6 import org.umu.cops.stack.COPSObjHeader.CType;
8 import java.io.ByteArrayOutputStream;
9 import java.net.Inet4Address;
10 import java.net.Inet6Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
15 * Tests for the first constructor of the COPSIpv6PdpRedirectAddress class.
16 * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSAcctTimer had been
17 * released and my assumptions may be incorrect.
19 public class COPSIpv6PdpRedirectAddressTest {
21 @Test(expected = IllegalArgumentException.class)
22 public void nullHost() throws Exception {
23 new COPSIpv6PdpRedirectAddress(null, 1234, (short)0);
26 @Test(expected = UnknownHostException.class)
27 public void invalidHost() throws Exception {
28 new COPSIpv6PdpRedirectAddress("foo", 1234, (short)0);
31 @Test(expected = IllegalArgumentException.class)
32 public void invalidPort() throws Exception {
33 new COPSIpv6PdpRedirectAddress("localhost", 0, (short)0);
37 public void validConstructor1() throws Exception {
38 final COPSIpv6PdpRedirectAddress lastAddr = new COPSIpv6PdpRedirectAddress("localhost", 1234, (short)0);
39 Assert.assertEquals(20, lastAddr.getDataLength());
40 Assert.assertEquals(new COPSObjHeader(CNum.PDP_REDIR, CType.STATELESS), lastAddr.getHeader());
41 Assert.assertEquals(1234, lastAddr.getTcpPort());
42 Assert.assertEquals(0, lastAddr.getReserved());
44 final ByteArrayOutputStream os = new ByteArrayOutputStream();
47 final String out = new String(os.toByteArray());
48 System.out.println(out);
49 final String[] lines = out.split("\n");
50 Assert.assertEquals(6, lines.length);
51 Assert.assertEquals("**Redirect PDP addr**", lines[0]);
52 Assert.assertEquals("C-num: PDP_REDIR", lines[1]);
53 Assert.assertEquals("C-type: STATELESS", lines[2]);
54 Assert.assertEquals("Ipv6PdpRedirectAddress", lines[3]);
55 Assert.assertTrue(lines[4].equals("Address: localhost") || lines[4].equals("Address: 0:0:0:0:0:0:0:1"));
56 Assert.assertEquals("Port: 1234", lines[5]);
59 @Test(expected = IllegalArgumentException.class)
60 public void nullObjHeader() throws Exception {
61 final byte[] addr = getLocalhostIpv6Address();
62 new COPSIpv6PdpRedirectAddress(null, addr, 1234, (short)0);
65 @Test(expected = IllegalArgumentException.class)
66 public void invalidCNum() throws Exception {
67 final byte[] addr = getLocalhostIpv6Address();
68 new COPSIpv6PdpRedirectAddress(new COPSObjHeader(CNum.ACCT_TIMER, CType.STATELESS), addr, 1234, (short)0);
71 @Test(expected = IllegalArgumentException.class)
72 public void invalidCType() throws Exception {
73 final byte[] addr = getLocalhostIpv6Address();
74 new COPSIpv6PdpRedirectAddress(new COPSObjHeader(CNum.PDP_REDIR, CType.DEF), addr, 1234, (short)0);
77 @Test(expected = IllegalArgumentException.class)
78 public void nullAddr() throws Exception {
79 new COPSIpv6PdpRedirectAddress(new COPSObjHeader(CNum.PDP_REDIR, CType.STATELESS), null, 1234, (short)0);
82 @Test(expected = IllegalArgumentException.class)
83 public void ipv4Addr() throws Exception {
84 final byte[] addr = new byte[] { 127, 0, 0, 1};
85 new COPSIpv6PdpRedirectAddress(new COPSObjHeader(CNum.PDP_REDIR, CType.STATELESS), addr, 1234, (short)0);
88 @Test(expected = IllegalArgumentException.class)
89 public void zeroPort() throws Exception {
90 final byte[] addr = getLocalhostIpv6Address();
91 new COPSIpv6PdpRedirectAddress(new COPSObjHeader(CNum.PDP_REDIR, CType.STATELESS), addr, 0, (short)0);
95 public void validConstructor2() throws Exception {
96 final byte[] addr = getLocalhostIpv6Address();
97 final COPSIpv6PdpRedirectAddress lastAddr = new COPSIpv6PdpRedirectAddress(new COPSObjHeader(CNum.PDP_REDIR,
98 CType.STATELESS), addr, 1234, (short)0);
99 Assert.assertEquals(20, lastAddr.getDataLength());
100 Assert.assertEquals(new COPSObjHeader(CNum.PDP_REDIR, CType.STATELESS), lastAddr.getHeader());
101 Assert.assertEquals(1234, lastAddr.getTcpPort());
102 Assert.assertEquals(0, lastAddr.getReserved());
104 final ByteArrayOutputStream os = new ByteArrayOutputStream();
107 final String out = new String(os.toByteArray());
108 System.out.println(out);
109 final String[] lines = out.split("\n");
110 Assert.assertEquals(6, lines.length);
111 Assert.assertEquals("**Redirect PDP addr**", lines[0]);
112 Assert.assertEquals("C-num: PDP_REDIR", lines[1]);
113 Assert.assertEquals("C-type: STATELESS", lines[2]);
114 Assert.assertEquals("Ipv6PdpRedirectAddress", lines[3]);
115 Assert.assertTrue(lines[4].equals("Address: localhost") || lines[4].equals("Address: 0:0:0:0:0:0:0:1"));
116 Assert.assertEquals("Port: 1234", lines[5]);
119 // The writeData() method will be tested implicitly via any of the COPSMsg tests
121 private byte[] getLocalhostIpv6Address() throws UnknownHostException {
122 final InetAddress[] addrs = Inet4Address.getAllByName("localhost");
123 for (final InetAddress addr : addrs) {
124 if (addr instanceof Inet6Address) {
125 return addr.getAddress();
128 throw new UnknownHostException("InetAddress could not be found");