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 17 package com.android.server.wifi.hotspot2.anqp; 18 19 import java.io.ByteArrayOutputStream; 20 import java.io.IOException; 21 22 /** 23 * Utility class for formatting IEI (Information Element Identity) data for testing. 24 */ 25 public class CellularNetworkTestUtil { 26 /** 27 * Format and return PLMN List IEI with the given PLMN list data. 28 * 29 * @param plmnList The array of PLMN data 30 * @return byte[] 31 * @throws IOException 32 */ formatPLMNListIEI(byte[][] plmnList)33 public static byte[] formatPLMNListIEI(byte[][] plmnList) throws IOException { 34 return formatPLMNListIEI(CellularNetwork.IEI_TYPE_PLMN_LIST, plmnList); 35 } 36 37 /** 38 * Format and return PLMN List IEI with the given IEI type and PLMN list data. This 39 * allows the test to use an invalid IEI type for testing purpose. 40 * 41 * @param ieiType The IEI type 42 * @param plmnList The array of PLMN data 43 * @return byte[] 44 * @throws IOException 45 */ formatPLMNListIEI(int ieiType, byte[][] plmnList)46 public static byte[] formatPLMNListIEI(int ieiType, byte[][] plmnList) throws IOException { 47 return formatPLMNListIEI(ieiType, plmnList, false); 48 } 49 50 /** 51 * Format and return PLMN List IEI with the given IEI type and PLMN list data. This also 52 * allows the test to intentionally setting an incorrect size value. 53 * 54 * @param ieiType The IEI type 55 * @param plmnList The array of PLMN data 56 * @param setWrongSize Flag for setting incorrect IEI size 57 * @return byte[] 58 * @throws IOException 59 */ formatPLMNListIEI(int ieiType, byte[][] plmnList, boolean setWrongSize)60 public static byte[] formatPLMNListIEI(int ieiType, byte[][] plmnList, boolean setWrongSize) 61 throws IOException { 62 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 63 64 // Calculate the total bytes for all the PLMNs. 65 int plmnsSize = getDataSize(plmnList); 66 67 // Use incorrect size intentionally. 68 if (setWrongSize) plmnsSize -= 1; 69 70 stream.write((byte) ieiType); 71 // One extra byte for the PLMN count field. 72 stream.write((byte) ((plmnsSize + 1) & CellularNetwork.IEI_CONTENT_LENGTH_MASK)); 73 stream.write((byte) plmnList.length); 74 for (byte[] plmn : plmnList) { 75 stream.write(plmn); 76 } 77 78 return stream.toByteArray(); 79 } 80 81 /** 82 * Return the number of bytes in a 2D array. 83 * 84 * @param dataArray The 2D array 85 * @return The number of bytes in the 2D array 86 */ getDataSize(byte[][] dataArray)87 public static int getDataSize(byte[][] dataArray) { 88 int size = 0; 89 for (byte[] data : dataArray) { 90 size += data.length; 91 } 92 return size; 93 } 94 } 95