Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onlab.packet; |
| 18 | |
| 19 | import java.nio.ByteBuffer; |
| 20 | |
| 21 | import static junit.framework.TestCase.fail; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | |
| 24 | /** |
| 25 | * Utilities for testing packet methods. |
| 26 | */ |
| 27 | public final class PacketTestUtils { |
| 28 | |
| 29 | private PacketTestUtils() { |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Tests that the Deserializer function is resilient to bad input parameters |
| 34 | * such as null input, negative offset and length, etc. |
| 35 | * |
| 36 | * @param deserializer deserializer function to test |
| 37 | */ |
| 38 | public static void testDeserializeBadInput(Deserializer deserializer) { |
| 39 | byte[] bytes = ByteBuffer.allocate(4).array(); |
| 40 | |
| 41 | try { |
| 42 | deserializer.deserialize(null, 0, 4); |
| 43 | fail("NullPointerException was not thrown"); |
| 44 | } catch (NullPointerException e) { |
| 45 | assertTrue(true); |
| 46 | } catch (DeserializationException e) { |
| 47 | fail("NullPointerException was not thrown"); |
| 48 | } |
| 49 | |
| 50 | // input byte array length, offset and length don't make sense |
| 51 | expectDeserializationException(deserializer, bytes, -1, 0); |
| 52 | expectDeserializationException(deserializer, bytes, 0, -1); |
| 53 | expectDeserializationException(deserializer, bytes, 0, 5); |
| 54 | expectDeserializationException(deserializer, bytes, 2, 3); |
| 55 | expectDeserializationException(deserializer, bytes, 5, 0); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Tests that the Deserializer function is resilient to truncated input, or |
| 60 | * cases where the input byte array does not contain enough bytes to |
| 61 | * deserialize the packet. |
| 62 | * |
| 63 | * @param deserializer deserializer function to test |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 64 | * @param header byte array of a full-size packet |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 65 | */ |
| 66 | public static void testDeserializeTruncated(Deserializer deserializer, |
| 67 | byte[] header) { |
| 68 | byte[] truncated; |
| 69 | |
| 70 | for (int i = 0; i < header.length; i++) { |
| 71 | truncated = new byte[i]; |
| 72 | |
| 73 | ByteBuffer.wrap(header).get(truncated); |
| 74 | |
| 75 | expectDeserializationException(deserializer, truncated, 0, truncated.length); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Run the given deserializer function against the given inputs and verify |
| 81 | * that a DeserializationException is thrown. The the test will fail if a |
| 82 | * DeserializationException is not thrown by the deserializer function. |
| 83 | * |
| 84 | * @param deserializer deserializer function to test |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 85 | * @param bytes input byte array |
| 86 | * @param offset input offset |
| 87 | * @param length input length |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 88 | */ |
| 89 | public static void expectDeserializationException(Deserializer deserializer, |
| 90 | byte[] bytes, int offset, int length) { |
| 91 | try { |
| 92 | deserializer.deserialize(bytes, offset, length); |
| 93 | fail("DeserializationException was not thrown"); |
| 94 | } catch (DeserializationException e) { |
| 95 | assertTrue(true); |
| 96 | } |
| 97 | } |
| 98 | } |