summaryrefslogtreecommitdiff
path: root/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElementTest.java
blob: dfb5abec249f80808aabb68e618b612dc1c330d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi.hotspot2.anqp;

import static org.junit.Assert.assertEquals;

import android.net.wifi.WifiSsid;

import androidx.test.filters.SmallTest;

import com.android.server.wifi.WifiBaseTest;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.ProtocolException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.HSOsuProvidersElement}.
 */
@SmallTest
public class HSOsuProvidersElementTest extends WifiBaseTest {
    private static final byte[] TEST_OSU_SSID_BYTES = "Test SSID".getBytes(StandardCharsets.UTF_8);
    private static final WifiSsid TEST_OSU_SSID =
            WifiSsid.createFromByteArray(TEST_OSU_SSID_BYTES);
    private static final List<OsuProviderInfo> TEST_PROVIDER_LIST =
            Arrays.asList(OsuProviderInfoTestUtil.TEST_OSU_PROVIDER_INFO);

    private static final HSOsuProvidersElement TEST_OSU_PROVIDERS_ELEMENT =
            new HSOsuProvidersElement(TEST_OSU_SSID, TEST_PROVIDER_LIST);

    /**
     * Utility function for generating test data.
     *
     * @param osuSsidBytes The OSU SSID bytes
     * @return byte[]
     */
    private static byte[] getTestData(byte[] osuSsidBytes) {
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            out.write((byte) osuSsidBytes.length);
            out.write(osuSsidBytes);
            out.write((byte) TEST_PROVIDER_LIST.size());
            out.write(OsuProviderInfoTestUtil.TEST_OSU_PROVIDER_INFO_RAW_BYTES);
            return out.toByteArray();
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * Verify that BufferUnderflowException will be thrown when parsing an empty buffer.
     * @throws Exception
     */
    @Test(expected = BufferUnderflowException.class)
    public void parseEmptyBuffer() throws Exception {
        HSOsuProvidersElement.parse(ByteBuffer.allocate(0));
    }

    /**
     * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer
     * (missing a byte at the end).
     *
     * @throws Exception
     */
    @Test(expected = BufferUnderflowException.class)
    public void parseTruncatedBuffer() throws Exception {
        ByteBuffer buffer = ByteBuffer.wrap(getTestData(TEST_OSU_SSID_BYTES));
        buffer.limit(buffer.remaining() - 1);
        HSOsuProvidersElement.parse(buffer);
    }

    /**
     * Verify that ProtocolException will be thrown when parsing a buffer containing an
     * invalid OSU SSID.
     *
     * @throws Exception
     */
    @Test(expected = ProtocolException.class)
    public void parseBufferWithInvalidLength() throws Exception {
        byte[] invalidSsidBytes = new byte[HSOsuProvidersElement.MAXIMUM_OSU_SSID_LENGTH + 1];
        ByteBuffer buffer = ByteBuffer.wrap(getTestData(invalidSsidBytes));
        HSOsuProvidersElement.parse(buffer);
    }

    /**
     * Verify that an expected {@link HSOsuProvidersElement} will be returned when parsing a buffer
     * containing pre-defined test data.
     *
     * @throws Exception
     */
    @Test
    public void parseBufferWithTestData() throws Exception {
        ByteBuffer buffer = ByteBuffer.wrap(getTestData(TEST_OSU_SSID_BYTES));
        assertEquals(TEST_OSU_PROVIDERS_ELEMENT, HSOsuProvidersElement.parse(buffer));
    }
}