1 /* 2 * Copyright (C) 2014 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.exchange.eas; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.telephony.TelephonyManager; 22 import android.test.suitebuilder.annotation.SmallTest; 23 24 import com.android.emailcommon.provider.EmailContent; 25 import com.android.exchange.adapter.Serializer; 26 import com.android.exchange.Eas; 27 import com.android.exchange.adapter.Tags; 28 import com.android.exchange.utility.ExchangeTestCase; 29 30 import java.io.IOException; 31 import java.util.Arrays; 32 33 /** 34 * You can run this entire test case with: 35 * runtest -c com.android.exchange.eas.EasProvisionTests exchange 36 */ 37 @SmallTest 38 public class EasProvisionTests extends ExchangeTestCase { 39 40 /** 41 * This test case will test PHASE_INITIAL along with a protocol version of Ex2007. 42 */ testPopulateRequestEntitySerializerPhaseInitialEx2007()43 public void testPopulateRequestEntitySerializerPhaseInitialEx2007() throws IOException { 44 // Set up some parameters for the test case 45 final String policyType = "Test_Policy"; 46 final String userAgent = "User_Agent"; 47 final String status = "Test_Status"; 48 final String policyKey = "Test_Policy_Key"; 49 final int phase = EasProvision.PHASE_INITIAL; 50 final double protocolVersion = Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE; 51 52 // Build the result that we are expecting 53 final Serializer expectedResult = new Serializer(); 54 expectedResult.start(Tags.PROVISION_PROVISION); 55 expectedResult.start(Tags.PROVISION_POLICIES); 56 expectedResult.start(Tags.PROVISION_POLICY); 57 expectedResult.data(Tags.PROVISION_POLICY_TYPE, policyType); 58 // PROVISION_POLICY, PROVISION_POLICIES, PROVISION_PROVISION 59 expectedResult.end().end().end().done(); 60 final byte[] expectedBytes = expectedResult.toByteArray(); 61 62 // Now run it through the code that we are testing 63 final Serializer generatedResult = EasProvision.generateRequestEntitySerializer( 64 mContext, userAgent, policyKey, policyType, status, phase, protocolVersion); 65 66 // Now let's analyze the results 67 assertTrue(Arrays.equals(generatedResult.toByteArray(), expectedBytes)); 68 } 69 70 /** 71 * This test case will test PHASE_INITIAL along with a protocol version of Ex2010. 72 */ testPopulateRequestEntitySerializerPhaseInitialEx2010()73 public void testPopulateRequestEntitySerializerPhaseInitialEx2010() throws IOException { 74 // Set up some parameters for the test case 75 final String policyType = "Test_Policy"; 76 final String userAgent = "User_Agent"; 77 final String status = "Test_Status"; 78 final String policyKey = "Test_Policy_Key"; 79 final int phase = EasProvision.PHASE_INITIAL; 80 final double protocolVersion = Eas.SUPPORTED_PROTOCOL_EX2010_SP1_DOUBLE; 81 82 // Build the result that we are expecting 83 final Serializer expectedResult = new Serializer(); 84 expectedResult.start(Tags.PROVISION_PROVISION); 85 EasProvision.expandedAddDeviceInformationToSerializer(expectedResult, mContext, userAgent); 86 expectedResult.start(Tags.PROVISION_POLICIES); 87 expectedResult.start(Tags.PROVISION_POLICY); 88 expectedResult.data(Tags.PROVISION_POLICY_TYPE, policyType); 89 // PROVISION_POLICY, PROVISION_POLICIES, PROVISION_PROVISION 90 expectedResult.end().end().end().done(); 91 final byte[] expectedBytes = expectedResult.toByteArray(); 92 93 // Now run it through the code that we are testing 94 final Serializer generatedResult = EasProvision.generateRequestEntitySerializer( 95 mContext, userAgent, policyKey, policyType, status, phase, protocolVersion); 96 97 // Now let's analyze the results 98 assertTrue(Arrays.equals(generatedResult.toByteArray(), expectedBytes)); 99 } 100 101 /** 102 * This test case will test PHASE_WIPE. 103 */ testPopulateRequestEntitySerializerPhaseWipe()104 public void testPopulateRequestEntitySerializerPhaseWipe() throws IOException { 105 // Set up some parameters for the test case 106 final String policyType = "Test_Policy"; 107 final String userAgent = "User_Agent"; 108 final String status = "Test_Status"; 109 final String policyKey = "Test_Policy_Key"; 110 final int phase = EasProvision.PHASE_WIPE; 111 final double protocolVersion = Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE; 112 113 // Build the result that we are expecting 114 final Serializer expectedResult = new Serializer(); 115 expectedResult.start(Tags.PROVISION_PROVISION); 116 expectedResult.start(Tags.PROVISION_REMOTE_WIPE); 117 expectedResult.data(Tags.PROVISION_STATUS, EasProvision.PROVISION_STATUS_OK); 118 expectedResult.end().end().done(); // PROVISION_REMOTE_WIPE, PROVISION_PROVISION 119 final byte[] expectedBytes = expectedResult.toByteArray(); 120 121 // Now run it through the code that we are testing 122 final Serializer generatedResult = EasProvision.generateRequestEntitySerializer( 123 mContext, userAgent, policyKey, policyType, status, phase, protocolVersion); 124 125 // Now let's analyze the results 126 assertTrue(Arrays.equals(generatedResult.toByteArray(), expectedBytes)); 127 } 128 129 /** 130 * This test case will test PHASE_ACKNOWLEDGE. 131 */ testPopulateRequestEntitySerializerPhaseAcknowledge()132 public void testPopulateRequestEntitySerializerPhaseAcknowledge() throws IOException { 133 // Set up some parameters for the test case 134 final String policyType = "Test_Policy"; 135 final String userAgent = "User_Agent"; 136 final String status = "Test_Status"; 137 final String policyKey = "Test_Policy_Key"; 138 final int phase = EasProvision.PHASE_ACKNOWLEDGE; 139 final double protocolVersion = Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE; 140 141 // Build the result that we are expecting 142 final Serializer expectedResult = new Serializer(); 143 expectedResult.start(Tags.PROVISION_PROVISION); 144 expectedResult.start(Tags.PROVISION_POLICIES); 145 expectedResult.start(Tags.PROVISION_POLICY); 146 expectedResult.data(Tags.PROVISION_POLICY_TYPE, policyType); 147 expectedResult.data(Tags.PROVISION_POLICY_KEY, policyKey); 148 expectedResult.data(Tags.PROVISION_STATUS, status); 149 // PROVISION_POLICY, PROVISION_POLICIES, PROVISION_PROVISION 150 expectedResult.end().end().end().done(); 151 final byte[] expectedBytes = expectedResult.toByteArray(); 152 153 // Now run it through the code that we are testing 154 final Serializer generatedResult = EasProvision.generateRequestEntitySerializer( 155 mContext, userAgent, policyKey, policyType, status, phase, protocolVersion); 156 157 // Now let's analyze the results 158 assertTrue(Arrays.equals(generatedResult.toByteArray(), expectedBytes)); 159 } 160 161 } 162