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