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 com.android.providers.telephony; 18 19 import android.content.ContentValues; 20 import android.content.pm.PackageManager; 21 import android.content.pm.ProviderInfo; 22 import android.content.res.Resources; 23 import android.database.Cursor; 24 import android.database.SQLException; 25 import android.test.mock.MockContentResolver; 26 import android.test.mock.MockContext; 27 import android.test.suitebuilder.annotation.SmallTest; 28 import android.text.TextUtils; 29 import android.util.Log; 30 31 import com.android.providers.telephony.CarrierProvider; 32 33 import junit.framework.TestCase; 34 35 import org.junit.Test; 36 37 38 /** 39 * Tests for testing CRUD operations of CarrierProvider. 40 * Uses TelephonyProviderTestable to set up in-memory database 41 * 42 * Build, install and run the tests by running the commands below: 43 * runtest --path <dir or file> 44 * runtest --path <dir or file> --test-method <testMethodName> 45 * e.g.) 46 * runtest --path tests/src/com/android/providers/telephony/CarrierProviderTest.java \ 47 * --test-method testInsertCarriers 48 */ 49 public class CarrierProviderTest extends TestCase { 50 51 private static final String TAG = "CarrierProviderTest"; 52 53 private MockContextWithProvider mContext; 54 private MockContentResolver mContentResolver; 55 private CarrierProviderTestable mCarrierProviderTestable; 56 57 public static final int dummy_type = 1; 58 public static final String dummy_mnc = "MNC001"; 59 public static final String dummy_mnc2 = "MNC002"; 60 public static final String dummy_mcc = "MCC005"; 61 public static final String dummy_key1 = "PUBKEY1"; 62 public static final String dummy_key2 = "PUBKEY2"; 63 public static final String dummy_mvno_type = "100"; 64 public static final String dummy_mvno_match_data = "101"; 65 public static final String dummy_key_identifier_data = "key_identifier1"; 66 public static final long dummy_key_expiration = 1496795015L; 67 68 69 /** 70 * This is used to give the CarrierProviderTest a mocked context which takes a 71 * CarrierProvider and attaches it to the ContentResolver. 72 */ 73 private class MockContextWithProvider extends MockContext { 74 private final MockContentResolver mResolver; 75 MockContextWithProvider(CarrierProvider carrierProvider)76 public MockContextWithProvider(CarrierProvider carrierProvider) { 77 mResolver = new MockContentResolver(); 78 79 ProviderInfo providerInfo = new ProviderInfo(); 80 providerInfo.authority = CarrierProvider.PROVIDER_NAME; 81 82 // Add context to given telephonyProvider 83 carrierProvider.attachInfoForTesting(this, providerInfo); 84 Log.d(TAG, "MockContextWithProvider: carrierProvider.getContext(): " 85 + carrierProvider.getContext()); 86 87 // Add given telephonyProvider to mResolver, so that mResolver can send queries 88 // to the provider. 89 mResolver.addProvider(CarrierProvider.PROVIDER_NAME, carrierProvider); 90 Log.d(TAG, "MockContextWithProvider: Add carrierProvider to mResolver"); 91 } 92 93 @Override getSystemService(String name)94 public Object getSystemService(String name) { 95 Log.d(TAG, "getSystemService: returning null"); 96 return null; 97 } 98 99 @Override getResources()100 public Resources getResources() { 101 Log.d(TAG, "getResources: returning null"); 102 return null; 103 } 104 105 @Override getContentResolver()106 public MockContentResolver getContentResolver() { 107 return mResolver; 108 } 109 110 // Gives permission to write to the APN table within the MockContext 111 @Override checkCallingOrSelfPermission(String permission)112 public int checkCallingOrSelfPermission(String permission) { 113 if (TextUtils.equals(permission, "android.permission.WRITE_APN_SETTINGS")) { 114 Log.d(TAG, "checkCallingOrSelfPermission: permission=" + permission 115 + ", returning PackageManager.PERMISSION_GRANTED"); 116 return PackageManager.PERMISSION_GRANTED; 117 } else { 118 Log.d(TAG, "checkCallingOrSelfPermission: permission=" + permission 119 + ", returning PackageManager.PERMISSION_DENIED"); 120 return PackageManager.PERMISSION_DENIED; 121 } 122 } 123 } 124 125 @Override setUp()126 protected void setUp() throws Exception { 127 super.setUp(); 128 mCarrierProviderTestable = new CarrierProviderTestable(); 129 mContext = new MockContextWithProvider(mCarrierProviderTestable); 130 mContentResolver = (MockContentResolver) mContext.getContentResolver(); 131 } 132 133 @Override tearDown()134 protected void tearDown() throws Exception { 135 super.tearDown(); 136 mCarrierProviderTestable.closeDatabase(); 137 } 138 139 /** 140 * Test inserting values in carrier key table. 141 */ 142 @Test 143 @SmallTest testInsertCertificates()144 public void testInsertCertificates() { 145 int count = -1; 146 ContentValues contentValues = new ContentValues(); 147 contentValues.put(CarrierDatabaseHelper.KEY_TYPE, dummy_type); 148 contentValues.put(CarrierDatabaseHelper.MCC, dummy_mcc); 149 contentValues.put(CarrierDatabaseHelper.MNC, dummy_mnc); 150 contentValues.put(CarrierDatabaseHelper.MVNO_TYPE, dummy_mvno_type); 151 contentValues.put(CarrierDatabaseHelper.MVNO_MATCH_DATA, dummy_mvno_match_data); 152 contentValues.put(CarrierDatabaseHelper.KEY_IDENTIFIER, dummy_key_identifier_data); 153 contentValues.put(CarrierDatabaseHelper.PUBLIC_KEY, dummy_key1.getBytes()); 154 contentValues.put(CarrierDatabaseHelper.EXPIRATION_TIME, dummy_key_expiration); 155 156 try { 157 mContentResolver.insert(CarrierProvider.CONTENT_URI, contentValues); 158 } catch (Exception e) { 159 Log.d(TAG, "Error inserting certificates:" + e); 160 } 161 try { 162 Cursor countCursor = mContentResolver.query(CarrierProvider.CONTENT_URI, 163 new String[]{"count(*) AS count"}, 164 null, 165 null, 166 null); 167 countCursor.moveToFirst(); 168 count = countCursor.getInt(0); 169 } catch (Exception e) { 170 Log.d(TAG, "Exception in getting count:" + e); 171 } 172 assertEquals(1, count); 173 } 174 175 /** 176 * Test update & query. 177 */ 178 @Test 179 @SmallTest testUpdateCertificates()180 public void testUpdateCertificates() { 181 String key = null; 182 ContentValues contentValues = new ContentValues(); 183 contentValues.put(CarrierDatabaseHelper.KEY_TYPE, dummy_type); 184 contentValues.put(CarrierDatabaseHelper.MCC, dummy_mcc); 185 contentValues.put(CarrierDatabaseHelper.MNC, dummy_mnc); 186 contentValues.put(CarrierDatabaseHelper.MVNO_TYPE, dummy_mvno_type); 187 contentValues.put(CarrierDatabaseHelper.MVNO_MATCH_DATA, dummy_mvno_match_data); 188 contentValues.put(CarrierDatabaseHelper.KEY_IDENTIFIER, dummy_key_identifier_data); 189 contentValues.put(CarrierDatabaseHelper.PUBLIC_KEY, dummy_key1.getBytes()); 190 contentValues.put(CarrierDatabaseHelper.EXPIRATION_TIME, dummy_key_expiration); 191 192 try { 193 mContentResolver.insert(CarrierProvider.CONTENT_URI, contentValues); 194 } catch (Exception e) { 195 Log.d(TAG, "Error inserting certificates:" + e); 196 } 197 198 try { 199 ContentValues updatedValues = new ContentValues(); 200 updatedValues.put(CarrierDatabaseHelper.PUBLIC_KEY, dummy_key2); 201 mContentResolver.update(CarrierProvider.CONTENT_URI, updatedValues, 202 "mcc=? and mnc=? and key_type=?", new String[] { dummy_mcc, dummy_mnc, 203 String.valueOf(dummy_type) }); 204 } catch (Exception e) { 205 Log.d(TAG, "Error updating values:" + e); 206 } 207 208 try { 209 String[] columns ={CarrierDatabaseHelper.PUBLIC_KEY}; 210 Cursor findEntry = mContentResolver.query(CarrierProvider.CONTENT_URI, columns, 211 "mcc=? and mnc=? and key_type=?", 212 new String[] { dummy_mcc, dummy_mnc, String.valueOf(dummy_type) }, null); 213 findEntry.moveToFirst(); 214 key = findEntry.getString(0); 215 } catch (Exception e) { 216 Log.d(TAG, "Query failed:" + e); 217 } 218 assertEquals(key, dummy_key2); 219 } 220 221 /** 222 * Test inserting multiple certs 223 */ 224 @Test 225 @SmallTest testMultipleCertificates()226 public void testMultipleCertificates() { 227 int count = -1; 228 ContentValues contentValues = new ContentValues(); 229 contentValues.put(CarrierDatabaseHelper.KEY_TYPE, dummy_type); 230 contentValues.put(CarrierDatabaseHelper.MCC, dummy_mcc); 231 contentValues.put(CarrierDatabaseHelper.MNC, dummy_mnc); 232 contentValues.put(CarrierDatabaseHelper.MVNO_TYPE, dummy_mvno_type); 233 contentValues.put(CarrierDatabaseHelper.MVNO_MATCH_DATA, dummy_mvno_match_data); 234 contentValues.put(CarrierDatabaseHelper.KEY_IDENTIFIER, dummy_key_identifier_data); 235 contentValues.put(CarrierDatabaseHelper.PUBLIC_KEY, dummy_key1.getBytes()); 236 237 ContentValues contentValuesNew = new ContentValues(); 238 contentValuesNew.put(CarrierDatabaseHelper.KEY_TYPE, dummy_type); 239 contentValuesNew.put(CarrierDatabaseHelper.MCC, dummy_mcc); 240 contentValuesNew.put(CarrierDatabaseHelper.MNC, dummy_mnc2); 241 contentValuesNew.put(CarrierDatabaseHelper.MVNO_TYPE, dummy_mvno_type); 242 contentValuesNew.put(CarrierDatabaseHelper.MVNO_MATCH_DATA, dummy_mvno_match_data); 243 contentValues.put(CarrierDatabaseHelper.KEY_IDENTIFIER, dummy_key_identifier_data); 244 contentValuesNew.put(CarrierDatabaseHelper.PUBLIC_KEY, dummy_key2.getBytes()); 245 246 try { 247 mContentResolver.insert(CarrierProvider.CONTENT_URI, contentValues); 248 mContentResolver.insert(CarrierProvider.CONTENT_URI, contentValuesNew); 249 } catch (Exception e) { 250 System.out.println("Error inserting certificates:: " + e); 251 } 252 253 try { 254 Cursor countCursor = mContentResolver.query(CarrierProvider.CONTENT_URI, 255 new String[]{"count(*) AS count"}, 256 null, 257 null, 258 null); 259 countCursor.moveToFirst(); 260 count = countCursor.getInt(0); 261 } catch (Exception e) { 262 Log.d(TAG, "Exception in getting count:" + e); 263 } 264 assertEquals(2, count); 265 } 266 267 /** 268 * Test inserting duplicate values in carrier key table. Ensure that a SQLException is thrown. 269 */ 270 @Test(expected = SQLException.class) testDuplicateFailure()271 public void testDuplicateFailure() { 272 ContentValues contentValues = new ContentValues(); 273 contentValues.put(CarrierDatabaseHelper.KEY_TYPE, dummy_type); 274 contentValues.put(CarrierDatabaseHelper.MCC, dummy_mcc); 275 contentValues.put(CarrierDatabaseHelper.MNC, dummy_mnc); 276 contentValues.put(CarrierDatabaseHelper.MVNO_TYPE, dummy_mvno_type); 277 contentValues.put(CarrierDatabaseHelper.MVNO_MATCH_DATA, dummy_mvno_match_data); 278 contentValues.put(CarrierDatabaseHelper.PUBLIC_KEY, dummy_key1.getBytes()); 279 280 try { 281 mContentResolver.insert(CarrierProvider.CONTENT_URI, contentValues); 282 } catch (Exception e) { 283 Log.d(TAG, "Error inserting certificates:: " + e); 284 } 285 try { 286 mContentResolver.insert(CarrierProvider.CONTENT_URI, contentValues); 287 } catch (Exception e) { 288 Log.d(TAG, "Error inserting certificates:: " + e); 289 } 290 } 291 292 /** 293 * Test delete. 294 */ 295 @Test 296 @SmallTest testDelete()297 public void testDelete() { 298 int numRowsDeleted = -1; 299 ContentValues contentValues = new ContentValues(); 300 contentValues.put(CarrierDatabaseHelper.KEY_TYPE, dummy_type); 301 contentValues.put(CarrierDatabaseHelper.MCC, dummy_mcc); 302 contentValues.put(CarrierDatabaseHelper.MNC, dummy_mnc); 303 contentValues.put(CarrierDatabaseHelper.MVNO_TYPE, dummy_mvno_type); 304 contentValues.put(CarrierDatabaseHelper.MVNO_MATCH_DATA, dummy_mvno_match_data); 305 contentValues.put(CarrierDatabaseHelper.KEY_IDENTIFIER, dummy_key_identifier_data); 306 contentValues.put(CarrierDatabaseHelper.PUBLIC_KEY, dummy_key1.getBytes()); 307 contentValues.put(CarrierDatabaseHelper.EXPIRATION_TIME, dummy_key_expiration); 308 309 try { 310 mContentResolver.insert(CarrierProvider.CONTENT_URI, contentValues); 311 } catch (Exception e) { 312 Log.d(TAG, "Error inserting certificates:" + e); 313 } 314 315 try { 316 String whereClause = "mcc=? and mnc=?"; 317 String[] whereArgs = new String[] { dummy_mcc, dummy_mnc }; 318 numRowsDeleted = mContentResolver.delete(CarrierProvider.CONTENT_URI, whereClause, whereArgs); 319 } catch (Exception e) { 320 Log.d(TAG, "Error updating values:" + e); 321 } 322 assertEquals(numRowsDeleted, 1); 323 } 324 325 } 326