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 static org.junit.Assert.assertEquals;
2054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
2154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport android.test.suitebuilder.annotation.SmallTest;
2254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
2354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport org.junit.Test;
2454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
2554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport java.io.ByteArrayOutputStream;
2654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport java.io.IOException;
2754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport java.net.ProtocolException;
2854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport java.nio.BufferUnderflowException;
2954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport java.nio.ByteBuffer;
3054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport java.util.ArrayList;
3154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuimport java.util.List;
3254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
3354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu/**
3454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.ThreeGPPNetworkElement}.
3554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu */
3654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu@SmallTest
3754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiupublic class ThreeGPPNetworkElementTest {
3854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    private static final byte[][] TEST_NETWORK1_PLMN_BYTES =
3954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            new byte[][] { new byte[] {0x21, 0x63, 0x54},
4054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu                           new byte[] {0x43, (byte) 0x85, 0x76} };
4154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    private static final List<String> TEST_NETWORK1_PLMN_LIST = new ArrayList<>();
4254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    static {
4354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        TEST_NETWORK1_PLMN_LIST.add("123456");
4454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        TEST_NETWORK1_PLMN_LIST.add("345678");
4554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
4654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    private static final CellularNetwork TEST_NETWORK1 =
4754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            new CellularNetwork(TEST_NETWORK1_PLMN_LIST);
4854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
4954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    private static final byte[][] TEST_NETWORK2_PLMN_BYTES =
5054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            new byte[][] { new byte[] {(byte) 0x87, 0x29, 0x10},
5154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu                           new byte[] {0x62, (byte) 0xF5, 0x73} };
5254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    private static final List<String> TEST_NETWORK2_PLMN_LIST = new ArrayList<>();
5354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    static {
5454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        TEST_NETWORK2_PLMN_LIST.add("789012");
5554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        TEST_NETWORK2_PLMN_LIST.add("26537");
5654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
5754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    private static final CellularNetwork TEST_NETWORK2 =
5854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            new CellularNetwork(TEST_NETWORK2_PLMN_LIST);
5954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
6054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    /**
6154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * Helper function for generating test data.
6254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     *
6354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @param version The GUD version number
6454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @param ieiList The array containing IEI data
6554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @return byte[]
6654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @throws IOException
6754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     */
6854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    private static byte[] getTestData(int version, byte[][] ieiList)
6954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            throws IOException {
7054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        ByteArrayOutputStream stream = new ByteArrayOutputStream();
7154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        int totalIeiSize = CellularNetworkTestUtil.getDataSize(ieiList);
7254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        stream.write((byte) version);
7354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        stream.write((byte) totalIeiSize);
7454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        for (byte[] iei : ieiList) {
7554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            stream.write(iei);
7654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        }
7754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        return stream.toByteArray();
7854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
7954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
8054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    /**
8154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * Verify that BufferUnderflowException will be thrown when parsing an empty buffer.
8254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     *
8354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @throws Exception
8454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     */
8554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    @Test(expected = BufferUnderflowException.class)
8654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    public void parseBufferWithEmptyBuffer() throws Exception {
8754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        ThreeGPPNetworkElement.parse(ByteBuffer.allocate(0));
8854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
8954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
9054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    /**
9154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * Verify that ProtocolException will be thrown when parsing an buffer contained
9254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * an unsupported version number.
9354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     *
9454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @throws Exception
9554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     */
9654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    @Test(expected = ProtocolException.class)
9754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    public void parseBufferWithUnsupportedVersionNumber() throws Exception {
9854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        byte[][] testIeiList = new byte[][] {
9954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK1_PLMN_BYTES) };
10054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        byte[] testData = getTestData(1, testIeiList);
10154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        ThreeGPPNetworkElement.parse(ByteBuffer.wrap(testData));
10254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
10354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
10454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    /**
10554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * Verify that Protocol will be thrown when parsing a truncated buffer (missing a
10654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * byte at the end), which will cause a inconsistency between the length value and
10754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * the buffer size.
10854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     *
10954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @throws Exception
11054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     */
11154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    @Test(expected = ProtocolException.class)
11254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    public void parseBufferWithIncompleteData() throws Exception {
11354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        byte[][] testIeiList = new byte[][] {
11454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK1_PLMN_BYTES) };
11554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        byte[] testData = getTestData(ThreeGPPNetworkElement.GUD_VERSION_1, testIeiList);
11654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        ThreeGPPNetworkElement.parse(ByteBuffer.wrap(testData, 0, testData.length - 1));
11754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
11854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
11954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    /**
12054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * Verify that the expected ThreeGPPNetworkElement is returned when parsing a buffer contained
12154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * the test data.
12254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     *
12354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     * @throws Exception
12454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu     */
12554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    @Test
12654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    public void parseBufferWithTestData() throws Exception {
12754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        byte[][] testIeiList = new byte[][] {
12854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK1_PLMN_BYTES),
12954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu            CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK2_PLMN_BYTES) };
13054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        byte[] testData = getTestData(ThreeGPPNetworkElement.GUD_VERSION_1, testIeiList);
13154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
13254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Setup the expected ThreeGPPNetworkElement.
13354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        List<CellularNetwork> networkList = new ArrayList<>();
13454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        networkList.add(TEST_NETWORK1);
13554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        networkList.add(TEST_NETWORK2);
13654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        ThreeGPPNetworkElement expected = new ThreeGPPNetworkElement(networkList);
13754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
13854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        assertEquals(expected, ThreeGPPNetworkElement.parse(ByteBuffer.wrap(testData)));
13954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu    }
14054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu}
141