1/* 2 * Copyright (C) 2012 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 17package com.android.server; 18 19import android.content.Context; 20import android.content.Intent; 21import android.test.AndroidTestCase; 22import android.provider.Settings; 23import android.util.Log; 24 25import java.io.File; 26import java.io.FileInputStream; 27import java.io.IOException; 28import java.util.HashSet; 29 30import libcore.io.IoUtils; 31 32/** 33 * Tests for {@link com.android.server.CertBlacklister} 34 */ 35public class CertBlacklisterTest extends AndroidTestCase { 36 37 private static final String BLACKLIST_ROOT = System.getenv("ANDROID_DATA") + "/misc/keychain/"; 38 39 public static final String PUBKEY_PATH = BLACKLIST_ROOT + "pubkey_blacklist.txt"; 40 public static final String SERIAL_PATH = BLACKLIST_ROOT + "serial_blacklist.txt"; 41 42 public static final String PUBKEY_KEY = "pubkey_blacklist"; 43 public static final String SERIAL_KEY = "serial_blacklist"; 44 45 private void overrideSettings(String key, String value) throws Exception { 46 Settings.Secure.putString(mContext.getContentResolver(), key, value); 47 Thread.sleep(1000); 48 } 49 50 public void testClearBlacklistPubkey() throws Exception { 51 // clear the gservices setting for a clean slate 52 overrideSettings(PUBKEY_KEY, ""); 53 // read the contents of the pubkey blacklist 54 String blacklist = IoUtils.readFileAsString(PUBKEY_PATH); 55 // Verify that it's empty 56 assertEquals("", blacklist); 57 } 58 59 public void testSetBlacklistPubkey() throws Exception { 60 // build a new thing to blacklist 61 String badPubkey = "7ccabd7db47e94a5759901b6a7dfd45d1c091ccc"; 62 // add the gservices override 63 overrideSettings(PUBKEY_KEY, badPubkey); 64 // check the contents again 65 String blacklist = IoUtils.readFileAsString(PUBKEY_PATH); 66 // make sure that we're equal to the string we sent out 67 assertEquals(badPubkey, blacklist); 68 } 69 70 public void testChangeBlacklistPubkey() throws Exception { 71 String badPubkey = "6ccabd7db47e94a5759901b6a7dfd45d1c091ccc"; 72 overrideSettings(PUBKEY_KEY, badPubkey); 73 badPubkey = "6ccabd7db47e94a5759901b6a7dfd45d1c091cce"; 74 overrideSettings(PUBKEY_KEY, badPubkey); 75 String blacklist = IoUtils.readFileAsString(PUBKEY_PATH); 76 assertEquals(badPubkey, blacklist); 77 } 78 79 public void testMultiBlacklistPubkey() throws Exception { 80 String badPubkey = "6ccabd7db47e94a5759901b6a7dfd45d1c091ccc,6ccabd7db47e94a5759901b6a7dfd45d1c091ccd"; 81 overrideSettings(PUBKEY_KEY, badPubkey); 82 String blacklist = IoUtils.readFileAsString(PUBKEY_PATH); 83 assertEquals(badPubkey, blacklist); 84 } 85 86 public void testInvalidMultiBlacklistPubkey() throws Exception { 87 String badPubkey = "6ccabd7db47e94a5759901b6a7dfd45d1c091ccc,ZZZZZ,6ccabd7db47e94a5759901b6a7dfd45d1c091ccd"; 88 overrideSettings(PUBKEY_KEY, badPubkey); 89 String blacklist = IoUtils.readFileAsString(PUBKEY_PATH); 90 assertEquals(badPubkey, blacklist); 91 } 92 93 public void testInvalidCharsBlacklistPubkey() throws Exception { 94 String badPubkey = "\n6ccabd7db47e94a5759901b6a7dfd45d1c091ccc,-ZZZZZ,+6ccabd7db47e94a5759901b6a7dfd45d1c091ccd"; 95 overrideSettings(PUBKEY_KEY, badPubkey); 96 String blacklist = IoUtils.readFileAsString(PUBKEY_PATH); 97 assertEquals(badPubkey, blacklist); 98 } 99 100 public void testLotsOfBlacklistedPubkeys() throws Exception { 101 StringBuilder bl = new StringBuilder(); 102 for (int i=0; i < 1000; i++) { 103 bl.append("6ccabd7db47e94a5759901b6a7dfd45d1c091ccc,"); 104 } 105 overrideSettings(PUBKEY_KEY, bl.toString()); 106 String blacklist = IoUtils.readFileAsString(PUBKEY_PATH); 107 assertEquals(bl.toString(), blacklist); 108 } 109 110 public void testClearBlacklistSerial() throws Exception { 111 // clear the gservices setting for a clean slate 112 overrideSettings(SERIAL_KEY, ""); 113 // read the contents of the pubkey blacklist 114 String blacklist = IoUtils.readFileAsString(SERIAL_PATH); 115 // Verify that it's empty 116 assertEquals("", blacklist); 117 } 118 119 public void testSetBlacklistSerial() throws Exception { 120 // build a new thing to blacklist 121 String badSerial = "22e514121e61c643b1e9b06bd4b9f7d0"; 122 // add the gservices override 123 overrideSettings(SERIAL_KEY, badSerial); 124 // check the contents again 125 String blacklist = IoUtils.readFileAsString(SERIAL_PATH); 126 // make sure that we're equal to the string we sent out 127 assertEquals(badSerial, blacklist); 128 } 129 130 public void testChangeBlacklistSerial() throws Exception { 131 String badSerial = "22e514121e61c643b1e9b06bd4b9f7d0"; 132 overrideSettings(SERIAL_KEY, badSerial); 133 badSerial = "22e514121e61c643b1e9b06bd4b9f7d1"; 134 overrideSettings(SERIAL_KEY, badSerial); 135 String blacklist = IoUtils.readFileAsString(SERIAL_PATH); 136 assertEquals(badSerial, blacklist); 137 } 138 139 public void testMultiBlacklistSerial() throws Exception { 140 String badSerial = "22e514121e61c643b1e9b06bd4b9f7d0,22e514121e61c643b1e9b06bd4b9f7d1"; 141 overrideSettings(SERIAL_KEY, badSerial); 142 String blacklist = IoUtils.readFileAsString(SERIAL_PATH); 143 assertEquals(badSerial, blacklist); 144 } 145 146 public void testInvalidMultiBlacklistSerial() throws Exception { 147 String badSerial = "22e514121e61c643b1e9b06bd4b9f7d0,ZZZZ,22e514121e61c643b1e9b06bd4b9f7d1"; 148 overrideSettings(SERIAL_KEY, badSerial); 149 String blacklist = IoUtils.readFileAsString(SERIAL_PATH); 150 assertEquals(badSerial, blacklist); 151 } 152 153 public void testInvalidCharsBlacklistSerial() throws Exception { 154 String badSerial = "\n22e514121e61c643b1e9b06bd4b9f7d0,-ZZZZ,+22e514121e61c643b1e9b06bd4b9f7d1"; 155 overrideSettings(SERIAL_KEY, badSerial); 156 String blacklist = IoUtils.readFileAsString(SERIAL_PATH); 157 assertEquals(badSerial, blacklist); 158 } 159 160 public void testLotsOfBlacklistedSerials() throws Exception { 161 StringBuilder bl = new StringBuilder(); 162 for (int i=0; i < 1000; i++) { 163 bl.append("22e514121e61c643b1e9b06bd4b9f7d0,"); 164 } 165 overrideSettings(SERIAL_KEY, bl.toString()); 166 String blacklist = IoUtils.readFileAsString(SERIAL_PATH); 167 assertEquals(bl.toString(), blacklist); 168 } 169} 170