blob: 250c783ed9242a14ee90add8fa9970e907bd22cb [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
weibit38c42ed2014-10-09 19:03:54 -070016package org.onlab.util;
17
18import org.junit.Test;
19
20import com.esotericsoftware.minlog.Log;
21
22import junit.framework.TestCase;
23
Carmelo Cascone6d9ab3a2016-05-31 11:25:58 -070024import java.nio.ByteBuffer;
25import java.util.Arrays;
26
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070027import static org.junit.Assert.fail;
28
weibit38c42ed2014-10-09 19:03:54 -070029/**
30 * Test of the Hexstring.
31 *
32 */
33
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070034public class HexStringTest {
weibit38c42ed2014-10-09 19:03:54 -070035
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 Hartd9df7bd2015-11-10 17:10:25 -080053 public void testToLongMsb() {
weibit38c42ed2014-10-09 19:03:54 -070054 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 Cascone6d9ab3a2016-05-31 11:25:58 -070061 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
weibit38c42ed2014-10-09 19:03:54 -070077 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