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; 20 21import android.test.suitebuilder.annotation.SmallTest; 22 23import org.junit.Test; 24 25import java.io.ByteArrayOutputStream; 26import java.io.IOException; 27import java.net.ProtocolException; 28import java.nio.BufferUnderflowException; 29import java.nio.ByteBuffer; 30import java.util.ArrayList; 31import java.util.List; 32 33/** 34 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.ThreeGPPNetworkElement}. 35 */ 36@SmallTest 37public class ThreeGPPNetworkElementTest { 38 private static final byte[][] TEST_NETWORK1_PLMN_BYTES = 39 new byte[][] { new byte[] {0x21, 0x63, 0x54}, 40 new byte[] {0x43, (byte) 0x85, 0x76} }; 41 private static final List<String> TEST_NETWORK1_PLMN_LIST = new ArrayList<>(); 42 static { 43 TEST_NETWORK1_PLMN_LIST.add("123456"); 44 TEST_NETWORK1_PLMN_LIST.add("345678"); 45 } 46 private static final CellularNetwork TEST_NETWORK1 = 47 new CellularNetwork(TEST_NETWORK1_PLMN_LIST); 48 49 private static final byte[][] TEST_NETWORK2_PLMN_BYTES = 50 new byte[][] { new byte[] {(byte) 0x87, 0x29, 0x10}, 51 new byte[] {0x62, (byte) 0xF5, 0x73} }; 52 private static final List<String> TEST_NETWORK2_PLMN_LIST = new ArrayList<>(); 53 static { 54 TEST_NETWORK2_PLMN_LIST.add("789012"); 55 TEST_NETWORK2_PLMN_LIST.add("26537"); 56 } 57 private static final CellularNetwork TEST_NETWORK2 = 58 new CellularNetwork(TEST_NETWORK2_PLMN_LIST); 59 60 /** 61 * Helper function for generating test data. 62 * 63 * @param version The GUD version number 64 * @param ieiList The array containing IEI data 65 * @return byte[] 66 * @throws IOException 67 */ 68 private static byte[] getTestData(int version, byte[][] ieiList) 69 throws IOException { 70 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 71 int totalIeiSize = CellularNetworkTestUtil.getDataSize(ieiList); 72 stream.write((byte) version); 73 stream.write((byte) totalIeiSize); 74 for (byte[] iei : ieiList) { 75 stream.write(iei); 76 } 77 return stream.toByteArray(); 78 } 79 80 /** 81 * Verify that BufferUnderflowException will be thrown when parsing an empty buffer. 82 * 83 * @throws Exception 84 */ 85 @Test(expected = BufferUnderflowException.class) 86 public void parseBufferWithEmptyBuffer() throws Exception { 87 ThreeGPPNetworkElement.parse(ByteBuffer.allocate(0)); 88 } 89 90 /** 91 * Verify that ProtocolException will be thrown when parsing an buffer contained 92 * an unsupported version number. 93 * 94 * @throws Exception 95 */ 96 @Test(expected = ProtocolException.class) 97 public void parseBufferWithUnsupportedVersionNumber() throws Exception { 98 byte[][] testIeiList = new byte[][] { 99 CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK1_PLMN_BYTES) }; 100 byte[] testData = getTestData(1, testIeiList); 101 ThreeGPPNetworkElement.parse(ByteBuffer.wrap(testData)); 102 } 103 104 /** 105 * Verify that Protocol will be thrown when parsing a truncated buffer (missing a 106 * byte at the end), which will cause a inconsistency between the length value and 107 * the buffer size. 108 * 109 * @throws Exception 110 */ 111 @Test(expected = ProtocolException.class) 112 public void parseBufferWithIncompleteData() throws Exception { 113 byte[][] testIeiList = new byte[][] { 114 CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK1_PLMN_BYTES) }; 115 byte[] testData = getTestData(ThreeGPPNetworkElement.GUD_VERSION_1, testIeiList); 116 ThreeGPPNetworkElement.parse(ByteBuffer.wrap(testData, 0, testData.length - 1)); 117 } 118 119 /** 120 * Verify that the expected ThreeGPPNetworkElement is returned when parsing a buffer contained 121 * the test data. 122 * 123 * @throws Exception 124 */ 125 @Test 126 public void parseBufferWithTestData() throws Exception { 127 byte[][] testIeiList = new byte[][] { 128 CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK1_PLMN_BYTES), 129 CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK2_PLMN_BYTES) }; 130 byte[] testData = getTestData(ThreeGPPNetworkElement.GUD_VERSION_1, testIeiList); 131 132 // Setup the expected ThreeGPPNetworkElement. 133 List<CellularNetwork> networkList = new ArrayList<>(); 134 networkList.add(TEST_NETWORK1); 135 networkList.add(TEST_NETWORK2); 136 ThreeGPPNetworkElement expected = new ThreeGPPNetworkElement(networkList); 137 138 assertEquals(expected, ThreeGPPNetworkElement.parse(ByteBuffer.wrap(testData))); 139 } 140} 141