summaryrefslogtreecommitdiff
path: root/wifi/tests/src/android/net/wifi/hotspot2/pps/HomeSpTest.java
blob: c7993e308d17bf53ff93dc1d233f0fd08e0869a8 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * 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 android.net.wifi.hotspot2.pps;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.os.Parcel;
import android.support.test.filters.SmallTest;

import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Unit tests for {@link android.net.wifi.hotspot2.pps.HomeSp}.
 */
@SmallTest
public class HomeSpTest {

    /**
     * Helper function for creating a map of home network IDs for testing.
     *
     * @return Map of home network IDs
     */
    private static Map<String, Long> createHomeNetworkIds() {
        Map<String, Long> homeNetworkIds = new HashMap<>();
        homeNetworkIds.put("ssid", 0x1234L);
        homeNetworkIds.put("nullhessid", null);
        return homeNetworkIds;
    }

    /**
     * Helper function for creating a HomeSp for testing.
     *
     * @param homeNetworkIds The map of home network IDs associated with HomeSp
     * @return {@link HomeSp}
     */
    private static HomeSp createHomeSp(Map<String, Long> homeNetworkIds) {
        HomeSp homeSp = new HomeSp();
        homeSp.setFqdn("fqdn");
        homeSp.setFriendlyName("friendly name");
        homeSp.setIconUrl("icon.url");
        homeSp.setHomeNetworkIds(homeNetworkIds);
        homeSp.setMatchAllOis(new long[] {0x11L, 0x22L});
        homeSp.setMatchAnyOis(new long[] {0x33L, 0x44L});
        homeSp.setOtherHomePartners(new String[] {"partner1", "partner2"});
        homeSp.setRoamingConsortiumOis(new long[] {0x55, 0x66});
        return homeSp;
    }

    /**
     * Helper function for creating a HomeSp with home network IDs for testing.
     *
     * @return {@link HomeSp}
     */
    private static HomeSp createHomeSpWithHomeNetworkIds() {
        return createHomeSp(createHomeNetworkIds());
    }

    /**
     * Helper function for creating a HomeSp without home network IDs for testing.
     *
     * @return {@link HomeSp}
     */
    private static HomeSp createHomeSpWithoutHomeNetworkIds() {
        return createHomeSp(null);
    }

    /**
     * Helper function for verifying HomeSp after parcel write then read.
     * @param writeHomeSp
     * @throws Exception
     */
    private static void verifyParcel(HomeSp writeHomeSp) throws Exception {
        Parcel parcel = Parcel.obtain();
        writeHomeSp.writeToParcel(parcel, 0);

        parcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
        HomeSp readHomeSp = HomeSp.CREATOR.createFromParcel(parcel);
        assertTrue(readHomeSp.equals(writeHomeSp));
    }

    /**
     * Verify parcel read/write for an empty HomeSp.
     *
     * @throws Exception
     */
    @Test
    public void verifyParcelWithEmptyHomeSp() throws Exception {
        verifyParcel(new HomeSp());
    }

    /**
     * Verify parcel read/write for a HomeSp containing Home Network IDs.
     *
     * @throws Exception
     */
    @Test
    public void verifyParcelWithHomeNetworkIds() throws Exception {
        verifyParcel(createHomeSpWithHomeNetworkIds());
    }

    /**
     * Verify parcel read/write for a HomeSp without Home Network IDs.
     *
     * @throws Exception
     */
    @Test
    public void verifyParcelWithoutHomeNetworkIds() throws Exception {
        verifyParcel(createHomeSpWithoutHomeNetworkIds());
    }

    /**
     * Verify that a HomeSp is valid when both FQDN and Friendly Name
     * are provided.
     *
     * @throws Exception
     */
    @Test
    public void validateValidHomeSp() throws Exception {
        HomeSp homeSp = createHomeSpWithHomeNetworkIds();
        assertTrue(homeSp.validate());
    }

    /**
     * Verify that a HomeSp is not valid when FQDN is not provided
     *
     * @throws Exception
     */
    @Test
    public void validateHomeSpWithoutFqdn() throws Exception {
        HomeSp homeSp = createHomeSpWithHomeNetworkIds();
        homeSp.setFqdn(null);
        assertFalse(homeSp.validate());
    }

    /**
     * Verify that a HomeSp is not valid when Friendly Name is not provided
     *
     * @throws Exception
     */
    @Test
    public void validateHomeSpWithoutFriendlyName() throws Exception {
        HomeSp homeSp = createHomeSpWithHomeNetworkIds();
        homeSp.setFriendlyName(null);
        assertFalse(homeSp.validate());
    }

    /**
     * Verify that a HomeSp is valid when the optional Home Network IDs are
     * not provided.
     *
     * @throws Exception
     */
    @Test
    public void validateHomeSpWithoutHomeNetworkIds() throws Exception {
        HomeSp homeSp = createHomeSpWithoutHomeNetworkIds();
        assertTrue(homeSp.validate());
    }

    /**
     * Verify that a HomeSp is invalid when the optional Home Network IDs
     * contained an invalid SSID (exceeding maximum number of bytes).
     *
     * @throws Exception
     */
    @Test
    public void validateHomeSpWithInvalidHomeNetworkIds() throws Exception {
        HomeSp homeSp = createHomeSpWithoutHomeNetworkIds();
        // HomeNetworkID with SSID exceeding the maximum length.
        Map<String, Long> homeNetworkIds = new HashMap<>();
        byte[] rawSsidBytes = new byte[33];
        Arrays.fill(rawSsidBytes, (byte) 'a');
        homeNetworkIds.put(new String(rawSsidBytes, StandardCharsets.UTF_8), 0x1234L);
        homeSp.setHomeNetworkIds(homeNetworkIds);
        assertFalse(homeSp.validate());
    }

    /**
     * Verify that copy constructor works when pass in a null source.
     *
     * @throws Exception
     */
    @Test
    public void validateCopyConstructorFromNullSource() throws Exception {
        HomeSp copySp = new HomeSp(null);
        HomeSp defaultSp = new HomeSp();
        assertTrue(copySp.equals(defaultSp));
    }

    /**
     * Verify that copy constructor works when pass in a valid source.
     *
     * @throws Exception
     */
    @Test
    public void validateCopyConstructorFromValidSource() throws Exception {
        HomeSp sourceSp = createHomeSpWithHomeNetworkIds();
        HomeSp copySp = new HomeSp(sourceSp);
        assertTrue(copySp.equals(sourceSp));
    }
}