Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -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.openflow.util; |
| 19 | |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import junit.framework.TestCase; |
| 23 | |
| 24 | /** |
| 25 | * Does hexstring conversion work? |
| 26 | * |
| 27 | * @author Rob Sherwood (rob.sherwood@stanford.edu) |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | public class HexStringTest extends TestCase { |
| 32 | |
| 33 | @Test |
| 34 | public void testMarshalling() throws Exception { |
| 35 | String dpidStr = "00:00:00:23:20:2d:16:71"; |
| 36 | long dpid = HexString.toLong(dpidStr); |
| 37 | String testStr = HexString.toHexString(dpid); |
| 38 | TestCase.assertEquals(dpidStr, testStr); |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | public void testToLong() { |
| 43 | String dpidStr = "3e:1f:01:fc:72:8c:63:31"; |
| 44 | long valid = 0x3e1f01fc728c6331L; |
| 45 | long testLong = HexString.toLong(dpidStr); |
| 46 | TestCase.assertEquals(valid, testLong); |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void testToLongMSB() { |
| 51 | String dpidStr = "ca:7c:5e:d1:64:7a:95:9b"; |
| 52 | long valid = -3856102927509056101L; |
| 53 | long testLong = HexString.toLong(dpidStr); |
| 54 | TestCase.assertEquals(valid, testLong); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testToLongError() { |
| 59 | String dpidStr = "09:08:07:06:05:04:03:02:01"; |
| 60 | try { |
| 61 | HexString.toLong(dpidStr); |
| 62 | fail("HexString.toLong() should have thrown a NumberFormatException"); |
| 63 | } |
| 64 | catch (NumberFormatException expected) { |
| 65 | // do nothing |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void testToStringBytes() { |
| 71 | byte[] dpid = { 0, 0, 0, 0, 0, 0, 0, -1 }; |
| 72 | String valid = "00:00:00:00:00:00:00:ff"; |
| 73 | String testString = HexString.toHexString(dpid); |
| 74 | TestCase.assertEquals(valid, testString); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testFromHexStringError() { |
| 79 | String invalidStr = "00:00:00:00:00:00:ffff"; |
| 80 | try { |
| 81 | HexString.fromHexString(invalidStr); |
| 82 | fail("HexString.fromHexString() should have thrown a NumberFormatException"); |
| 83 | } |
| 84 | catch (NumberFormatException expected) { |
| 85 | // do nothing |
| 86 | } |
| 87 | } |
| 88 | } |