Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.*; |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 21 | |
Carmelo Cascone | 6d9ab3a | 2016-05-31 11:25:58 -0700 | [diff] [blame] | 22 | import java.nio.ByteBuffer; |
Sho SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 23 | |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Test of the Hexstring. |
| 26 | * |
| 27 | */ |
| 28 | |
Sho SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 29 | public class HexStringTest { |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 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); |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 36 | assertEquals(dpidStr, testStr); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 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); |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 44 | assertEquals(valid, testLong); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Test |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 48 | public void testToLongMsb() { |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 49 | String dpidStr = "ca:7c:5e:d1:64:7a:95:9b"; |
| 50 | long valid = -3856102927509056101L; |
| 51 | long testLong = HexString.toLong(dpidStr); |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 52 | assertEquals(valid, testLong); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
Carmelo Cascone | 6d9ab3a | 2016-05-31 11:25:58 -0700 | [diff] [blame] | 56 | public void testFromHexString() { |
| 57 | String dpidStr = "3e:1f:01:fc:72:8c:63:31"; |
| 58 | String dpidStrNoSep = "3e1f01fc728c6331"; |
| 59 | long valid = 0x3e1f01fc728c6331L; |
| 60 | byte[] validBytes = ByteBuffer.allocate(Long.BYTES).putLong(valid).array(); |
| 61 | byte[] testBytes = HexString.fromHexString(dpidStr); |
| 62 | byte[] testBytesNoSep = HexString.fromHexString(dpidStrNoSep, null); |
| 63 | byte[] testBytesUCase = HexString.fromHexString(dpidStr.toUpperCase()); |
| 64 | byte[] testBytesUCaseNoSep = HexString.fromHexString(dpidStrNoSep.toUpperCase(), null); |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 65 | assertArrayEquals(validBytes, testBytes); |
| 66 | assertArrayEquals(validBytes, testBytesNoSep); |
| 67 | assertArrayEquals(validBytes, testBytesUCase); |
| 68 | assertArrayEquals(validBytes, testBytesUCaseNoSep); |
Carmelo Cascone | 6d9ab3a | 2016-05-31 11:25:58 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 71 | @Test(expected = NumberFormatException.class) |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 72 | public void testToLongError() { |
| 73 | String dpidStr = "09:08:07:06:05:04:03:02:01"; |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 74 | HexString.toLong(dpidStr); |
| 75 | fail("HexString.toLong() should have thrown a NumberFormatException"); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testToStringBytes() { |
| 80 | byte[] dpid = {0, 0, 0, 0, 0, 0, 0, -1 }; |
| 81 | String valid = "00:00:00:00:00:00:00:ff"; |
| 82 | String testString = HexString.toHexString(dpid); |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 83 | assertEquals(valid, testString); |
| 84 | |
| 85 | String validNoSep = "00000000000000ff"; |
| 86 | assertEquals(validNoSep, HexString.toHexString(dpid, null)); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 89 | @Test(expected = NumberFormatException.class) |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 90 | public void testFromHexStringError() { |
| 91 | String invalidStr = "00:00:00:00:00:00:ffff"; |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 92 | HexString.fromHexString(invalidStr); |
| 93 | fail("HexString.fromHexString() should have thrown a NumberFormatException"); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |