Merge "The second patch of an estimated 4 to complete the COPS message refactoring...
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSIpv6LastPdpAddrTest.java
1 package org.umu.cops.stack;
2
3 import org.junit.Assert;
4 import org.junit.Test;
5 import org.umu.cops.stack.COPSObjHeader.CNum;
6 import org.umu.cops.stack.COPSObjHeader.CType;
7
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;
13
14 /**
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.
18  */
19 public class COPSIpv6LastPdpAddrTest {
20
21     @Test(expected = IllegalArgumentException.class)
22     public void nullHost() throws Exception {
23         new COPSIpv6LastPdpAddr(null, 1234, (short)0);
24     }
25
26     @Test(expected = UnknownHostException.class)
27     public void invalidHost() throws Exception {
28         new COPSIpv6LastPdpAddr("foo", 1234, (short)0);
29     }
30
31     @Test(expected = IllegalArgumentException.class)
32     public void invalidPort() throws Exception {
33         new COPSIpv6LastPdpAddr("localhost", 0, (short)0);
34     }
35
36     @Test
37     public void validConstructor1() throws Exception {
38         final COPSIpv6LastPdpAddr lastAddr = new COPSIpv6LastPdpAddr("localhost", 1234, (short)0);
39         Assert.assertEquals(20, lastAddr.getDataLength());
40         Assert.assertEquals(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.STATELESS), lastAddr.getHeader());
41         Assert.assertEquals(1234, lastAddr.getTcpPort());
42         Assert.assertEquals(0, lastAddr.getReserved());
43
44         final ByteArrayOutputStream os = new ByteArrayOutputStream();
45         lastAddr.dump(os);
46
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("**Last PDP addr**", lines[0]);
52         Assert.assertEquals("C-num: LAST_PDP_ADDR", lines[1]);
53         Assert.assertEquals("C-type: STATELESS", lines[2]);
54         Assert.assertEquals("Ipv6LastPdpAddr", lines[3]);
55         Assert.assertEquals("Address: localhost", lines[4]);
56         Assert.assertEquals("Port: 1234", lines[5]);
57     }
58
59     @Test(expected = IllegalArgumentException.class)
60     public void nullObjHeader() throws Exception {
61         final byte[] addr = getLocalhostIpv6Address();
62         new COPSIpv6LastPdpAddr(null, addr, 1234, (short)0);
63     }
64
65     @Test(expected = IllegalArgumentException.class)
66     public void invalidCNum() throws Exception {
67         final byte[] addr = getLocalhostIpv6Address();
68         new COPSIpv6LastPdpAddr(new COPSObjHeader(CNum.ACCT_TIMER, CType.STATELESS), addr, 1234, (short)0);
69     }
70
71     @Test(expected = IllegalArgumentException.class)
72     public void invalidCType() throws Exception {
73         final byte[] addr = getLocalhostIpv6Address();
74         new COPSIpv6LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF), addr, 1234, (short)0);
75     }
76
77     @Test(expected = IllegalArgumentException.class)
78     public void nullAddr() throws Exception {
79         new COPSIpv6LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.STATELESS), null, 1234, (short)0);
80     }
81
82     @Test(expected = IllegalArgumentException.class)
83     public void ipv4Addr() throws Exception {
84         final byte[] addr = new byte[] { 127, 0, 0, 1};
85         new COPSIpv6LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.STATELESS), addr, 1234, (short)0);
86     }
87
88     @Test(expected = IllegalArgumentException.class)
89     public void zeroPort() throws Exception {
90         final byte[] addr = getLocalhostIpv6Address();
91         new COPSIpv6LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.STATELESS), addr, 0, (short)0);
92     }
93
94     @Test
95     public void validConstructor2() throws Exception {
96         final byte[] addr = getLocalhostIpv6Address();
97         final COPSIpv6LastPdpAddr lastAddr = new COPSIpv6LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR,
98                 CType.STATELESS), addr, 1234, (short)0);
99         Assert.assertEquals(20, lastAddr.getDataLength());
100         Assert.assertEquals(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.STATELESS), lastAddr.getHeader());
101         Assert.assertEquals(1234, lastAddr.getTcpPort());
102         Assert.assertEquals(0, lastAddr.getReserved());
103
104         final ByteArrayOutputStream os = new ByteArrayOutputStream();
105         lastAddr.dump(os);
106
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("**Last PDP addr**", lines[0]);
112         Assert.assertEquals("C-num: LAST_PDP_ADDR", lines[1]);
113         Assert.assertEquals("C-type: STATELESS", lines[2]);
114         Assert.assertEquals("Ipv6LastPdpAddr", lines[3]);
115         Assert.assertEquals("Address: localhost", lines[4]);
116         Assert.assertEquals("Port: 1234", lines[5]);
117     }
118
119     // The writeData() method will be tested implicitly via any of the COPSMsg tests
120
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();
126             }
127         }
128         throw new UnknownHostException("InetAddress could not be found");
129     }
130 }