Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import com.esotericsoftware.minlog.Log; |
| 21 | |
| 22 | import junit.framework.TestCase; |
| 23 | |
Sho SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.fail; |
| 25 | |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Test of the Hexstring. |
| 28 | * |
| 29 | */ |
| 30 | |
Sho SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 31 | public class HexStringTest { |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 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 |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 50 | public void testToLongMsb() { |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 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 | } catch (NumberFormatException expected) { |
| 64 | Log.info("HexString.toLong() have thrown a NumberFormatException"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testToStringBytes() { |
| 70 | byte[] dpid = {0, 0, 0, 0, 0, 0, 0, -1 }; |
| 71 | String valid = "00:00:00:00:00:00:00:ff"; |
| 72 | String testString = HexString.toHexString(dpid); |
| 73 | TestCase.assertEquals(valid, testString); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testFromHexStringError() { |
| 78 | String invalidStr = "00:00:00:00:00:00:ffff"; |
| 79 | try { |
| 80 | HexString.fromHexString(invalidStr); |
| 81 | fail("HexString.fromHexString() should have thrown a NumberFormatException"); |
| 82 | } catch (NumberFormatException expected) { |
| 83 | Log.info("HexString.toLong() have thrown a NumberFormatException"); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |