1/*
2 * Copyright (C) 2017 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.net.wifi.WifiSsid;
22import android.support.test.filters.SmallTest;
23
24import org.junit.Test;
25
26import java.io.ByteArrayOutputStream;
27import java.io.IOException;
28import java.net.ProtocolException;
29import java.nio.BufferUnderflowException;
30import java.nio.ByteBuffer;
31import java.nio.charset.StandardCharsets;
32import java.util.Arrays;
33import java.util.List;
34
35/**
36 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.HSOsuProvidersElement}.
37 */
38@SmallTest
39public class HSOsuProvidersElementTest {
40    private static final byte[] TEST_OSU_SSID_BYTES = "Test SSID".getBytes(StandardCharsets.UTF_8);
41    private static final WifiSsid TEST_OSU_SSID =
42            WifiSsid.createFromByteArray(TEST_OSU_SSID_BYTES);
43    private static final List<OsuProviderInfo> TEST_PROVIDER_LIST =
44            Arrays.asList(OsuProviderInfoTestUtil.TEST_OSU_PROVIDER_INFO);
45
46    private static final HSOsuProvidersElement TEST_OSU_PROVIDERS_ELEMENT =
47            new HSOsuProvidersElement(TEST_OSU_SSID, TEST_PROVIDER_LIST);
48
49    /**
50     * Utility function for generating test data.
51     *
52     * @param osuSsidBytes The OSU SSID bytes
53     * @return byte[]
54     */
55    private static byte[] getTestData(byte[] osuSsidBytes) {
56        try {
57            ByteArrayOutputStream out = new ByteArrayOutputStream();
58            out.write((byte) osuSsidBytes.length);
59            out.write(osuSsidBytes);
60            out.write((byte) TEST_PROVIDER_LIST.size());
61            out.write(OsuProviderInfoTestUtil.TEST_OSU_PROVIDER_INFO_RAW_BYTES);
62            return out.toByteArray();
63        } catch (IOException e) {
64            return null;
65        }
66    }
67
68    /**
69     * Verify that BufferUnderflowException will be thrown when parsing an empty buffer.
70     * @throws Exception
71     */
72    @Test(expected = BufferUnderflowException.class)
73    public void parseEmptyBuffer() throws Exception {
74        HSOsuProvidersElement.parse(ByteBuffer.allocate(0));
75    }
76
77    /**
78     * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer
79     * (missing a byte at the end).
80     *
81     * @throws Exception
82     */
83    @Test(expected = BufferUnderflowException.class)
84    public void parseTruncatedBuffer() throws Exception {
85        ByteBuffer buffer = ByteBuffer.wrap(getTestData(TEST_OSU_SSID_BYTES));
86        buffer.limit(buffer.remaining() - 1);
87        HSOsuProvidersElement.parse(buffer);
88    }
89
90    /**
91     * Verify that ProtocolException will be thrown when parsing a buffer containing an
92     * invalid OSU SSID.
93     *
94     * @throws Exception
95     */
96    @Test(expected = ProtocolException.class)
97    public void parseBufferWithInvalidLength() throws Exception {
98        byte[] invalidSsidBytes = new byte[HSOsuProvidersElement.MAXIMUM_OSU_SSID_LENGTH + 1];
99        ByteBuffer buffer = ByteBuffer.wrap(getTestData(invalidSsidBytes));
100        HSOsuProvidersElement.parse(buffer);
101    }
102
103    /**
104     * Verify that an expected {@link HSOsuProvidersElement} will be returned when parsing a buffer
105     * containing pre-defined test data.
106     *
107     * @throws Exception
108     */
109    @Test
110    public void parseBufferWithTestData() throws Exception {
111        ByteBuffer buffer = ByteBuffer.wrap(getTestData(TEST_OSU_SSID_BYTES));
112        assertEquals(TEST_OSU_PROVIDERS_ELEMENT, HSOsuProvidersElement.parse(buffer));
113    }
114}
115