mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-11-27 11:12:15 -05:00
make FileImportCache generic, iterable, and add unit test
This commit is contained in:
parent
11e5261f07
commit
c0edaf9a5e
@ -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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -24,6 +24,7 @@ import android.net.Uri;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.os.Messenger;
|
import android.os.Messenger;
|
||||||
|
import android.os.Parcel;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.Constants;
|
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.FileHelper;
|
||||||
import org.sufficientlysecure.keychain.helper.OtherHelper;
|
import org.sufficientlysecure.keychain.helper.OtherHelper;
|
||||||
import org.sufficientlysecure.keychain.helper.Preferences;
|
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.HkpKeyserver;
|
||||||
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
|
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
|
||||||
import org.sufficientlysecure.keychain.keyimport.KeybaseKeyserver;
|
import org.sufficientlysecure.keychain.keyimport.KeybaseKeyserver;
|
||||||
@ -387,14 +388,16 @@ public class KeychainIntentService extends IntentService
|
|||||||
}
|
}
|
||||||
} else if (ACTION_IMPORT_KEYRING.equals(action)) {
|
} else if (ACTION_IMPORT_KEYRING.equals(action)) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
List<ParcelableKeyRing> entries;
|
List<ParcelableKeyRing> entries;
|
||||||
if (data.containsKey(IMPORT_KEY_LIST)) {
|
if (data.containsKey(IMPORT_KEY_LIST)) {
|
||||||
// get entries from intent
|
// get entries from intent
|
||||||
entries = data.getParcelableArrayList(IMPORT_KEY_LIST);
|
entries = data.getParcelableArrayList(IMPORT_KEY_LIST);
|
||||||
} else {
|
} else {
|
||||||
// get entries from cached file
|
// get entries from cached file
|
||||||
FileImportCache cache = new FileImportCache(this);
|
FileImportCache<ParcelableKeyRing> cache =
|
||||||
entries = cache.readCache();
|
new FileImportCache<ParcelableKeyRing>(this);
|
||||||
|
entries = cache.readCacheIntoList();
|
||||||
}
|
}
|
||||||
|
|
||||||
PgpImportExport pgpImportExport = new PgpImportExport(this, this);
|
PgpImportExport pgpImportExport = new PgpImportExport(this, this);
|
||||||
@ -523,6 +526,7 @@ public class KeychainIntentService extends IntentService
|
|||||||
|
|
||||||
Intent importIntent = new Intent(this, KeychainIntentService.class);
|
Intent importIntent = new Intent(this, KeychainIntentService.class);
|
||||||
importIntent.setAction(ACTION_IMPORT_KEYRING);
|
importIntent.setAction(ACTION_IMPORT_KEYRING);
|
||||||
|
|
||||||
Bundle importData = new Bundle();
|
Bundle importData = new Bundle();
|
||||||
// This is not going through binder, nothing to fear of
|
// This is not going through binder, nothing to fear of
|
||||||
importData.putParcelableArrayList(IMPORT_KEY_LIST, keyRings);
|
importData.putParcelableArrayList(IMPORT_KEY_LIST, keyRings);
|
||||||
|
@ -40,7 +40,7 @@ import org.sufficientlysecure.keychain.Constants;
|
|||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.helper.OtherHelper;
|
import org.sufficientlysecure.keychain.helper.OtherHelper;
|
||||||
import org.sufficientlysecure.keychain.helper.Preferences;
|
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.ImportKeysListEntry;
|
||||||
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
|
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
|
||||||
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
|
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
|
||||||
@ -503,7 +503,7 @@ public class ImportKeysActivity extends ActionBarActivity {
|
|||||||
// to prevent Java Binder problems on heavy imports
|
// to prevent Java Binder problems on heavy imports
|
||||||
// read FileImportCache for more info.
|
// read FileImportCache for more info.
|
||||||
try {
|
try {
|
||||||
FileImportCache cache = new FileImportCache(this);
|
FileImportCache<ParcelableKeyRing> cache = new FileImportCache<ParcelableKeyRing>(this);
|
||||||
cache.writeCache(selectedEntries);
|
cache.writeCache(selectedEntries);
|
||||||
|
|
||||||
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
|
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
|
||||||
|
@ -23,14 +23,14 @@ import android.os.Parcelable;
|
|||||||
|
|
||||||
import org.sufficientlysecure.keychain.Constants;
|
import org.sufficientlysecure.keychain.Constants;
|
||||||
import org.sufficientlysecure.keychain.KeychainApplication;
|
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.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
|
||||||
import java.io.ObjectOutputStream;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -66,13 +66,14 @@ public class FileImportCache<E extends Parcelable> {
|
|||||||
|
|
||||||
File tempFile = new File(mContext.getCacheDir(), FILENAME);
|
File tempFile = new File(mContext.getCacheDir(), FILENAME);
|
||||||
|
|
||||||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tempFile));
|
DataOutputStream oos = new DataOutputStream(new FileOutputStream(tempFile));
|
||||||
|
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
E ring = it.next();
|
|
||||||
Parcel p = Parcel.obtain(); // creating empty parcel object
|
Parcel p = Parcel.obtain(); // creating empty parcel object
|
||||||
p.writeParcelable(ring, 0); // saving bundle as parcel
|
p.writeParcelable(it.next(), 0); // saving bundle as parcel
|
||||||
oos.writeObject(p.marshall()); // writing parcel to file
|
byte[] buf = p.marshall();
|
||||||
|
oos.writeInt(buf.length);
|
||||||
|
oos.write(buf);
|
||||||
p.recycle();
|
p.recycle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,44 +99,37 @@ public class FileImportCache<E extends Parcelable> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final File tempFile = new File(cacheDir, FILENAME);
|
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>() {
|
return new Iterator<E>() {
|
||||||
|
|
||||||
E mRing = null;
|
E mRing = null;
|
||||||
boolean closed = false;
|
boolean closed = false;
|
||||||
|
byte[] buf = new byte[512];
|
||||||
|
|
||||||
private void readNext() {
|
private void readNext() {
|
||||||
if (mRing != null || closed) {
|
if (mRing != null || closed) {
|
||||||
Log.e(Constants.TAG, "err!");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (ois.available() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] data = (byte[]) ois.readObject();
|
int length = ois.readInt();
|
||||||
Log.e(Constants.TAG, "bla");
|
while (buf.length < length) {
|
||||||
if (data == null) {
|
buf = new byte[buf.length * 2];
|
||||||
if (!closed) {
|
|
||||||
closed = true;
|
|
||||||
ois.close();
|
|
||||||
tempFile.delete();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
ois.readFully(buf, 0, length);
|
||||||
|
|
||||||
Parcel parcel = Parcel.obtain(); // creating empty parcel object
|
Parcel parcel = Parcel.obtain(); // creating empty parcel object
|
||||||
parcel.unmarshall(data, 0, data.length);
|
parcel.unmarshall(buf, 0, length);
|
||||||
parcel.setDataPosition(0);
|
parcel.setDataPosition(0);
|
||||||
mRing = parcel.readParcelable(KeychainApplication.class.getClassLoader());
|
mRing = parcel.readParcelable(KeychainApplication.class.getClassLoader());
|
||||||
parcel.recycle();
|
parcel.recycle();
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (EOFException e) {
|
||||||
Log.e(Constants.TAG, "Encountered ClassNotFoundException during cache read, this is a bug!");
|
// aight
|
||||||
|
close();
|
||||||
} catch (IOException e) {
|
} 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
|
@Override
|
||||||
public void finalize() throws Throwable {
|
public void finalize() throws Throwable {
|
||||||
|
close();
|
||||||
super.finalize();
|
super.finalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void close() {
|
||||||
if (!closed) {
|
if (!closed) {
|
||||||
try {
|
try {
|
||||||
ois.close();
|
ois.close();
|
||||||
tempFile.delete();
|
tempFile.delete();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// never mind
|
// nvm
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
closed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user