Committed by
Gerrit Code Review
Unit tests for open flow message encode and decode
Change-Id: Id0d6975b760943d3fc2ad97d176fc538eab2c5bf
Showing
6 changed files
with
480 additions
and
0 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.openflow; | ||
| 17 | + | ||
| 18 | +import java.net.SocketAddress; | ||
| 19 | + | ||
| 20 | +import org.jboss.netty.channel.Channel; | ||
| 21 | +import org.jboss.netty.channel.ChannelConfig; | ||
| 22 | +import org.jboss.netty.channel.ChannelFactory; | ||
| 23 | +import org.jboss.netty.channel.ChannelFuture; | ||
| 24 | +import org.jboss.netty.channel.ChannelPipeline; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * Adapter for testing against a netty channel. | ||
| 28 | + */ | ||
| 29 | +public class ChannelAdapter implements Channel { | ||
| 30 | + @Override | ||
| 31 | + public Integer getId() { | ||
| 32 | + return null; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + @Override | ||
| 36 | + public ChannelFactory getFactory() { | ||
| 37 | + return null; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + @Override | ||
| 41 | + public Channel getParent() { | ||
| 42 | + return null; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + @Override | ||
| 46 | + public ChannelConfig getConfig() { | ||
| 47 | + return null; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + @Override | ||
| 51 | + public ChannelPipeline getPipeline() { | ||
| 52 | + return null; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + @Override | ||
| 56 | + public boolean isOpen() { | ||
| 57 | + return false; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + @Override | ||
| 61 | + public boolean isBound() { | ||
| 62 | + return false; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + @Override | ||
| 66 | + public boolean isConnected() { | ||
| 67 | + return false; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + @Override | ||
| 71 | + public SocketAddress getLocalAddress() { | ||
| 72 | + return null; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + @Override | ||
| 76 | + public SocketAddress getRemoteAddress() { | ||
| 77 | + return null; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + @Override | ||
| 81 | + public ChannelFuture write(Object o) { | ||
| 82 | + return null; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + @Override | ||
| 86 | + public ChannelFuture write(Object o, SocketAddress socketAddress) { | ||
| 87 | + return null; | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + @Override | ||
| 91 | + public ChannelFuture bind(SocketAddress socketAddress) { | ||
| 92 | + return null; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + @Override | ||
| 96 | + public ChannelFuture connect(SocketAddress socketAddress) { | ||
| 97 | + return null; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + @Override | ||
| 101 | + public ChannelFuture disconnect() { | ||
| 102 | + return null; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + @Override | ||
| 106 | + public ChannelFuture unbind() { | ||
| 107 | + return null; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + @Override | ||
| 111 | + public ChannelFuture close() { | ||
| 112 | + return null; | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + @Override | ||
| 116 | + public ChannelFuture getCloseFuture() { | ||
| 117 | + return null; | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + @Override | ||
| 121 | + public int getInterestOps() { | ||
| 122 | + return 0; | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + @Override | ||
| 126 | + public boolean isReadable() { | ||
| 127 | + return false; | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + @Override | ||
| 131 | + public boolean isWritable() { | ||
| 132 | + return false; | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + @Override | ||
| 136 | + public ChannelFuture setInterestOps(int i) { | ||
| 137 | + return null; | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + @Override | ||
| 141 | + public ChannelFuture setReadable(boolean b) { | ||
| 142 | + return null; | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + @Override | ||
| 146 | + public Object getAttachment() { | ||
| 147 | + return null; | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + @Override | ||
| 151 | + public void setAttachment(Object o) { | ||
| 152 | + | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + @Override | ||
| 156 | + public int compareTo(Channel o) { | ||
| 157 | + return 0; | ||
| 158 | + } | ||
| 159 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.openflow; | ||
| 17 | + | ||
| 18 | +import org.jboss.netty.channel.Channel; | ||
| 19 | +import org.jboss.netty.channel.ChannelEvent; | ||
| 20 | +import org.jboss.netty.channel.ChannelHandler; | ||
| 21 | +import org.jboss.netty.channel.ChannelHandlerContext; | ||
| 22 | +import org.jboss.netty.channel.ChannelPipeline; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * Adapter for testing against a netty channel handler context. | ||
| 26 | + */ | ||
| 27 | +public class ChannelHandlerContextAdapter implements ChannelHandlerContext { | ||
| 28 | + @Override | ||
| 29 | + public Channel getChannel() { | ||
| 30 | + return null; | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + @Override | ||
| 34 | + public ChannelPipeline getPipeline() { | ||
| 35 | + return null; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + @Override | ||
| 39 | + public String getName() { | ||
| 40 | + return null; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + @Override | ||
| 44 | + public ChannelHandler getHandler() { | ||
| 45 | + return null; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @Override | ||
| 49 | + public boolean canHandleUpstream() { | ||
| 50 | + return false; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @Override | ||
| 54 | + public boolean canHandleDownstream() { | ||
| 55 | + return false; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + @Override | ||
| 59 | + public void sendUpstream(ChannelEvent channelEvent) { | ||
| 60 | + | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + @Override | ||
| 64 | + public void sendDownstream(ChannelEvent channelEvent) { | ||
| 65 | + | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + @Override | ||
| 69 | + public Object getAttachment() { | ||
| 70 | + return null; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + @Override | ||
| 74 | + public void setAttachment(Object o) { | ||
| 75 | + | ||
| 76 | + } | ||
| 77 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.openflow; | ||
| 17 | + | ||
| 18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 19 | +import org.projectfloodlight.openflow.protocol.OFMessage; | ||
| 20 | +import org.projectfloodlight.openflow.protocol.OFType; | ||
| 21 | +import org.projectfloodlight.openflow.protocol.OFVersion; | ||
| 22 | + | ||
| 23 | +import com.google.common.hash.PrimitiveSink; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * Adapter for testing against an OpenFlow message. | ||
| 27 | + */ | ||
| 28 | +public class OfMessageAdapter implements OFMessage { | ||
| 29 | + @Override | ||
| 30 | + public OFVersion getVersion() { | ||
| 31 | + return null; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + @Override | ||
| 35 | + public OFType getType() { | ||
| 36 | + return null; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + @Override | ||
| 40 | + public long getXid() { | ||
| 41 | + return 0; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + @Override | ||
| 45 | + public void writeTo(ChannelBuffer channelBuffer) { } | ||
| 46 | + | ||
| 47 | + @Override | ||
| 48 | + public Builder createBuilder() { | ||
| 49 | + return null; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + @Override | ||
| 53 | + public void putTo(PrimitiveSink sink) { } | ||
| 54 | +} |
openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/OFMessageDecoderTest.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.openflow.controller.impl; | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 20 | +import org.jboss.netty.buffer.ChannelBuffers; | ||
| 21 | +import org.junit.Test; | ||
| 22 | +import org.onosproject.openflow.ChannelAdapter; | ||
| 23 | +import org.onosproject.openflow.ChannelHandlerContextAdapter; | ||
| 24 | +import org.projectfloodlight.openflow.protocol.OFHello; | ||
| 25 | + | ||
| 26 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 27 | +import static org.hamcrest.Matchers.instanceOf; | ||
| 28 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 29 | +import static org.hamcrest.Matchers.nullValue; | ||
| 30 | + | ||
| 31 | +/** | ||
| 32 | + * Tests for the OpenFlow message decoder. | ||
| 33 | + */ | ||
| 34 | +public class OFMessageDecoderTest { | ||
| 35 | + | ||
| 36 | + static class ConnectedChannel extends ChannelAdapter { | ||
| 37 | + @Override | ||
| 38 | + public boolean isConnected() { | ||
| 39 | + return true; | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + private ChannelBuffer getHelloMessageBuffer() { | ||
| 44 | + // OFHello, OF version 1, xid of 0, total of 8 bytes | ||
| 45 | + byte[] messageData = {0x1, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x0}; | ||
| 46 | + ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(); | ||
| 47 | + channelBuffer.writeBytes(messageData); | ||
| 48 | + return channelBuffer; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Tests decoding a message on a closed channel. | ||
| 53 | + * | ||
| 54 | + * @throws Exception when an exception is thrown from the decoder | ||
| 55 | + */ | ||
| 56 | + @Test | ||
| 57 | + public void testDecodeNoChannel() throws Exception { | ||
| 58 | + OFMessageDecoder decoder = new OFMessageDecoder(); | ||
| 59 | + ChannelBuffer channelBuffer = getHelloMessageBuffer(); | ||
| 60 | + Object message = | ||
| 61 | + decoder.decode(new ChannelHandlerContextAdapter(), | ||
| 62 | + new ChannelAdapter(), | ||
| 63 | + channelBuffer); | ||
| 64 | + assertThat(message, nullValue()); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * Tests decoding a message. | ||
| 69 | + * | ||
| 70 | + * @throws Exception when an exception is thrown from the decoder | ||
| 71 | + */ | ||
| 72 | + @Test | ||
| 73 | + public void testDecode() throws Exception { | ||
| 74 | + OFMessageDecoder decoder = new OFMessageDecoder(); | ||
| 75 | + ChannelBuffer channelBuffer = getHelloMessageBuffer(); | ||
| 76 | + Object message = | ||
| 77 | + decoder.decode(new ChannelHandlerContextAdapter(), | ||
| 78 | + new ConnectedChannel(), | ||
| 79 | + channelBuffer); | ||
| 80 | + assertThat(message, notNullValue()); | ||
| 81 | + assertThat(message, instanceOf(OFHello.class)); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | +} |
openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/OFMessageEncoderTest.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.openflow.controller.impl; | ||
| 17 | + | ||
| 18 | +import java.nio.charset.StandardCharsets; | ||
| 19 | +import java.util.List; | ||
| 20 | + | ||
| 21 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onosproject.openflow.OfMessageAdapter; | ||
| 24 | +import org.projectfloodlight.openflow.protocol.OFMessage; | ||
| 25 | + | ||
| 26 | +import com.google.common.collect.ImmutableList; | ||
| 27 | + | ||
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 29 | +import static org.hamcrest.Matchers.is; | ||
| 30 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Tests for the OpenFlow message encoder. | ||
| 34 | + */ | ||
| 35 | +public class OFMessageEncoderTest { | ||
| 36 | + | ||
| 37 | + static class MockOfMessage extends OfMessageAdapter { | ||
| 38 | + static int nextId = 1; | ||
| 39 | + final int id; | ||
| 40 | + | ||
| 41 | + MockOfMessage() { | ||
| 42 | + id = nextId++; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + @Override | ||
| 46 | + public void writeTo(ChannelBuffer channelBuffer) { | ||
| 47 | + String message = "message" + Integer.toString(id) + " "; | ||
| 48 | + channelBuffer.writeBytes(message.getBytes(StandardCharsets.UTF_8)); | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * Tests that encoding a non-list returns the object specified. | ||
| 54 | + * | ||
| 55 | + * @throws Exception on exception in the encoder | ||
| 56 | + */ | ||
| 57 | + @Test | ||
| 58 | + public void testNoList() throws Exception { | ||
| 59 | + OFMessageEncoder encoder = new OFMessageEncoder(); | ||
| 60 | + MockOfMessage message = new MockOfMessage(); | ||
| 61 | + OFMessage returnedMessage = | ||
| 62 | + (OFMessage) encoder.encode(null, null, message); | ||
| 63 | + assertThat(message, is(returnedMessage)); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + /** | ||
| 67 | + * Tests that encoding a list returns the proper encoded payload. | ||
| 68 | + * | ||
| 69 | + * @throws Exception on exception in the encoder | ||
| 70 | + */ | ||
| 71 | + @Test | ||
| 72 | + public void testList() throws Exception { | ||
| 73 | + OFMessageEncoder encoder = new OFMessageEncoder(); | ||
| 74 | + MockOfMessage message1 = new MockOfMessage(); | ||
| 75 | + MockOfMessage message2 = new MockOfMessage(); | ||
| 76 | + MockOfMessage message3 = new MockOfMessage(); | ||
| 77 | + List<MockOfMessage> messages = ImmutableList.of(message1, message2, message3); | ||
| 78 | + ChannelBuffer returnedChannel = | ||
| 79 | + (ChannelBuffer) encoder.encode(null, null, messages); | ||
| 80 | + assertThat(returnedChannel, notNullValue()); | ||
| 81 | + byte[] channelBytes = returnedChannel.array(); | ||
| 82 | + String expectedListMessage = "message1 message2 message3 "; | ||
| 83 | + String listMessage = | ||
| 84 | + (new String(channelBytes, StandardCharsets.UTF_8)) | ||
| 85 | + .substring(0, expectedListMessage.length()); | ||
| 86 | + assertThat(listMessage, is(expectedListMessage)); | ||
| 87 | + } | ||
| 88 | +} |
| ... | @@ -45,6 +45,24 @@ | ... | @@ -45,6 +45,24 @@ |
| 45 | <groupId>org.onosproject</groupId> | 45 | <groupId>org.onosproject</groupId> |
| 46 | <artifactId>onlab-junit</artifactId> | 46 | <artifactId>onlab-junit</artifactId> |
| 47 | </dependency> | 47 | </dependency> |
| 48 | + <dependency> | ||
| 49 | + <groupId>junit</groupId> | ||
| 50 | + <artifactId>junit</artifactId> | ||
| 51 | + <version>4.11</version> | ||
| 52 | + <scope>test</scope> | ||
| 53 | + </dependency> | ||
| 54 | + <dependency> | ||
| 55 | + <groupId>org.hamcrest</groupId> | ||
| 56 | + <artifactId>hamcrest-core</artifactId> | ||
| 57 | + <version>1.3</version> | ||
| 58 | + <scope>test</scope> | ||
| 59 | + </dependency> | ||
| 60 | + <dependency> | ||
| 61 | + <groupId>org.hamcrest</groupId> | ||
| 62 | + <artifactId>hamcrest-library</artifactId> | ||
| 63 | + <version>1.3</version> | ||
| 64 | + <scope>test</scope> | ||
| 65 | + </dependency> | ||
| 48 | </dependencies> | 66 | </dependencies> |
| 49 | 67 | ||
| 50 | <build> | 68 | <build> | ... | ... |
-
Please register or login to post a comment