| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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.chrome.browser.identity; | 5 package org.chromium.chrome.browser.identity; |
| 6 | 6 |
| 7 import android.support.test.filters.SmallTest; | 7 import android.support.test.filters.SmallTest; |
| 8 import android.test.InstrumentationTestCase; | 8 |
| 9 import org.junit.Assert; |
| 10 import org.junit.Test; |
| 11 import org.junit.runner.RunWith; |
| 9 | 12 |
| 10 import org.chromium.base.test.util.AdvancedMockContext; | 13 import org.chromium.base.test.util.AdvancedMockContext; |
| 11 import org.chromium.base.test.util.Feature; | 14 import org.chromium.base.test.util.Feature; |
| 12 import org.chromium.chrome.browser.util.HashUtil; | 15 import org.chromium.chrome.browser.util.HashUtil; |
| 16 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 13 | 17 |
| 14 public class SettingsSecureBasedIdentificationGeneratorTest extends Instrumentat
ionTestCase { | 18 @RunWith(ChromeJUnit4ClassRunner.class) |
| 15 | 19 public class SettingsSecureBasedIdentificationGeneratorTest { |
| 16 private static final String FLAG_ANDROID_ID = "android_id"; | 20 private static final String FLAG_ANDROID_ID = "android_id"; |
| 17 | 21 |
| 22 @Test |
| 18 @SmallTest | 23 @SmallTest |
| 19 @Feature({"ChromeToMobile", "Omaha"}) | 24 @Feature({"ChromeToMobile", "Omaha"}) |
| 20 public void testAndroidIdSuccessWithSalt() { | 25 public void testAndroidIdSuccessWithSalt() { |
| 21 String androidId = "42"; | 26 String androidId = "42"; |
| 22 String salt = "mySalt"; | 27 String salt = "mySalt"; |
| 23 String expected = HashUtil.getMd5Hash(new HashUtil.Params(androidId).wit
hSalt(salt)); | 28 String expected = HashUtil.getMd5Hash(new HashUtil.Params(androidId).wit
hSalt(salt)); |
| 24 runTest(androidId, salt, expected); | 29 runTest(androidId, salt, expected); |
| 25 } | 30 } |
| 26 | 31 |
| 32 @Test |
| 27 @SmallTest | 33 @SmallTest |
| 28 @Feature({"ChromeToMobile", "Omaha"}) | 34 @Feature({"ChromeToMobile", "Omaha"}) |
| 29 public void testAndroidIdSuccessWithoutSalt() { | 35 public void testAndroidIdSuccessWithoutSalt() { |
| 30 String androidId = "42"; | 36 String androidId = "42"; |
| 31 String expected = HashUtil.getMd5Hash(new HashUtil.Params(androidId)); | 37 String expected = HashUtil.getMd5Hash(new HashUtil.Params(androidId)); |
| 32 runTest(androidId, null, expected); | 38 runTest(androidId, null, expected); |
| 33 } | 39 } |
| 34 | 40 |
| 41 @Test |
| 35 @SmallTest | 42 @SmallTest |
| 36 @Feature({"ChromeToMobile", "Omaha"}) | 43 @Feature({"ChromeToMobile", "Omaha"}) |
| 37 public void testAndroidIdFailureWithSalt() { | 44 public void testAndroidIdFailureWithSalt() { |
| 38 String androidId = null; | 45 String androidId = null; |
| 39 String salt = "mySalt"; | 46 String salt = "mySalt"; |
| 40 String expected = ""; | 47 String expected = ""; |
| 41 runTest(androidId, salt, expected); | 48 runTest(androidId, salt, expected); |
| 42 } | 49 } |
| 43 | 50 |
| 51 @Test |
| 44 @SmallTest | 52 @SmallTest |
| 45 @Feature({"ChromeToMobile", "Omaha"}) | 53 @Feature({"ChromeToMobile", "Omaha"}) |
| 46 public void testAndroidIdFailureWithoutSalt() { | 54 public void testAndroidIdFailureWithoutSalt() { |
| 47 String androidId = null; | 55 String androidId = null; |
| 48 String salt = null; | 56 String salt = null; |
| 49 String expected = ""; | 57 String expected = ""; |
| 50 runTest(androidId, salt, expected); | 58 runTest(androidId, salt, expected); |
| 51 } | 59 } |
| 52 | 60 |
| 53 private void runTest(String androidId, String salt, String expectedUniqueId)
{ | 61 private void runTest(String androidId, String salt, String expectedUniqueId)
{ |
| 54 AdvancedMockContext context = new AdvancedMockContext(); | 62 AdvancedMockContext context = new AdvancedMockContext(); |
| 55 TestGenerator generator = new TestGenerator(context, androidId); | 63 TestGenerator generator = new TestGenerator(context, androidId); |
| 56 | 64 |
| 57 // Get a unique ID and ensure it is as expected. | 65 // Get a unique ID and ensure it is as expected. |
| 58 String result = generator.getUniqueId(salt); | 66 String result = generator.getUniqueId(salt); |
| 59 assertEquals(expectedUniqueId, result); | 67 Assert.assertEquals(expectedUniqueId, result); |
| 60 } | 68 } |
| 61 | 69 |
| 62 private static class TestGenerator extends SettingsSecureBasedIdentification
Generator { | 70 private static class TestGenerator extends SettingsSecureBasedIdentification
Generator { |
| 63 private final AdvancedMockContext mContext; | 71 private final AdvancedMockContext mContext; |
| 64 private final String mAndroidId; | 72 private final String mAndroidId; |
| 65 | 73 |
| 66 TestGenerator(AdvancedMockContext context, String androidId) { | 74 TestGenerator(AdvancedMockContext context, String androidId) { |
| 67 super(context); | 75 super(context); |
| 68 mContext = context; | 76 mContext = context; |
| 69 mAndroidId = androidId; | 77 mAndroidId = androidId; |
| 70 } | 78 } |
| 71 | 79 |
| 72 @Override | 80 @Override |
| 73 String getAndroidId() { | 81 String getAndroidId() { |
| 74 mContext.setFlag(FLAG_ANDROID_ID); | 82 mContext.setFlag(FLAG_ANDROID_ID); |
| 75 return mAndroidId; | 83 return mAndroidId; |
| 76 } | 84 } |
| 77 } | 85 } |
| 78 } | 86 } |
| OLD | NEW |