make FileImportCache generic, iterable, and add unit test

This commit is contained in:
Vincent Breitmoser 2014-08-01 16:49:41 +02:00
parent 11e5261f07
commit c0edaf9a5e
4 changed files with 89 additions and 31 deletions

View File

@ -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<Bundle> cache = new FileImportCache<Bundle>(Robolectric.application);
ArrayList<Bundle> list = new ArrayList<Bundle>();
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<Bundle> 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"));
}
}
}

View File

@ -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<ParcelableKeyRing> 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<ParcelableKeyRing> cache =
new FileImportCache<ParcelableKeyRing>(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);

View File

@ -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<ParcelableKeyRing> cache = new FileImportCache<ParcelableKeyRing>(this);
cache.writeCache(selectedEntries);
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);

View File

@ -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<E extends Parcelable> {
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<E extends Parcelable> {
}
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>() {
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<E extends Parcelable> {
@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;
}
};
}
}