7d081495d7e230e2266a5f3363c11062fcb59b20
[openflowjava.git] / third-party / openflow-codec / src / test / java / org / openflow / codec / protocol / OFPErrorMsgTest.java
1 package org.openflow.codec.protocol;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 import junit.framework.TestCase;
7
8 import org.openflow.codec.io.DataBuffers;
9 import org.openflow.codec.io.IDataBuffer;
10 import org.openflow.codec.protocol.OFPErrorMsg.OFPHelloFailedCode;
11 import org.openflow.codec.protocol.factory.OFPBasicFactoryImpl;
12 import org.openflow.codec.protocol.factory.OFPMessageFactory;
13 import org.openflow.codec.util.OFTestCase;
14
15 public class OFPErrorMsgTest extends OFTestCase
16 {
17         public void testWriteRead() throws Exception
18         {
19                 OFPErrorMsg msg = (OFPErrorMsg) messageFactory
20                                 .getMessage(OFPType.ERROR);
21                 msg.setMessageFactory(messageFactory);
22                 msg.setErrorType(OFPErrorType.OFPET_HELLO_FAILED);
23                 msg.setErrorCode((short) OFPHelloFailedCode.OFPHFC_INCOMPATIBLE
24                                 .ordinal());
25                 IDataBuffer bb = DataBuffers.allocate(1024);
26                 bb.clear();
27                 msg.writeTo(bb);
28                 bb.flip();
29                 msg.readFrom(bb);
30                 TestCase.assertEquals(OFPErrorType.OFPET_HELLO_FAILED,
31                                 msg.getErrorType());
32                 TestCase.assertEquals(
33                                 (short) OFPHelloFailedCode.OFPHFC_INCOMPATIBLE.ordinal(),
34                                 msg.getErrorCode());
35                 TestCase.assertNull(msg.getOffendingMsg(bb));
36
37                 msg.setOffendingMsg(new OFPHello(), bb);
38                 bb.clear();
39                 msg.writeTo(bb);
40                 bb.flip();
41                 msg.readFrom(bb);
42                 TestCase.assertEquals(OFPErrorType.OFPET_HELLO_FAILED,
43                                 msg.getErrorType());
44                 TestCase.assertEquals(
45                                 (short) OFPHelloFailedCode.OFPHFC_INCOMPATIBLE.ordinal(),
46                                 msg.getErrorCode());
47                 TestCase.assertNotNull(msg.getOffendingMsg(bb));
48                 TestCase.assertEquals(OFPHello.MINIMUM_LENGTH,
49                                 msg.getOffendingMsg(bb).length);
50         }
51
52         public void testWriteReadExperimenter() throws Exception
53         {
54                 OFPErrorMsg msg = (OFPErrorMsg) messageFactory
55                                 .getMessage(OFPType.ERROR);
56                 msg.setMessageFactory(messageFactory);
57                 msg.setErrorType(OFPErrorType.OFPET_EXPERIMENTER);
58                 msg.setExpType((short) 2);
59                 msg.setExperimenter(10);
60                 msg.setErrorData(new byte[] { 1, 2, 3, 4 });
61                 IDataBuffer bb = DataBuffers.allocate(1024);
62                 bb.clear();
63                 msg.writeTo(bb);
64                 bb.flip();
65                 msg.readFrom(bb);
66                 TestCase.assertEquals(OFPErrorType.OFPET_EXPERIMENTER,
67                                 msg.getErrorType());
68                 TestCase.assertEquals((short) 2, msg.getExpType());
69                 TestCase.assertEquals(10, msg.getExperimenter());
70                 TestCase.assertNull(msg.getOffendingMsg(bb));
71                 TestCase.assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 4 },
72                                 msg.getErrorData()));
73
74         }
75
76         public void testGarbageAtEnd()
77         {
78                 // This is a OFPErrorMsg msg (12 bytes), that encaps a OFVendor msg (24
79                 // bytes)
80                 // AND some zeros at the end (40 bytes) for a total of 76 bytes
81                 // THIS is what an NEC sends in reply to Nox's VENDOR request
82                 byte[] oferrorRaw = { 0x01, 0x01, 0x00, 0x4c, 0x00, 0x00, 0x10,
83                                 (byte) 0xcc, 0x00, 0x01, 0x00, 0x01, 0x01, 0x04, 0x00, 0x18,
84                                 0x00, 0x00, 0x10, (byte) 0xcc, 0x00, 0x00, 0x23, 0x20, 0x00,
85                                 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90                                 0x00 };
91                 OFPMessageFactory factory = new OFPBasicFactoryImpl();
92                 IDataBuffer buffer = DataBuffers.allocate(1024).wrap(oferrorRaw);
93
94                 List<OFPMessage> msgs = factory
95                                 .parseMessages(buffer, oferrorRaw.length);
96                 TestCase.assertEquals(1, msgs.size());
97                 OFPMessage msg = msgs.get(0);
98                 TestCase.assertEquals(76, msg.getLengthU());
99                 IDataBuffer buffer1 = DataBuffers.allocate(1024);
100                 msg.writeTo(buffer1);
101                 TestCase.assertEquals(76, buffer1.position());
102         }
103 }