Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (C) 2013 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.cts.verifier.security;
     18 
     19 import android.app.admin.DevicePolicyManager;
     20 import android.content.ActivityNotFoundException;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.security.KeyChain;
     24 import android.util.Log;
     25 import android.view.View;
     26 import android.widget.Button;
     27 
     28 import com.android.cts.verifier.R;
     29 
     30 import java.io.File;
     31 import java.io.FileOutputStream;
     32 import java.io.IOException;
     33 import java.io.InputStream;
     34 
     35 import com.android.cts.verifier.ArrayTestListAdapter;
     36 import com.android.cts.verifier.DialogTestListActivity;
     37 import com.android.cts.verifier.TestListActivity;
     38 import com.android.cts.verifier.TestListAdapter.TestListItem;
     39 import com.android.cts.verifier.TestResult;
     40 
     41 public class CAInstallNotificationVerifierActivity extends DialogTestListActivity {
     42     static final String TAG = CAInstallNotificationVerifierActivity.class.getSimpleName();
     43 
     44     private static final String CERT_ASSET_NAME = "myCA.cer";
     45 
     46     // From @hidden field in android.provider.Settings
     47     private static final String ACTION_TRUSTED_CREDENTIALS_USER
     48             = "com.android.settings.TRUSTED_CREDENTIALS_USER";
     49 
     50     public CAInstallNotificationVerifierActivity() {
     51         super(R.layout.cainstallnotify_main, R.string.cacert_test, R.string.cacert_info,
     52                 R.string.cacert_info);
     53     }
     54 
     55     @Override
     56     protected void setupTests(final ArrayTestListAdapter testAdapter) {
     57         testAdapter.add(new InstallCertItem(this,
     58                 R.string.cacert_install_cert,
     59                 "install_cert",
     60                 KeyChain.createInstallIntent()));
     61         testAdapter.add(new DialogTestListItem(this,
     62                 R.string.cacert_check_cert_in_settings,
     63                 "check_cert",
     64                 R.string.cacert_check_cert_in_settings,
     65                 new Intent(ACTION_TRUSTED_CREDENTIALS_USER)));
     66         testAdapter.add(new DialogTestListItem(this,
     67                 R.string.cacert_remove_screen_lock,
     68                 "remove_screen_lock",
     69                 R.string.cacert_remove_screen_lock,
     70                 new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD)));
     71         testAdapter.add(new DialogTestListItem(this,
     72                 R.string.cacert_check_notification,
     73                 "check_notification") {
     74                     @Override
     75                     public void performTest(DialogTestListActivity activity) {
     76                         setTestResult(this, TestResult.TEST_RESULT_PASSED);
     77                     }
     78                 });
     79         testAdapter.add(new DialogTestListItem(this,
     80                 R.string.cacert_dismiss_notification,
     81                 "dismiss_notification") {
     82                     @Override
     83                     public void performTest(DialogTestListActivity activity) {
     84                         setTestResult(this, TestResult.TEST_RESULT_PASSED);
     85                     }
     86                 });
     87     }
     88 
     89     private class InstallCertItem extends DialogTestListItem {
     90         public InstallCertItem(Context context, int nameResId, String testId, Intent intent) {
     91             super(context, nameResId, testId, nameResId, intent);
     92         }
     93 
     94         @Override
     95         public void performTest(DialogTestListActivity activity) {
     96             final File certStagingFile = new File("/sdcard/", CERT_ASSET_NAME);
     97             InputStream is = null;
     98             FileOutputStream os = null;
     99             try {
    100                 try {
    101                     is = getAssets().open(CERT_ASSET_NAME);
    102                     os = new FileOutputStream(certStagingFile);
    103                     byte[] buffer = new byte[1024];
    104                     int length;
    105                     while ((length = is.read(buffer)) > 0) {
    106                         os.write(buffer, 0, length);
    107                     }
    108                 } finally {
    109                     if (is != null) is.close();
    110                     if (os != null) os.close();
    111                     certStagingFile.setReadable(true, false);
    112                 }
    113             } catch (IOException ioe) {
    114                 Log.w(TAG, "Problem moving cert file to /sdcard/", ioe);
    115                 return;
    116             }
    117             super.performTest(activity);
    118         }
    119     }
    120 }
    121