summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/CellularNetworkTestUtil.java
blob: dad2919f09ff8da92a45a938a3cd7a2758102a43 (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
/*
 * Copyright (C) 2016 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 java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Utility class for formatting IEI (Information Element Identity) data for testing.
 */
public class CellularNetworkTestUtil {
    /**
     * Format and return PLMN List IEI with the given PLMN list data.
     *
     * @param plmnList The array of PLMN data
     * @return byte[]
     * @throws IOException
     */
    public static byte[] formatPLMNListIEI(byte[][] plmnList) throws IOException {
        return formatPLMNListIEI(CellularNetwork.IEI_TYPE_PLMN_LIST, plmnList);
    }

    /**
     * Format and return PLMN List IEI with the given IEI type and PLMN list data.  This
     * allows the test to use an invalid IEI type for testing purpose.
     *
     * @param ieiType The IEI type
     * @param plmnList The array of PLMN data
     * @return byte[]
     * @throws IOException
     */
    public static byte[] formatPLMNListIEI(int ieiType, byte[][] plmnList) throws IOException {
        return formatPLMNListIEI(ieiType, plmnList, false);
    }

    /**
     * Format and return PLMN List IEI with the given IEI type and PLMN list data.  This also
     * allows the test to intentionally setting an incorrect size value.
     *
     * @param ieiType The IEI type
     * @param plmnList The array of PLMN data
     * @param setWrongSize Flag for setting incorrect IEI size
     * @return byte[]
     * @throws IOException
     */
    public static byte[] formatPLMNListIEI(int ieiType, byte[][] plmnList, boolean setWrongSize)
            throws IOException {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        // Calculate the total bytes for all the PLMNs.
        int plmnsSize = getDataSize(plmnList);

        // Use incorrect size intentionally.
        if (setWrongSize) plmnsSize -= 1;

        stream.write((byte) ieiType);
        // One extra byte for the PLMN count field.
        stream.write((byte) ((plmnsSize + 1) & CellularNetwork.IEI_CONTENT_LENGTH_MASK));
        stream.write((byte) plmnList.length);
        for (byte[] plmn : plmnList) {
            stream.write(plmn);
        }

        return stream.toByteArray();
    }

    /**
     * Return the number of bytes in a 2D array.
     *
     * @param dataArray The 2D array
     * @return The number of bytes in the 2D array
     */
    public static int getDataSize(byte[][] dataArray) {
        int size = 0;
        for (byte[] data : dataArray) {
            size += data.length;
        }
        return size;
    }
}