\rModified JUnit tests to allow IP Address form of 'localhost' to support Windows...
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSIpv4LastPdpAddrTest.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.InetAddress;
10 import java.net.UnknownHostException;
11
12 /**
13  * Tests for the first constructor of the COPSIpv4LastPdpAddr class.
14  * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSAcctTimer had been
15  * released and my assumptions may be incorrect.
16  */
17 public class COPSIpv4LastPdpAddrTest {
18
19     @Test(expected = IllegalArgumentException.class)
20     public void nullHost() throws Exception {
21         new COPSIpv4LastPdpAddr(null, 1234, (short)0);
22     }
23
24     @Test(expected = UnknownHostException.class)
25     public void invalidHost() throws Exception {
26         new COPSIpv4LastPdpAddr("foo", 1234, (short)0);
27     }
28
29     @Test(expected = IllegalArgumentException.class)
30     public void invalidPort() throws Exception {
31         new COPSIpv4LastPdpAddr("localhost", 0, (short)0);
32     }
33
34     @Test
35     public void validConstructor1() throws Exception {
36         final COPSIpv4LastPdpAddr lastAddr = new COPSIpv4LastPdpAddr("localhost", 1234, (short)0);
37         Assert.assertEquals(8, lastAddr.getDataLength());
38         Assert.assertEquals(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF), lastAddr.getHeader());
39         Assert.assertEquals(1234, lastAddr.getTcpPort());
40         Assert.assertEquals(0, lastAddr.getReserved());
41
42         final ByteArrayOutputStream os = new ByteArrayOutputStream();
43         lastAddr.dump(os);
44
45         final String out = new String(os.toByteArray());
46         System.out.println(out);
47         final String[] lines = out.split("\n");
48         Assert.assertEquals(6, lines.length);
49         Assert.assertEquals("**Last PDP addr**", lines[0]);
50         Assert.assertEquals("C-num: LAST_PDP_ADDR", lines[1]);
51         Assert.assertEquals("C-type: DEF", lines[2]);
52         Assert.assertEquals("Ipv4LastPdpAddress", lines[3]);
53         Assert.assertTrue(lines[4].equals("Address: localhost") || lines[4].equals("Address: 127.0.0.1"));
54         Assert.assertEquals("Port: 1234", lines[5]);
55     }
56
57     @Test(expected = IllegalArgumentException.class)
58     public void nullObjHeader() throws Exception {
59         final byte[] addr = InetAddress.getByName("localhost").getAddress();
60         new COPSIpv4LastPdpAddr(null, addr, 1234, (short)0);
61     }
62
63     @Test(expected = IllegalArgumentException.class)
64     public void invalidCNum() throws Exception {
65         final byte[] addr = InetAddress.getByName("localhost").getAddress();
66         new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.ACCT_TIMER, CType.DEF), addr, 1234, (short)0);
67     }
68
69     @Test(expected = IllegalArgumentException.class)
70     public void invalidCType() throws Exception {
71         final byte[] addr = InetAddress.getByName("localhost").getAddress();
72         new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.STATELESS), addr, 1234, (short)0);
73     }
74
75     @Test(expected = IllegalArgumentException.class)
76     public void nullAddr() throws Exception {
77         new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF), null, 1234, (short)0);
78     }
79
80     @Test(expected = IllegalArgumentException.class)
81     public void zeroPort() throws Exception {
82         final byte[] addr = InetAddress.getByName("localhost").getAddress();
83         new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF), addr, 0, (short)0);
84     }
85
86     @Test
87     public void validConstructor2() throws Exception {
88         final byte[] addr = InetAddress.getByName("localhost").getAddress();
89         final COPSIpv4LastPdpAddr lastAddr = new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF),
90                 addr, 1234, (short)0);
91         Assert.assertEquals(8, lastAddr.getDataLength());
92         Assert.assertEquals(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF), lastAddr.getHeader());
93         Assert.assertEquals(1234, lastAddr.getTcpPort());
94         Assert.assertEquals(0, lastAddr.getReserved());
95
96         final ByteArrayOutputStream os = new ByteArrayOutputStream();
97         lastAddr.dump(os);
98
99         final String out = new String(os.toByteArray());
100         System.out.println(out);
101         final String[] lines = out.split("\n");
102         Assert.assertEquals(6, lines.length);
103         Assert.assertEquals("**Last PDP addr**", lines[0]);
104         Assert.assertEquals("C-num: LAST_PDP_ADDR", lines[1]);
105         Assert.assertEquals("C-type: DEF", lines[2]);
106         Assert.assertEquals("Ipv4LastPdpAddress", lines[3]);
107         Assert.assertTrue(lines[4].equals("Address: localhost") || lines[4].equals("Address: 127.0.0.1"));
108         Assert.assertEquals("Port: 1234", lines[5]);
109     }
110
111     // The writeData() method will be tested implicitly via any of the COPSMsg tests
112 }