Removed license headers erroneously added to openflowJ
[controller.git] / third-party / openflowj / src / test / java / org / openflow / protocol / OFErrorTest.java
1 package org.openflow.protocol;
2
3 import java.nio.ByteBuffer;
4 import java.util.List;
5
6 import junit.framework.TestCase;
7
8 import org.openflow.protocol.OFError.OFErrorType;
9 import org.openflow.protocol.OFError.OFHelloFailedCode;
10 import org.openflow.protocol.factory.BasicFactory;
11 import org.openflow.protocol.factory.OFMessageFactory;
12 import org.openflow.util.OFTestCase;
13
14 public class OFErrorTest extends OFTestCase {
15     public void testWriteRead() throws Exception {
16         OFError msg = (OFError) messageFactory.getMessage(OFType.ERROR);
17         msg.setMessageFactory(messageFactory);
18         msg.setErrorType((short) OFErrorType.OFPET_HELLO_FAILED.ordinal());
19         msg.setErrorCode((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
20                 .ordinal());
21         ByteBuffer bb = ByteBuffer.allocate(1024);
22         bb.clear();
23         msg.writeTo(bb);
24         bb.flip();
25         msg.readFrom(bb);
26         TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.ordinal(),
27                 msg.getErrorType());
28         TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
29                 .ordinal(), msg.getErrorType());
30         TestCase.assertNull(msg.getOffendingMsg());
31
32         msg.setOffendingMsg(new OFHello());
33         bb.clear();
34         msg.writeTo(bb);
35         bb.flip();
36         msg.readFrom(bb);
37         TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.ordinal(),
38                 msg.getErrorType());
39         TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
40                 .ordinal(), msg.getErrorType());
41         TestCase.assertNotNull(msg.getOffendingMsg());
42         TestCase.assertEquals(OFHello.MINIMUM_LENGTH,
43                 msg.getOffendingMsg().length);
44     }
45
46     public void testGarbageAtEnd() {
47         // This is a OFError msg (12 bytes), that encaps a OFVendor msg (24
48         // bytes)
49         // AND some zeros at the end (40 bytes) for a total of 76 bytes
50         // THIS is what an NEC sends in reply to Nox's VENDOR request
51         byte[] oferrorRaw = { 0x01, 0x01, 0x00, 0x4c, 0x00, 0x00, 0x10,
52                 (byte) 0xcc, 0x00, 0x01, 0x00, 0x01, 0x01, 0x04, 0x00, 0x18,
53                 0x00, 0x00, 0x10, (byte) 0xcc, 0x00, 0x00, 0x23, 0x20, 0x00,
54                 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59                 0x00 };
60         OFMessageFactory factory = new BasicFactory();
61         ByteBuffer oferrorBuf = ByteBuffer.wrap(oferrorRaw);
62         List<OFMessage> msgs = factory.parseMessages(oferrorBuf,
63                 oferrorRaw.length);
64         TestCase.assertEquals(1, msgs.size());
65         OFMessage msg = msgs.get(0);
66         TestCase.assertEquals(76, msg.getLengthU());
67         ByteBuffer out = ByteBuffer.allocate(1024);
68         msg.writeTo(out);
69         TestCase.assertEquals(76, out.position());
70     }
71 }