1 /* 2 * Copyright (C) 2019 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.p2p; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 22 import android.os.Parcel; 23 24 import androidx.test.filters.SmallTest; 25 26 import org.junit.Test; 27 28 /** 29 * Unit test harness for {@link android.net.wifi.p2p.WifiP2pConfig} 30 */ 31 @SmallTest 32 public class WifiP2pConfigTest { 33 34 private static final String DEVICE_ADDRESS = "aa:bb:cc:dd:ee:ff"; 35 /** 36 * Check network name setter 37 */ 38 @Test testBuilderInvalidNetworkName()39 public void testBuilderInvalidNetworkName() throws Exception { 40 WifiP2pConfig.Builder b = new WifiP2pConfig.Builder(); 41 42 // sunny case 43 try { 44 b.setNetworkName("DIRECT-ab-Hello"); 45 } catch (IllegalArgumentException e) { 46 fail("Unexpected IllegalArgumentException"); 47 } 48 49 // sunny case, no trailing string 50 try { 51 b.setNetworkName("DIRECT-WR"); 52 } catch (IllegalArgumentException e) { 53 fail("Unexpected IllegalArgumentException"); 54 } 55 56 // less than 9 characters. 57 try { 58 b.setNetworkName("DIRECT-z"); 59 fail("expected IllegalArgumentException"); 60 } catch (IllegalArgumentException e) { } 61 62 // not starts with DIRECT-xy. 63 try { 64 b.setNetworkName("ABCDEFGHIJK"); 65 fail("expected IllegalArgumentException"); 66 } catch (IllegalArgumentException e) { } 67 68 // not starts with uppercase DIRECT-xy 69 try { 70 b.setNetworkName("direct-ab"); 71 fail("expected IllegalArgumentException"); 72 } catch (IllegalArgumentException e) { } 73 74 // x and y are not selected from upper case letters, lower case letters or 75 // numbers. 76 try { 77 b.setNetworkName("direct-a?"); 78 fail("expected IllegalArgumentException"); 79 } catch (IllegalArgumentException e) { } 80 } 81 82 /** 83 * Check passphrase setter 84 */ 85 @Test testBuilderInvalidPassphrase()86 public void testBuilderInvalidPassphrase() throws Exception { 87 WifiP2pConfig.Builder b = new WifiP2pConfig.Builder(); 88 89 // sunny case 90 try { 91 b.setPassphrase("abcd1234"); 92 } catch (IllegalArgumentException e) { 93 fail("Unexpected IllegalArgumentException"); 94 } 95 96 // null string. 97 try { 98 b.setPassphrase(null); 99 fail("should throw IllegalArgumentException"); 100 } catch (IllegalArgumentException e) { 101 // expected exception. 102 } 103 104 // less than 8 characters. 105 try { 106 b.setPassphrase("12abcde"); 107 fail("should throw IllegalArgumentException"); 108 } catch (IllegalArgumentException e) { 109 // expected exception. 110 } 111 112 // more than 63 characters. 113 try { 114 b.setPassphrase( 115 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+/"); 116 fail("should throw IllegalArgumentException"); 117 } catch (IllegalArgumentException e) { 118 // expected exception. 119 } 120 } 121 122 @Test 123 /* 124 * Verify WifiP2pConfig basic operations 125 */ testWifiP2pConfig()126 public void testWifiP2pConfig() throws Exception { 127 WifiP2pConfig config = new WifiP2pConfig(); 128 config.deviceAddress = DEVICE_ADDRESS; 129 130 WifiP2pConfig copiedConfig = new WifiP2pConfig(config); 131 // no equals operator, use toString for comparison. 132 assertEquals(config.toString(), copiedConfig.toString()); 133 134 Parcel parcelW = Parcel.obtain(); 135 config.writeToParcel(parcelW, 0); 136 byte[] bytes = parcelW.marshall(); 137 parcelW.recycle(); 138 139 Parcel parcelR = Parcel.obtain(); 140 parcelR.unmarshall(bytes, 0, bytes.length); 141 parcelR.setDataPosition(0); 142 WifiP2pConfig configFromParcel = WifiP2pConfig.CREATOR.createFromParcel(parcelR); 143 144 // no equals operator, use toString for comparison. 145 assertEquals(config.toString(), configFromParcel.toString()); 146 147 } 148 149 @Test 150 /* 151 * Verify WifiP2pConfig invalidate API 152 */ testInvalidate()153 public void testInvalidate() throws Exception { 154 WifiP2pConfig config = new WifiP2pConfig(); 155 config.deviceAddress = DEVICE_ADDRESS; 156 config.invalidate(); 157 assertEquals("", config.deviceAddress); 158 } 159 160 } 161