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.hotspot2.anqp; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertNull; 21 22import android.test.suitebuilder.annotation.SmallTest; 23 24import org.junit.Test; 25 26import java.net.ProtocolException; 27import java.nio.BufferUnderflowException; 28import java.nio.ByteBuffer; 29import java.util.ArrayList; 30import java.util.List; 31 32/** 33 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.CellularNetwork}. 34 */ 35@SmallTest 36public class CellularNetworkTest { 37 private static final byte[] TEST_PLMN_BYTES_1 = new byte[] {0x12, 0x34, 0x56}; 38 private static final String TEST_PLMN_STRING_1 = "214653"; 39 private static final byte[] TEST_PLMN_BYTES_2 = new byte[] {0x13, (byte) 0xF9, 0x32}; 40 private static final String TEST_PLMN_STRING_2 = "31923"; 41 42 /** 43 * Verify that BufferUnderflowException will be thrown when parsing an empty buffer. 44 * 45 * @throws Exception 46 */ 47 @Test(expected = BufferUnderflowException.class) 48 public void parseBufferWithEmptyBuffer() throws Exception { 49 CellularNetwork.parse(ByteBuffer.allocate(0)); 50 } 51 52 /** 53 * Verify that a null will be returned when parsing a buffer contained an unsupported IEI type. 54 * 55 * @throws Exception 56 */ 57 @Test 58 public void parseBufferWithInvalidIEIType() throws Exception { 59 byte[][] plmnsData = new byte[][] {TEST_PLMN_BYTES_1, TEST_PLMN_BYTES_2}; 60 byte[] testData = CellularNetworkTestUtil.formatPLMNListIEI(1, plmnsData); 61 assertNull(CellularNetwork.parse(ByteBuffer.wrap(testData))); 62 } 63 64 /** 65 * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer 66 * (missing a byte at the end). 67 * 68 * @throws Exception 69 */ 70 @Test(expected = BufferUnderflowException.class) 71 public void parseBufferWithIncompleteData() throws Exception { 72 byte[][] plmnsData = new byte[][] {TEST_PLMN_BYTES_1, TEST_PLMN_BYTES_2}; 73 byte[] testData = CellularNetworkTestUtil.formatPLMNListIEI(plmnsData); 74 CellularNetwork.parse(ByteBuffer.wrap(testData, 0, testData.length - 1)); 75 } 76 77 /** 78 * Verify that ProtocolException will be thrown when IEI size and the PLMN count doesn't 79 * match. 80 * 81 * @throws Exception 82 */ 83 @Test(expected = ProtocolException.class) 84 public void parseBufferWithMismatchIEISizeAndPLMNCount() throws Exception { 85 byte[][] plmnsData = new byte[][] {TEST_PLMN_BYTES_1, TEST_PLMN_BYTES_2}; 86 // Get test data with IEI size set to incorrect value. 87 byte[] testData = CellularNetworkTestUtil.formatPLMNListIEI( 88 CellularNetwork.IEI_TYPE_PLMN_LIST, plmnsData, true); 89 CellularNetwork.parse(ByteBuffer.wrap(testData)); 90 } 91 92 /** 93 * Verify that the expected ProtocolPortTuple is returned when parsing a buffer contained 94 * the test data. 95 * 96 * @throws Exception 97 */ 98 @Test 99 public void parseBufferWithTestData() throws Exception { 100 byte[][] plmnsData = new byte[][] {TEST_PLMN_BYTES_1, TEST_PLMN_BYTES_2}; 101 byte[] testData = CellularNetworkTestUtil.formatPLMNListIEI(plmnsData); 102 103 // Setup the expected CellularNetwork. 104 List<String> plmnList = new ArrayList<>(); 105 plmnList.add(TEST_PLMN_STRING_1); 106 plmnList.add(TEST_PLMN_STRING_2); 107 CellularNetwork expected = new CellularNetwork(plmnList); 108 109 assertEquals(expected, CellularNetwork.parse(ByteBuffer.wrap(testData))); 110 } 111} 112