Completed COPS Message refactoring. Was planning on one additional patch starting...
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSClientOpenMsgTest.java
1 package org.umu.cops.stack;
2
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.umu.cops.stack.COPSClientSI.CSIType;
8 import org.umu.cops.stack.COPSHeader.ClientType;
9 import org.umu.cops.stack.COPSHeader.Flag;
10 import org.umu.cops.stack.COPSHeader.OPCode;
11
12 import java.io.ByteArrayOutputStream;
13 import java.net.InetAddress;
14 import java.net.Socket;
15
16 /**
17  * Tests for the first constructor of the COPSClientOpenMsg class.
18  * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSClientOpenMsg had been
19  * released and my assumptions may be incorrect.
20  */
21 public class COPSClientOpenMsgTest {
22
23     private final static int testPort = 7777;
24     TestCOPSServer server;
25     Socket outSocket;
26
27     @Before
28     public void setup() throws Exception {
29         server = new TestCOPSServer(testPort);
30         server.start();
31         outSocket = new Socket(InetAddress.getLocalHost(), testPort);
32     }
33
34     @After
35     public void tearDown() throws Exception {
36         outSocket.close();
37         server.close();
38     }
39
40     @Test(expected = IllegalArgumentException.class)
41     public void version0() {
42         new COPSClientOpenMsg(0, Flag.SOLICITED, ClientType.TYPE_1, new COPSPepId(new COPSData()),
43                 null, null, null);
44     }
45
46     @Test(expected = IllegalArgumentException.class)
47     public void nullFlag() {
48         new COPSClientOpenMsg(1, null, ClientType.TYPE_1, new COPSPepId(new COPSData()), null, null, null);
49     }
50
51     @Test(expected = IllegalArgumentException.class)
52     public void nullClientType() {
53         new COPSClientOpenMsg(1, Flag.SOLICITED, null, new COPSPepId(new COPSData()), null, null, null);
54     }
55
56     @Test(expected = IllegalArgumentException.class)
57     public void nullPepId() {
58         new COPSClientOpenMsg(1, Flag.SOLICITED, ClientType.TYPE_1, null, null, null, null);
59     }
60
61     @Test(expected = IllegalArgumentException.class)
62     public void nullHeader() {
63         final COPSHeader hdr = null;
64         new COPSClientOpenMsg(hdr, new COPSPepId(new COPSData()), null, null, null);
65     }
66
67     @Test(expected = IllegalArgumentException.class)
68     public void invalidHeader() {
69         final COPSHeader hdr = new COPSHeader(1, Flag.UNSOLICITED, OPCode.CAT, ClientType.TYPE_1);
70         new COPSClientOpenMsg(hdr, new COPSPepId(new COPSData()), null, null, null);
71     }
72
73     @Test
74     public void validMinimal() {
75         final COPSClientOpenMsg msg = new COPSClientOpenMsg(1, Flag.SOLICITED, ClientType.TYPE_2,
76                 new COPSPepId(new COPSData("12345")), null, null, null);
77
78         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
79         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
80         Assert.assertEquals(ClientType.TYPE_2, msg.getHeader().getClientType());
81         Assert.assertEquals(new COPSPepId(new COPSData("12345")), msg.getPepId());
82         Assert.assertNull(msg.getClientSI());
83         Assert.assertNull(msg.getPdpAddress());
84         Assert.assertNull(msg.getIntegrity());
85     }
86
87     @Test
88     public void validAllIpv4() throws Exception {
89         final COPSClientOpenMsg msg = new COPSClientOpenMsg(1, Flag.UNSOLICITED, ClientType.TYPE_1,
90                 new COPSPepId(new COPSData("12345")),
91                 new COPSClientSI(CSIType.NAMED, new COPSData("123456")),
92                 new COPSIpv4LastPdpAddr("localhost", 7777, (short)0),
93                 new COPSIntegrity(3, 4, new COPSData("1234567")));
94
95         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
96         Assert.assertEquals(Flag.UNSOLICITED, msg.getHeader().getFlag());
97         Assert.assertEquals(ClientType.TYPE_1, msg.getHeader().getClientType());
98         Assert.assertEquals(new COPSPepId(new COPSData("12345")), msg.getPepId());
99         Assert.assertEquals(new COPSClientSI(CSIType.NAMED, new COPSData("123456")), msg.getClientSI());
100         Assert.assertEquals(new COPSIpv4LastPdpAddr("localhost", 7777, (short) 0), msg.getPdpAddress());
101         Assert.assertEquals(new COPSIntegrity(3, 4, new COPSData("1234567")), msg.getIntegrity());
102     }
103
104     @Test
105     public void validAllIpv6() throws Exception {
106         final COPSClientOpenMsg msg = new COPSClientOpenMsg(1, Flag.UNSOLICITED, ClientType.TYPE_1,
107                 new COPSPepId(new COPSData("12345")),
108                 new COPSClientSI(CSIType.NAMED, new COPSData("123456")),
109                 new COPSIpv6LastPdpAddr("localhost", 7777, (short)0),
110                 new COPSIntegrity(3, 4, new COPSData("1234567")));
111
112         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
113         Assert.assertEquals(Flag.UNSOLICITED, msg.getHeader().getFlag());
114         Assert.assertEquals(ClientType.TYPE_1, msg.getHeader().getClientType());
115         Assert.assertEquals(new COPSPepId(new COPSData("12345")), msg.getPepId());
116         Assert.assertEquals(new COPSClientSI(CSIType.NAMED, new COPSData("123456")), msg.getClientSI());
117         Assert.assertEquals(new COPSIpv6LastPdpAddr("localhost", 7777, (short)0), msg.getPdpAddress());
118         Assert.assertEquals(new COPSIntegrity(3, 4, new COPSData("1234567")), msg.getIntegrity());
119     }
120
121     /**
122      * This test is responsible for creating a COPSClientOpenMsg object without any nulls or empty collections
123      * and then is dumped to an OutputStream.
124      * @throws Exception - Test should fail if any exception is thrown
125      */
126     @Test
127     public void testDumpAll() throws Exception {
128         final COPSClientOpenMsg msg = new COPSClientOpenMsg(1, Flag.UNSOLICITED, ClientType.TYPE_1,
129                 new COPSPepId(new COPSData("12345")),
130                 new COPSClientSI(CSIType.NAMED, new COPSData("123456")),
131                 new COPSIpv4LastPdpAddr("localhost", 7777, (short)0),
132                 new COPSIntegrity(3, 4, new COPSData("1234567")));
133
134         final ByteArrayOutputStream os = new ByteArrayOutputStream();
135         msg.dump(os);
136
137         final String out = new String(os.toByteArray());
138         System.out.println(out);
139         final String[] lines = out.split("\n");
140         Assert.assertEquals(26, lines.length);
141
142         // Only checking COPSMsg elements as the COPSObjectMsg elements have already been validated in their own tests
143         Assert.assertEquals("**MSG HEADER**", lines[0]);
144         Assert.assertEquals("Version: 1", lines[1]);
145         Assert.assertEquals("Flags: UNSOLICITED", lines[2]);
146         Assert.assertEquals("OpCode: OPN", lines[3]);
147         Assert.assertEquals("Client-type: TYPE_1", lines[4]);
148     }
149
150     /**
151      * This test is responsible for creating a COPSClientOpenMsg object with the minimal necessary attributes to make
152      * it valid. It is then streamed over a socket (unmarshalled) then reassembled (marshalled).
153      * @throws Exception - Test should fail if any exception is thrown
154      */
155     @Test
156     public void testWriteMinimal() throws Exception {
157         final COPSClientOpenMsg msg = new COPSClientOpenMsg(1, Flag.SOLICITED, ClientType.TYPE_2,
158                 new COPSPepId(new COPSData("12345")), null, null, null);
159
160         msg.writeData(outSocket);
161
162         final long start = System.currentTimeMillis();
163         while (server.copsMsgs.size() < 1) {
164             Thread.sleep(5);
165             if (System.currentTimeMillis() - start > 2000) break;
166         }
167
168         Assert.assertEquals(1, server.copsMsgs.size());
169         Assert.assertEquals(msg, server.copsMsgs.get(0));
170     }
171
172     /**
173      * This test is responsible for creating a COPSClientOpenMsg object without any nulls for IPv4 addresses
174      * and then is streamed over a socket (unmarshalled) then reassembled (marshalled)
175      * @throws Exception - Test should fail if any exception is thrown
176      */
177     @Test
178     public void testWriteAllIpv4() throws Exception {
179         final COPSClientOpenMsg msg = new COPSClientOpenMsg(1, Flag.UNSOLICITED, ClientType.TYPE_1,
180                 new COPSPepId(new COPSData("12345")),
181                 new COPSClientSI(CSIType.NAMED, new COPSData("123456")),
182                 new COPSIpv4LastPdpAddr("localhost", 7777, (short)0),
183                 new COPSIntegrity(3, 4, new COPSData("1234567")));
184
185         msg.writeData(outSocket);
186
187         final long start = System.currentTimeMillis();
188         while (server.copsMsgs.size() < 1) {
189             Thread.sleep(5);
190             if (System.currentTimeMillis() - start > 2000) break;
191         }
192
193         Assert.assertEquals(1, server.copsMsgs.size());
194         Assert.assertEquals(msg, server.copsMsgs.get(0));
195     }
196
197     /**
198      * This test is responsible for creating a COPSClientOpenMsg object without any nulls for IPv6 addresses
199      * and then is streamed over a socket (unmarshalled) then reassembled (marshalled)
200      * @throws Exception - Test should fail if any exception is thrown
201      */
202     @Test
203     public void testWriteAllIpv6() throws Exception {
204         final COPSClientOpenMsg msg = new COPSClientOpenMsg(1, Flag.UNSOLICITED, ClientType.TYPE_2,
205                 new COPSPepId(new COPSData("12345")),
206                 new COPSClientSI(CSIType.NAMED, new COPSData("123456")),
207                 new COPSIpv6LastPdpAddr("localhost", 7777, (short)0),
208                 new COPSIntegrity(3, 4, new COPSData("1234567")));
209
210         msg.writeData(outSocket);
211
212         final long start = System.currentTimeMillis();
213         while (server.copsMsgs.size() < 1) {
214             Thread.sleep(5);
215             if (System.currentTimeMillis() - start > 2000) break;
216         }
217
218         Assert.assertEquals(1, server.copsMsgs.size());
219         Assert.assertEquals(msg, server.copsMsgs.get(0));
220     }
221
222 }