EasSyncServiceTests.java revision 8eeede0d74ba2b139ce96d883541f12a562cc17e
1/*
2 * Copyright (C) 2009 Marc Blank
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.exchange;
19
20import com.android.email.provider.EmailContent.Account;
21
22import org.apache.http.Header;
23import org.apache.http.client.methods.HttpPost;
24import org.apache.http.client.methods.HttpRequestBase;
25
26import android.content.Context;
27import android.test.AndroidTestCase;
28import android.util.Base64;
29
30import java.io.File;
31import java.io.IOException;
32
33/**
34 * You can run this entire test case with:
35 *   runtest -c com.android.exchange.EasSyncServiceTests email
36 */
37
38public class EasSyncServiceTests extends AndroidTestCase {
39    static private final String USER = "user";
40    static private final String PASSWORD = "password";
41    static private final String HOST = "xxx.host.zzz";
42    static private final String ID = "id";
43    static private final String TYPE = "type";
44
45    Context mMockContext;
46
47    @Override
48    public void setUp() throws Exception {
49        super.setUp();
50        mMockContext = getContext();
51    }
52
53   /**
54     * Test that our unique file name algorithm works as expected.
55     * @throws IOException
56     */
57    public void testCreateUniqueFile() throws IOException {
58        // Delete existing files, if they exist
59        EasSyncService svc = new EasSyncService();
60        svc.mContext = mMockContext;
61        try {
62            String fileName = "A11achm3n1.doc";
63            File uniqueFile = svc.createUniqueFileInternal(null, fileName);
64            assertEquals(fileName, uniqueFile.getName());
65            if (uniqueFile.createNewFile()) {
66                uniqueFile = svc.createUniqueFileInternal(null, fileName);
67                assertEquals("A11achm3n1-2.doc", uniqueFile.getName());
68                if (uniqueFile.createNewFile()) {
69                    uniqueFile = svc.createUniqueFileInternal(null, fileName);
70                    assertEquals("A11achm3n1-3.doc", uniqueFile.getName());
71                }
72           }
73            fileName = "A11achm3n1";
74            uniqueFile = svc.createUniqueFileInternal(null, fileName);
75            assertEquals(fileName, uniqueFile.getName());
76            if (uniqueFile.createNewFile()) {
77                uniqueFile = svc.createUniqueFileInternal(null, fileName);
78                assertEquals("A11achm3n1-2", uniqueFile.getName());
79            }
80        } finally {
81            // These are the files that should be created earlier in the test.  Make sure
82            // they are deleted for the next go-around
83            File directory = getContext().getFilesDir();
84            String[] fileNames = new String[] {"A11achm3n1.doc", "A11achm3n1-2.doc", "A11achm3n1"};
85            int length = fileNames.length;
86            for (int i = 0; i < length; i++) {
87                File file = new File(directory, fileNames[i]);
88                if (file.exists()) {
89                    file.delete();
90                }
91            }
92        }
93    }
94
95    public void testAddHeaders() {
96        HttpRequestBase method = new HttpPost();
97        EasSyncService svc = new EasSyncService();
98        svc.mAuthString = "auth";
99        svc.mProtocolVersion = "12.1";
100        svc.mDeviceType = "android";
101        svc.mAccount = null;
102        // With second argument false, there should be no header
103        svc.setHeaders(method, false);
104        Header[] headers = method.getHeaders("X-MS-PolicyKey");
105        assertEquals(0, headers.length);
106        // With second argument true, there should always be a header
107        // The value will be "0" without an account
108        method.removeHeaders("X-MS-PolicyKey");
109        svc.setHeaders(method, true);
110        headers = method.getHeaders("X-MS-PolicyKey");
111        assertEquals(1, headers.length);
112        assertEquals("0", headers[0].getValue());
113        // With an account, but null security key, the header's value should be "0"
114        Account account = new Account();
115        account.mSecuritySyncKey = null;
116        svc.mAccount = account;
117        method.removeHeaders("X-MS-PolicyKey");
118        svc.setHeaders(method, true);
119        headers = method.getHeaders("X-MS-PolicyKey");
120        assertEquals(1, headers.length);
121        assertEquals("0", headers[0].getValue());
122        // With an account and security key, the header's value should be the security key
123        account.mSecuritySyncKey = "key";
124        svc.mAccount = account;
125        method.removeHeaders("X-MS-PolicyKey");
126        svc.setHeaders(method, true);
127        headers = method.getHeaders("X-MS-PolicyKey");
128        assertEquals(1, headers.length);
129        assertEquals("key", headers[0].getValue());
130    }
131
132    public void testGetProtocolVersionDouble() {
133        assertEquals(Eas.SUPPORTED_PROTOCOL_EX2003_DOUBLE,
134                Eas.getProtocolVersionDouble(Eas.SUPPORTED_PROTOCOL_EX2003));
135        assertEquals(Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE,
136                Eas.getProtocolVersionDouble(Eas.SUPPORTED_PROTOCOL_EX2007));
137        assertEquals(Eas.SUPPORTED_PROTOCOL_EX2007_SP1_DOUBLE,
138                Eas.getProtocolVersionDouble(Eas.SUPPORTED_PROTOCOL_EX2007_SP1));
139    }
140
141    private EasSyncService setupService(String user) {
142        EasSyncService svc = new EasSyncService();
143        svc.mUserName = user;
144        svc.mPassword = PASSWORD;
145        svc.mDeviceId = ID;
146        svc.mDeviceType = TYPE;
147        svc.mHostAddress = HOST;
148        return svc;
149    }
150
151    public void testMakeUriString() throws IOException {
152        // Simple user name and command
153        EasSyncService svc = setupService(USER);
154        String uriString = svc.makeUriString("OPTIONS", null);
155        // These next two should now be cached
156        assertNotNull(svc.mAuthString);
157        assertNotNull(svc.mCmdString);
158        assertEquals("Basic " + Base64.encodeToString((USER+":"+PASSWORD).getBytes(),
159                Base64.NO_WRAP), svc.mAuthString);
160        assertEquals("&User=" + USER + "&DeviceId=" + ID + "&DeviceType=" + TYPE, svc.mCmdString);
161        assertEquals("https://" + HOST + "/Microsoft-Server-ActiveSync?Cmd=OPTIONS" +
162                svc.mCmdString, uriString);
163        // User name that requires encoding
164        String user = "name_with_underscore@foo%bar.com";
165        svc = setupService(user);
166        uriString = svc.makeUriString("OPTIONS", null);
167        assertEquals("Basic " + Base64.encodeToString((user+":"+PASSWORD).getBytes(),
168                Base64.NO_WRAP), svc.mAuthString);
169        String safeUserName = "name_with_underscore%40foo%25bar.com";
170        assertEquals("&User=" + safeUserName + "&DeviceId=" + ID + "&DeviceType=" + TYPE,
171                svc.mCmdString);
172        assertEquals("https://" + HOST + "/Microsoft-Server-ActiveSync?Cmd=OPTIONS" +
173                svc.mCmdString, uriString);
174    }
175}
176