-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multithreaded Correctness and Callback support #14
Open
JRProd
wants to merge
13
commits into
BottleRocketStudios:develop
Choose a base branch
from
JRProd:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
63751bb
These changes are notes and fixes for multithreaded correctness withi…
4ff97a7
Addes changes to KeychainAuth that were added to SharedPref. Synchron…
892f2ca
Notes for future changes
7d8b19d
Added callback support for apply and commit. Callback is optional but…
2229df1
Adds tests for the callback listener. Tests success, Null SecretKey, …
0db4276
Updated the readme with changes and the the listener, fixed one spell…
1f7db90
Merge pull request #1 from lazysp333/feature/Callback
JRProd ff6e4aa
Removed unused import
fc0261a
Merge branch 'develop' of github.com:lazysp333/Android-Vault into dev…
6488f1f
Removes unused lock
21fcf52
Suggested changes from the PR and added two tests, NullSecretKey and …
dfacc40
Removes the nullKey test
f36de77
Merge pull request #2 from lazysp333/feature/Callback
JRProd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
211 changes: 211 additions & 0 deletions
211
...vault/src/androidTest/java/com/bottlerocketstudios/vault/test/TestSharedPrefListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* | ||
* Copyright (c) 2016. Bottle Rocket LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bottlerocketstudios.vault.test; | ||
|
||
import android.content.SharedPreferences; | ||
import android.test.AndroidTestCase; | ||
import android.util.Log; | ||
|
||
import com.bottlerocketstudios.vault.SharedPreferenceVault; | ||
import com.bottlerocketstudios.vault.SharedPreferenceVaultFactory; | ||
import com.bottlerocketstudios.vault.StandardSharedPreferenceVault; | ||
import com.bottlerocketstudios.vault.keys.generator.Aes256RandomKeyFactory; | ||
|
||
import java.security.GeneralSecurityException; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
/* | ||
* Test to see if the listener is called correctly when it should be. | ||
*/ | ||
public class TestSharedPrefListener extends AndroidTestCase { | ||
private static final String TAG = TestSharedPrefListener.class.getSimpleName(); | ||
|
||
private static final String KEY_FILE_NAME = "listenerKeyTest"; | ||
private static final String PREF_FILE_NAME = "listenerPrefTest"; | ||
private static final String KEY_ALIAS_1 = "listenerKeyAlias"; | ||
private static final int KEY_INDEX_1 = 1; | ||
private static final String PRESHARED_SECRET_1 = "a;sdl564546a6s6w2828d4fsdfbsijd;saj;9dj9"; | ||
|
||
private static final String TEST_STRING_KEY = "testKey"; | ||
private static final String TEST_STRING_VALUE = " This is a test. "; | ||
|
||
private SharedPreferenceVault mSharedPreferenceVault; | ||
|
||
// Initialization function. Should be called by all tests but only initialized once. On previous | ||
// invocations, the storage is cleared and that is it | ||
private boolean shouldInit = true; | ||
private void init() { | ||
Log.i(TAG, "Initializing"); | ||
if(shouldInit) { | ||
mSharedPreferenceVault = null; | ||
try { | ||
Log.i(TAG, "Creating SharedPreference vault."); | ||
mSharedPreferenceVault = SharedPreferenceVaultFactory.getAppKeyedCompatAes256Vault( | ||
getContext(), | ||
PREF_FILE_NAME, | ||
KEY_FILE_NAME, | ||
KEY_ALIAS_1, | ||
KEY_INDEX_1, | ||
PRESHARED_SECRET_1); | ||
} catch (GeneralSecurityException e) { | ||
Log.e(TAG, "Caught java.security.GeneralSecurityException", e); | ||
assertTrue("Exception creating vault", false); | ||
} | ||
|
||
assertNotNull("Unable to create initial vault", mSharedPreferenceVault); | ||
assertTrue("Testing StandardSharedPreferenceVault", | ||
mSharedPreferenceVault instanceof StandardSharedPreferenceVault); | ||
|
||
shouldInit = false; | ||
} | ||
cleanStorage(); | ||
} | ||
|
||
private void cleanStorage() { | ||
mSharedPreferenceVault.rekeyStorage(Aes256RandomKeyFactory.createKey()); | ||
assertNull("Rekey of storage did not clear existing value", | ||
mSharedPreferenceVault.getString(TEST_STRING_KEY, null)); | ||
} | ||
|
||
public void testSuccessCommit() { | ||
init(); | ||
SharedPreferences.Editor editor = mSharedPreferenceVault.setSharedPrefVaultWriteListener( | ||
new SharedPrefVaultWriteListenerExtension(new OnComplete() { | ||
@Override | ||
public void onComplete(boolean success) { | ||
assertTrue("Error when committing to sharedPrefs", success); | ||
} | ||
}) | ||
).edit(); | ||
|
||
editor.putString(TEST_STRING_KEY, TEST_STRING_VALUE).commit(); | ||
} | ||
|
||
public void testSuccessApply() { | ||
init(); | ||
SharedPreferences.Editor editor = mSharedPreferenceVault.setSharedPrefVaultWriteListener( | ||
new SharedPrefVaultWriteListenerExtension(new OnComplete() { | ||
@Override | ||
public void onComplete(boolean success) { | ||
assertTrue("Error when applying to sharedPrefs", success); | ||
} | ||
}) | ||
).edit(); | ||
|
||
editor.putString(TEST_STRING_KEY, TEST_STRING_VALUE).apply(); | ||
} | ||
|
||
public void testNullSecretKeyFailureCommit() { | ||
init(); | ||
mSharedPreferenceVault.clearStorage(); | ||
SharedPreferences.Editor editor = mSharedPreferenceVault.setSharedPrefVaultWriteListener( | ||
new SharedPrefVaultWriteListenerExtension(new OnComplete() { | ||
@Override | ||
public void onComplete(boolean success) { | ||
assertFalse("The key is still initialized", success); | ||
} | ||
}) | ||
).edit(); | ||
|
||
editor.putString(TEST_STRING_KEY, TEST_STRING_VALUE).commit(); | ||
} | ||
|
||
public void testNullSecretKeyFailureApply() { | ||
init(); | ||
mSharedPreferenceVault.clearStorage(); | ||
SharedPreferences.Editor editor = mSharedPreferenceVault.setSharedPrefVaultWriteListener( | ||
new SharedPrefVaultWriteListenerExtension(new OnComplete() { | ||
@Override | ||
public void onComplete(boolean success) { | ||
assertFalse("The key is still initialized", success); | ||
} | ||
}) | ||
).edit(); | ||
|
||
editor.putString(TEST_STRING_KEY, TEST_STRING_VALUE).apply(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lazysp333 Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These have been explained and fixed |
||
} | ||
|
||
private final SecretKey malformedSecretKey = new SecretKey() { | ||
private final String mKey = "ThisIsMySuperSecretKey"; | ||
|
||
@Override | ||
public String getAlgorithm() { | ||
return "BestAlgorithEver"; | ||
} | ||
|
||
@Override | ||
public String getFormat() { | ||
return mKey; | ||
} | ||
|
||
@Override | ||
public byte[] getEncoded() { | ||
return mKey.getBytes(); | ||
} | ||
}; | ||
public void testMalformedKeyFailureCommit() { | ||
init(); | ||
mSharedPreferenceVault.rekeyStorage(malformedSecretKey); | ||
SharedPreferences.Editor editor = mSharedPreferenceVault.setSharedPrefVaultWriteListener( | ||
new SharedPrefVaultWriteListenerExtension(new OnComplete() { | ||
@Override | ||
public void onComplete(boolean success) { | ||
assertFalse("Malformed key still works", success); | ||
} | ||
}) | ||
).edit(); | ||
|
||
editor.putString(TEST_STRING_KEY, TEST_STRING_VALUE).commit(); | ||
} | ||
|
||
public void testMalformedKeyFailureApply() { | ||
init(); | ||
mSharedPreferenceVault.rekeyStorage(malformedSecretKey); | ||
SharedPreferences.Editor editor = mSharedPreferenceVault.setSharedPrefVaultWriteListener( | ||
new SharedPrefVaultWriteListenerExtension(new OnComplete() { | ||
@Override | ||
public void onComplete(boolean success) { | ||
assertFalse("Malformed key still works", success); | ||
} | ||
}) | ||
).edit(); | ||
|
||
editor.putString(TEST_STRING_KEY, TEST_STRING_VALUE).apply(); | ||
} | ||
|
||
public interface OnComplete { | ||
void onComplete(boolean success); | ||
} | ||
|
||
private class SharedPrefVaultWriteListenerExtension implements SharedPreferenceVault.SharedPrefVaultWriteListener { | ||
private final OnComplete mListener; | ||
|
||
public SharedPrefVaultWriteListenerExtension(OnComplete listener) { | ||
mListener = listener; | ||
} | ||
|
||
@Override | ||
public void onSuccess() { | ||
mListener.onComplete(true); | ||
} | ||
|
||
@Override | ||
public void onError() { | ||
mListener.onComplete(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lazysp333 Should this be
editor.putString(null, TEST_STRING_VALUE).commit();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dev777 This is the null secret key, AKA the user is trying to encrypt data without having a secret key set. That is why I clear the storage before.
If you would also like, I can add a test case for a attempt to set a null shared pref key as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lazysp333 I think that renaming this test case to
testNullSecretKeyFailureCommit
and then adding the test case for null shared pref key makes sense. Same recommendation for the testNullKeyFailureApply comment as wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dev777 Another thing, putting a null value in this IS acceptable in its current state, the
editor.commit()
returns true (same with apply). Is this expected behavior?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These have been explained and fixed