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 | |
Carmelo Cascone | 6d9ab3a | 2016-05-31 11:25:58 -0700 | [diff] [blame] | 24 | import java.nio.ByteBuffer; |
| 25 | import java.util.Arrays; |
| 26 | |
Sho SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.fail; |
| 28 | |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Test of the Hexstring. |
| 31 | * |
| 32 | */ |
| 33 | |
Sho SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 34 | public class HexStringTest { |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 35 | |
| 36 | @Test |
| 37 | public void testMarshalling() throws Exception { |
| 38 | String dpidStr = "00:00:00:23:20:2d:16:71"; |
| 39 | long dpid = HexString.toLong(dpidStr); |
| 40 | String testStr = HexString.toHexString(dpid); |
| 41 | TestCase.assertEquals(dpidStr, testStr); |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void testToLong() { |
| 46 | String dpidStr = "3e:1f:01:fc:72:8c:63:31"; |
| 47 | long valid = 0x3e1f01fc728c6331L; |
| 48 | long testLong = HexString.toLong(dpidStr); |
| 49 | TestCase.assertEquals(valid, testLong); |
| 50 | } |
| 51 | |
| 52 | @Test |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 53 | public void testToLongMsb() { |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 54 | String dpidStr = "ca:7c:5e:d1:64:7a:95:9b"; |
| 55 | long valid = -3856102927509056101L; |
| 56 | long testLong = HexString.toLong(dpidStr); |
| 57 | TestCase.assertEquals(valid, testLong); |
| 58 | } |
| 59 | |
| 60 | @Test |
Carmelo Cascone | 6d9ab3a | 2016-05-31 11:25:58 -0700 | [diff] [blame] | 61 | public void testFromHexString() { |
| 62 | String dpidStr = "3e:1f:01:fc:72:8c:63:31"; |
| 63 | String dpidStrNoSep = "3e1f01fc728c6331"; |
| 64 | long valid = 0x3e1f01fc728c6331L; |
| 65 | byte[] validBytes = ByteBuffer.allocate(Long.BYTES).putLong(valid).array(); |
| 66 | byte[] testBytes = HexString.fromHexString(dpidStr); |
| 67 | byte[] testBytesNoSep = HexString.fromHexString(dpidStrNoSep, null); |
| 68 | byte[] testBytesUCase = HexString.fromHexString(dpidStr.toUpperCase()); |
| 69 | byte[] testBytesUCaseNoSep = HexString.fromHexString(dpidStrNoSep.toUpperCase(), null); |
| 70 | TestCase.assertTrue(Arrays.equals(validBytes, testBytes)); |
| 71 | TestCase.assertTrue(Arrays.equals(validBytes, testBytesNoSep)); |
| 72 | TestCase.assertTrue(Arrays.equals(validBytes, testBytesUCase)); |
| 73 | TestCase.assertTrue(Arrays.equals(validBytes, testBytesUCaseNoSep)); |
| 74 | } |
| 75 | |
| 76 | @Test |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 77 | public void testToLongError() { |
| 78 | String dpidStr = "09:08:07:06:05:04:03:02:01"; |
| 79 | try { |
| 80 | HexString.toLong(dpidStr); |
| 81 | fail("HexString.toLong() should have thrown a NumberFormatException"); |
| 82 | } catch (NumberFormatException expected) { |
| 83 | Log.info("HexString.toLong() have thrown a NumberFormatException"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testToStringBytes() { |
| 89 | byte[] dpid = {0, 0, 0, 0, 0, 0, 0, -1 }; |
| 90 | String valid = "00:00:00:00:00:00:00:ff"; |
| 91 | String testString = HexString.toHexString(dpid); |
| 92 | TestCase.assertEquals(valid, testString); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testFromHexStringError() { |
| 97 | String invalidStr = "00:00:00:00:00:00:ffff"; |
| 98 | try { |
| 99 | HexString.fromHexString(invalidStr); |
| 100 | fail("HexString.fromHexString() should have thrown a NumberFormatException"); |
| 101 | } catch (NumberFormatException expected) { |
| 102 | Log.info("HexString.toLong() have thrown a NumberFormatException"); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |