-
Notifications
You must be signed in to change notification settings - Fork 44
Support for encryption of shared preferences #681
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
Merged
QuintinWillison
merged 8 commits into
main
from
593-support-for-encryption-of-shared-preferences
Aug 5, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c4e3edb
Added posibility to provide custom storage implementation instead of …
martin-morek 8c5bd79
Test correct storage is used
martin-morek 6dcd8a8
Renamed method reset to clear, added documentation comment
martin-morek 74ff146
Extracted code repetition into separate method
martin-morek 872859f
Added missing documentation
martin-morek 16bf523
Names refactoring
martin-morek eb54a3b
Merge branch 'main' into 593-support-for-encryption-of-shared-prefere…
martin-morek 135ba26
Merge branch 'main' into 593-support-for-encryption-of-shared-prefere…
martin-morek 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
149 changes: 149 additions & 0 deletions
149
android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java
This file contains hidden or 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,149 @@ | ||
| package io.ably.lib.push; | ||
|
|
||
| import android.content.Context; | ||
| import android.test.AndroidTestCase; | ||
| import io.ably.lib.types.RegistrationToken; | ||
| import junit.extensions.TestSetup; | ||
| import junit.framework.TestSuite; | ||
| import org.junit.BeforeClass; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.HashMap; | ||
|
|
||
| public class LocalDeviceStorageTest extends AndroidTestCase { | ||
| private Context context; | ||
| private ActivationContext activationContext; | ||
|
|
||
|
|
||
| private HashMap<String, Object> hashMap = new HashMap<>(); | ||
|
|
||
| private Storage inMemoryStorage = new Storage() { | ||
| @Override | ||
| public void put(String key, String value) { | ||
| hashMap.put(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void put(String key, int value) { | ||
| hashMap.put(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public String get(String key, String defaultValue) { | ||
| Object value = hashMap.get(key); | ||
| return value != null ? (String) value : defaultValue; | ||
| } | ||
|
|
||
| @Override | ||
| public int get(String key, int defaultValue) { | ||
| Object value = hashMap.get(key); | ||
| return value != null ? (int) value : defaultValue; | ||
| } | ||
|
|
||
| @Override | ||
| public void clear(Field[] fields) { | ||
| hashMap = new HashMap<>(); | ||
| } | ||
| }; | ||
|
|
||
| @BeforeClass | ||
| public void setUp() { | ||
| context = getContext(); | ||
| activationContext = new ActivationContext(context.getApplicationContext()); | ||
| } | ||
|
|
||
| public static junit.framework.Test suite() { | ||
| TestSuite suite = new TestSuite(); | ||
| suite.addTest(new TestSetup(new TestSuite(LocalDeviceStorageTest.class)) {}); | ||
| return suite; | ||
| } | ||
|
|
||
| public void test_shared_preferences_storage_used_by_default() { | ||
| LocalDevice localDevice = new LocalDevice(activationContext, null); | ||
| /* initialize properties in storage */ | ||
| localDevice.create(); | ||
|
|
||
| /* verify custom storage is not used */ | ||
| assertTrue(hashMap.isEmpty()); | ||
|
|
||
| /* load properties */ | ||
| assertNotNull(localDevice.id); | ||
| assertNotNull(localDevice.deviceSecret); | ||
| } | ||
|
|
||
| public void test_shared_preferences_storage_works_correctly() { | ||
| LocalDevice localDevice = new LocalDevice(activationContext, null); | ||
|
|
||
| RegistrationToken registrationToken= new RegistrationToken(RegistrationToken.Type.FCM, "ABLY"); | ||
| /* initialize properties in storage */ | ||
| localDevice.create(); | ||
| localDevice.setAndPersistRegistrationToken(registrationToken); | ||
|
|
||
| /* verify custom storage is not used */ | ||
| assertTrue(hashMap.isEmpty()); | ||
|
|
||
| /* load properties */ | ||
| assertNotNull(localDevice.id); | ||
| assertNotNull(localDevice.deviceSecret); | ||
| assertTrue(localDevice.isCreated()); | ||
| assertEquals("FCM", localDevice.getRegistrationToken().type.name()); | ||
| assertEquals("ABLY", localDevice.getRegistrationToken().token); | ||
|
|
||
| /* reset all properties */ | ||
| localDevice.reset(); | ||
|
|
||
| /* properties were cleared */ | ||
| assertNull(localDevice.id); | ||
| assertNull(localDevice.deviceSecret); | ||
| assertNull(localDevice.getRegistrationToken()); | ||
| } | ||
|
|
||
| public void test_custom_storage_used_if_provided() { | ||
| LocalDevice localDevice = new LocalDevice(activationContext, inMemoryStorage); | ||
| /* initialize properties in storage */ | ||
| localDevice.create(); | ||
|
|
||
| /* verify in memory storage is used */ | ||
| assertFalse(hashMap.isEmpty()); | ||
|
|
||
| /* load properties */ | ||
| assertNotNull(localDevice.id); | ||
| assertNotNull(localDevice.deviceSecret); | ||
|
|
||
| String deviceId = localDevice.id; | ||
| String deviceSecret = localDevice.deviceSecret; | ||
|
|
||
| /* values are the same */ | ||
| assertEquals(deviceId, hashMap.get("ABLY_DEVICE_ID")); | ||
| assertEquals(deviceSecret, hashMap.get("ABLY_DEVICE_SECRET")); | ||
| } | ||
|
|
||
| public void test_custom_storage_works_correctly() { | ||
| LocalDevice localDevice = new LocalDevice(activationContext, inMemoryStorage); | ||
|
|
||
| RegistrationToken registrationToken= new RegistrationToken(RegistrationToken.Type.FCM, "ABLY"); | ||
| /* initialize properties in storage */ | ||
| localDevice.create(); | ||
| localDevice.setAndPersistRegistrationToken(registrationToken); | ||
|
|
||
| /* verify custom storage is used */ | ||
| assertFalse(hashMap.isEmpty()); | ||
|
|
||
| /* load properties */ | ||
| assertNotNull(localDevice.id); | ||
| assertNotNull(localDevice.deviceSecret); | ||
| assertTrue(localDevice.isCreated()); | ||
| assertEquals("FCM", localDevice.getRegistrationToken().type.name()); | ||
| assertEquals("ABLY", localDevice.getRegistrationToken().token); | ||
|
|
||
| /* reset all properties */ | ||
| localDevice.reset(); | ||
| /* verify custom storage was cleared out */ | ||
| assertTrue(hashMap.isEmpty()); | ||
|
|
||
| /* properties were cleared */ | ||
| assertNull(localDevice.id); | ||
| assertNull(localDevice.deviceSecret); | ||
| assertNull(localDevice.getRegistrationToken()); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java
This file contains hidden or 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,52 @@ | ||
| package io.ably.lib.push; | ||
|
|
||
| import android.content.SharedPreferences; | ||
| import android.preference.PreferenceManager; | ||
|
|
||
| import java.lang.reflect.Field; | ||
|
|
||
| public class SharedPreferenceStorage implements Storage{ | ||
|
|
||
| private final ActivationContext activationContext; | ||
|
|
||
| public SharedPreferenceStorage(ActivationContext activationContext) { | ||
| this.activationContext = activationContext; | ||
| } | ||
|
|
||
| private SharedPreferences sharedPreferences() { | ||
| return PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); | ||
| } | ||
|
|
||
| @Override | ||
| public void put(String key, String value) { | ||
| sharedPreferences().edit().putString(key, value).apply(); | ||
| } | ||
|
|
||
| @Override | ||
| public void put(String key, int value) { | ||
| sharedPreferences().edit().putInt(key, value).apply(); | ||
| } | ||
|
|
||
| @Override | ||
| public String get(String key, String defaultValue) { | ||
| return sharedPreferences().getString(key, defaultValue); | ||
| } | ||
|
|
||
| @Override | ||
| public int get(String key, int defaultValue) { | ||
| return sharedPreferences().getInt(key, defaultValue); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear(Field[] fields) { | ||
| SharedPreferences.Editor editor = activationContext.getPreferences().edit(); | ||
| for (Field f : fields) { | ||
| try { | ||
| editor.remove((String) f.get(null)); | ||
| } catch (IllegalAccessException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| editor.commit(); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.