1/*
2 * Copyright (C) 2010 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
17package com.android.email.activity.setup;
18
19import android.content.Context;
20import android.test.InstrumentationTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22
23import com.android.emailcommon.VendorPolicyLoader.Provider;
24import com.android.email.activity.setup.AccountSettingsUtils;
25import com.android.email.tests.R;
26
27/**
28 * This is a series of unit tests for the AccountSettingsUtils class.
29 *
30 * To run these tests,
31 *  runtest -c com.android.email.activity.setup.AccountSettingsUtilsTests email
32 */
33@SmallTest
34public class AccountSettingsUtilsTests extends InstrumentationTestCase {
35
36    private Context mTestContext;
37
38    @Override
39    protected void setUp() throws Exception {
40        super.setUp();
41        mTestContext = getInstrumentation().getContext();
42    }
43
44    /**
45     * Test server name inferences
46     *
47     * Incoming: Prepend "imap" or "pop3" to domain, unless "pop", "pop3",
48     *          "imap", or "mail" are found.
49     * Outgoing: Prepend "smtp" if "pop", "pop3", "imap" are found.
50     *          Leave "mail" as-is.
51     * TBD: Are there any useful defaults for exchange?
52     */
53    public void brokentestGuessServerName() {
54        assertEquals("foo.x.y.z", AccountSettingsUtils.inferServerName(mTestContext, "x.y.z",
55                "foo", null));
56        assertEquals("Pop.y.z", AccountSettingsUtils.inferServerName(mTestContext, "Pop.y.z",
57                "foo", null));
58        assertEquals("poP3.y.z", AccountSettingsUtils.inferServerName(mTestContext, "poP3.y.z",
59                "foo", null));
60        assertEquals("iMAp.y.z", AccountSettingsUtils.inferServerName(mTestContext, "iMAp.y.z",
61                "foo", null));
62        assertEquals("MaiL.y.z", AccountSettingsUtils.inferServerName(mTestContext, "MaiL.y.z",
63                "foo", null));
64
65        assertEquals("bar.x.y.z", AccountSettingsUtils.inferServerName(mTestContext, "x.y.z",
66                null, "bar"));
67        assertEquals("bar.y.z", AccountSettingsUtils.inferServerName(mTestContext, "Pop.y.z",
68                null, "bar"));
69        assertEquals("bar.y.z", AccountSettingsUtils.inferServerName(mTestContext, "poP3.y.z",
70                null, "bar"));
71        assertEquals("bar.y.z", AccountSettingsUtils.inferServerName(mTestContext, "iMAp.y.z",
72                null, "bar"));
73        assertEquals("MaiL.y.z", AccountSettingsUtils.inferServerName(mTestContext, "MaiL.y.z",
74                null, "bar"));
75    }
76
77    public void testFindProviderForDomain() {
78        Provider testProvider;
79        // <provider id="gmail" label="Gmail" domain="gmail.com">
80        //   <incoming uri="imap+ssl+://imap.gmail.com" username="$email" />
81        //   <outgoing uri="smtp+ssl+://smtp.gmail.com" username="$email" />
82        // </provider>
83        testProvider = AccountSettingsUtils.findProviderForDomain(
84                mTestContext, "gmail.com", R.xml.test_providers);
85        assertNotNull(testProvider);
86        assertEquals("imap+ssl+://imap.gmail.com", testProvider.incomingUriTemplate);
87        assertEquals("smtp+ssl+://smtp.gmail.com", testProvider.outgoingUriTemplate);
88        assertEquals("gmail.com", testProvider.domain);
89
90        // <provider id="rr-generic" label="RoadRunner" domain="*.rr.com">
91        //   <incoming uri="pop3://pop-server.$domain" username="$email" />
92        //   <outgoing uri="smtp://mobile-smtp.roadrunner.com" username="$email" />
93        // </provider>
94        testProvider = AccountSettingsUtils.findProviderForDomain(
95                mTestContext, "elmore.rr.com", R.xml.test_providers);
96        assertNotNull(testProvider);
97        assertEquals("pop3://pop-server.$domain", testProvider.incomingUriTemplate);
98        assertEquals("smtp://mobile-smtp.roadrunner.com", testProvider.outgoingUriTemplate);
99        assertEquals("elmore.rr.com", testProvider.domain);
100
101        // Domain matches 2 providers; first one wins
102        testProvider = AccountSettingsUtils.findProviderForDomain(
103                mTestContext, "leonard.rr.com", R.xml.test_providers);
104        assertNotNull(testProvider);
105        assertEquals("pop3://pop-server.firstonewins.com", testProvider.incomingUriTemplate);
106
107        // Domains that don't exist
108        testProvider = AccountSettingsUtils.findProviderForDomain(
109                mTestContext, "nonexist.com", R.xml.test_providers);
110        assertNull(testProvider);
111    }
112
113    public void testMatchProvider() {
114        assertTrue(AccountSettingsUtils.matchProvider("foo.com", "foo.com"));
115        assertFalse(AccountSettingsUtils.matchProvider("foo.co", "foo.com"));
116        assertFalse(AccountSettingsUtils.matchProvider("", "foo.com"));
117
118        assertTrue(AccountSettingsUtils.matchProvider("foo.com", "fo?.com"));
119        assertTrue(AccountSettingsUtils.matchProvider("foo.com", "f??.com"));
120        assertTrue(AccountSettingsUtils.matchProvider("fzz.com", "f??.com"));
121        assertTrue(AccountSettingsUtils.matchProvider("foo.com", "???.???"));
122        assertFalse(AccountSettingsUtils.matchProvider("foo.com", "???.????"));
123
124        assertTrue(AccountSettingsUtils.matchProvider("foo.com", "*.com"));
125        assertTrue(AccountSettingsUtils.matchProvider("foo.com", "foo.*"));
126        assertTrue(AccountSettingsUtils.matchProvider("foo.com", "*.*"));
127        assertFalse(AccountSettingsUtils.matchProvider("foo.com", "fox.*"));
128        assertTrue(AccountSettingsUtils.matchProvider("foo.com", "*.???"));
129        assertFalse(AccountSettingsUtils.matchProvider("foo.com", "*.?"));
130
131        assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "food.barge.comb"));
132        assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.com"));
133        assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.gag.com"));
134        assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.*.com"));
135        assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.*.*"));
136        assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.*.*"));
137        assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.*com"));
138        assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "*.bar.com"));
139        assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "*.*.com"));
140        assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "*.*.*"));
141        assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.*"));
142
143        assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.???.*"));
144        assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.*??.*"));
145    }
146
147    public void testExpandTemplates() {
148        Provider testProvider;
149        // <provider id="cox" label="Cox" domain="cox.net">
150        //   <incoming uri="pop3+ssl+://pop.east.cox.net" username="$user" />
151        //   <outgoing uri="smtp+ssl+://smtp.east.cox.net" username="$user" />
152        // </provider>
153        testProvider = new Provider();
154        testProvider.domain = "cox.net";
155        testProvider.incomingUriTemplate = "pop3+ssl+://pop.east.$domain";
156        testProvider.outgoingUriTemplate = "smtp+ssl+://smtp.east.$domain";
157        testProvider.incomingUsernameTemplate = "$user";
158        testProvider.outgoingUsernameTemplate = "$user";
159        testProvider.expandTemplates("replUser@cox.net");
160        assertEquals("replUser", testProvider.incomingUsername);
161        assertEquals("replUser", testProvider.outgoingUsername);
162        assertEquals("pop3+ssl+://pop.east.cox.net", testProvider.incomingUri);
163        assertEquals("smtp+ssl+://smtp.east.cox.net", testProvider.outgoingUri);
164
165        // <provider id="earthlink" label="Earthlink" domain="earthlink.net">
166        //   <incoming uri="pop3://pop.earthlink.net" username="$email" />
167        //   <outgoing uri="smtp://smtpauth.earthlink.net:587" username="$email" />
168        // </provider>
169        testProvider = new Provider();
170        testProvider.domain = "earthlink.net";
171        testProvider.incomingUriTemplate = "pop3://pop.earthlink.net";
172        testProvider.outgoingUriTemplate = "smtp://smtpauth.earthlink.net:587";
173        testProvider.incomingUsernameTemplate = "$email";
174        testProvider.outgoingUsernameTemplate = "$email";
175        testProvider.expandTemplates("replUser@earthlink.net");
176        assertEquals("replUser@earthlink.net", testProvider.incomingUsername);
177        assertEquals("replUser@earthlink.net", testProvider.outgoingUsername);
178        assertEquals("pop3://pop.earthlink.net", testProvider.incomingUri);
179        assertEquals("smtp://smtpauth.earthlink.net:587", testProvider.outgoingUri);
180
181        // <provider id="tuffmail" label="Tuffmail" domain="tuffmail.com">
182        //   <incoming uri="imap+ssl+://mail.mxes.net" username="$user_$domain" />
183        //   <outgoing uri="smtp+ssl+://smtp.mxes.net" username="$user_$domain" />
184        // </provider>
185        testProvider = new Provider();
186        testProvider.domain = "tuffmail.com";
187        testProvider.incomingUriTemplate = "imap+ssl+://mail.mxes.net";
188        testProvider.outgoingUriTemplate = "smtp+ssl+://smtp.mxes.net";
189        testProvider.incomingUsernameTemplate = "$user_$domain";
190        testProvider.outgoingUsernameTemplate = "$user_$domain";
191        testProvider.expandTemplates("replUser@tuffmail.com");
192        assertEquals("replUser_tuffmail.com", testProvider.incomingUsername);
193        assertEquals("replUser_tuffmail.com", testProvider.outgoingUsername);
194        assertEquals("imap+ssl+://mail.mxes.net", testProvider.incomingUri);
195        assertEquals("smtp+ssl+://smtp.mxes.net", testProvider.outgoingUri);
196
197        // Everything hardcoded; not effective in the wild
198        testProvider = new Provider();
199        testProvider.domain = "yahoo.com";
200        testProvider.incomingUriTemplate = "imap+ssl+://pop.yahoo.com";
201        testProvider.outgoingUriTemplate = "smtp+ssl+://smtp.yahoo.com";
202        testProvider.incomingUsernameTemplate = "joe_smith";
203        testProvider.outgoingUsernameTemplate = "joe_smith";
204        testProvider.expandTemplates("replUser@yahoo.com");
205        assertEquals("joe_smith", testProvider.incomingUsername);
206        assertEquals("joe_smith", testProvider.outgoingUsername);
207        assertEquals("imap+ssl+://pop.yahoo.com", testProvider.incomingUri);
208        assertEquals("smtp+ssl+://smtp.yahoo.com", testProvider.outgoingUri);
209    }
210}
211