Home | History | Annotate | Download | only in provider
      1 /*
      2  * Copyright (C) 2011 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.provider;
     18 
     19 import com.android.emailcommon.provider.Account;
     20 import com.android.emailcommon.provider.EmailContent;
     21 import com.android.emailcommon.provider.EmailContent.Attachment;
     22 import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
     23 import com.android.emailcommon.provider.EmailContent.Message;
     24 import com.android.emailcommon.provider.Mailbox;
     25 import com.android.emailcommon.provider.Policy;
     26 
     27 import android.content.Context;
     28 import android.os.Parcel;
     29 import android.test.ProviderTestCase2;
     30 import android.test.suitebuilder.annotation.MediumTest;
     31 
     32 import java.util.ArrayList;
     33 
     34 /**
     35  * This is a series of unit tests for the Policy class
     36  *
     37  * You can run this entire test case with:
     38  *   runtest -c com.android.email.provider.PolicyTests email
     39  */
     40 
     41 @MediumTest
     42 public class PolicyTests extends ProviderTestCase2<EmailProvider> {
     43 
     44     private static final String CANT_DOWNLOAD_SELECTION = "(" + AttachmentColumns.FLAGS + "&" +
     45         Attachment.FLAG_POLICY_DISALLOWS_DOWNLOAD + ")!=0";
     46 
     47     private Context mMockContext;
     48 
     49     public PolicyTests() {
     50         super(EmailProvider.class, EmailContent.AUTHORITY);
     51     }
     52 
     53     @Override
     54     public void setUp() throws Exception {
     55         super.setUp();
     56         mMockContext = getMockContext();
     57         // Invalidate all caches, since we reset the database for each test
     58         ContentCache.invalidateAllCaches();
     59     }
     60 
     61     @Override
     62     public void tearDown() throws Exception {
     63         super.tearDown();
     64     }
     65 
     66     public void testGetAccountIdWithPolicyKey() {
     67         String securitySyncKey = "key";
     68         // Setup two accounts with policies
     69         Account account1 = ProviderTestUtils.setupAccount("acct1", true, mMockContext);
     70         Policy policy1 = new Policy();
     71         Policy.setAccountPolicy(mMockContext, account1, policy1, securitySyncKey);
     72         Account account2 = ProviderTestUtils.setupAccount("acct2", true, mMockContext);
     73         Policy policy2 = new Policy();
     74         Policy.setAccountPolicy(mMockContext, account2, policy2, securitySyncKey);
     75         // Get the accounts back from the database
     76         account1.refresh(mMockContext);
     77         account2.refresh(mMockContext);
     78         // Both should have valid policies
     79         assertTrue(account1.mPolicyKey > 0);
     80         // And they should be findable via getAccountIdWithPolicyKey
     81         assertTrue(account2.mPolicyKey > 0);
     82         assertEquals(account1.mId, Policy.getAccountIdWithPolicyKey(mMockContext,
     83                 account1.mPolicyKey));
     84         assertEquals(account2.mId, Policy.getAccountIdWithPolicyKey(mMockContext,
     85                 account2.mPolicyKey));
     86     }
     87 
     88     public void testSetAndClearAccountPolicy() {
     89         String securitySyncKey = "key";
     90         Account account = ProviderTestUtils.setupAccount("acct", true, mMockContext);
     91         // Nothing up my sleeve
     92         assertEquals(0, account.mPolicyKey);
     93         assertEquals(0, EmailContent.count(mMockContext, Policy.CONTENT_URI));
     94         Policy policy = new Policy();
     95         Policy.setAccountPolicy(mMockContext, account, policy, securitySyncKey);
     96         account.refresh(mMockContext);
     97         // We should have a policyKey now
     98         assertTrue(account.mPolicyKey > 0);
     99         Policy dbPolicy = Policy.restorePolicyWithId(mMockContext, account.mPolicyKey);
    100         // The policy should exist in the database
    101         assertNotNull(dbPolicy);
    102         // And it should be the same as the original
    103         assertEquals(policy, dbPolicy);
    104         // The account should have the security sync key set
    105         assertEquals(securitySyncKey, account.mSecuritySyncKey);
    106         Policy.clearAccountPolicy(mMockContext, account);
    107         account.refresh(mMockContext);
    108         // Make sure policyKey is cleared and policy is deleted
    109         assertEquals(0, account.mPolicyKey);
    110         assertEquals(0, EmailContent.count(mMockContext, Policy.CONTENT_URI));
    111         account.refresh(mMockContext);
    112         // The account's security sync key should also be null
    113         assertNull(account.mSecuritySyncKey);
    114     }
    115 
    116     private Attachment setupSimpleAttachment(String name, long size, Account acct) {
    117         Attachment att = ProviderTestUtils.setupAttachment(-1, name, size, false, mMockContext);
    118         att.mAccountKey = acct.mId;
    119         return att;
    120     }
    121     public void testSetAttachmentFlagsForNewPolicy() {
    122         Account acct = ProviderTestUtils.setupAccount("acct1", true, mMockContext);
    123         Policy policy1 = new Policy();
    124         policy1.mDontAllowAttachments = true;
    125         Policy.setAccountPolicy(mMockContext, acct, policy1, null);
    126         Mailbox box = ProviderTestUtils.setupMailbox("box1", acct.mId, true, mMockContext);
    127         Message msg1 = ProviderTestUtils.setupMessage("message1", acct.mId, box.mId, false, false,
    128                 mMockContext);
    129         ArrayList<Attachment> atts = new ArrayList<Attachment>();
    130         Attachment att1 = setupSimpleAttachment("fileName1", 10001L, acct);
    131         atts.add(att1);
    132         Attachment att2 = setupSimpleAttachment("fileName2", 20001L, acct);
    133         atts.add(att2);
    134         msg1.mAttachments = atts;
    135         msg1.save(mMockContext);
    136         Message msg2 = ProviderTestUtils.setupMessage("message2", acct.mId, box.mId, false, false,
    137                 mMockContext);
    138         atts.clear();
    139         Attachment att3 = setupSimpleAttachment("fileName3", 70001L, acct);
    140         atts.add(att3);
    141         Attachment att4 = setupSimpleAttachment("fileName4", 5001L, acct);
    142         atts.add(att4);
    143         msg2.mAttachments = atts;
    144         msg2.save(mMockContext);
    145         // Make sure we've got our 4 attachments
    146         assertEquals(4, EmailContent.count(mMockContext, Attachment.CONTENT_URI));
    147         // All should be downloadable
    148         assertEquals(0, EmailContent.count(mMockContext, Attachment.CONTENT_URI,
    149                 CANT_DOWNLOAD_SELECTION, null));
    150         // Enforce our no-attachments policy
    151         Policy.setAttachmentFlagsForNewPolicy(mMockContext, acct, policy1);
    152         // None should be downloadable
    153         assertEquals(4, EmailContent.count(mMockContext, Attachment.CONTENT_URI,
    154                 CANT_DOWNLOAD_SELECTION, null));
    155 
    156         Policy policy2 = new Policy();
    157         policy2.mMaxAttachmentSize = 20000;
    158         // Switch to new policy that sets a limit, but otherwise allows attachments
    159         Policy.setAttachmentFlagsForNewPolicy(mMockContext, acct, policy2);
    160         // Two shouldn't be downloadable
    161         assertEquals(2, EmailContent.count(mMockContext, Attachment.CONTENT_URI,
    162                 CANT_DOWNLOAD_SELECTION, null));
    163         // Make sure they're the right ones (att2 and att3)
    164         att2 = Attachment.restoreAttachmentWithId(mMockContext, att2.mId);
    165         assertTrue((att2.mFlags & Attachment.FLAG_POLICY_DISALLOWS_DOWNLOAD) != 0);
    166         att3 = Attachment.restoreAttachmentWithId(mMockContext, att3.mId);
    167         assertTrue((att3.mFlags & Attachment.FLAG_POLICY_DISALLOWS_DOWNLOAD) != 0);
    168 
    169         Policy policy3 = new Policy();
    170         policy3.mMaxAttachmentSize = 5001;
    171         // Switch to new policy that sets a lower limit
    172         Policy.setAttachmentFlagsForNewPolicy(mMockContext, acct, policy3);
    173         // Three shouldn't be downloadable
    174         assertEquals(3, EmailContent.count(mMockContext, Attachment.CONTENT_URI,
    175                 CANT_DOWNLOAD_SELECTION, null));
    176         // Make sure the right one is downloadable
    177         att4 = Attachment.restoreAttachmentWithId(mMockContext, att4.mId);
    178         assertTrue((att4.mFlags & Attachment.FLAG_POLICY_DISALLOWS_DOWNLOAD) == 0);
    179 
    180         Policy policy4 = new Policy();
    181         // Switch to new policy that is without restrictions
    182         Policy.setAttachmentFlagsForNewPolicy(mMockContext, acct, policy4);
    183         // Nothing should be blocked now
    184         assertEquals(0, EmailContent.count(mMockContext, Attachment.CONTENT_URI,
    185                 CANT_DOWNLOAD_SELECTION, null));
    186    }
    187 
    188     public void testParcel() {
    189         Policy policy = new Policy();
    190         policy.mPasswordMode = Policy.PASSWORD_MODE_STRONG;
    191         policy.mPasswordMinLength = 6;
    192         policy.mPasswordComplexChars = 5;
    193         policy.mPasswordExpirationDays = 4;
    194         policy.mPasswordHistory = 3;
    195         policy.mPasswordMaxFails = 8;
    196         policy.mMaxScreenLockTime = 600;
    197         policy.mRequireRemoteWipe = true;
    198         policy.mRequireEncryption = true;
    199         policy.mRequireEncryptionExternal = true;
    200         policy.mRequireManualSyncWhenRoaming = true;
    201         policy.mDontAllowCamera = false;
    202         policy.mDontAllowAttachments = true;
    203         policy.mDontAllowHtml = false;
    204         policy.mMaxAttachmentSize = 22222;
    205         policy.mMaxTextTruncationSize = 33333;
    206         policy.mMaxHtmlTruncationSize = 44444;
    207         policy.mMaxEmailLookback = 5;
    208         policy.mMaxCalendarLookback = 6;
    209         policy.mPasswordRecoveryEnabled = true;
    210         Parcel parcel = Parcel.obtain();
    211         policy.writeToParcel(parcel, 0);
    212         parcel.setDataPosition(0);
    213         Policy readPolicy = Policy.CREATOR.createFromParcel(parcel);
    214         assertEquals(policy, readPolicy);
    215     }
    216 }
    217