45d52576e75c2760d1499497964258f42457564c
[openflowjava.git] / third-party / openflowj_netty / src / test / java / org / openflow / protocol / OFErrorTest.java
1 /**
2 *    Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
3 *    University
4
5 *    Licensed under the Apache License, Version 2.0 (the "License"); you may
6 *    not use this file except in compliance with the License. You may obtain
7 *    a copy of the License at
8 *
9 *         http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *    Unless required by applicable law or agreed to in writing, software
12 *    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 *    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 *    License for the specific language governing permissions and limitations
15 *    under the License.
16 **/
17
18 package org.openflow.protocol;
19
20 import java.util.List;
21
22 import junit.framework.TestCase;
23
24 import org.jboss.netty.buffer.ChannelBuffer;
25 import org.jboss.netty.buffer.ChannelBuffers;
26 import org.openflow.protocol.OFError.OFErrorType;
27 import org.openflow.protocol.OFError.OFHelloFailedCode;
28 import org.openflow.protocol.factory.BasicFactory;
29 import org.openflow.protocol.factory.MessageParseException;
30 import org.openflow.protocol.factory.OFMessageFactory;
31 import org.openflow.util.OFTestCase;
32
33 public class OFErrorTest extends OFTestCase {
34     public void testWriteRead() throws Exception {
35         OFError msg = (OFError) messageFactory.getMessage(OFType.ERROR);
36         msg.setMessageFactory(messageFactory);
37         msg.setErrorType((short) OFErrorType.OFPET_HELLO_FAILED.getValue());
38         msg.setErrorCode((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
39                 .ordinal());
40         ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
41         bb.clear();
42         msg.writeTo(bb);
43         msg.readFrom(bb);
44         TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.getValue(),
45                 msg.getErrorType());
46         TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
47                 .ordinal(), msg.getErrorType());
48         TestCase.assertNull(msg.getOffendingMsg());
49
50         msg.setOffendingMsg(new OFHello());
51         bb.clear();
52         msg.writeTo(bb);
53         msg.readFrom(bb);
54         TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.getValue(),
55                 msg.getErrorType());
56         TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
57                 .ordinal(), msg.getErrorType());
58         TestCase.assertNotNull(msg.getOffendingMsg());
59         TestCase.assertEquals(OFHello.MINIMUM_LENGTH,
60                 msg.getOffendingMsg().length);
61     }
62
63     public void testGarbageAtEnd() throws MessageParseException {
64         // This is a OFError msg (12 bytes), that encaps a OFVendor msg (24
65         // bytes)
66         // AND some zeros at the end (40 bytes) for a total of 76 bytes
67         // THIS is what an NEC sends in reply to Nox's VENDOR request
68         byte[] oferrorRaw = { 0x01, 0x01, 0x00, 0x4c, 0x00, 0x00, 0x10,
69                 (byte) 0xcc, 0x00, 0x01, 0x00, 0x01, 0x01, 0x04, 0x00, 0x18,
70                 0x00, 0x00, 0x10, (byte) 0xcc, 0x00, 0x00, 0x23, 0x20, 0x00,
71                 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76                 0x00 };
77         OFMessageFactory factory = new BasicFactory();
78         ChannelBuffer oferrorBuf = 
79                 ChannelBuffers.wrappedBuffer(oferrorRaw);
80         List<OFMessage> msg = factory.parseMessage(oferrorBuf);
81         TestCase.assertNotNull(msg);
82         TestCase.assertEquals(msg.size(), 1);
83         TestCase.assertEquals(76, msg.get(0).getLengthU());
84         ChannelBuffer out = ChannelBuffers.dynamicBuffer();
85         msg.get(0).writeTo(out);
86         TestCase.assertEquals(76, out.readableBytes());
87     }
88 }