11package com .securepreferences ;
22
33import java .io .File ;
4+ import java .security .GeneralSecurityException ;
45import java .util .Map ;
56
7+ import android .content .Context ;
68import android .content .SharedPreferences ;
79import android .content .SharedPreferences .Editor ;
810import android .preference .PreferenceManager ;
@@ -15,6 +17,7 @@ public class TestSecurePreferences extends AndroidTestCase {
1517 final static String DEFAULT_VALUE = "testingvaluebar" ;
1618
1719 public static final String TAG = "TestSecurePreferences" ;
20+
1821 public static final String MY_CUSTOM_PREFS = "my_custom_prefs" ;
1922 public static final String USER_PREFS_WITH_PASSWORD = "user_prefs_with_password" ;
2023
@@ -24,16 +27,6 @@ protected void setUp() throws Exception {
2427 SecurePreferences .setLoggingEnabled (true );
2528 }
2629
27- @ Override
28- protected void tearDown () throws Exception {
29- SecurePreferences securePrefs = new SecurePreferences (mContext );
30- Editor edit = securePrefs .edit ();
31- edit .clear ();
32- edit .commit ();
33-
34- super .tearDown ();
35- }
36-
3730 private void deletePrefFile (String prefFileName ) {
3831 File f = getContext ().getDatabasePath (prefFileName );
3932 if (f !=null && f .exists ()){
@@ -47,41 +40,83 @@ private void deletePrefFile(String prefFileName) {
4740 }
4841
4942 public void testKeyGenerated () {
50- SecurePreferences securePrefs = new SecurePreferences (mContext );
43+ //both use the default prefs file
44+ SecurePreferences securePrefs = new SecurePreferences (mContext );
45+ SharedPreferences normalPrefs = PreferenceManager
46+ .getDefaultSharedPreferences (mContext );
5147
52- Map <String , String > prefs = securePrefs .getAll ();
48+ Map <String , String > allOfTheSecurePrefs = securePrefs .getAll ();
49+ Map <String , ?> allOfTheNormalPrefs = normalPrefs .getAll ();
5350
54- assertFalse (
55- "securePrefs should contain at least one entry of the enc key" ,
56- prefs .isEmpty ());
57- }
51+ assertTrue (
52+ "securePrefs should be empty as the key is excluded from the getAll map" ,
53+ allOfTheSecurePrefs .isEmpty ());
54+
55+ assertTrue (
56+ "The normal prefs version should contain a single entry the key" ,
57+ allOfTheNormalPrefs .size () == 1 );
58+
59+ clearPrefs (securePrefs );
60+ }
5861
5962 public void testKeyGeneratedCustomPrefFile () {
63+ //fails?
6064 SecurePreferences securePrefs = new SecurePreferences (mContext , null , MY_CUSTOM_PREFS );
6165
62- Map < String , String > prefs = securePrefs . getAll ( );
66+ SharedPreferences normalPrefs = getContext (). getSharedPreferences ( MY_CUSTOM_PREFS , Context . MODE_PRIVATE );
6367
64- assertFalse (
65- "securePrefs should contain at least one entry of the enc key" ,
66- prefs .isEmpty ());
67- deletePrefFile (MY_CUSTOM_PREFS );
68+ Map <String , String > allOfTheSecurePrefs = securePrefs .getAll ();
69+ Map <String , ?> allOfTheNormalPrefs = normalPrefs .getAll ();
70+
71+ assertTrue (
72+ "securePrefs should be empty as the key is excluded from the getAll map" ,
73+ allOfTheSecurePrefs .isEmpty ());
74+
75+ assertTrue (
76+ "The normal prefs version should contain a single entry the key" ,
77+ allOfTheNormalPrefs .size () == 1 );
6878
79+ deletePrefFile (MY_CUSTOM_PREFS );
6980 }
7081
7182
7283 public void testKeyGeneratedFromUserPassword () {
7384 SecurePreferences securePrefs = new SecurePreferences (mContext , "password" , USER_PREFS_WITH_PASSWORD );
7485
75- Map <String , String > prefs = securePrefs .getAll ();
86+ SharedPreferences normalPrefs = getContext ().getSharedPreferences (USER_PREFS_WITH_PASSWORD , Context .MODE_PRIVATE );
87+
88+ Map <String , ?> allThePrefs = normalPrefs .getAll ();
7689
7790 assertTrue (
78- "securePrefs should not contain a key as it's a generated via user password." ,
79- prefs .isEmpty ());
91+ "the securePrefs should not contain any enteries as the key is generated via user password." ,
92+ allThePrefs .isEmpty ());
8093
8194 deletePrefFile (USER_PREFS_WITH_PASSWORD );
8295 }
8396
8497
98+ public void testChangeUserPassword () {
99+ SecurePreferences securePrefs = new SecurePreferences (mContext , "password" , USER_PREFS_WITH_PASSWORD );
100+ Editor editor = securePrefs .edit ();
101+ final String key = "pwchgfoo" ;
102+ final String value = "pwchgbar" ;
103+ editor .putString (key ,value );
104+ editor .commit ();
105+
106+ String cipherText = securePrefs .getUnencryptedString (key , null );
107+ try {
108+ securePrefs .handlePasswordChange ("newPassword" , getContext ());
109+ } catch (GeneralSecurityException e ) {
110+ fail ("error changing passwd: " + e .getMessage ());
111+ }
112+
113+ String cipherTextFromNewPassword = securePrefs .getUnencryptedString (key , null );
114+
115+ assertNotSame ("The two cipher texts should not be the same" , cipherText , cipherTextFromNewPassword );
116+
117+ deletePrefFile (USER_PREFS_WITH_PASSWORD );
118+ }
119+
85120 public void testSaveString () {
86121 final String key = "foo" ;
87122 final String value = "bar" ;
@@ -92,9 +127,37 @@ public void testSaveString() {
92127
93128 String retrievedValue = securePrefs .getString (key , null );
94129 assertEquals (value , retrievedValue );
130+
131+ clearPrefs (securePrefs );
95132 }
96133
97- public void testSaveFloat () {
134+ public void testSaveStringInCustomPref () {
135+ final String key = "customfoo" ;
136+ final String value = "custombar" ;
137+
138+ SecurePreferences securePrefs = new SecurePreferences (mContext , null , MY_CUSTOM_PREFS );
139+ Editor edit = securePrefs .edit ();
140+ edit .putString (key , value );
141+ edit .commit ();
142+
143+ String retrievedValue = securePrefs .getString (key , null );
144+ assertEquals (value , retrievedValue );
145+
146+ clearPrefs (securePrefs );
147+ }
148+
149+ /**
150+ * tear down
151+ * @param prefs
152+ */
153+ private void clearPrefs (SharedPreferences prefs ) {
154+ Editor edit = prefs .edit ();
155+ //tear down
156+ edit .clear ();
157+ edit .commit ();
158+ }
159+
160+ public void testSaveFloat () {
98161 final String key = "foo" ;
99162 final float value = 0.99f ;
100163 SharedPreferences securePrefs = new SecurePreferences (mContext );
@@ -105,27 +168,30 @@ public void testSaveFloat() {
105168 float retrievedValue = securePrefs .getFloat (key , -1 );
106169
107170 assertEquals (value , retrievedValue );
171+
172+ clearPrefs (securePrefs );
108173 }
109174
110175 public void testSaveUnencrpyted () {
111176 final String key = "foo" ;
112177 final String value = "bar" ;
113178
114179 SecurePreferences securePrefs = new SecurePreferences (mContext );
115- SecurePreferences .Editor secureEdit = ( SecurePreferences . Editor ) securePrefs
180+ SecurePreferences .Editor secureEdit = securePrefs
116181 .edit ();
117182 secureEdit .putUnencryptedString (key , value );
118183 secureEdit .commit ();
119184
120185 String retrievedValue = securePrefs .getUnencryptedString (key , null );
121186 assertEquals (value , retrievedValue );
187+ clearPrefs (securePrefs );
122188 }
123189
124190 public void testKeyIsEncrpyted () {
125191
126192
127193 SecurePreferences securePrefs = new SecurePreferences (mContext );
128- SecurePreferences .Editor secureEdit = ( SecurePreferences . Editor ) securePrefs
194+ SecurePreferences .Editor secureEdit = securePrefs
129195 .edit ();
130196 secureEdit .putUnencryptedString (DEFAULT_KEY , DEFAULT_VALUE );
131197 secureEdit .commit ();
@@ -137,6 +203,8 @@ public void testKeyIsEncrpyted() {
137203 String retrievedValue = normalPrefs .getString (DEFAULT_KEY , null );
138204
139205 assertNull (DEFAULT_VALUE , retrievedValue );
206+
207+ clearPrefs (securePrefs );
140208 }
141209
142210 public void testDestroyKeys (){
@@ -149,10 +217,11 @@ public void testDestroyKeys(){
149217
150218 try {
151219 String retrievedValue = securePrefs .getString (DEFAULT_KEY , null );
152- fail ("Null pointer should be thrown" );
220+ fail ("Null pointer should be thrown not retrievedValue:" + retrievedValue );
153221 }catch (NullPointerException e ){
154222
155223 }
224+ clearPrefs (securePrefs );
156225 }
157226
158227}
0 commit comments