1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 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 7 * 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. 15 */ 16 17package com.android.server.wifi.util; 18 19import static org.junit.Assert.*; 20 21import org.junit.Test; 22 23import java.util.ArrayList; 24import java.util.Arrays; 25 26/** 27 * Unit tests for {@link com.android.server.wifi.util.NativeUtil}. 28 */ 29public class NativeUtilTest { 30 /** 31 * Test that parsing a typical colon-delimited MAC address works. 32 */ 33 @Test 34 public void testMacAddressToByteArray() throws Exception { 35 assertArrayEquals(new byte[]{0x61, 0x52, 0x43, 0x34, 0x25, 0x16}, 36 NativeUtil.macAddressToByteArray("61:52:43:34:25:16")); 37 } 38 39 /** 40 * Test that parsing an empty MAC address works. 41 */ 42 @Test 43 public void testEmptyMacAddressToByteArray() throws Exception { 44 assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0}, 45 NativeUtil.macAddressToByteArray("")); 46 assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0}, 47 NativeUtil.macAddressToByteArray(null)); 48 } 49 50 /** 51 * Test that conversion of byte array of mac address to typical colon-delimited MAC address 52 * works. 53 */ 54 @Test 55 public void testByteArrayToMacAddress() throws Exception { 56 assertEquals("61:52:43:34:25:16", 57 NativeUtil.macAddressFromByteArray(new byte[]{0x61, 0x52, 0x43, 0x34, 0x25, 0x16})); 58 } 59 60 /** 61 * Test that parsing a typical colon-delimited MAC OUI address works. 62 */ 63 @Test 64 public void testMacAddressOuiToByteArray() throws Exception { 65 assertArrayEquals(new byte[]{0x61, 0x52, 0x43}, 66 NativeUtil.macAddressOuiToByteArray("61:52:43")); 67 } 68 69 /** 70 * Test that parsing a hex string to byte array works. 71 */ 72 @Test 73 public void testHexStringToByteArray() throws Exception { 74 assertArrayEquals(new byte[]{0x45, 0x12, 0x23, 0x34}, 75 NativeUtil.hexStringToByteArray("45122334")); 76 } 77 78 /** 79 * Test that conversion of byte array to hexstring works. 80 */ 81 @Test 82 public void testHexStringFromByteArray() throws Exception { 83 assertEquals("45122334", 84 NativeUtil.hexStringFromByteArray(new byte[]{0x45, 0x12, 0x23, 0x34})); 85 } 86 87 /** 88 * Test that conversion of ssid bytes to quoted ASCII string ssid works. 89 */ 90 @Test 91 public void testAsciiSsidDecode() throws Exception { 92 assertEquals( 93 new ArrayList<>( 94 Arrays.asList((byte) 's', (byte) 's', (byte) 'i', (byte) 'd', (byte) '_', 95 (byte) 't', (byte) 'e', (byte) 's', (byte) 't', (byte) '1', 96 (byte) '2', (byte) '3')), 97 NativeUtil.decodeSsid("\"ssid_test123\"")); 98 } 99 100 /** 101 * Test that conversion of ssid bytes to hex string ssid works. 102 */ 103 @Test 104 public void testNonAsciiSsidDecode() throws Exception { 105 assertEquals( 106 new ArrayList<>( 107 Arrays.asList((byte) 0xf5, (byte) 0xe4, (byte) 0xab, (byte) 0x78, 108 (byte) 0xab, (byte) 0x34, (byte) 0x32, (byte) 0x43, (byte) 0x9a)), 109 NativeUtil.decodeSsid("f5e4ab78ab3432439a")); 110 } 111 112 /** 113 * Test that conversion of ssid bytes to quoted ASCII string ssid works. 114 */ 115 @Test 116 public void testAsciiSsidEncode() throws Exception { 117 assertEquals( 118 "\"ssid_test123\"", 119 NativeUtil.encodeSsid(new ArrayList<>( 120 Arrays.asList((byte) 's', (byte) 's', (byte) 'i', (byte) 'd', (byte) '_', 121 (byte) 't', (byte) 'e', (byte) 's', (byte) 't', (byte) '1', 122 (byte) '2', (byte) '3')))); 123 } 124 125 /** 126 * Test that conversion of byte array to hex string works when the byte array contains 0. 127 */ 128 @Test 129 public void testNullCharInAsciiSsidEncode() throws Exception { 130 assertEquals( 131 "007369645f74657374313233", 132 NativeUtil.encodeSsid(new ArrayList<>( 133 Arrays.asList((byte) 0x00, (byte) 's', (byte) 'i', (byte) 'd', (byte) '_', 134 (byte) 't', (byte) 'e', (byte) 's', (byte) 't', (byte) '1', 135 (byte) '2', (byte) '3')))); 136 } 137 138 /** 139 * Test that conversion of byte array to hex string ssid works. 140 */ 141 @Test 142 public void testNonAsciiSsidEncode() throws Exception { 143 assertEquals( 144 "f5e4ab78ab3432439a", 145 NativeUtil.encodeSsid(new ArrayList<>( 146 Arrays.asList((byte) 0xf5, (byte) 0xe4, (byte) 0xab, (byte) 0x78, 147 (byte) 0xab, (byte) 0x34, (byte) 0x32, (byte) 0x43, (byte) 0x9a)))); 148 } 149 150 /** 151 * Test that parsing of quoted SSID to byte array and vice versa works. 152 */ 153 @Test 154 public void testSsidEncodeDecode() throws Exception { 155 String ssid = "\"ssid_test123\""; 156 assertEquals(ssid, NativeUtil.encodeSsid(NativeUtil.decodeSsid(ssid))); 157 } 158 159 /** 160 * Test that the enclosing quotes are removed properly. 161 */ 162 @Test 163 public void testRemoveEnclosingQuotes() throws Exception { 164 assertEquals("abcdefgh", NativeUtil.removeEnclosingQuotes("\"abcdefgh\"")); 165 assertEquals("abcdefgh", NativeUtil.removeEnclosingQuotes("abcdefgh")); 166 } 167} 168