Rob Vaterlaus | a1969e5 | 2014-03-04 16:53:51 -0800 | [diff] [blame] | 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.projectfloodlight.openflow.util; |
| 19 | |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | |
| 22 | import org.junit.Test; |
| 23 | |
| 24 | /** |
| 25 | * Does hexstring conversion work? |
| 26 | * |
| 27 | * @author Rob Sherwood (rob.sherwood@stanford.edu) |
| 28 | */ |
| 29 | public class HexStringTest { |
| 30 | |
| 31 | @Test |
| 32 | public void testMarshalling() throws Exception { |
| 33 | String dpidStr = "00:00:00:23:20:2d:16:71"; |
| 34 | long dpid = HexString.toLong(dpidStr); |
| 35 | String testStr = HexString.toHexString(dpid); |
| 36 | assertEquals(dpidStr, testStr); |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | public void testToLong() { |
| 41 | String dpidStr = "3e:1f:01:fc:72:8c:63:31"; |
| 42 | long valid = 0x3e1f01fc728c6331L; |
| 43 | long testLong = HexString.toLong(dpidStr); |
| 44 | assertEquals(valid, testLong); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testToLong2() { |
| 49 | String dpidStr = "1f:1:fc:72:3:f:31"; |
| 50 | long valid = 0x1f01fc72030f31L; |
| 51 | long testLong = HexString.toLong(dpidStr); |
| 52 | assertEquals(valid, testLong); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testToLongMSB() { |
| 57 | String dpidStr = "ca:7c:5e:d1:64:7a:95:9b"; |
| 58 | long valid = -3856102927509056101L; |
| 59 | long testLong = HexString.toLong(dpidStr); |
| 60 | assertEquals(valid, testLong); |
| 61 | } |
| 62 | |
| 63 | @Test(expected=NumberFormatException.class) |
| 64 | public void testToLongErrorTooManyBytes() { |
| 65 | HexString.toLong("09:08:07:06:05:04:03:02:01"); |
| 66 | } |
| 67 | |
| 68 | @Test(expected=NumberFormatException.class) |
| 69 | public void testToLongErrorByteValueTooLong() { |
| 70 | HexString.toLong("234:01"); |
| 71 | } |
| 72 | |
| 73 | @Test(expected=NumberFormatException.class) |
| 74 | public void testToLongErrorEmptyByte() { |
| 75 | HexString.toLong("03::01"); |
| 76 | } |
| 77 | |
| 78 | @Test(expected=NumberFormatException.class) |
| 79 | public void testToLongErrorInvalidHexDigit() { |
| 80 | HexString.toLong("ss:01"); |
| 81 | } |
| 82 | |
| 83 | @Test(expected=NumberFormatException.class) |
| 84 | public void testToLongErrorEmptyString() { |
| 85 | HexString.toLong(""); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | @Test |
| 90 | public void testToStringBytes() { |
| 91 | byte[] dpid = { 0, 0, 0, 0, 0, 0, 0, -1 }; |
| 92 | String valid = "00:00:00:00:00:00:00:ff"; |
| 93 | String testString = HexString.toHexString(dpid); |
| 94 | assertEquals(valid, testString); |
| 95 | } |
| 96 | |
| 97 | @Test(expected=NumberFormatException.class) |
| 98 | public void testFromHexStringError() { |
| 99 | String invalidStr = "00:00:00:00:00:00:ffff"; |
| 100 | HexString.fromHexString(invalidStr); |
| 101 | } |
| 102 | } |
| 103 | |