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 android.content.Context;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23import android.util.Base64;
24
25import com.android.emailcommon.provider.Account;
26
27import org.apache.http.Header;
28import org.apache.http.client.methods.HttpPost;
29import org.apache.http.client.methods.HttpRequestBase;
30
31import java.io.IOException;
32
33/**
34 * You can run this entire test case with:
35 *   runtest -c com.android.exchange.EasSyncServiceTests exchange
36 */
37@SmallTest
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
44    Context mMockContext;
45
46    @Override
47    public void setUp() throws Exception {
48        super.setUp();
49        mMockContext = getContext();
50    }
51
52    public void testAddHeaders() {
53        HttpRequestBase method = new HttpPost();
54        EasSyncService svc = new EasSyncService();
55        svc.mAuthString = "auth";
56        svc.mProtocolVersion = "12.1";
57        svc.mAccount = null;
58        // With second argument false, there should be no header
59        svc.setHeaders(method, false);
60        Header[] headers = method.getHeaders("X-MS-PolicyKey");
61        assertEquals(0, headers.length);
62        // With second argument true, there should always be a header
63        // The value will be "0" without an account
64        method.removeHeaders("X-MS-PolicyKey");
65        svc.setHeaders(method, true);
66        headers = method.getHeaders("X-MS-PolicyKey");
67        assertEquals(1, headers.length);
68        assertEquals("0", headers[0].getValue());
69        // With an account, but null security key, the header's value should be "0"
70        Account account = new Account();
71        account.mSecuritySyncKey = null;
72        svc.mAccount = account;
73        method.removeHeaders("X-MS-PolicyKey");
74        svc.setHeaders(method, true);
75        headers = method.getHeaders("X-MS-PolicyKey");
76        assertEquals(1, headers.length);
77        assertEquals("0", headers[0].getValue());
78        // With an account and security key, the header's value should be the security key
79        account.mSecuritySyncKey = "key";
80        svc.mAccount = account;
81        method.removeHeaders("X-MS-PolicyKey");
82        svc.setHeaders(method, true);
83        headers = method.getHeaders("X-MS-PolicyKey");
84        assertEquals(1, headers.length);
85        assertEquals("key", headers[0].getValue());
86    }
87
88    public void testGetProtocolVersionDouble() {
89        assertEquals(Eas.SUPPORTED_PROTOCOL_EX2003_DOUBLE,
90                Eas.getProtocolVersionDouble(Eas.SUPPORTED_PROTOCOL_EX2003));
91        assertEquals(Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE,
92                Eas.getProtocolVersionDouble(Eas.SUPPORTED_PROTOCOL_EX2007));
93        assertEquals(Eas.SUPPORTED_PROTOCOL_EX2007_SP1_DOUBLE,
94                Eas.getProtocolVersionDouble(Eas.SUPPORTED_PROTOCOL_EX2007_SP1));
95    }
96
97    private EasSyncService setupService(String user) {
98        EasSyncService svc = new EasSyncService();
99        svc.mUserName = user;
100        svc.mPassword = PASSWORD;
101        svc.mDeviceId = ID;
102        svc.mHostAddress = HOST;
103        return svc;
104    }
105
106    public void testMakeUriString() throws IOException {
107        // Simple user name and command
108        EasSyncService svc = setupService(USER);
109        String uriString = svc.makeUriString("Sync", null);
110        // These next two should now be cached
111        assertNotNull(svc.mAuthString);
112        assertNotNull(svc.mUserString);
113        assertEquals("Basic " + Base64.encodeToString((USER+":"+PASSWORD).getBytes(),
114                Base64.NO_WRAP), svc.mAuthString);
115        assertEquals("&User=" + USER + "&DeviceId=" + ID + "&DeviceType=" +
116                EasSyncService.DEVICE_TYPE, svc.mUserString);
117        assertEquals("https://" + HOST + "/Microsoft-Server-ActiveSync?Cmd=Sync" +
118                svc.mUserString, uriString);
119        // User name that requires encoding
120        String user = "name_with_underscore@foo%bar.com";
121        svc = setupService(user);
122        uriString = svc.makeUriString("Sync", null);
123        assertEquals("Basic " + Base64.encodeToString((user+":"+PASSWORD).getBytes(),
124                Base64.NO_WRAP), svc.mAuthString);
125        String safeUserName = "name_with_underscore%40foo%25bar.com";
126        assertEquals("&User=" + safeUserName + "&DeviceId=" + ID + "&DeviceType=" +
127                EasSyncService.DEVICE_TYPE, svc.mUserString);
128        assertEquals("https://" + HOST + "/Microsoft-Server-ActiveSync?Cmd=Sync" +
129                svc.mUserString, uriString);
130    }
131
132    public void testResetHeartbeats() {
133        EasSyncService svc = new EasSyncService();
134        // Test case in which the minimum and force heartbeats need to come up
135        svc.mPingMaxHeartbeat = 1000;
136        svc.mPingMinHeartbeat = 200;
137        svc.mPingHeartbeat = 300;
138        svc.mPingForceHeartbeat = 100;
139        svc.mPingHeartbeatDropped = true;
140        svc.resetHeartbeats(400);
141        assertEquals(400, svc.mPingMinHeartbeat);
142        assertEquals(1000, svc.mPingMaxHeartbeat);
143        assertEquals(400, svc.mPingHeartbeat);
144        assertEquals(400, svc.mPingForceHeartbeat);
145        assertFalse(svc.mPingHeartbeatDropped);
146
147        // Test case in which the force heartbeat needs to come up
148        svc.mPingMaxHeartbeat = 1000;
149        svc.mPingMinHeartbeat = 200;
150        svc.mPingHeartbeat = 100;
151        svc.mPingForceHeartbeat = 100;
152        svc.mPingHeartbeatDropped = true;
153        svc.resetHeartbeats(150);
154        assertEquals(200, svc.mPingMinHeartbeat);
155        assertEquals(1000, svc.mPingMaxHeartbeat);
156        assertEquals(150, svc.mPingHeartbeat);
157        assertEquals(150, svc.mPingForceHeartbeat);
158        assertFalse(svc.mPingHeartbeatDropped);
159
160        // Test case in which the maximum needs to come down
161        svc.mPingMaxHeartbeat = 1000;
162        svc.mPingMinHeartbeat = 200;
163        svc.mPingHeartbeat = 800;
164        svc.mPingForceHeartbeat = 100;
165        svc.mPingHeartbeatDropped = true;
166        svc.resetHeartbeats(600);
167        assertEquals(200, svc.mPingMinHeartbeat);
168        assertEquals(600, svc.mPingMaxHeartbeat);
169        assertEquals(600, svc.mPingHeartbeat);
170        assertEquals(100, svc.mPingForceHeartbeat);
171        assertFalse(svc.mPingHeartbeatDropped);
172    }
173}
174