Change LocalKeyStore error reporting

The error reporting assures an exception is thrown if
setKeyStoreFile(null) is called without a prior call to
setKeyStoreLocation(String directory).

Also, fix TrustManagerFactoryTest indentation.
This commit is contained in:
Joe Steele 2013-12-04 12:10:12 -05:00
parent 3fd7470d68
commit dab8d3807f
2 changed files with 21 additions and 9 deletions

View File

@ -40,11 +40,17 @@ public class LocalKeyStore {
private LocalKeyStore() {
if (sKeyStoreLocation == null) {
Log.e(K9.LOG_TAG, "Local key store location has not been initialized");
} else {
try {
upgradeKeyStoreFile();
setKeyStoreFile(null);
} catch (CertificateException e) {
/*
* Can happen if setKeyStoreLocation(String directory) has not been
* called before the first call to getInstance(). Not necessarily an
* error, presuming setKeyStoreFile(File) is called next with a
* non-null File.
*/
Log.w(K9.LOG_TAG, "Local key store has not been initialized");
}
}
@ -56,8 +62,11 @@ public class LocalKeyStore {
* {@link File} containing locally saved certificates. May be 0
* length, in which case it is deleted and recreated. May be
* {@code null}, in which case a default file location is used.
* @throws CertificateException
* Occurs if {@code file == null} and
* {@code setKeyStoreLocation(directory)} was not called previously.
*/
public synchronized void setKeyStoreFile(File file) {
public synchronized void setKeyStoreFile(File file) throws CertificateException {
if (file == null) {
file = new File(getKeyStoreFilePath(KEY_STORE_FILE_VERSION));
}
@ -164,14 +173,17 @@ public class LocalKeyStore {
}
}
private void upgradeKeyStoreFile() {
private void upgradeKeyStoreFile() throws CertificateException {
if (KEY_STORE_FILE_VERSION > 0) {
// Blow away version "0" because certificate aliases have changed.
new File(getKeyStoreFilePath(0)).delete();
}
}
private String getKeyStoreFilePath(int version) {
private String getKeyStoreFilePath(int version) throws CertificateException {
if (sKeyStoreLocation == null) {
throw new CertificateException("Local key store location has not been initialized");
}
if (version < 1) {
return sKeyStoreLocation + File.separator + "KeyStore.bks";
} else {

View File

@ -73,7 +73,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
+ "zOztD2HZfxhIz3uH2gXmqUo=\n"
+ "-----END CERTIFICATE-----\n";
private static final String CERT3 =
private static final String CERT3 =
"-----BEGIN CERTIFICATE-----\n"
+ "MIIDjDCCAnSgAwIBAgIBATANBgkqhkiG9w0BAQUFADBNMQswCQYDVQQGEwJDSDET\n"
+ "MBEGA1UECAwKU29tZS1TdGF0ZTEMMAoGA1UECgwDSy05MRswGQYDVQQDDBJ0ZXN0\n"
@ -96,7 +96,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
+ "Dcf5/G8bZe2DnavBQfML1wI5d7NUWE8CWb95SsIvFXI0qZE0oIR+axBVl9u97uaO\n"
+ "-----END CERTIFICATE-----\n";
private static final String DIGI_CERT =
private static final String DIGI_CERT =
"-----BEGIN CERTIFICATE-----\n"
+ "MIIG5jCCBc6gAwIBAgIQAze5KDR8YKauxa2xIX84YDANBgkqhkiG9w0BAQUFADBs\n"
+ "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
@ -137,7 +137,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
+ "zVMSW4SOwg/H7ZMZ2cn6j1g0djIvruFQFGHUqFijyDATI+/GJYw2jxyA\n"
+ "-----END CERTIFICATE-----\n";
private static final String GITHUB_CERT =
private static final String GITHUB_CERT =
"-----BEGIN CERTIFICATE-----\n"
+ "MIIHOjCCBiKgAwIBAgIQBH++LkveAITSyvjj7P5wWDANBgkqhkiG9w0BAQUFADBp\n"
+ "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"