Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.email; |
| 18 | |
Ben Komalo | 505ac4b | 2011-06-21 18:16:26 -0700 | [diff] [blame] | 19 | import android.app.admin.DevicePolicyManager; |
| 20 | import android.content.Context; |
| 21 | import android.content.ContextWrapper; |
| 22 | import android.test.ProviderTestCase2; |
| 23 | import android.test.suitebuilder.annotation.MediumTest; |
| 24 | import android.test.suitebuilder.annotation.SmallTest; |
| 25 | |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 26 | import com.android.email.provider.ContentCache; |
Marc Blank | 9ba506c | 2011-02-08 18:54:56 -0800 | [diff] [blame] | 27 | import com.android.email.provider.EmailProvider; |
| 28 | import com.android.email.provider.ProviderTestUtils; |
Marc Blank | f5418f1 | 2011-06-13 15:32:27 -0700 | [diff] [blame] | 29 | import com.android.emailcommon.provider.Account; |
Marc Blank | a7bc031 | 2011-02-10 10:26:56 -0800 | [diff] [blame] | 30 | import com.android.emailcommon.provider.EmailContent; |
Marc Blank | a7bc031 | 2011-02-10 10:26:56 -0800 | [diff] [blame] | 31 | import com.android.emailcommon.provider.EmailContent.Message; |
Ben Komalo | 53ea83e | 2011-05-13 17:26:27 -0700 | [diff] [blame] | 32 | import com.android.emailcommon.provider.Mailbox; |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 33 | import com.android.emailcommon.provider.Policy; |
| 34 | import com.android.emailcommon.service.LegacyPolicySet; |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 35 | |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 36 | /** |
| 37 | * This is a series of unit tests for backup/restore of the SecurityPolicy class. |
Marc Blank | 2b2b344 | 2010-08-02 13:47:23 -0700 | [diff] [blame] | 38 | * |
Marc Blank | 9b4988d | 2010-06-09 16:18:57 -0700 | [diff] [blame] | 39 | * You can run this entire test case with: |
| 40 | * runtest -c com.android.email.SecurityPolicyTests email |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 41 | */ |
Marc Blank | 9b4988d | 2010-06-09 16:18:57 -0700 | [diff] [blame] | 42 | |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 43 | @MediumTest |
| 44 | public class SecurityPolicyTests extends ProviderTestCase2<EmailProvider> { |
| 45 | |
| 46 | private Context mMockContext; |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 47 | private SecurityPolicy mSecurityPolicy; |
Andrew Stadler | d628608 | 2010-02-01 16:48:16 -0800 | [diff] [blame] | 48 | |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 49 | public SecurityPolicyTests() { |
Marc Blank | 31d9acb | 2011-02-11 15:05:17 -0800 | [diff] [blame] | 50 | super(EmailProvider.class, EmailContent.AUTHORITY); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 53 | private static final Policy EMPTY_POLICY = new Policy(); |
| 54 | |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 55 | @Override |
| 56 | protected void setUp() throws Exception { |
| 57 | super.setUp(); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 58 | mMockContext = new MockContext2(getMockContext(), mContext); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 59 | // Invalidate all caches, since we reset the database for each test |
Marc Blank | 6e418aa | 2011-06-18 18:03:11 -0700 | [diff] [blame] | 60 | ContentCache.invalidateAllCaches(); |
Ben Komalo | cb1d65c | 2011-07-25 16:04:07 -0700 | [diff] [blame] | 61 | Controller.getInstance(mMockContext).markForTest(true); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Delete any dummy accounts we set up for this test |
| 66 | */ |
| 67 | @Override |
| 68 | protected void tearDown() throws Exception { |
Ben Komalo | cb1d65c | 2011-07-25 16:04:07 -0700 | [diff] [blame] | 69 | Controller.getInstance(mMockContext).markForTest(false); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 70 | super.tearDown(); |
| 71 | } |
| 72 | |
| 73 | /** |
Makoto Onuki | 0fb092b | 2010-06-04 15:26:11 -0700 | [diff] [blame] | 74 | * Private context wrapper used to add back getPackageName() for these tests. |
| 75 | * |
Andy Stadler | a0d0805 | 2011-01-19 11:40:48 -0800 | [diff] [blame] | 76 | * This class also implements {@link Context} method(s) that are called during tests. |
Andrew Stadler | d628608 | 2010-02-01 16:48:16 -0800 | [diff] [blame] | 77 | */ |
| 78 | private static class MockContext2 extends ContextWrapper { |
| 79 | |
| 80 | private final Context mRealContext; |
| 81 | |
| 82 | public MockContext2(Context mockContext, Context realContext) { |
| 83 | super(mockContext); |
| 84 | mRealContext = realContext; |
| 85 | } |
| 86 | |
| 87 | @Override |
Makoto Onuki | 0fb092b | 2010-06-04 15:26:11 -0700 | [diff] [blame] | 88 | public Context getApplicationContext() { |
| 89 | return this; |
| 90 | } |
| 91 | |
| 92 | @Override |
Andrew Stadler | d628608 | 2010-02-01 16:48:16 -0800 | [diff] [blame] | 93 | public String getPackageName() { |
| 94 | return mRealContext.getPackageName(); |
| 95 | } |
Andy Stadler | a0d0805 | 2011-01-19 11:40:48 -0800 | [diff] [blame] | 96 | |
| 97 | @Override |
| 98 | public Object getSystemService(String name) { |
| 99 | return mRealContext.getSystemService(name); |
| 100 | } |
Andrew Stadler | d628608 | 2010-02-01 16:48:16 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 104 | * Create a Policy using the arguments formerly used to create a PolicySet; this minimizes the |
| 105 | * changes needed for re-using the PolicySet unit test logic |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 106 | */ |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 107 | private Policy setupPolicy(int minPasswordLength, int passwordMode, int maxPasswordFails, |
| 108 | int maxScreenLockTime, boolean requireRemoteWipe, int passwordExpirationDays, |
| 109 | int passwordHistory, int passwordComplexChars, boolean requireEncryption, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 110 | boolean dontAllowCamera) |
Ben Komalo | d09cff0 | 2011-05-06 14:57:47 -0700 | [diff] [blame] | 111 | throws IllegalArgumentException { |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 112 | Policy policy = new Policy(); |
| 113 | policy.mPasswordMinLength = minPasswordLength; |
| 114 | policy.mPasswordMode = passwordMode; |
| 115 | policy.mPasswordMaxFails = maxPasswordFails; |
| 116 | policy.mMaxScreenLockTime = maxScreenLockTime; |
| 117 | policy.mRequireRemoteWipe = requireRemoteWipe; |
| 118 | policy.mPasswordExpirationDays = passwordExpirationDays; |
| 119 | policy.mPasswordHistory = passwordHistory; |
| 120 | policy.mPasswordComplexChars = passwordComplexChars; |
| 121 | policy.mRequireEncryption = requireEncryption; |
Ben Komalo | d09cff0 | 2011-05-06 14:57:47 -0700 | [diff] [blame] | 122 | policy.mDontAllowCamera = dontAllowCamera; |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 123 | return policy; |
Marc Blank | 1d6dab2 | 2010-06-14 12:24:02 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 126 | /** |
| 127 | * Test business logic of aggregating accounts with policies |
| 128 | */ |
| 129 | public void testAggregator() { |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 130 | mSecurityPolicy = SecurityPolicy.getInstance(mMockContext); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 131 | |
Andrew Stadler | d628608 | 2010-02-01 16:48:16 -0800 | [diff] [blame] | 132 | // with no accounts, should return empty set |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 133 | assertEquals(EMPTY_POLICY, mSecurityPolicy.computeAggregatePolicy()); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 134 | |
Andrew Stadler | d628608 | 2010-02-01 16:48:16 -0800 | [diff] [blame] | 135 | // with accounts having no security, empty set |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 136 | ProviderTestUtils.setupAccount("no-sec-1", true, mMockContext); |
| 137 | ProviderTestUtils.setupAccount("no-sec-2", true, mMockContext); |
| 138 | assertEquals(EMPTY_POLICY, mSecurityPolicy.computeAggregatePolicy()); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 139 | |
| 140 | // with a single account in security mode, should return same security as in account |
Andrew Stadler | 3d2b3b3 | 2010-02-05 11:10:39 -0800 | [diff] [blame] | 141 | // first test with partially-populated policies |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 142 | Account a3 = ProviderTestUtils.setupAccount("sec-3", true, mMockContext); |
| 143 | Policy p3ain = setupPolicy(10, Policy.PASSWORD_MODE_SIMPLE, 0, 0, false, 0, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 144 | false, false); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 145 | SecurityPolicy.setAccountPolicy(mMockContext, a3, p3ain, null); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 146 | Policy p3aout = mSecurityPolicy.computeAggregatePolicy(); |
Andrew Stadler | 3d2b3b3 | 2010-02-05 11:10:39 -0800 | [diff] [blame] | 147 | assertNotNull(p3aout); |
| 148 | assertEquals(p3ain, p3aout); |
| 149 | |
| 150 | // Repeat that test with fully-populated policies |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 151 | Policy p3bin = setupPolicy(10, Policy.PASSWORD_MODE_SIMPLE, 15, 16, false, 6, 2, 3, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 152 | false, false); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 153 | SecurityPolicy.setAccountPolicy(mMockContext, a3, p3bin, null); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 154 | Policy p3bout = mSecurityPolicy.computeAggregatePolicy(); |
Andrew Stadler | 3d2b3b3 | 2010-02-05 11:10:39 -0800 | [diff] [blame] | 155 | assertNotNull(p3bout); |
| 156 | assertEquals(p3bin, p3bout); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 157 | |
| 158 | // add another account which mixes it up (some fields will change, others will not) |
| 159 | // pw length and pw mode - max logic - will change because larger #s here |
| 160 | // fail count and lock timer - min logic - will *not* change because larger #s here |
| 161 | // wipe required - OR logic - will *not* change here because false |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 162 | // expiration - will not change because 0 (unspecified) |
| 163 | // max complex chars - max logic - will change |
Andy Stadler | 469f298 | 2011-01-13 13:12:55 -0800 | [diff] [blame] | 164 | // encryption required - OR logic - will *not* change here because false |
Ben Komalo | d09cff0 | 2011-05-06 14:57:47 -0700 | [diff] [blame] | 165 | // don't allow camera - OR logic - will change here because it's true |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 166 | Policy p4in = setupPolicy(20, Policy.PASSWORD_MODE_STRONG, 25, 26, false, 0, 5, 7, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 167 | false, true); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 168 | Account a4 = ProviderTestUtils.setupAccount("sec-4", true, mMockContext); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 169 | SecurityPolicy.setAccountPolicy(mMockContext, a4, p4in, null); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 170 | Policy p4out = mSecurityPolicy.computeAggregatePolicy(); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 171 | assertNotNull(p4out); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 172 | assertEquals(20, p4out.mPasswordMinLength); |
| 173 | assertEquals(Policy.PASSWORD_MODE_STRONG, p4out.mPasswordMode); |
| 174 | assertEquals(15, p4out.mPasswordMaxFails); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 175 | assertEquals(16, p4out.mMaxScreenLockTime); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 176 | assertEquals(6, p4out.mPasswordExpirationDays); |
Marc Blank | 9b4988d | 2010-06-09 16:18:57 -0700 | [diff] [blame] | 177 | assertEquals(5, p4out.mPasswordHistory); |
| 178 | assertEquals(7, p4out.mPasswordComplexChars); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 179 | assertFalse(p4out.mRequireRemoteWipe); |
Andy Stadler | 469f298 | 2011-01-13 13:12:55 -0800 | [diff] [blame] | 180 | assertFalse(p4out.mRequireEncryption); |
Andy Stadler | 7fd14be | 2011-03-02 16:41:05 -0800 | [diff] [blame] | 181 | assertFalse(p4out.mRequireEncryptionExternal); |
Ben Komalo | d09cff0 | 2011-05-06 14:57:47 -0700 | [diff] [blame] | 182 | assertTrue(p4out.mDontAllowCamera); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 183 | |
| 184 | // add another account which mixes it up (the remaining fields will change) |
| 185 | // pw length and pw mode - max logic - will *not* change because smaller #s here |
| 186 | // fail count and lock timer - min logic - will change because smaller #s here |
| 187 | // wipe required - OR logic - will change here because true |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 188 | // expiration time - min logic - will change because lower here |
| 189 | // history & complex chars - will not change because 0 (unspecified) |
Andy Stadler | 469f298 | 2011-01-13 13:12:55 -0800 | [diff] [blame] | 190 | // encryption required - OR logic - will change here because true |
Ben Komalo | d09cff0 | 2011-05-06 14:57:47 -0700 | [diff] [blame] | 191 | // don't allow camera - OR logic - will *not* change here because it's already true |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 192 | Policy p5in = setupPolicy(4, Policy.PASSWORD_MODE_SIMPLE, 5, 6, true, 1, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 193 | true, false); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 194 | Account a5 = ProviderTestUtils.setupAccount("sec-5", true, mMockContext); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 195 | SecurityPolicy.setAccountPolicy(mMockContext, a5, p5in, null); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 196 | Policy p5out = mSecurityPolicy.computeAggregatePolicy(); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 197 | assertNotNull(p5out); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 198 | assertEquals(20, p5out.mPasswordMinLength); |
| 199 | assertEquals(Policy.PASSWORD_MODE_STRONG, p5out.mPasswordMode); |
| 200 | assertEquals(5, p5out.mPasswordMaxFails); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 201 | assertEquals(6, p5out.mMaxScreenLockTime); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 202 | assertEquals(1, p5out.mPasswordExpirationDays); |
Andy Stadler | 469f298 | 2011-01-13 13:12:55 -0800 | [diff] [blame] | 203 | assertEquals(5, p5out.mPasswordHistory); |
| 204 | assertEquals(7, p5out.mPasswordComplexChars); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 205 | assertTrue(p5out.mRequireRemoteWipe); |
Andy Stadler | 7fd14be | 2011-03-02 16:41:05 -0800 | [diff] [blame] | 206 | assertFalse(p5out.mRequireEncryptionExternal); |
Ben Komalo | d09cff0 | 2011-05-06 14:57:47 -0700 | [diff] [blame] | 207 | assertTrue(p5out.mDontAllowCamera); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Ben Komalo | 505ac4b | 2011-06-21 18:16:26 -0700 | [diff] [blame] | 210 | private long assertAccountPolicyConsistent(long accountId, long oldKey) { |
| 211 | Account account = Account.restoreAccountWithId(mMockContext, accountId); |
| 212 | long policyKey = account.mPolicyKey; |
| 213 | |
| 214 | assertTrue(policyKey > 0); |
| 215 | |
| 216 | // Found a policy. Ensure it matches. |
| 217 | Policy policy = Policy.restorePolicyWithId(mMockContext, policyKey); |
| 218 | assertNotNull(policy); |
| 219 | assertEquals(account.mPolicyKey, policy.mId); |
| 220 | assertEquals( |
| 221 | accountId, |
| 222 | Policy.getAccountIdWithPolicyKey(mMockContext, policy.mId)); |
| 223 | |
| 224 | // Assert the old one isn't there. |
| 225 | if (oldKey > 0) { |
| 226 | assertNull("old policy not cleaned up", |
| 227 | Policy.restorePolicyWithId(mMockContext, oldKey)); |
| 228 | } |
| 229 | |
| 230 | return policyKey; |
| 231 | } |
| 232 | |
| 233 | @SmallTest |
| 234 | public void testSettingAccountPolicy() { |
| 235 | Account account = ProviderTestUtils.setupAccount("testaccount", true, mMockContext); |
| 236 | long accountId = account.mId; |
| 237 | Policy initial = setupPolicy(10, Policy.PASSWORD_MODE_SIMPLE, 0, 0, false, 0, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 238 | false, false); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 239 | SecurityPolicy.setAccountPolicy(mMockContext, account, initial, null); |
Ben Komalo | 505ac4b | 2011-06-21 18:16:26 -0700 | [diff] [blame] | 240 | |
| 241 | long oldKey = assertAccountPolicyConsistent(account.mId, 0); |
| 242 | |
| 243 | Policy updated = setupPolicy(10, Policy.PASSWORD_MODE_SIMPLE, 0, 0, false, 0, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 244 | false, false); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 245 | SecurityPolicy.setAccountPolicy(mMockContext, account, updated, null); |
Ben Komalo | 505ac4b | 2011-06-21 18:16:26 -0700 | [diff] [blame] | 246 | oldKey = assertAccountPolicyConsistent(account.mId, oldKey); |
| 247 | |
| 248 | // Remove the policy |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 249 | SecurityPolicy.clearAccountPolicy( |
Ben Komalo | 505ac4b | 2011-06-21 18:16:26 -0700 | [diff] [blame] | 250 | mMockContext, Account.restoreAccountWithId(mMockContext, accountId)); |
| 251 | assertNull("old policy not cleaned up", |
| 252 | Policy.restorePolicyWithId(mMockContext, oldKey)); |
| 253 | } |
| 254 | |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 255 | /** |
Marc Blank | 9b4988d | 2010-06-09 16:18:57 -0700 | [diff] [blame] | 256 | * Test equality. Note, the tests for inequality are poor, as each field should |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 257 | * be tested individually. |
| 258 | */ |
| 259 | @SmallTest |
Marc Blank | 9b4988d | 2010-06-09 16:18:57 -0700 | [diff] [blame] | 260 | public void testEquals() { |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 261 | Policy p1 = |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 262 | setupPolicy(1, Policy.PASSWORD_MODE_STRONG, 3, 4, true, 7, 8, 9, false, false); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 263 | Policy p2 = |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 264 | setupPolicy(1, Policy.PASSWORD_MODE_STRONG, 3, 4, true, 7, 8, 9, false, false); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 265 | Policy p3 = |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 266 | setupPolicy(2, Policy.PASSWORD_MODE_SIMPLE, 5, 6, true, 7, 8, 9, false, false); |
Ben Komalo | d09cff0 | 2011-05-06 14:57:47 -0700 | [diff] [blame] | 267 | Policy p4 = |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 268 | setupPolicy(1, Policy.PASSWORD_MODE_STRONG, 3, 4, true, 7, 8, 9, false, true); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 269 | assertTrue(p1.equals(p2)); |
| 270 | assertFalse(p2.equals(p3)); |
Ben Komalo | d09cff0 | 2011-05-06 14:57:47 -0700 | [diff] [blame] | 271 | assertFalse(p1.equals(p4)); |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Andrew Stadler | 2a5eeea | 2010-02-08 17:42:42 -0800 | [diff] [blame] | 274 | /** |
| 275 | * Test the API to set/clear policy hold flags in an account |
| 276 | */ |
| 277 | public void testSetClearHoldFlag() { |
Andrew Stadler | 2a5eeea | 2010-02-08 17:42:42 -0800 | [diff] [blame] | 278 | Account a1 = ProviderTestUtils.setupAccount("holdflag-1", false, mMockContext); |
| 279 | a1.mFlags = Account.FLAGS_NOTIFY_NEW_MAIL; |
| 280 | a1.save(mMockContext); |
| 281 | Account a2 = ProviderTestUtils.setupAccount("holdflag-2", false, mMockContext); |
Jim Shuma | 9e2ddca | 2010-03-16 18:08:52 -0700 | [diff] [blame] | 282 | a2.mFlags = Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_SECURITY_HOLD; |
Andrew Stadler | 2a5eeea | 2010-02-08 17:42:42 -0800 | [diff] [blame] | 283 | a2.save(mMockContext); |
| 284 | |
| 285 | // confirm clear until set |
| 286 | Account a1a = Account.restoreAccountWithId(mMockContext, a1.mId); |
| 287 | assertEquals(Account.FLAGS_NOTIFY_NEW_MAIL, a1a.mFlags); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 288 | SecurityPolicy.setAccountHoldFlag(mMockContext, a1, true); |
Andrew Stadler | 2a5eeea | 2010-02-08 17:42:42 -0800 | [diff] [blame] | 289 | assertEquals(Account.FLAGS_NOTIFY_NEW_MAIL | Account.FLAGS_SECURITY_HOLD, a1.mFlags); |
| 290 | Account a1b = Account.restoreAccountWithId(mMockContext, a1.mId); |
| 291 | assertEquals(Account.FLAGS_NOTIFY_NEW_MAIL | Account.FLAGS_SECURITY_HOLD, a1b.mFlags); |
| 292 | |
| 293 | // confirm set until cleared |
| 294 | Account a2a = Account.restoreAccountWithId(mMockContext, a2.mId); |
Jim Shuma | 9e2ddca | 2010-03-16 18:08:52 -0700 | [diff] [blame] | 295 | assertEquals(Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_SECURITY_HOLD, a2a.mFlags); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 296 | SecurityPolicy.setAccountHoldFlag(mMockContext, a2, false); |
Jim Shuma | 9e2ddca | 2010-03-16 18:08:52 -0700 | [diff] [blame] | 297 | assertEquals(Account.FLAGS_VIBRATE_ALWAYS, a2.mFlags); |
Andrew Stadler | 2a5eeea | 2010-02-08 17:42:42 -0800 | [diff] [blame] | 298 | Account a2b = Account.restoreAccountWithId(mMockContext, a2.mId); |
Jim Shuma | 9e2ddca | 2010-03-16 18:08:52 -0700 | [diff] [blame] | 299 | assertEquals(Account.FLAGS_VIBRATE_ALWAYS, a2b.mFlags); |
Andrew Stadler | 2a5eeea | 2010-02-08 17:42:42 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 303 | * Test the response to disabling DeviceAdmin status |
| 304 | */ |
| 305 | public void testDisableAdmin() { |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 306 | Account a1 = ProviderTestUtils.setupAccount("disable-1", true, mMockContext); |
| 307 | Policy p1 = setupPolicy(10, Policy.PASSWORD_MODE_SIMPLE, 0, 0, false, 0, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 308 | false, false); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 309 | SecurityPolicy.setAccountPolicy(mMockContext, a1, p1, "security-sync-key-1"); |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 310 | |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 311 | Account a2 = ProviderTestUtils.setupAccount("disable-2", true, mMockContext); |
| 312 | Policy p2 = setupPolicy(20, Policy.PASSWORD_MODE_STRONG, 25, 26, false, 0, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 313 | false, false); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 314 | SecurityPolicy.setAccountPolicy(mMockContext, a2, p2, "security-sync-key-2"); |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 315 | |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 316 | Account a3 = ProviderTestUtils.setupAccount("disable-3", true, mMockContext); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 317 | SecurityPolicy.clearAccountPolicy(mMockContext, a3); |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 318 | |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 319 | mSecurityPolicy = SecurityPolicy.getInstance(mMockContext); |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 320 | |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 321 | // Confirm that "enabling" device admin does not change security status (policy & sync key) |
| 322 | Policy before = mSecurityPolicy.getAggregatePolicy(); |
| 323 | mSecurityPolicy.onAdminEnabled(true); // "enabled" should not change anything |
| 324 | Policy after1 = mSecurityPolicy.getAggregatePolicy(); |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 325 | assertEquals(before, after1); |
| 326 | Account a1a = Account.restoreAccountWithId(mMockContext, a1.mId); |
| 327 | assertNotNull(a1a.mSecuritySyncKey); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 328 | assertTrue(a1a.mPolicyKey > 0); |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 329 | Account a2a = Account.restoreAccountWithId(mMockContext, a2.mId); |
| 330 | assertNotNull(a2a.mSecuritySyncKey); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 331 | assertTrue(a2a.mPolicyKey > 0); |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 332 | Account a3a = Account.restoreAccountWithId(mMockContext, a3.mId); |
| 333 | assertNull(a3a.mSecuritySyncKey); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 334 | assertTrue(a3a.mPolicyKey == 0); |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 335 | |
Marc Blank | 4e4aba9 | 2011-07-18 17:33:38 -0700 | [diff] [blame] | 336 | mSecurityPolicy.deleteSecuredAccounts(mMockContext); |
| 337 | Policy after2 = mSecurityPolicy.getAggregatePolicy(); |
| 338 | assertEquals(EMPTY_POLICY, after2); |
| 339 | Account a1b = Account.restoreAccountWithId(mMockContext, a1.mId); |
| 340 | assertNull(a1b); |
| 341 | Account a2b = Account.restoreAccountWithId(mMockContext, a2.mId); |
| 342 | assertNull(a2b); |
| 343 | Account a3b = Account.restoreAccountWithId(mMockContext, a3.mId); |
| 344 | assertNull(a3b.mSecuritySyncKey); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Test the scanner that finds expiring accounts |
| 349 | */ |
| 350 | public void testFindExpiringAccount() { |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 351 | ProviderTestUtils.setupAccount("expiring-1", true, mMockContext); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 352 | |
| 353 | // With no expiring accounts, this should return null. |
Andy Stadler | 7fd14be | 2011-03-02 16:41:05 -0800 | [diff] [blame] | 354 | long nextExpiringAccountId = SecurityPolicy.findShortestExpiration(mMockContext); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 355 | assertEquals(-1, nextExpiringAccountId); |
| 356 | |
| 357 | // Add a single expiring account |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 358 | Account a2 = |
| 359 | ProviderTestUtils.setupAccount("expiring-2", true, mMockContext); |
| 360 | Policy p2 = setupPolicy(20, Policy.PASSWORD_MODE_STRONG, 25, 26, false, 30, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 361 | false, true); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 362 | SecurityPolicy.setAccountPolicy(mMockContext, a2, p2, null); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 363 | |
| 364 | // The expiring account should be returned |
Andy Stadler | 7fd14be | 2011-03-02 16:41:05 -0800 | [diff] [blame] | 365 | nextExpiringAccountId = SecurityPolicy.findShortestExpiration(mMockContext); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 366 | assertEquals(a2.mId, nextExpiringAccountId); |
| 367 | |
| 368 | // Add an account with a longer expiration |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 369 | Account a3 = ProviderTestUtils.setupAccount("expiring-3", true, mMockContext); |
| 370 | Policy p3 = setupPolicy(20, Policy.PASSWORD_MODE_STRONG, 25, 26, false, 60, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 371 | false, true); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 372 | SecurityPolicy.setAccountPolicy(mMockContext, a3, p3, null); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 373 | |
| 374 | // The original expiring account (a2) should be returned |
Andy Stadler | 7fd14be | 2011-03-02 16:41:05 -0800 | [diff] [blame] | 375 | nextExpiringAccountId = SecurityPolicy.findShortestExpiration(mMockContext); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 376 | assertEquals(a2.mId, nextExpiringAccountId); |
| 377 | |
| 378 | // Add an account with a shorter expiration |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 379 | Account a4 = ProviderTestUtils.setupAccount("expiring-4", true, mMockContext); |
| 380 | Policy p4 = setupPolicy(20, Policy.PASSWORD_MODE_STRONG, 25, 26, false, 15, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 381 | false, true); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 382 | SecurityPolicy.setAccountPolicy(mMockContext, a4, p4, null); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 383 | |
| 384 | // The new expiring account (a4) should be returned |
Andy Stadler | 7fd14be | 2011-03-02 16:41:05 -0800 | [diff] [blame] | 385 | nextExpiringAccountId = SecurityPolicy.findShortestExpiration(mMockContext); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 386 | assertEquals(a4.mId, nextExpiringAccountId); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Lightweight subclass of the Controller class allows injection of mock context |
| 391 | */ |
| 392 | public static class TestController extends Controller { |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 393 | protected TestController(Context providerContext, Context systemContext) { |
| 394 | super(systemContext); |
| 395 | setProviderContext(providerContext); |
Ben Komalo | cb1d65c | 2011-07-25 16:04:07 -0700 | [diff] [blame] | 396 | markForTest(true); |
Marc Blank | 02d59d2 | 2010-10-25 11:49:29 -0700 | [diff] [blame] | 397 | } |
Andrew Stadler | af55e3e | 2010-02-16 14:33:08 -0800 | [diff] [blame] | 398 | } |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 399 | |
| 400 | /** |
| 401 | * Test the scanner that wipes expiring accounts |
| 402 | */ |
| 403 | public void testWipeExpiringAccounts() { |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 404 | mSecurityPolicy = SecurityPolicy.getInstance(mMockContext); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 405 | TestController testController = new TestController(mMockContext, getContext()); |
| 406 | |
| 407 | // Two accounts - a1 is normal, a2 has security (but no expiration) |
| 408 | Account a1 = ProviderTestUtils.setupAccount("expired-1", true, mMockContext); |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 409 | Account a2 = ProviderTestUtils.setupAccount("expired-2", true, mMockContext); |
| 410 | Policy p2 = setupPolicy(20, Policy.PASSWORD_MODE_STRONG, 25, 26, false, 0, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 411 | false, true); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 412 | SecurityPolicy.setAccountPolicy(mMockContext, a2, p2, null); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 413 | |
| 414 | // Add a mailbox & messages to each account |
| 415 | long account1Id = a1.mId; |
| 416 | long account2Id = a2.mId; |
| 417 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
| 418 | long box1Id = box1.mId; |
| 419 | ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, true, mMockContext); |
| 420 | ProviderTestUtils.setupMessage("message2", account1Id, box1Id, false, true, mMockContext); |
| 421 | Mailbox box2 = ProviderTestUtils.setupMailbox("box2", account2Id, true, mMockContext); |
| 422 | long box2Id = box2.mId; |
| 423 | ProviderTestUtils.setupMessage("message3", account2Id, box2Id, false, true, mMockContext); |
| 424 | ProviderTestUtils.setupMessage("message4", account2Id, box2Id, false, true, mMockContext); |
| 425 | |
| 426 | // Run the expiration code - should do nothing |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 427 | boolean wiped = SecurityPolicy.wipeExpiredAccounts(mMockContext, testController); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 428 | assertFalse(wiped); |
| 429 | // check mailboxes & messages not wiped |
| 430 | assertEquals(2, EmailContent.count(mMockContext, Account.CONTENT_URI)); |
| 431 | assertEquals(2, EmailContent.count(mMockContext, Mailbox.CONTENT_URI)); |
| 432 | assertEquals(4, EmailContent.count(mMockContext, Message.CONTENT_URI)); |
| 433 | |
| 434 | // Add 3rd account that really expires |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 435 | Account a3 = ProviderTestUtils.setupAccount("expired-3", true, mMockContext); |
| 436 | Policy p3 = setupPolicy(20, Policy.PASSWORD_MODE_STRONG, 25, 26, false, 30, 0, 0, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 437 | false, true); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 438 | SecurityPolicy.setAccountPolicy(mMockContext, a3, p3, null); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 439 | |
| 440 | // Add mailbox & messages to 3rd account |
| 441 | long account3Id = a3.mId; |
| 442 | Mailbox box3 = ProviderTestUtils.setupMailbox("box3", account3Id, true, mMockContext); |
| 443 | long box3Id = box3.mId; |
| 444 | ProviderTestUtils.setupMessage("message5", account3Id, box3Id, false, true, mMockContext); |
| 445 | ProviderTestUtils.setupMessage("message6", account3Id, box3Id, false, true, mMockContext); |
| 446 | |
| 447 | // check new counts |
| 448 | assertEquals(3, EmailContent.count(mMockContext, Account.CONTENT_URI)); |
| 449 | assertEquals(3, EmailContent.count(mMockContext, Mailbox.CONTENT_URI)); |
| 450 | assertEquals(6, EmailContent.count(mMockContext, Message.CONTENT_URI)); |
| 451 | |
| 452 | // Run the expiration code - wipe acct #3 |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 453 | wiped = SecurityPolicy.wipeExpiredAccounts(mMockContext, testController); |
Andy Stadler | 1ca111c | 2010-12-01 12:58:36 -0800 | [diff] [blame] | 454 | assertTrue(wiped); |
| 455 | // check new counts - account survives but data is wiped |
| 456 | assertEquals(3, EmailContent.count(mMockContext, Account.CONTENT_URI)); |
| 457 | assertEquals(2, EmailContent.count(mMockContext, Mailbox.CONTENT_URI)); |
| 458 | assertEquals(4, EmailContent.count(mMockContext, Message.CONTENT_URI)); |
| 459 | |
| 460 | // Check security hold states - only #3 should be in hold |
| 461 | Account account = Account.restoreAccountWithId(mMockContext, account1Id); |
| 462 | assertEquals(0, account.mFlags & Account.FLAGS_SECURITY_HOLD); |
| 463 | account = Account.restoreAccountWithId(mMockContext, account2Id); |
| 464 | assertEquals(0, account.mFlags & Account.FLAGS_SECURITY_HOLD); |
| 465 | account = Account.restoreAccountWithId(mMockContext, account3Id); |
| 466 | assertEquals(Account.FLAGS_SECURITY_HOLD, account.mFlags & Account.FLAGS_SECURITY_HOLD); |
| 467 | } |
Andy Stadler | a0d0805 | 2011-01-19 11:40:48 -0800 | [diff] [blame] | 468 | |
| 469 | /** |
Andy Stadler | 22759ba | 2011-03-16 09:48:08 -0700 | [diff] [blame] | 470 | * Test the code that converts from exchange-style quality to DPM/Lockscreen style quality. |
| 471 | */ |
| 472 | public void testGetDPManagerPasswordQuality() { |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 473 | // Policy.PASSWORD_MODE_NONE -> DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED |
| 474 | Policy p1 = setupPolicy(0, Policy.PASSWORD_MODE_NONE, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 475 | 0, 0, false, 0, 0, 0, false, false); |
Andy Stadler | 22759ba | 2011-03-16 09:48:08 -0700 | [diff] [blame] | 476 | assertEquals(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 477 | p1.getDPManagerPasswordQuality()); |
| 478 | |
| 479 | // PASSWORD_MODE_SIMPLE -> PASSWORD_QUALITY_NUMERIC |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 480 | Policy p2 = setupPolicy(4, Policy.PASSWORD_MODE_SIMPLE, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 481 | 0, 0, false, 0, 0, 0, false, false); |
Andy Stadler | 22759ba | 2011-03-16 09:48:08 -0700 | [diff] [blame] | 482 | assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC, |
| 483 | p2.getDPManagerPasswordQuality()); |
| 484 | |
| 485 | // PASSWORD_MODE_STRONG -> PASSWORD_QUALITY_ALPHANUMERIC |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 486 | Policy p3 = setupPolicy(4, Policy.PASSWORD_MODE_STRONG, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 487 | 0, 0, false, 0, 0, 0, false, false); |
Andy Stadler | 22759ba | 2011-03-16 09:48:08 -0700 | [diff] [blame] | 488 | assertEquals(DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC, |
| 489 | p3.getDPManagerPasswordQuality()); |
| 490 | |
| 491 | // PASSWORD_MODE_STRONG + complex chars -> PASSWORD_QUALITY_COMPLEX |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 492 | Policy p4 = setupPolicy(4, Policy.PASSWORD_MODE_STRONG, |
Ben Komalo | e76962b | 2011-07-01 12:34:03 -0700 | [diff] [blame] | 493 | 0, 0, false, 0, 0 , 2, false, false); |
Andy Stadler | 22759ba | 2011-03-16 09:48:08 -0700 | [diff] [blame] | 494 | assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, |
| 495 | p4.getDPManagerPasswordQuality()); |
| 496 | } |
Marc Blank | aeee10e | 2011-04-27 17:12:06 -0700 | [diff] [blame] | 497 | |
| 498 | private boolean policySetEqualsPolicy(PolicySet ps, Policy policy) { |
| 499 | if ((ps.mPasswordMode >> LegacyPolicySet.PASSWORD_MODE_SHIFT) != policy.mPasswordMode) { |
| 500 | return false; |
| 501 | } |
| 502 | if (ps.mMinPasswordLength != policy.mPasswordMinLength) return false; |
| 503 | if (ps.mPasswordComplexChars != policy.mPasswordComplexChars) return false; |
| 504 | if (ps.mPasswordHistory != policy.mPasswordHistory) return false; |
| 505 | if (ps.mPasswordExpirationDays != policy.mPasswordExpirationDays) return false; |
| 506 | if (ps.mMaxPasswordFails != policy.mPasswordMaxFails) return false; |
| 507 | if (ps.mMaxScreenLockTime != policy.mMaxScreenLockTime) return false; |
| 508 | if (ps.mRequireRemoteWipe != policy.mRequireRemoteWipe) return false; |
| 509 | if (ps.mRequireEncryption != policy.mRequireEncryption) return false; |
| 510 | if (ps.mRequireEncryptionExternal != policy.mRequireEncryptionExternal) return false; |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | public void testPolicyFlagsToPolicy() { |
| 515 | // Policy flags; the three sets included here correspond to policies for three test |
| 516 | // accounts that, between them, use all of the possible policies |
| 517 | long flags = 67096612L; |
| 518 | PolicySet ps = new PolicySet(flags); |
| 519 | Policy policy = LegacyPolicySet.flagsToPolicy(flags); |
| 520 | assertTrue(policySetEqualsPolicy(ps, policy)); |
| 521 | flags = 52776591691846L; |
| 522 | ps = new PolicySet(flags); |
| 523 | policy = LegacyPolicySet.flagsToPolicy(flags); |
| 524 | assertTrue(policySetEqualsPolicy(ps, policy)); |
| 525 | flags = 1689605957029924L; |
| 526 | ps = new PolicySet(flags); |
| 527 | policy = LegacyPolicySet.flagsToPolicy(flags); |
| 528 | assertTrue(policySetEqualsPolicy(ps, policy)); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * The old PolicySet class fields and constructor; we use this to test conversion to the |
| 533 | * new Policy table scheme |
| 534 | */ |
| 535 | private static class PolicySet { |
| 536 | private final int mMinPasswordLength; |
| 537 | private final int mPasswordMode; |
| 538 | private final int mMaxPasswordFails; |
| 539 | private final int mMaxScreenLockTime; |
| 540 | private final boolean mRequireRemoteWipe; |
| 541 | private final int mPasswordExpirationDays; |
| 542 | private final int mPasswordHistory; |
| 543 | private final int mPasswordComplexChars; |
| 544 | private final boolean mRequireEncryption; |
| 545 | private final boolean mRequireEncryptionExternal; |
| 546 | |
| 547 | /** |
| 548 | * Create from values encoded in an account flags int |
| 549 | */ |
| 550 | private PolicySet(long flags) { |
| 551 | mMinPasswordLength = (int) ((flags & LegacyPolicySet.PASSWORD_LENGTH_MASK) |
| 552 | >> LegacyPolicySet.PASSWORD_LENGTH_SHIFT); |
| 553 | mPasswordMode = |
| 554 | (int) (flags & LegacyPolicySet.PASSWORD_MODE_MASK); |
| 555 | mMaxPasswordFails = (int) ((flags & LegacyPolicySet.PASSWORD_MAX_FAILS_MASK) |
| 556 | >> LegacyPolicySet.PASSWORD_MAX_FAILS_SHIFT); |
| 557 | mMaxScreenLockTime = (int) ((flags & LegacyPolicySet.SCREEN_LOCK_TIME_MASK) |
| 558 | >> LegacyPolicySet.SCREEN_LOCK_TIME_SHIFT); |
| 559 | mRequireRemoteWipe = 0 != (flags & LegacyPolicySet.REQUIRE_REMOTE_WIPE); |
| 560 | mPasswordExpirationDays = (int) ((flags & LegacyPolicySet.PASSWORD_EXPIRATION_MASK) |
| 561 | >> LegacyPolicySet.PASSWORD_EXPIRATION_SHIFT); |
| 562 | mPasswordHistory = (int) ((flags & LegacyPolicySet.PASSWORD_HISTORY_MASK) |
| 563 | >> LegacyPolicySet.PASSWORD_HISTORY_SHIFT); |
| 564 | mPasswordComplexChars = (int) ((flags & LegacyPolicySet.PASSWORD_COMPLEX_CHARS_MASK) |
| 565 | >> LegacyPolicySet.PASSWORD_COMPLEX_CHARS_SHIFT); |
| 566 | mRequireEncryption = 0 != (flags & LegacyPolicySet.REQUIRE_ENCRYPTION); |
| 567 | mRequireEncryptionExternal = 0 != (flags & LegacyPolicySet.REQUIRE_ENCRYPTION_EXTERNAL); |
| 568 | } |
| 569 | } |
Andrew Stadler | 345fb8b | 2010-01-26 17:24:15 -0800 | [diff] [blame] | 570 | } |