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 
17 package android.net.wifi;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.fail;
22 
23 import android.net.wifi.util.HexEncoding;
24 import android.os.Parcel;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import org.junit.Test;
29 
30 import java.nio.charset.Charset;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Arrays;
33 import java.util.List;
34 
35 /**
36  * Unit tests for {@link android.net.wifi.WifiSsid}.
37  */
38 @SmallTest
39 public class WifiSsidTest {
40 
41     private static final String TEST_SSID_UTF_8 = "Test SSID";
42     private static final String TEST_SSID_UTF_8_QUOTED = "\"" + TEST_SSID_UTF_8 + "\"";
43     private static final byte[] TEST_SSID_UTF_8_BYTES =
44             TEST_SSID_UTF_8.getBytes(StandardCharsets.UTF_8);
45     private static final String TEST_SSID_UTF_8_HEX =
46             HexEncoding.encodeToString(TEST_SSID_UTF_8_BYTES, false /* upperCase */);
47 
48     private static final byte[] TEST_SSID_NON_UTF_8_BYTES =
49             "服務集識別碼".getBytes(Charset.forName("GBK"));
50     private static final String TEST_SSID_NON_UTF_8_HEX =
51             HexEncoding.encodeToString(TEST_SSID_NON_UTF_8_BYTES, false /* upperCase */);
52 
53     /**
54      * Verify the behavior of fromByteArray()
55      */
56     @Test
testFromByteArray()57     public void testFromByteArray() {
58         WifiSsid wifiSsidUtf8 = WifiSsid.fromBytes(TEST_SSID_UTF_8_BYTES);
59         assertThat(wifiSsidUtf8).isNotNull();
60         assertThat(wifiSsidUtf8.getBytes()).isEqualTo(TEST_SSID_UTF_8_BYTES);
61         assertThat(wifiSsidUtf8.getUtf8Text()).isEqualTo(TEST_SSID_UTF_8);
62         assertThat(wifiSsidUtf8.toString()).isEqualTo(TEST_SSID_UTF_8_QUOTED);
63 
64         WifiSsid wifiSsidNonUtf8 = WifiSsid.fromBytes(TEST_SSID_NON_UTF_8_BYTES);
65         assertThat(wifiSsidNonUtf8).isNotNull();
66         assertThat(wifiSsidNonUtf8.getBytes()).isEqualTo(TEST_SSID_NON_UTF_8_BYTES);
67         assertThat(wifiSsidNonUtf8.getUtf8Text()).isNull();
68         assertThat(wifiSsidNonUtf8.toString()).isEqualTo(TEST_SSID_NON_UTF_8_HEX);
69 
70         WifiSsid wifiSsidEmpty = WifiSsid.fromBytes(new byte[0]);
71         assertThat(wifiSsidEmpty).isNotNull();
72         assertThat(wifiSsidEmpty.getBytes()).isEmpty();
73         assertThat(wifiSsidEmpty.getUtf8Text().toString()).isEmpty();
74         assertThat(wifiSsidEmpty.toString()).isEmpty();
75 
76         WifiSsid wifiSsidNull = WifiSsid.fromBytes(null);
77         assertThat(wifiSsidNull).isNotNull();
78         assertThat(wifiSsidNull.getBytes()).isEmpty();
79         assertThat(wifiSsidNull.getUtf8Text().toString()).isEmpty();
80         assertThat(wifiSsidNull.toString()).isEmpty();
81     }
82 
83     /**
84      * Verify the behavior of fromUtf8String()
85      */
86     @Test
testFromUtf8String()87     public void testFromUtf8String() {
88         WifiSsid wifiSsidUtf8 = WifiSsid.fromUtf8Text(TEST_SSID_UTF_8);
89         assertThat(wifiSsidUtf8).isNotNull();
90         assertThat(wifiSsidUtf8.getBytes()).isEqualTo(TEST_SSID_UTF_8_BYTES);
91         assertThat(wifiSsidUtf8.getUtf8Text()).isEqualTo(TEST_SSID_UTF_8);
92         assertThat(wifiSsidUtf8.toString()).isEqualTo(TEST_SSID_UTF_8_QUOTED);
93 
94         WifiSsid wifiSsidEmpty = WifiSsid.fromUtf8Text("");
95         assertThat(wifiSsidEmpty).isNotNull();
96         assertThat(wifiSsidEmpty.getBytes()).isEmpty();
97         assertThat(wifiSsidEmpty.getUtf8Text().toString()).isEmpty();
98         assertThat(wifiSsidEmpty.toString()).isEmpty();
99 
100         WifiSsid wifiSsidNull = WifiSsid.fromUtf8Text(null);
101         assertThat(wifiSsidNull).isNotNull();
102         assertThat(wifiSsidNull.getBytes()).isEmpty();
103         assertThat(wifiSsidNull.getUtf8Text().toString()).isEmpty();
104         assertThat(wifiSsidNull.toString()).isEmpty();
105     }
106 
107     /**
108      * Verify the behavior of fromString()
109      */
110     @Test
testFromString()111     public void testFromString() {
112         WifiSsid wifiSsidUtf8 = WifiSsid.fromString(TEST_SSID_UTF_8_QUOTED);
113         assertThat(wifiSsidUtf8).isNotNull();
114         assertThat(wifiSsidUtf8.getBytes()).isEqualTo(TEST_SSID_UTF_8_BYTES);
115         assertThat(wifiSsidUtf8.getUtf8Text()).isEqualTo(TEST_SSID_UTF_8);
116         assertThat(wifiSsidUtf8.toString()).isEqualTo(TEST_SSID_UTF_8_QUOTED);
117 
118         WifiSsid wifiSsidUtf8Hex = WifiSsid.fromString(TEST_SSID_UTF_8_HEX);
119         assertThat(wifiSsidUtf8Hex).isNotNull();
120         assertThat(wifiSsidUtf8Hex.getBytes()).isEqualTo(TEST_SSID_UTF_8_BYTES);
121         assertThat(wifiSsidUtf8Hex.getUtf8Text()).isEqualTo(TEST_SSID_UTF_8);
122         assertThat(wifiSsidUtf8Hex.toString()).isEqualTo(TEST_SSID_UTF_8_QUOTED);
123 
124         WifiSsid wifiSsidNonUtf8 = WifiSsid.fromString(TEST_SSID_NON_UTF_8_HEX);
125         assertThat(wifiSsidNonUtf8).isNotNull();
126         assertThat(wifiSsidNonUtf8.getBytes()).isEqualTo(TEST_SSID_NON_UTF_8_BYTES);
127         assertThat(wifiSsidNonUtf8.getUtf8Text()).isNull();
128         assertThat(wifiSsidNonUtf8.toString()).isEqualTo(TEST_SSID_NON_UTF_8_HEX);
129 
130         WifiSsid wifiSsidEmpty = WifiSsid.fromUtf8Text("");
131         assertThat(wifiSsidEmpty).isNotNull();
132         assertThat(wifiSsidEmpty.getBytes()).isEmpty();
133         assertThat(wifiSsidEmpty.getUtf8Text().toString()).isEmpty();
134         assertThat(wifiSsidEmpty.toString()).isEmpty();
135 
136         WifiSsid wifiSsidNull = WifiSsid.fromUtf8Text(null);
137         assertThat(wifiSsidNull).isNotNull();
138         assertThat(wifiSsidNull.getBytes()).isEmpty();
139         assertThat(wifiSsidNull.getUtf8Text().toString()).isEmpty();
140         assertThat(wifiSsidNull.toString()).isEmpty();
141 
142         try {
143             WifiSsid.fromString("0123456");
144             fail("Expected IllegalArgumentException for odd-length hexadecimal string");
145         } catch (IllegalArgumentException e) {
146             // Success
147         }
148     }
149 
150     /**
151      * Verify that SSID created from bytes, UTF-8 String, and toString()-formatted String with the
152      * same content are equal.
153      *
154      * @throws Exception
155      */
156     @Test
testEquals()157     public void testEquals() throws Exception {
158         WifiSsid fromBytesUtf8 = WifiSsid.fromBytes(TEST_SSID_UTF_8_BYTES);
159         WifiSsid fromUtf8StringUtf8 = WifiSsid.fromUtf8Text(TEST_SSID_UTF_8);
160         WifiSsid fromStringUtf8 = WifiSsid.fromString(TEST_SSID_UTF_8_QUOTED);
161         assertThat(fromBytesUtf8).isNotNull();
162         assertThat(fromUtf8StringUtf8).isNotNull();
163         assertThat(fromStringUtf8).isNotNull();
164         assertThat(fromBytesUtf8).isEqualTo(fromUtf8StringUtf8);
165         assertThat(fromBytesUtf8).isEqualTo(fromStringUtf8);
166         assertThat(fromUtf8StringUtf8).isEqualTo(fromStringUtf8);
167 
168         WifiSsid fromBytesNonUtf8 = WifiSsid.fromBytes(TEST_SSID_NON_UTF_8_BYTES);
169         WifiSsid fromStringNonUtf8 = WifiSsid.fromString(TEST_SSID_NON_UTF_8_HEX);
170         assertThat(fromBytesNonUtf8).isNotNull();
171         assertThat(fromStringNonUtf8).isNotNull();
172         assertThat(fromBytesNonUtf8).isEqualTo(fromStringNonUtf8);
173 
174         assertThat(fromBytesUtf8).isNotEqualTo(fromBytesNonUtf8);
175     }
176 
177     /**
178      * Verify the behavior of the Parcelable interface implementation.
179      */
180     @Test
testParcelable()181     public void testParcelable() throws Exception {
182         List<WifiSsid> testWifiSsids = Arrays.asList(
183                 WifiSsid.fromBytes(TEST_SSID_UTF_8_BYTES),
184                 WifiSsid.fromBytes(TEST_SSID_NON_UTF_8_BYTES),
185                 WifiSsid.fromUtf8Text(TEST_SSID_UTF_8),
186                 WifiSsid.fromString(TEST_SSID_UTF_8_QUOTED),
187                 WifiSsid.fromString(TEST_SSID_UTF_8_HEX),
188                 WifiSsid.fromString(TEST_SSID_NON_UTF_8_HEX));
189 
190         for (WifiSsid wifiSsid : testWifiSsids) {
191             Parcel parcel = Parcel.obtain();
192             wifiSsid.writeToParcel(parcel, 0);
193             parcel.setDataPosition(0);
194             assertThat(WifiSsid.CREATOR.createFromParcel(parcel)).isEqualTo(wifiSsid);
195         }
196     }
197 }
198