blob: 1060a734727010b0e817a1f0b37fcfb56890aed2 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Yuta HIGUCHI65849892017-08-18 13:09:41 -070020import static org.junit.Assert.*;
weibit38c42ed2014-10-09 19:03:54 -070021
Carmelo Cascone6d9ab3a2016-05-31 11:25:58 -070022import java.nio.ByteBuffer;
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070023
weibit38c42ed2014-10-09 19:03:54 -070024/**
25 * Test of the Hexstring.
26 *
27 */
28
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070029public class HexStringTest {
weibit38c42ed2014-10-09 19:03:54 -070030
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 HIGUCHI65849892017-08-18 13:09:41 -070036 assertEquals(dpidStr, testStr);
weibit38c42ed2014-10-09 19:03:54 -070037 }
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 HIGUCHI65849892017-08-18 13:09:41 -070044 assertEquals(valid, testLong);
weibit38c42ed2014-10-09 19:03:54 -070045 }
46
47 @Test
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080048 public void testToLongMsb() {
weibit38c42ed2014-10-09 19:03:54 -070049 String dpidStr = "ca:7c:5e:d1:64:7a:95:9b";
50 long valid = -3856102927509056101L;
51 long testLong = HexString.toLong(dpidStr);
Yuta HIGUCHI65849892017-08-18 13:09:41 -070052 assertEquals(valid, testLong);
weibit38c42ed2014-10-09 19:03:54 -070053 }
54
55 @Test
Carmelo Cascone6d9ab3a2016-05-31 11:25:58 -070056 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 HIGUCHI65849892017-08-18 13:09:41 -070065 assertArrayEquals(validBytes, testBytes);
66 assertArrayEquals(validBytes, testBytesNoSep);
67 assertArrayEquals(validBytes, testBytesUCase);
68 assertArrayEquals(validBytes, testBytesUCaseNoSep);
Carmelo Cascone6d9ab3a2016-05-31 11:25:58 -070069 }
70
Yuta HIGUCHI65849892017-08-18 13:09:41 -070071 @Test(expected = NumberFormatException.class)
weibit38c42ed2014-10-09 19:03:54 -070072 public void testToLongError() {
73 String dpidStr = "09:08:07:06:05:04:03:02:01";
Yuta HIGUCHI65849892017-08-18 13:09:41 -070074 HexString.toLong(dpidStr);
75 fail("HexString.toLong() should have thrown a NumberFormatException");
weibit38c42ed2014-10-09 19:03:54 -070076 }
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 HIGUCHI65849892017-08-18 13:09:41 -070083 assertEquals(valid, testString);
84
85 String validNoSep = "00000000000000ff";
86 assertEquals(validNoSep, HexString.toHexString(dpid, null));
weibit38c42ed2014-10-09 19:03:54 -070087 }
88
Yuta HIGUCHI65849892017-08-18 13:09:41 -070089 @Test(expected = NumberFormatException.class)
weibit38c42ed2014-10-09 19:03:54 -070090 public void testFromHexStringError() {
91 String invalidStr = "00:00:00:00:00:00:ffff";
Yuta HIGUCHI65849892017-08-18 13:09:41 -070092 HexString.fromHexString(invalidStr);
93 fail("HexString.fromHexString() should have thrown a NumberFormatException");
weibit38c42ed2014-10-09 19:03:54 -070094 }
95}
96