1 package org.umu.cops.stack;
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
7 import org.pcmm.rcd.IPCMMClient;
8 import org.umu.cops.stack.COPSError.ErrorTypes;
9 import org.umu.cops.stack.COPSHeader.Flag;
10 import org.umu.cops.stack.COPSHeader.OPCode;
12 import java.io.ByteArrayOutputStream;
13 import java.net.InetAddress;
14 import java.net.Socket;
17 * Tests for the first constructor of the COPSClientCloseMsg class.
18 * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSClientCloseMsg had been
19 * released and my assumptions may be incorrect.
21 public class COPSClientCloseMsgTest {
23 TestCOPSServer server;
27 public void setup() throws Exception {
28 server = new TestCOPSServer();
30 outSocket = new Socket(InetAddress.getLocalHost(), server.getPort());
34 public void tearDown() throws Exception {
39 @Test(expected = IllegalArgumentException.class)
40 public void version0() {
41 new COPSClientCloseMsg(0, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
42 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
45 @Test(expected = IllegalArgumentException.class)
46 public void nullFlag() {
47 new COPSClientCloseMsg(1, null, IPCMMClient.CLIENT_TYPE,
48 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
51 @Test(expected = IllegalArgumentException.class)
52 public void nullError() {
53 new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null, null);
56 @Test(expected = IllegalArgumentException.class)
57 public void nullHeader() {
58 final COPSHeader hdr = null;
59 new COPSClientCloseMsg(hdr, new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
62 @Test(expected = IllegalArgumentException.class)
63 public void invalidHeader() {
64 final COPSHeader hdr = new COPSHeader(1, Flag.UNSOLICITED, OPCode.NA, IPCMMClient.CLIENT_TYPE);
65 new COPSClientCloseMsg(hdr, new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
69 public void validMinimal() {
70 final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
71 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
73 Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
74 Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
75 Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
76 Assert.assertEquals(new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), msg.getError());
77 Assert.assertNull(msg.getRedirAddr());
78 Assert.assertNull(msg.getIntegrity());
82 public void validAll() throws Exception {
83 final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
84 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED),
85 new COPSIpv4PdpRedirectAddress("localhost", 7777, (short)0),
88 Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
89 Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
90 Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
91 Assert.assertEquals(new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), msg.getError());
92 Assert.assertEquals(new COPSIpv4PdpRedirectAddress("localhost", 7777, (short) 0), msg.getRedirAddr());
93 Assert.assertEquals(new COPSIntegrity(), msg.getIntegrity());
97 * This test is responsible for creating a COPSClientCloseMsg object without any nulls or empty collections
98 * and then is dumped to an OutputStream.
99 * @throws Exception - Test should fail if any exception is thrown
102 public void testDumpAll() throws Exception {
103 final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
104 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED),
105 new COPSIpv4PdpRedirectAddress("localhost", 7777, (short)0),
106 new COPSIntegrity());
108 final ByteArrayOutputStream os = new ByteArrayOutputStream();
111 final String out = new String(os.toByteArray());
112 System.out.println(out);
113 final String[] lines = out.split("\n");
114 Assert.assertEquals(22, lines.length);
116 // Only checking COPSMsg elements as the COPSObjectMsg elements have already been validated in their own tests
117 Assert.assertEquals("**MSG HEADER**", lines[0]);
118 Assert.assertEquals("Version: 1", lines[1]);
119 Assert.assertEquals("Flags: SOLICITED", lines[2]);
120 Assert.assertEquals("OpCode: CC", lines[3]);
121 Assert.assertEquals("Client-type: -32758", lines[4]);
125 * This test is responsible for creating a COPSClientCloseMsg object with the minimal necessary attributes to make
126 * it valid. It is then streamed over a socket (unmarshalled) then reassembled (marshalled).
127 * @throws Exception - Test should fail if any exception is thrown
130 public void testWriteMinimal() throws Exception {
131 final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
132 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
134 msg.writeData(outSocket);
136 final long start = System.currentTimeMillis();
137 while (server.copsMsgs.size() < 1) {
139 if (System.currentTimeMillis() - start > 2000) break;
142 Assert.assertEquals(1, server.copsMsgs.size());
143 Assert.assertEquals(msg, server.copsMsgs.get(0));
147 * This test is responsible for creating a COPSClientCloseMsg object without any nulls or empty collections
148 * and then is streamed over a socket (unmarshalled) then reassembled (marshalled)
149 * @throws Exception - Test should fail if any exception is thrown
152 public void testWriteAll() throws Exception {
153 final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
154 new COPSError(ErrorTypes.BAD_HANDLE_REF, ErrorTypes.MA),
155 new COPSIpv4PdpRedirectAddress("localhost", 7777, (short)0),
156 new COPSIntegrity(8, 9, new COPSData("12345")));
158 msg.writeData(outSocket);
160 final long start = System.currentTimeMillis();
161 while (server.copsMsgs.size() < 1) {
163 if (System.currentTimeMillis() - start > 2000) break;
166 Assert.assertEquals(1, server.copsMsgs.size());
167 Assert.assertEquals(msg, server.copsMsgs.get(0));