1 package org.umu.cops.stack;
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
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;
12 import java.io.ByteArrayOutputStream;
13 import java.net.InetAddress;
14 import java.net.Socket;
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.
21 public class COPSClientOpenMsgTest {
23 private final static int testPort = 7777;
24 TestCOPSServer server;
28 public void setup() throws Exception {
29 server = new TestCOPSServer(testPort);
31 outSocket = new Socket(InetAddress.getLocalHost(), testPort);
35 public void tearDown() throws Exception {
40 @Test(expected = IllegalArgumentException.class)
41 public void version0() {
42 new COPSClientOpenMsg(0, Flag.SOLICITED, ClientType.TYPE_1, new COPSPepId(new COPSData()),
46 @Test(expected = IllegalArgumentException.class)
47 public void nullFlag() {
48 new COPSClientOpenMsg(1, null, ClientType.TYPE_1, new COPSPepId(new COPSData()), null, null, null);
51 @Test(expected = IllegalArgumentException.class)
52 public void nullClientType() {
53 new COPSClientOpenMsg(1, Flag.SOLICITED, null, new COPSPepId(new COPSData()), null, null, null);
56 @Test(expected = IllegalArgumentException.class)
57 public void nullPepId() {
58 new COPSClientOpenMsg(1, Flag.SOLICITED, ClientType.TYPE_1, null, null, null, null);
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);
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);
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);
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());
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")));
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());
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")));
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());
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
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")));
134 final ByteArrayOutputStream os = new ByteArrayOutputStream();
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);
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]);
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
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);
160 msg.writeData(outSocket);
162 final long start = System.currentTimeMillis();
163 while (server.copsMsgs.size() < 1) {
165 if (System.currentTimeMillis() - start > 2000) break;
168 Assert.assertEquals(1, server.copsMsgs.size());
169 Assert.assertEquals(msg, server.copsMsgs.get(0));
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
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")));
185 msg.writeData(outSocket);
187 final long start = System.currentTimeMillis();
188 while (server.copsMsgs.size() < 1) {
190 if (System.currentTimeMillis() - start > 2000) break;
193 Assert.assertEquals(1, server.copsMsgs.size());
194 Assert.assertEquals(msg, server.copsMsgs.get(0));
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
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")));
210 msg.writeData(outSocket);
212 final long start = System.currentTimeMillis();
213 while (server.copsMsgs.size() < 1) {
215 if (System.currentTimeMillis() - start > 2000) break;
218 Assert.assertEquals(1, server.copsMsgs.size());
219 Assert.assertEquals(msg, server.copsMsgs.get(0));