diff --git a/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/util/FileImportCacheTest.java b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/util/FileImportCacheTest.java new file mode 100644 index 000000000..b5708b46f --- /dev/null +++ b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/util/FileImportCacheTest.java @@ -0,0 +1,54 @@ +package org.sufficientlysecure.keychain.util; + +import android.os.Bundle; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.shadows.ShadowLog; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(RobolectricTestRunner.class) +@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19 +public class FileImportCacheTest { + + @Before + public void setUp() throws Exception { + ShadowLog.stream = System.out; + } + + @Test + public void testInputOutput() throws Exception { + + FileImportCache cache = new FileImportCache(Robolectric.application); + + ArrayList list = new ArrayList(); + + for (int i = 0; i < 50; i++) { + Bundle b = new Bundle(); + b.putInt("key1", i); + b.putString("key2", Integer.toString(i)); + list.add(b); + } + + // write to cache file + cache.writeCache(list); + + // read back + List last = cache.readCacheIntoList(); + + for (int i = 0; i < list.size(); i++) { + Assert.assertEquals("input values should be equal to output values", + list.get(i).getInt("key1"), last.get(i).getInt("key1")); + Assert.assertEquals("input values should be equal to output values", + list.get(i).getString("key2"), last.get(i).getString("key2")); + } + + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 8d4717eeb..c87e490be 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -24,6 +24,7 @@ import android.net.Uri; import android.os.Bundle; import android.os.Message; import android.os.Messenger; +import android.os.Parcel; import android.os.RemoteException; import org.sufficientlysecure.keychain.Constants; @@ -31,7 +32,7 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.FileHelper; import org.sufficientlysecure.keychain.helper.OtherHelper; import org.sufficientlysecure.keychain.helper.Preferences; -import org.sufficientlysecure.keychain.keyimport.FileImportCache; +import org.sufficientlysecure.keychain.util.FileImportCache; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.KeybaseKeyserver; @@ -387,14 +388,16 @@ public class KeychainIntentService extends IntentService } } else if (ACTION_IMPORT_KEYRING.equals(action)) { try { + List entries; if (data.containsKey(IMPORT_KEY_LIST)) { // get entries from intent entries = data.getParcelableArrayList(IMPORT_KEY_LIST); } else { // get entries from cached file - FileImportCache cache = new FileImportCache(this); - entries = cache.readCache(); + FileImportCache cache = + new FileImportCache(this); + entries = cache.readCacheIntoList(); } PgpImportExport pgpImportExport = new PgpImportExport(this, this); @@ -523,6 +526,7 @@ public class KeychainIntentService extends IntentService Intent importIntent = new Intent(this, KeychainIntentService.class); importIntent.setAction(ACTION_IMPORT_KEYRING); + Bundle importData = new Bundle(); // This is not going through binder, nothing to fear of importData.putParcelableArrayList(IMPORT_KEY_LIST, keyRings); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index dbc557f9a..4a606a1b3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -40,7 +40,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.OtherHelper; import org.sufficientlysecure.keychain.helper.Preferences; -import org.sufficientlysecure.keychain.keyimport.FileImportCache; +import org.sufficientlysecure.keychain.util.FileImportCache; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; @@ -503,7 +503,7 @@ public class ImportKeysActivity extends ActionBarActivity { // to prevent Java Binder problems on heavy imports // read FileImportCache for more info. try { - FileImportCache cache = new FileImportCache(this); + FileImportCache cache = new FileImportCache(this); cache.writeCache(selectedEntries); intent.putExtra(KeychainIntentService.EXTRA_DATA, data); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileImportCache.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileImportCache.java index e09cdd502..5a4bf5311 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileImportCache.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileImportCache.java @@ -23,14 +23,14 @@ import android.os.Parcelable; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.KeychainApplication; -import org.sufficientlysecure.keychain.util.Log; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -66,13 +66,14 @@ public class FileImportCache { File tempFile = new File(mContext.getCacheDir(), FILENAME); - ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tempFile)); + DataOutputStream oos = new DataOutputStream(new FileOutputStream(tempFile)); while (it.hasNext()) { - E ring = it.next(); Parcel p = Parcel.obtain(); // creating empty parcel object - p.writeParcelable(ring, 0); // saving bundle as parcel - oos.writeObject(p.marshall()); // writing parcel to file + p.writeParcelable(it.next(), 0); // saving bundle as parcel + byte[] buf = p.marshall(); + oos.writeInt(buf.length); + oos.write(buf); p.recycle(); } @@ -98,44 +99,37 @@ public class FileImportCache { } final File tempFile = new File(cacheDir, FILENAME); - final ObjectInputStream ois = new ObjectInputStream(new FileInputStream(tempFile)); + final DataInputStream ois = new DataInputStream(new FileInputStream(tempFile)); return new Iterator() { E mRing = null; boolean closed = false; + byte[] buf = new byte[512]; private void readNext() { if (mRing != null || closed) { - Log.e(Constants.TAG, "err!"); return; } try { - if (ois.available() == 0) { - return; - } - byte[] data = (byte[]) ois.readObject(); - Log.e(Constants.TAG, "bla"); - if (data == null) { - if (!closed) { - closed = true; - ois.close(); - tempFile.delete(); - } - return; + int length = ois.readInt(); + while (buf.length < length) { + buf = new byte[buf.length * 2]; } + ois.readFully(buf, 0, length); Parcel parcel = Parcel.obtain(); // creating empty parcel object - parcel.unmarshall(data, 0, data.length); + parcel.unmarshall(buf, 0, length); parcel.setDataPosition(0); mRing = parcel.readParcelable(KeychainApplication.class.getClassLoader()); parcel.recycle(); - } catch (ClassNotFoundException e) { - Log.e(Constants.TAG, "Encountered ClassNotFoundException during cache read, this is a bug!"); + } catch (EOFException e) { + // aight + close(); } catch (IOException e) { - Log.e(Constants.TAG, "Encountered IOException during cache read!"); + Log.e(Constants.TAG, "Encountered IOException during cache read!", e); } } @@ -163,17 +157,23 @@ public class FileImportCache { @Override public void finalize() throws Throwable { + close(); super.finalize(); + } + + private void close() { if (!closed) { try { ois.close(); tempFile.delete(); } catch (IOException e) { - // never mind + // nvm } } + closed = true; } + }; } }