Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: content/public/android/javatests/src/org/chromium/content/browser/crypto/CipherFactoryTest.java

Issue 2708243004: Auto convert content shell tests to JUnit4 (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.content.browser.crypto; 5 package org.chromium.content.browser.crypto;
6 6
7 import android.os.Bundle; 7 import android.os.Bundle;
8 import android.support.test.filters.MediumTest; 8 import android.support.test.filters.MediumTest;
9 import android.test.InstrumentationTestCase; 9
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
10 14
11 import org.chromium.base.ThreadUtils; 15 import org.chromium.base.ThreadUtils;
16 import org.chromium.base.test.BaseJUnit4ClassRunner;
12 import org.chromium.content.browser.crypto.CipherFactory.CipherDataObserver; 17 import org.chromium.content.browser.crypto.CipherFactory.CipherDataObserver;
13 18
14 import java.io.IOException; 19 import java.io.IOException;
15 import java.security.GeneralSecurityException; 20 import java.security.GeneralSecurityException;
16 import java.util.Arrays; 21 import java.util.Arrays;
17 22
18 import javax.crypto.Cipher; 23 import javax.crypto.Cipher;
19 24
20 /** 25 /**
21 * Functional tests for the {@link CipherFactory}. Confirms that saving and rest oring data works, as 26 * Functional tests for the {@link CipherFactory}. Confirms that saving and rest oring data works, as
22 * well as that {@link Cipher} instances properly encrypt and decrypt data. 27 * well as that {@link Cipher} instances properly encrypt and decrypt data.
23 * 28 *
24 * Tests that confirm that the class is thread-safe would require putting potent ially flaky hooks 29 * Tests that confirm that the class is thread-safe would require putting potent ially flaky hooks
25 * throughout the class to simulate artificial blockages. 30 * throughout the class to simulate artificial blockages.
26 */ 31 */
27 public class CipherFactoryTest extends InstrumentationTestCase { 32 @RunWith(BaseJUnit4ClassRunner.class)
33 public class CipherFactoryTest {
28 private static final byte[] INPUT_DATA = {1, 16, 84}; 34 private static final byte[] INPUT_DATA = {1, 16, 84};
29 35
30 /** Generates non-random byte[] for testing. */ 36 /** Generates non-random byte[] for testing. */
31 private static class DeterministicParameterGenerator extends ByteArrayGenera tor { 37 private static class DeterministicParameterGenerator extends ByteArrayGenera tor {
32 @Override 38 @Override
33 public byte[] getBytes(int numBytes) throws IOException, GeneralSecurity Exception { 39 public byte[] getBytes(int numBytes) throws IOException, GeneralSecurity Exception {
34 return getBytes(numBytes, (byte) 0); 40 return getBytes(numBytes, (byte) 0);
35 } 41 }
36 42
37 /** 43 /**
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 @Override 76 @Override
71 public void run() { 77 public void run() {
72 // Do nothing. 78 // Do nothing.
73 } 79 }
74 }; 80 };
75 81
76 /** 82 /**
77 * Overrides the {@link ByteArrayGenerator} used by the {@link CipherFactory } to ensure 83 * Overrides the {@link ByteArrayGenerator} used by the {@link CipherFactory } to ensure
78 * deterministic results. 84 * deterministic results.
79 */ 85 */
80 @Override 86 @Before
81 protected void setUp() throws Exception { 87 public void setUp() throws Exception {
82 super.setUp();
83 mNumberProvider = new DeterministicParameterGenerator(); 88 mNumberProvider = new DeterministicParameterGenerator();
84 CipherFactory.getInstance().setRandomNumberProviderForTests(mNumberProvi der); 89 CipherFactory.getInstance().setRandomNumberProviderForTests(mNumberProvi der);
85 } 90 }
86 91
87 /** 92 /**
88 * {@link Cipher} instances initialized using the same parameters work in ex actly the same way. 93 * {@link Cipher} instances initialized using the same parameters work in ex actly the same way.
89 */ 94 */
95 @Test
90 @MediumTest 96 @MediumTest
91 public void testCipherUse() throws Exception { 97 public void testCipherUse() throws Exception {
92 // Check encryption. 98 // Check encryption.
93 Cipher aEncrypt = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_M ODE); 99 Cipher aEncrypt = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_M ODE);
94 Cipher bEncrypt = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_M ODE); 100 Cipher bEncrypt = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_M ODE);
95 byte[] output = sameOutputDifferentCiphers(INPUT_DATA, aEncrypt, bEncryp t); 101 byte[] output = sameOutputDifferentCiphers(INPUT_DATA, aEncrypt, bEncryp t);
96 102
97 // Check decryption. 103 // Check decryption.
98 Cipher aDecrypt = CipherFactory.getInstance().getCipher(Cipher.DECRYPT_M ODE); 104 Cipher aDecrypt = CipherFactory.getInstance().getCipher(Cipher.DECRYPT_M ODE);
99 Cipher bDecrypt = CipherFactory.getInstance().getCipher(Cipher.DECRYPT_M ODE); 105 Cipher bDecrypt = CipherFactory.getInstance().getCipher(Cipher.DECRYPT_M ODE);
100 byte[] decrypted = sameOutputDifferentCiphers(output, aDecrypt, bDecrypt ); 106 byte[] decrypted = sameOutputDifferentCiphers(output, aDecrypt, bDecrypt );
101 assertTrue(Arrays.equals(decrypted, INPUT_DATA)); 107 Assert.assertTrue(Arrays.equals(decrypted, INPUT_DATA));
102 } 108 }
103 109
104 /** 110 /**
105 * Restoring a {@link Bundle} containing the same parameters already in use by the 111 * Restoring a {@link Bundle} containing the same parameters already in use by the
106 * {@link CipherFactory} should keep the same keys. 112 * {@link CipherFactory} should keep the same keys.
107 */ 113 */
114 @Test
108 @MediumTest 115 @MediumTest
109 public void testSameBundleRestoration() throws Exception { 116 public void testSameBundleRestoration() throws Exception {
110 // Create two bundles with the same saved state. 117 // Create two bundles with the same saved state.
111 Bundle aBundle = new Bundle(); 118 Bundle aBundle = new Bundle();
112 Bundle bBundle = new Bundle(); 119 Bundle bBundle = new Bundle();
113 120
114 byte[] sameIv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 100); 121 byte[] sameIv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 100);
115 aBundle.putByteArray(CipherFactory.BUNDLE_IV, sameIv); 122 aBundle.putByteArray(CipherFactory.BUNDLE_IV, sameIv);
116 bBundle.putByteArray(CipherFactory.BUNDLE_IV, sameIv); 123 bBundle.putByteArray(CipherFactory.BUNDLE_IV, sameIv);
117 124
118 byte[] sameKey = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte ) 200); 125 byte[] sameKey = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte ) 200);
119 aBundle.putByteArray(CipherFactory.BUNDLE_KEY, sameKey); 126 aBundle.putByteArray(CipherFactory.BUNDLE_KEY, sameKey);
120 bBundle.putByteArray(CipherFactory.BUNDLE_KEY, sameKey); 127 bBundle.putByteArray(CipherFactory.BUNDLE_KEY, sameKey);
121 128
122 // Restore using the first bundle, then the second. Both should succeed. 129 // Restore using the first bundle, then the second. Both should succeed.
123 assertTrue(CipherFactory.getInstance().restoreFromBundle(aBundle)); 130 Assert.assertTrue(CipherFactory.getInstance().restoreFromBundle(aBundle) );
124 Cipher aCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE); 131 Cipher aCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE);
125 assertTrue(CipherFactory.getInstance().restoreFromBundle(bBundle)); 132 Assert.assertTrue(CipherFactory.getInstance().restoreFromBundle(bBundle) );
126 Cipher bCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE); 133 Cipher bCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE);
127 134
128 // Make sure the CipherFactory instances are using the same key. 135 // Make sure the CipherFactory instances are using the same key.
129 sameOutputDifferentCiphers(INPUT_DATA, aCipher, bCipher); 136 sameOutputDifferentCiphers(INPUT_DATA, aCipher, bCipher);
130 } 137 }
131 138
132 /** 139 /**
133 * Restoring a {@link Bundle} containing a different set of parameters from those already in use 140 * Restoring a {@link Bundle} containing a different set of parameters from those already in use
134 * by the {@link CipherFactory} should fail. Any Ciphers created after the f ailed restoration 141 * by the {@link CipherFactory} should fail. Any Ciphers created after the f ailed restoration
135 * attempt should use the already-existing keys. 142 * attempt should use the already-existing keys.
136 */ 143 */
144 @Test
137 @MediumTest 145 @MediumTest
138 public void testDifferentBundleRestoration() throws Exception { 146 public void testDifferentBundleRestoration() throws Exception {
139 // Restore one set of parameters. 147 // Restore one set of parameters.
140 Bundle aBundle = new Bundle(); 148 Bundle aBundle = new Bundle();
141 byte[] aIv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 50 ); 149 byte[] aIv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 50 );
142 byte[] aKey = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 1 00); 150 byte[] aKey = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 1 00);
143 aBundle.putByteArray(CipherFactory.BUNDLE_IV, aIv); 151 aBundle.putByteArray(CipherFactory.BUNDLE_IV, aIv);
144 aBundle.putByteArray(CipherFactory.BUNDLE_KEY, aKey); 152 aBundle.putByteArray(CipherFactory.BUNDLE_KEY, aKey);
145 assertTrue(CipherFactory.getInstance().restoreFromBundle(aBundle)); 153 Assert.assertTrue(CipherFactory.getInstance().restoreFromBundle(aBundle) );
146 Cipher aCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE); 154 Cipher aCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE);
147 155
148 // Restore using a different set of parameters. 156 // Restore using a different set of parameters.
149 Bundle bBundle = new Bundle(); 157 Bundle bBundle = new Bundle();
150 byte[] bIv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 15 0); 158 byte[] bIv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 15 0);
151 byte[] bKey = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 2 00); 159 byte[] bKey = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 2 00);
152 bBundle.putByteArray(CipherFactory.BUNDLE_IV, bIv); 160 bBundle.putByteArray(CipherFactory.BUNDLE_IV, bIv);
153 bBundle.putByteArray(CipherFactory.BUNDLE_KEY, bKey); 161 bBundle.putByteArray(CipherFactory.BUNDLE_KEY, bKey);
154 assertFalse(CipherFactory.getInstance().restoreFromBundle(bBundle)); 162 Assert.assertFalse(CipherFactory.getInstance().restoreFromBundle(bBundle ));
155 Cipher bCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE); 163 Cipher bCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE);
156 164
157 // Make sure they're using the same (original) key by encrypting the sam e data. 165 // Make sure they're using the same (original) key by encrypting the sam e data.
158 sameOutputDifferentCiphers(INPUT_DATA, aCipher, bCipher); 166 sameOutputDifferentCiphers(INPUT_DATA, aCipher, bCipher);
159 } 167 }
160 168
161 /** 169 /**
162 * Restoration from a {@link Bundle} missing data should fail. 170 * Restoration from a {@link Bundle} missing data should fail.
163 */ 171 */
172 @Test
164 @MediumTest 173 @MediumTest
165 public void testIncompleteBundleRestoration() throws Exception { 174 public void testIncompleteBundleRestoration() throws Exception {
166 // Make sure we handle the null case. 175 // Make sure we handle the null case.
167 assertFalse(CipherFactory.getInstance().restoreFromBundle(null)); 176 Assert.assertFalse(CipherFactory.getInstance().restoreFromBundle(null));
168 177
169 // Try restoring without the key. 178 // Try restoring without the key.
170 Bundle aBundle = new Bundle(); 179 Bundle aBundle = new Bundle();
171 byte[] iv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 50) ; 180 byte[] iv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 50) ;
172 aBundle.putByteArray(CipherFactory.BUNDLE_IV, iv); 181 aBundle.putByteArray(CipherFactory.BUNDLE_IV, iv);
173 assertFalse(CipherFactory.getInstance().restoreFromBundle(aBundle)); 182 Assert.assertFalse(CipherFactory.getInstance().restoreFromBundle(aBundle ));
174 183
175 // Try restoring without the initialization vector. 184 // Try restoring without the initialization vector.
176 Bundle bBundle = new Bundle(); 185 Bundle bBundle = new Bundle();
177 byte[] key = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 10 0); 186 byte[] key = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 10 0);
178 bBundle.putByteArray(CipherFactory.BUNDLE_KEY, key); 187 bBundle.putByteArray(CipherFactory.BUNDLE_KEY, key);
179 assertFalse(CipherFactory.getInstance().restoreFromBundle(bBundle)); 188 Assert.assertFalse(CipherFactory.getInstance().restoreFromBundle(bBundle ));
180 } 189 }
181 190
182 /** 191 /**
183 * Parameters should only be saved when they're needed by the {@link CipherF actory}. Restoring 192 * Parameters should only be saved when they're needed by the {@link CipherF actory}. Restoring
184 * parameters from a {@link Bundle} before this point should result in {@lin k Cipher}s using the 193 * parameters from a {@link Bundle} before this point should result in {@lin k Cipher}s using the
185 * restored parameters instead of any generated ones. 194 * restored parameters instead of any generated ones.
186 */ 195 */
196 @Test
187 @MediumTest 197 @MediumTest
188 public void testRestorationSucceedsBeforeCipherCreated() throws Exception { 198 public void testRestorationSucceedsBeforeCipherCreated() throws Exception {
189 byte[] iv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 50) ; 199 byte[] iv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 50) ;
190 byte[] key = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 10 0); 200 byte[] key = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 10 0);
191 Bundle bundle = new Bundle(); 201 Bundle bundle = new Bundle();
192 bundle.putByteArray(CipherFactory.BUNDLE_IV, iv); 202 bundle.putByteArray(CipherFactory.BUNDLE_IV, iv);
193 bundle.putByteArray(CipherFactory.BUNDLE_KEY, key); 203 bundle.putByteArray(CipherFactory.BUNDLE_KEY, key);
194 204
195 // The keys should be initialized only after restoration. 205 // The keys should be initialized only after restoration.
196 assertNull(CipherFactory.getInstance().getCipherData(false)); 206 Assert.assertNull(CipherFactory.getInstance().getCipherData(false));
197 assertTrue(CipherFactory.getInstance().restoreFromBundle(bundle)); 207 Assert.assertTrue(CipherFactory.getInstance().restoreFromBundle(bundle)) ;
198 assertNotNull(CipherFactory.getInstance().getCipherData(false)); 208 Assert.assertNotNull(CipherFactory.getInstance().getCipherData(false));
199 } 209 }
200 210
201 /** 211 /**
202 * If the {@link CipherFactory} has already generated parameters, restoratio ns of different data 212 * If the {@link CipherFactory} has already generated parameters, restoratio ns of different data
203 * should fail. All {@link Cipher}s should use the generated parameters. 213 * should fail. All {@link Cipher}s should use the generated parameters.
204 */ 214 */
215 @Test
205 @MediumTest 216 @MediumTest
206 public void testRestorationDiscardsAfterOtherCipherAlreadyCreated() throws E xception { 217 public void testRestorationDiscardsAfterOtherCipherAlreadyCreated() throws E xception {
207 byte[] iv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 50) ; 218 byte[] iv = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 50) ;
208 byte[] key = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 10 0); 219 byte[] key = mNumberProvider.getBytes(CipherFactory.NUM_BYTES, (byte) 10 0);
209 Bundle bundle = new Bundle(); 220 Bundle bundle = new Bundle();
210 bundle.putByteArray(CipherFactory.BUNDLE_IV, iv); 221 bundle.putByteArray(CipherFactory.BUNDLE_IV, iv);
211 bundle.putByteArray(CipherFactory.BUNDLE_KEY, key); 222 bundle.putByteArray(CipherFactory.BUNDLE_KEY, key);
212 223
213 // The keys should be initialized after creating the cipher, so the keys shouldn't match. 224 // The keys should be initialized after creating the cipher, so the keys shouldn't match.
214 Cipher aCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE); 225 Cipher aCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE);
215 assertFalse(CipherFactory.getInstance().restoreFromBundle(bundle)); 226 Assert.assertFalse(CipherFactory.getInstance().restoreFromBundle(bundle) );
216 Cipher bCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE); 227 Cipher bCipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MO DE);
217 228
218 // B's cipher should use the keys generated for A. 229 // B's cipher should use the keys generated for A.
219 sameOutputDifferentCiphers(INPUT_DATA, aCipher, bCipher); 230 sameOutputDifferentCiphers(INPUT_DATA, aCipher, bCipher);
220 } 231 }
221 232
222 /** 233 /**
223 * Data saved out to the {@link Bundle} should match what is held by the {@l ink CipherFactory}. 234 * Data saved out to the {@link Bundle} should match what is held by the {@l ink CipherFactory}.
224 */ 235 */
236 @Test
225 @MediumTest 237 @MediumTest
226 public void testSavingToBundle() throws Exception { 238 public void testSavingToBundle() throws Exception {
227 // Nothing should get saved out before Cipher data exists. 239 // Nothing should get saved out before Cipher data exists.
228 Bundle initialBundle = new Bundle(); 240 Bundle initialBundle = new Bundle();
229 CipherFactory.getInstance().saveToBundle(initialBundle); 241 CipherFactory.getInstance().saveToBundle(initialBundle);
230 assertFalse(initialBundle.containsKey(CipherFactory.BUNDLE_IV)); 242 Assert.assertFalse(initialBundle.containsKey(CipherFactory.BUNDLE_IV));
231 assertFalse(initialBundle.containsKey(CipherFactory.BUNDLE_KEY)); 243 Assert.assertFalse(initialBundle.containsKey(CipherFactory.BUNDLE_KEY));
232 244
233 // Check that Cipher data gets saved if it exists. 245 // Check that Cipher data gets saved if it exists.
234 CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MODE); 246 CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MODE);
235 Bundle afterBundle = new Bundle(); 247 Bundle afterBundle = new Bundle();
236 CipherFactory.getInstance().saveToBundle(afterBundle); 248 CipherFactory.getInstance().saveToBundle(afterBundle);
237 assertTrue(afterBundle.containsKey(CipherFactory.BUNDLE_IV)); 249 Assert.assertTrue(afterBundle.containsKey(CipherFactory.BUNDLE_IV));
238 assertTrue(afterBundle.containsKey(CipherFactory.BUNDLE_KEY)); 250 Assert.assertTrue(afterBundle.containsKey(CipherFactory.BUNDLE_KEY));
239 251
240 // Confirm the saved keys match by restoring it. 252 // Confirm the saved keys match by restoring it.
241 assertTrue(CipherFactory.getInstance().restoreFromBundle(afterBundle)); 253 Assert.assertTrue(CipherFactory.getInstance().restoreFromBundle(afterBun dle));
242 } 254 }
243 255
244 /** 256 /**
245 * Checks that an observer is notified when cipher data is created. 257 * Checks that an observer is notified when cipher data is created.
246 */ 258 */
259 @Test
247 @MediumTest 260 @MediumTest
248 public void testCipherFactoryObserver() throws Exception { 261 public void testCipherFactoryObserver() throws Exception {
249 TestCipherDataObserver observer = new TestCipherDataObserver(); 262 TestCipherDataObserver observer = new TestCipherDataObserver();
250 CipherFactory.getInstance().addCipherDataObserver(observer); 263 CipherFactory.getInstance().addCipherDataObserver(observer);
251 assertEquals(0, observer.getTimesNotified()); 264 Assert.assertEquals(0, observer.getTimesNotified());
252 CipherFactory.getInstance().getCipher(Cipher.DECRYPT_MODE); 265 CipherFactory.getInstance().getCipher(Cipher.DECRYPT_MODE);
253 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable); 266 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable);
254 assertEquals(1, observer.getTimesNotified()); 267 Assert.assertEquals(1, observer.getTimesNotified());
255 CipherFactory.getInstance().getCipher(Cipher.DECRYPT_MODE); 268 CipherFactory.getInstance().getCipher(Cipher.DECRYPT_MODE);
256 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable); 269 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable);
257 assertEquals(1, observer.getTimesNotified()); 270 Assert.assertEquals(1, observer.getTimesNotified());
258 CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MODE); 271 CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MODE);
259 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable); 272 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable);
260 assertEquals(1, observer.getTimesNotified()); 273 Assert.assertEquals(1, observer.getTimesNotified());
261 CipherFactory.getInstance().removeCipherDataObserver(observer); 274 CipherFactory.getInstance().removeCipherDataObserver(observer);
262 } 275 }
263 276
264 /** 277 /**
265 * Verifies that if the observer is attached after cipher data has already b een 278 * Verifies that if the observer is attached after cipher data has already b een
266 * created the observer doesn't fire. 279 * created the observer doesn't fire.
267 */ 280 */
281 @Test
268 @MediumTest 282 @MediumTest
269 public void testCipherFactoryObserverTooLate() throws Exception { 283 public void testCipherFactoryObserverTooLate() throws Exception {
270 CipherFactory.getInstance().getCipher(Cipher.DECRYPT_MODE); 284 CipherFactory.getInstance().getCipher(Cipher.DECRYPT_MODE);
271 // Ensures that cipher finishes initializing before running the rest of the test. 285 // Ensures that cipher finishes initializing before running the rest of the test.
272 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable); 286 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable);
273 TestCipherDataObserver observer = new TestCipherDataObserver(); 287 TestCipherDataObserver observer = new TestCipherDataObserver();
274 CipherFactory.getInstance().addCipherDataObserver(observer); 288 CipherFactory.getInstance().addCipherDataObserver(observer);
275 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable); 289 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable);
276 assertEquals(0, observer.getTimesNotified()); 290 Assert.assertEquals(0, observer.getTimesNotified());
277 CipherFactory.getInstance().getCipher(Cipher.DECRYPT_MODE); 291 CipherFactory.getInstance().getCipher(Cipher.DECRYPT_MODE);
278 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable); 292 ThreadUtils.runOnUiThreadBlocking(mEmptyRunnable);
279 assertEquals(0, observer.getTimesNotified()); 293 Assert.assertEquals(0, observer.getTimesNotified());
280 } 294 }
281 295
282 /** 296 /**
283 * Confirm that the two {@link Cipher}s are functionally equivalent. 297 * Confirm that the two {@link Cipher}s are functionally equivalent.
284 * @return The input after it has been operated on (e.g. decrypted or encryp ted). 298 * @return The input after it has been operated on (e.g. decrypted or encryp ted).
285 */ 299 */
286 private byte[] sameOutputDifferentCiphers(byte[] input, Cipher aCipher, Ciph er bCipher) 300 private byte[] sameOutputDifferentCiphers(byte[] input, Cipher aCipher, Ciph er bCipher)
287 throws Exception { 301 throws Exception {
288 assertNotNull(aCipher); 302 Assert.assertNotNull(aCipher);
289 assertNotNull(bCipher); 303 Assert.assertNotNull(bCipher);
290 assertNotSame(aCipher, bCipher); 304 Assert.assertNotSame(aCipher, bCipher);
291 305
292 byte[] aOutput = aCipher.doFinal(input); 306 byte[] aOutput = aCipher.doFinal(input);
293 byte[] bOutput = bCipher.doFinal(input); 307 byte[] bOutput = bCipher.doFinal(input);
294 308
295 assertNotNull(aOutput); 309 Assert.assertNotNull(aOutput);
296 assertNotNull(bOutput); 310 Assert.assertNotNull(bOutput);
297 assertTrue(Arrays.equals(aOutput, bOutput)); 311 Assert.assertTrue(Arrays.equals(aOutput, bOutput));
298 312
299 return aOutput; 313 return aOutput;
300 } 314 }
301 } 315 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698