Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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.telephony.cts; |
| 18 | |
Adam Lesinski | 82ac6a2 | 2014-06-06 14:09:56 -0700 | [diff] [blame] | 19 | import android.bluetooth.BluetoothAdapter; |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Madan Ankapura | 138292c | 2010-12-09 18:25:29 -0800 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
Chenjie Luo | d82745f | 2014-09-26 12:49:43 -0700 | [diff] [blame] | 22 | import android.cts.util.ReadElf; |
| 23 | import android.cts.util.TestThread; |
bs | c9ba1c7 | 2012-06-22 20:59:53 +0900 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 25 | import android.net.wifi.WifiInfo; |
| 26 | import android.net.wifi.WifiManager; |
Andreas Huber | 30393d2 | 2010-09-30 09:33:24 -0700 | [diff] [blame] | 27 | import android.os.Build; |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 28 | import android.os.Looper; |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 29 | import android.telephony.CellLocation; |
| 30 | import android.telephony.PhoneStateListener; |
| 31 | import android.telephony.TelephonyManager; |
| 32 | import android.test.AndroidTestCase; |
xinhe | a5c70e1 | 2014-10-02 11:07:49 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 34 | |
Catherine Liu | 9b742b1 | 2012-08-23 16:41:56 -0500 | [diff] [blame] | 35 | import com.android.internal.telephony.PhoneConstants; |
| 36 | |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 37 | import java.util.regex.Pattern; |
| 38 | |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 39 | public class TelephonyManagerTest extends AndroidTestCase { |
| 40 | private TelephonyManager mTelephonyManager; |
| 41 | private boolean mOnCellLocationChangedCalled = false; |
| 42 | private final Object mLock = new Object(); |
| 43 | private static final int TOLERANCE = 1000; |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 44 | private PhoneStateListener mListener; |
bs | c9ba1c7 | 2012-06-22 20:59:53 +0900 | [diff] [blame] | 45 | private static ConnectivityManager mCm; |
xinhe | a5c70e1 | 2014-10-02 11:07:49 -0700 | [diff] [blame] | 46 | private static final String TAG = "android.telephony.cts.TelephonyManagerTest"; |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 47 | |
| 48 | @Override |
| 49 | protected void setUp() throws Exception { |
| 50 | super.setUp(); |
| 51 | mTelephonyManager = |
| 52 | (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE); |
xinhe | a5c70e1 | 2014-10-02 11:07:49 -0700 | [diff] [blame] | 53 | mCm = (ConnectivityManager)getContext().getSystemService(Context.CONNECTIVITY_SERVICE); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected void tearDown() throws Exception { |
| 58 | if (mListener != null) { |
| 59 | // unregister the listener |
| 60 | mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_NONE); |
| 61 | } |
| 62 | super.tearDown(); |
| 63 | } |
| 64 | |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 65 | public void testListen() throws Throwable { |
xinhe | a5c70e1 | 2014-10-02 11:07:49 -0700 | [diff] [blame] | 66 | if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) { |
| 67 | Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE"); |
| 68 | return; |
| 69 | } |
| 70 | |
Brett Chabot | 45d7163 | 2009-11-06 20:22:55 -0800 | [diff] [blame] | 71 | if (mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) { |
| 72 | // TODO: temp workaround, need to adjust test to for CDMA |
| 73 | return; |
| 74 | } |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 75 | |
| 76 | // Test register |
| 77 | TestThread t = new TestThread(new Runnable() { |
| 78 | public void run() { |
| 79 | Looper.prepare(); |
| 80 | |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 81 | mListener = new PhoneStateListener() { |
| 82 | @Override |
| 83 | public void onCellLocationChanged(CellLocation location) { |
Mateusz Madetko | 2c66b38 | 2015-03-09 15:32:20 +0100 | [diff] [blame] | 84 | if(!mOnCellLocationChangedCalled) { |
| 85 | synchronized (mLock) { |
| 86 | mOnCellLocationChangedCalled = true; |
| 87 | mLock.notify(); |
| 88 | } |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | }; |
| 92 | mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_CELL_LOCATION); |
xinhe | a91e743 | 2014-10-06 15:48:12 -0700 | [diff] [blame] | 93 | CellLocation.requestLocationUpdate(); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 94 | Looper.loop(); |
| 95 | } |
| 96 | }); |
| 97 | t.start(); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 98 | synchronized (mLock) { |
| 99 | while (!mOnCellLocationChangedCalled) { |
| 100 | mLock.wait(); |
| 101 | } |
| 102 | } |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 103 | assertTrue(mOnCellLocationChangedCalled); |
| 104 | |
| 105 | // Test unregister |
| 106 | t = new TestThread(new Runnable() { |
| 107 | public void run() { |
| 108 | Looper.prepare(); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 109 | // unregister the listener |
| 110 | mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_NONE); |
| 111 | mOnCellLocationChangedCalled = false; |
| 112 | // unregister again, to make sure doing so does not call the listener |
| 113 | mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_NONE); |
xinhe | a91e743 | 2014-10-06 15:48:12 -0700 | [diff] [blame] | 114 | CellLocation.requestLocationUpdate(); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 115 | Looper.loop(); |
| 116 | } |
| 117 | }); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 118 | |
xinhe | a91e743 | 2014-10-06 15:48:12 -0700 | [diff] [blame] | 119 | t.start(); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 120 | synchronized (mLock) { |
Brett Chabot | ec61146 | 2009-12-22 15:54:34 -0800 | [diff] [blame] | 121 | mLock.wait(TOLERANCE); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 122 | } |
xinhe | a91e743 | 2014-10-06 15:48:12 -0700 | [diff] [blame] | 123 | assertFalse(mOnCellLocationChangedCalled); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * The getter methods here are all related to the information about the telephony. |
| 128 | * These getters are related to concrete location, phone, service provider company, so |
| 129 | * it's no need to get details of these information, just make sure they are in right |
| 130 | * condition(>0 or not null). |
| 131 | */ |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 132 | public void testTelephonyManager() { |
| 133 | assertTrue(mTelephonyManager.getNetworkType() >= TelephonyManager.NETWORK_TYPE_UNKNOWN); |
| 134 | assertTrue(mTelephonyManager.getPhoneType() >= TelephonyManager.PHONE_TYPE_NONE); |
| 135 | assertTrue(mTelephonyManager.getSimState() >= TelephonyManager.SIM_STATE_UNKNOWN); |
| 136 | assertTrue(mTelephonyManager.getDataActivity() >= TelephonyManager.DATA_ACTIVITY_NONE); |
| 137 | assertTrue(mTelephonyManager.getDataState() >= TelephonyManager.DATA_DISCONNECTED); |
| 138 | assertTrue(mTelephonyManager.getCallState() >= TelephonyManager.CALL_STATE_IDLE); |
| 139 | |
Yuhao Zheng | b71fbd4 | 2013-10-04 10:22:43 -0700 | [diff] [blame] | 140 | // Make sure devices without MMS service won't fail on this |
| 141 | if (mTelephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE) { |
| 142 | assertFalse(mTelephonyManager.getMmsUserAgent().isEmpty()); |
| 143 | assertFalse(mTelephonyManager.getMmsUAProfUrl().isEmpty()); |
| 144 | } |
| 145 | |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 146 | // The following methods may return null. Simply call them to make sure they do not |
| 147 | // throw any exceptions. |
| 148 | mTelephonyManager.getVoiceMailNumber(); |
| 149 | mTelephonyManager.getSimOperatorName(); |
| 150 | mTelephonyManager.getNetworkCountryIso(); |
| 151 | mTelephonyManager.getCellLocation(); |
| 152 | mTelephonyManager.getSimSerialNumber(); |
| 153 | mTelephonyManager.getSimOperator(); |
| 154 | mTelephonyManager.getNetworkOperatorName(); |
| 155 | mTelephonyManager.getSubscriberId(); |
| 156 | mTelephonyManager.getLine1Number(); |
| 157 | mTelephonyManager.getNetworkOperator(); |
| 158 | mTelephonyManager.getSimCountryIso(); |
| 159 | mTelephonyManager.getVoiceMailAlphaTag(); |
| 160 | mTelephonyManager.getNeighboringCellInfo(); |
| 161 | mTelephonyManager.isNetworkRoaming(); |
| 162 | mTelephonyManager.getDeviceId(); |
Amit Mahajan | 82267c4 | 2015-06-15 12:13:25 -0700 | [diff] [blame] | 163 | mTelephonyManager.getDeviceId(mTelephonyManager.getDefaultSim()); |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 164 | mTelephonyManager.getDeviceSoftwareVersion(); |
Amit Mahajan | 82267c4 | 2015-06-15 12:13:25 -0700 | [diff] [blame] | 165 | mTelephonyManager.getPhoneCount(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Tests that the phone count returned is valid. |
| 170 | */ |
| 171 | public void testGetPhoneCount() { |
| 172 | int phoneCount = mTelephonyManager.getPhoneCount(); |
| 173 | int phoneType = mTelephonyManager.getPhoneType(); |
| 174 | switch (phoneType) { |
| 175 | case TelephonyManager.PHONE_TYPE_GSM: |
| 176 | case TelephonyManager.PHONE_TYPE_CDMA: |
| 177 | assertTrue("Phone count should be > 0", phoneCount > 0); |
| 178 | break; |
| 179 | case TelephonyManager.PHONE_TYPE_NONE: |
Amit Mahajan | 4dad595 | 2015-08-06 13:47:28 -0700 | [diff] [blame] | 180 | assertTrue("Phone count should be 0", phoneCount == 0 || phoneCount == 1); |
Amit Mahajan | 82267c4 | 2015-06-15 12:13:25 -0700 | [diff] [blame] | 181 | break; |
| 182 | default: |
| 183 | throw new IllegalArgumentException("Did you add a new phone type? " + phoneType); |
| 184 | } |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 185 | } |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
Doug Zongker | feb81e9 | 2010-08-24 13:17:13 -0700 | [diff] [blame] | 188 | * Tests that the device properly reports either a valid IMEI if |
| 189 | * GSM, a valid MEID or ESN if CDMA, or a valid MAC address if |
| 190 | * only a WiFi device. |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 191 | */ |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 192 | public void testGetDeviceId() { |
| 193 | String deviceId = mTelephonyManager.getDeviceId(); |
Amit Mahajan | 82267c4 | 2015-06-15 12:13:25 -0700 | [diff] [blame] | 194 | verifyDeviceId(deviceId); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Tests that the device properly reports either a valid IMEI if |
| 199 | * GSM, a valid MEID or ESN if CDMA, or a valid MAC address if |
| 200 | * only a WiFi device. |
| 201 | */ |
| 202 | public void testGetDeviceIdForSlotId() { |
| 203 | String deviceId = mTelephonyManager.getDeviceId(mTelephonyManager.getDefaultSim()); |
| 204 | verifyDeviceId(deviceId); |
| 205 | // Also verify that no exception is thrown for any slot id (including invalid ones) |
| 206 | for (int i = -1; i <= mTelephonyManager.getPhoneCount(); i++) { |
| 207 | mTelephonyManager.getDeviceId(i); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private void verifyDeviceId(String deviceId) { |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 212 | int phoneType = mTelephonyManager.getPhoneType(); |
| 213 | switch (phoneType) { |
| 214 | case TelephonyManager.PHONE_TYPE_GSM: |
Doug Zongker | d966f9e | 2011-03-03 16:59:02 -0800 | [diff] [blame] | 215 | assertGsmDeviceId(deviceId); |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 216 | break; |
| 217 | |
| 218 | case TelephonyManager.PHONE_TYPE_CDMA: |
Catherine Liu | 9b742b1 | 2012-08-23 16:41:56 -0500 | [diff] [blame] | 219 | // LTE device is using IMEI as device id |
| 220 | if (mTelephonyManager.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE) { |
| 221 | assertGsmDeviceId(deviceId); |
| 222 | } else { |
| 223 | assertCdmaDeviceId(deviceId); |
| 224 | } |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 225 | break; |
| 226 | |
| 227 | case TelephonyManager.PHONE_TYPE_NONE: |
bs | c9ba1c7 | 2012-06-22 20:59:53 +0900 | [diff] [blame] | 228 | boolean nwSupported = mCm.isNetworkSupported(mCm.TYPE_WIFI); |
Hoang Van Tham | 2b599f0 | 2015-06-04 11:44:01 +0700 | [diff] [blame] | 229 | PackageManager packageManager = getContext().getPackageManager(); |
Unsuk Jung | 7a31d61 | 2015-09-09 14:38:22 -0700 | [diff] [blame] | 230 | // only check serial number & MAC address if device report wifi feature |
Hoang Van Tham | 2b599f0 | 2015-06-04 11:44:01 +0700 | [diff] [blame] | 231 | if (packageManager.hasSystemFeature(PackageManager.FEATURE_WIFI)) { |
bs | c9ba1c7 | 2012-06-22 20:59:53 +0900 | [diff] [blame] | 232 | assertSerialNumber(); |
Adam Lesinski | 82ac6a2 | 2014-06-06 14:09:56 -0700 | [diff] [blame] | 233 | assertMacAddress(getWifiMacAddress()); |
xinhe | a5c70e1 | 2014-10-02 11:07:49 -0700 | [diff] [blame] | 234 | } else if (mCm.getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH) != null) { |
Adam Lesinski | 82ac6a2 | 2014-06-06 14:09:56 -0700 | [diff] [blame] | 235 | assertSerialNumber(); |
| 236 | assertMacAddress(getBluetoothMacAddress()); |
bs | c9ba1c7 | 2012-06-22 20:59:53 +0900 | [diff] [blame] | 237 | } else { |
xinhe | a5c70e1 | 2014-10-02 11:07:49 -0700 | [diff] [blame] | 238 | assertTrue(mCm.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET) != null); |
bs | c9ba1c7 | 2012-06-22 20:59:53 +0900 | [diff] [blame] | 239 | } |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 240 | break; |
| 241 | |
| 242 | default: |
| 243 | throw new IllegalArgumentException("Did you add a new phone type? " + phoneType); |
| 244 | } |
| 245 | } |
| 246 | |
Doug Zongker | d966f9e | 2011-03-03 16:59:02 -0800 | [diff] [blame] | 247 | private static void assertGsmDeviceId(String deviceId) { |
| 248 | // IMEI may include the check digit |
| 249 | String imeiPattern = "[0-9]{14,15}"; |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 250 | assertTrue("IMEI device id " + deviceId + " does not match pattern " + imeiPattern, |
| 251 | Pattern.matches(imeiPattern, deviceId)); |
Doug Zongker | d966f9e | 2011-03-03 16:59:02 -0800 | [diff] [blame] | 252 | if (deviceId.length() == 15) { |
| 253 | // if the ID is 15 digits, the 15th must be a check digit. |
| 254 | assertImeiCheckDigit(deviceId); |
| 255 | } |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | private static void assertImeiCheckDigit(String deviceId) { |
| 259 | int expectedCheckDigit = getLuhnCheckDigit(deviceId.substring(0, 14)); |
| 260 | int actualCheckDigit = Character.digit(deviceId.charAt(14), 10); |
| 261 | assertEquals("Incorrect check digit for " + deviceId, expectedCheckDigit, actualCheckDigit); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Use decimal value (0-9) to index into array to get sum of its digits |
| 266 | * needed by Lunh check. |
| 267 | * |
| 268 | * Example: DOUBLE_DIGIT_SUM[6] = 3 because 6 * 2 = 12 => 1 + 2 = 3 |
| 269 | */ |
| 270 | private static final int[] DOUBLE_DIGIT_SUM = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9}; |
| 271 | |
| 272 | /** |
| 273 | * Calculate the check digit by starting from the right, doubling every |
| 274 | * each digit, summing all the digits including the doubled ones, and |
| 275 | * finding a number to make the sum divisible by 10. |
| 276 | * |
| 277 | * @param deviceId not including the check digit |
| 278 | * @return the check digit |
| 279 | */ |
| 280 | private static int getLuhnCheckDigit(String deviceId) { |
| 281 | int sum = 0; |
| 282 | int dontDoubleModulus = deviceId.length() % 2; |
| 283 | for (int i = deviceId.length() - 1; i >= 0; --i) { |
| 284 | int digit = Character.digit(deviceId.charAt(i), 10); |
| 285 | if (i % 2 == dontDoubleModulus) { |
| 286 | sum += digit; |
| 287 | } else { |
| 288 | sum += DOUBLE_DIGIT_SUM[digit]; |
| 289 | } |
| 290 | } |
| 291 | sum %= 10; |
| 292 | return sum == 0 ? 0 : 10 - sum; |
| 293 | } |
| 294 | |
Doug Zongker | feb81e9 | 2010-08-24 13:17:13 -0700 | [diff] [blame] | 295 | private static void assertCdmaDeviceId(String deviceId) { |
Doug Zongker | d966f9e | 2011-03-03 16:59:02 -0800 | [diff] [blame] | 296 | // CDMA device IDs may either be a 14-hex-digit MEID or an |
| 297 | // 8-hex-digit ESN. If it's an ESN, it may not be a |
| 298 | // pseudo-ESN. |
Doug Zongker | feb81e9 | 2010-08-24 13:17:13 -0700 | [diff] [blame] | 299 | if (deviceId.length() == 14) { |
Brian Muramatsu | de5f210 | 2010-09-28 11:02:01 -0700 | [diff] [blame] | 300 | assertMeidFormat(deviceId); |
Doug Zongker | feb81e9 | 2010-08-24 13:17:13 -0700 | [diff] [blame] | 301 | } else if (deviceId.length() == 8) { |
| 302 | assertHexadecimalEsnFormat(deviceId); |
| 303 | } else { |
| 304 | fail("device id on CDMA must be 14-digit hex MEID or 8-digit hex ESN."); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | private static void assertHexadecimalEsnFormat(String deviceId) { |
| 309 | String esnPattern = "[0-9a-fA-F]{8}"; |
| 310 | assertTrue("ESN hex device id " + deviceId + " does not match pattern " + esnPattern, |
| 311 | Pattern.matches(esnPattern, deviceId)); |
| 312 | assertFalse("ESN hex device id " + deviceId + " must not be a pseudo-ESN", |
| 313 | "80".equals(deviceId.substring(0, 2))); |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Brian Muramatsu | de5f210 | 2010-09-28 11:02:01 -0700 | [diff] [blame] | 316 | private static void assertMeidFormat(String deviceId) { |
Doug Zongker | d966f9e | 2011-03-03 16:59:02 -0800 | [diff] [blame] | 317 | // MEID must NOT include the check digit. |
| 318 | String meidPattern = "[0-9a-fA-F]{14}"; |
| 319 | assertTrue("MEID device id " + deviceId + " does not match pattern " + meidPattern, |
| 320 | Pattern.matches(meidPattern, deviceId)); |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Brian Muramatsu | afcbb78 | 2010-08-17 13:53:59 -0700 | [diff] [blame] | 323 | private void assertSerialNumber() { |
| 324 | assertNotNull("Non-telephony devices must have a Build.SERIAL number.", |
| 325 | Build.SERIAL); |
| 326 | assertTrue("Hardware id must be no longer than 20 characters.", |
| 327 | Build.SERIAL.length() <= 20); |
| 328 | assertTrue("Hardware id must be alphanumeric.", |
| 329 | Pattern.matches("[0-9A-Za-z]+", Build.SERIAL)); |
| 330 | } |
| 331 | |
Adam Lesinski | 82ac6a2 | 2014-06-06 14:09:56 -0700 | [diff] [blame] | 332 | private void assertMacAddress(String macAddress) { |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 333 | String macPattern = "([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}"; |
| 334 | assertTrue("MAC Address " + macAddress + " does not match pattern " + macPattern, |
| 335 | Pattern.matches(macPattern, macAddress)); |
| 336 | } |
| 337 | |
| 338 | /** @return mac address which requires the WiFi system to be enabled */ |
Adam Lesinski | 82ac6a2 | 2014-06-06 14:09:56 -0700 | [diff] [blame] | 339 | private String getWifiMacAddress() { |
Brian Muramatsu | 5eed71e | 2010-05-19 15:52:03 -0700 | [diff] [blame] | 340 | WifiManager wifiManager = (WifiManager) getContext() |
| 341 | .getSystemService(Context.WIFI_SERVICE); |
| 342 | |
| 343 | boolean enabled = wifiManager.isWifiEnabled(); |
| 344 | |
| 345 | try { |
| 346 | if (!enabled) { |
| 347 | wifiManager.setWifiEnabled(true); |
| 348 | } |
| 349 | |
| 350 | WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
| 351 | return wifiInfo.getMacAddress(); |
| 352 | |
| 353 | } finally { |
| 354 | if (!enabled) { |
| 355 | wifiManager.setWifiEnabled(false); |
| 356 | } |
| 357 | } |
| 358 | } |
Brian Muramatsu | ef3a2ec | 2010-10-22 16:58:40 -0700 | [diff] [blame] | 359 | |
Adam Lesinski | 82ac6a2 | 2014-06-06 14:09:56 -0700 | [diff] [blame] | 360 | private String getBluetoothMacAddress() { |
| 361 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 362 | if (adapter == null) { |
| 363 | return ""; |
| 364 | } |
| 365 | |
| 366 | return adapter.getAddress(); |
| 367 | } |
| 368 | |
Brian Muramatsu | ef3a2ec | 2010-10-22 16:58:40 -0700 | [diff] [blame] | 369 | private static final String ISO_COUNTRY_CODE_PATTERN = "[a-z]{2}"; |
| 370 | |
| 371 | public void testGetNetworkCountryIso() { |
Madan Ankapura | 138292c | 2010-12-09 18:25:29 -0800 | [diff] [blame] | 372 | PackageManager packageManager = getContext().getPackageManager(); |
Brian Muramatsu | ef3a2ec | 2010-10-22 16:58:40 -0700 | [diff] [blame] | 373 | String countryCode = mTelephonyManager.getNetworkCountryIso(); |
Madan Ankapura | 138292c | 2010-12-09 18:25:29 -0800 | [diff] [blame] | 374 | if (packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) { |
| 375 | assertTrue("Country code '" + countryCode + "' did not match " |
| 376 | + ISO_COUNTRY_CODE_PATTERN, |
| 377 | Pattern.matches(ISO_COUNTRY_CODE_PATTERN, countryCode)); |
| 378 | } else { |
Brian Muramatsu | bb7617c | 2010-12-14 15:27:22 -0800 | [diff] [blame] | 379 | // Non-telephony may still have the property defined if it has a SIM. |
Madan Ankapura | 138292c | 2010-12-09 18:25:29 -0800 | [diff] [blame] | 380 | } |
Brian Muramatsu | ef3a2ec | 2010-10-22 16:58:40 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | public void testGetSimCountryIso() { |
Madan Ankapura | 138292c | 2010-12-09 18:25:29 -0800 | [diff] [blame] | 384 | PackageManager packageManager = getContext().getPackageManager(); |
Brian Muramatsu | ef3a2ec | 2010-10-22 16:58:40 -0700 | [diff] [blame] | 385 | String countryCode = mTelephonyManager.getSimCountryIso(); |
Madan Ankapura | 138292c | 2010-12-09 18:25:29 -0800 | [diff] [blame] | 386 | if (packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) { |
| 387 | assertTrue("Country code '" + countryCode + "' did not match " |
| 388 | + ISO_COUNTRY_CODE_PATTERN, |
| 389 | Pattern.matches(ISO_COUNTRY_CODE_PATTERN, countryCode)); |
| 390 | } else { |
Brian Muramatsu | bb7617c | 2010-12-14 15:27:22 -0800 | [diff] [blame] | 391 | // Non-telephony may still have the property defined if it has a SIM. |
Madan Ankapura | 138292c | 2010-12-09 18:25:29 -0800 | [diff] [blame] | 392 | } |
Brian Muramatsu | ef3a2ec | 2010-10-22 16:58:40 -0700 | [diff] [blame] | 393 | } |
Phil Dubach | 781d2da | 2009-07-15 14:45:26 -0700 | [diff] [blame] | 394 | } |