1/*
2 * Copyright (C) 2016 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;
18
19import android.net.wifi.WifiConfiguration;
20import android.net.wifi.WifiEnterpriseConfig;
21
22/**
23 * Helper for creating and populating WifiConfigurations in unit tests.
24 */
25public class WifiConfigurationTestUtil {
26    /**
27     * These values are used to describe AP's security setting. One AP can support multiple of them,
28     * only if there is no conflict.
29     */
30    public static final int SECURITY_NONE = 0;
31    public static final int SECURITY_WEP =  1 << 0;
32    public static final int SECURITY_PSK =  1 << 1;
33    public static final int SECURITY_EAP =  1 << 2;
34
35    /**
36     * Construct a {@link android.net.wifi.WifiConfiguration}.
37     * @param networkId the configuration's networkId
38     * @param uid the configuration's creator uid
39     * @param ssid the configuration's ssid
40     * @param shared whether the configuration is shared with other users on the device
41     * @param enabled whether the configuration is enabled
42     * @param fqdn the configuration's FQDN (Hotspot 2.0 only)
43     * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only)
44     * @return the constructed {@link android.net.wifi.WifiConfiguration}
45     */
46    public static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid,
47            boolean shared, boolean enabled, String fqdn, String providerFriendlyName) {
48        final WifiConfiguration config = new WifiConfiguration();
49        config.SSID = ssid;
50        config.networkId = networkId;
51        config.creatorUid = uid;
52        config.shared = shared;
53        config.status = enabled ? WifiConfiguration.Status.ENABLED
54                : WifiConfiguration.Status.DISABLED;
55        if (fqdn != null) {
56            config.FQDN = fqdn;
57            config.providerFriendlyName = providerFriendlyName;
58            config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM);
59        }
60        return config;
61    }
62
63    /**
64     * Construct a {@link android.net.wifi.WifiConfiguration}.
65     * @param networkId the configuration's networkId
66     * @param uid the configuration's creator uid
67     * @param ssid the configuration's ssid
68     * @param shared whether the configuration is shared with other users on the device
69     * @param enabled whether the configuration is enabled
70     * @param fqdn the configuration's FQDN (Hotspot 2.0 only)
71     * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only)
72     * @param security the configuration's security type
73     * @return the constructed {@link android.net.wifi.WifiConfiguration}
74     */
75    public static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid,
76            boolean shared, boolean enabled, String fqdn, String providerFriendlyName,
77            int security) {
78        WifiConfiguration config = generateWifiConfig(networkId, uid, ssid, shared, enabled, fqdn,
79                providerFriendlyName);
80
81        if (security == SECURITY_NONE) {
82            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
83        } else {
84            if (((security & SECURITY_WEP) != 0) || ((security & SECURITY_PSK) != 0)) {
85                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
86            }
87
88            if ((security & SECURITY_EAP) != 0) {
89                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
90                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
91            }
92        }
93        return config;
94    }
95}
96