154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu/*
254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * Copyright (C) 2016 The Android Open Source Project
354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu *
454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * Licensed under the Apache License, Version 2.0 (the "License");
554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * you may not use this file except in compliance with the License.
654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * You may obtain a copy of the License at
754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu *
854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu *      http://www.apache.org/licenses/LICENSE-2.0
954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu *
1054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * Unless required by applicable law or agreed to in writing, software
1154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * distributed under the License is distributed on an "AS IS" BASIS,
1254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * See the License for the specific language governing permissions and
1454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * limitations under the License.
1554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu */
1654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
1754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiupackage com.android.server.wifi.hotspot2.anqp;
1854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
1954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport java.io.ByteArrayOutputStream;
2054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport java.io.IOException;
2154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
2254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu/**
2354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * Utility class for formatting IEI (Information Element Identity) data for testing.
2454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu */
2554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiupublic class CellularNetworkTestUtil {
2654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    /**
2754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * Format and return PLMN List IEI with the given PLMN list data.
2854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     *
2954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @param plmnList The array of PLMN data
3054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @return byte[]
3154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @throws IOException
3254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     */
3354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    public static byte[] formatPLMNListIEI(byte[][] plmnList) throws IOException {
3454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        return formatPLMNListIEI(CellularNetwork.IEI_TYPE_PLMN_LIST, plmnList);
3554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
3654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
3754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    /**
3854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * Format and return PLMN List IEI with the given IEI type and PLMN list data.  This
3954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * allows the test to use an invalid IEI type for testing purpose.
4054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     *
4154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @param ieiType The IEI type
4254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @param plmnList The array of PLMN data
4354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @return byte[]
4454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @throws IOException
4554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     */
4654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    public static byte[] formatPLMNListIEI(int ieiType, byte[][] plmnList) throws IOException {
4754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        return formatPLMNListIEI(ieiType, plmnList, false);
4854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
4954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
5054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    /**
5154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * Format and return PLMN List IEI with the given IEI type and PLMN list data.  This also
5254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * allows the test to intentionally setting an incorrect size value.
5354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     *
5454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @param ieiType The IEI type
5554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @param plmnList The array of PLMN data
5654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @param setWrongSize Flag for setting incorrect IEI size
5754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @return byte[]
5854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @throws IOException
5954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     */
6054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    public static byte[] formatPLMNListIEI(int ieiType, byte[][] plmnList, boolean setWrongSize)
6154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            throws IOException {
6254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        ByteArrayOutputStream stream = new ByteArrayOutputStream();
6354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
6454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Calculate the total bytes for all the PLMNs.
6554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        int plmnsSize = getDataSize(plmnList);
6654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
6754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Use incorrect size intentionally.
6854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        if (setWrongSize) plmnsSize -= 1;
6954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
7054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        stream.write((byte) ieiType);
7154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // One extra byte for the PLMN count field.
7254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        stream.write((byte) ((plmnsSize + 1) & CellularNetwork.IEI_CONTENT_LENGTH_MASK));
7354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        stream.write((byte) plmnList.length);
7454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        for (byte[] plmn : plmnList) {
7554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            stream.write(plmn);
7654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        }
7754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
7854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        return stream.toByteArray();
7954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
8054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
8154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    /**
8254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * Return the number of bytes in a 2D array.
8354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     *
8454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @param dataArray The 2D array
8554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @return The number of bytes in the 2D array
8654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     */
8754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    public static int getDataSize(byte[][] dataArray) {
8854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        int size = 0;
8954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        for (byte[] data : dataArray) {
9054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            size += data.length;
9154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        }
9254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        return size;
9354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
9454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu}
95