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.emailcommon.provider;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 /**
     23  * Unit tests for the HostAuth inner class.
     24  * These tests must be locally complete - no server(s) required.
     25  */
     26 @SmallTest
     27 public class HostAuthTests extends AndroidTestCase {
     28 
     29     /**
     30      * Test user name and password are set correctly
     31      */
     32     public void testSetLogin() {
     33         HostAuth ha = new HostAuth();
     34         ha.setLogin("user:password");
     35         assertEquals("user", ha.mLogin);
     36         assertEquals("password", ha.mPassword);
     37 
     38         // special characters are not removed during insertion
     39         ha.setLogin("%20us%20er%20:password");
     40         assertEquals("%20us%20er%20", ha.mLogin);
     41         assertEquals("password", ha.mPassword);
     42 
     43         // special characters are not removed during insertion
     44         ha.setLogin("user:%20pass%20word%20");
     45         assertEquals("user", ha.mLogin);
     46         assertEquals("%20pass%20word%20", ha.mPassword);
     47 
     48         ha.setLogin("user:");
     49         assertEquals("user", ha.mLogin);
     50         assertEquals("", ha.mPassword);
     51 
     52         ha.setLogin(":password");
     53         assertEquals("", ha.mLogin);
     54         assertEquals("password", ha.mPassword);
     55 
     56         ha.setLogin("");
     57         assertNull(ha.mLogin);
     58         assertNull(ha.mPassword);
     59 
     60         ha.setLogin(null);
     61         assertNull(ha.mLogin);
     62         assertNull(ha.mPassword);
     63 
     64         ha.setLogin("userpassword");
     65         assertEquals("userpassword", ha.mLogin);
     66         assertNull(ha.mPassword);
     67     }
     68 
     69     /**
     70      * Test the authentication flag is set correctly when setting user name and password
     71      */
     72     public void testSetLoginAuthenticate() {
     73         HostAuth ha = new HostAuth();
     74 
     75         ha.mFlags = 0x00000000;
     76         ha.setLogin("user", "password");
     77         assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
     78 
     79         ha.mFlags = 0x00000000;
     80         ha.setLogin("user", "");
     81         assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
     82 
     83         ha.mFlags = 0x00000000;
     84         ha.setLogin("", "password");
     85         assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
     86 
     87         ha.mFlags = 0x00000000;
     88         ha.setLogin("user", null);
     89         assertEquals(HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
     90 
     91         ha.mFlags = 0xffffffff;
     92         ha.setLogin(null, "password");
     93         assertEquals(~HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
     94 
     95         ha.mFlags = 0xffffffff;
     96         ha.setLogin(null, null);
     97         assertEquals(~HostAuth.FLAG_AUTHENTICATE, ha.mFlags);
     98     }
     99 
    100     /**
    101      * Test setting the connection using a protocol and flags
    102      */
    103     public void testSetConnectionFlags() {
    104         HostAuth ha = new HostAuth();
    105 
    106         // Different port types don't affect flags
    107         ha.setConnection("imap", "server", 123, 0);
    108         assertEquals(0, ha.mFlags);
    109         ha.setConnection("imap", "server", -1, 0);
    110         assertEquals(0, ha.mFlags);
    111 
    112         // Different protocol types don't affect flags
    113         ha.setConnection("pop3", "server", 123, 0);
    114         assertEquals(0, ha.mFlags);
    115         ha.setConnection("pop3", "server", -1, 0);
    116         assertEquals(0, ha.mFlags);
    117         ha.setConnection("eas", "server", 123, 0);
    118         assertEquals(0, ha.mFlags);
    119         ha.setConnection("eas", "server", -1, 0);
    120         assertEquals(0, ha.mFlags);
    121         ha.setConnection("smtp", "server", 123, 0);
    122         assertEquals(0, ha.mFlags);
    123         ha.setConnection("smtp", "server", -1, 0);
    124         assertEquals(0, ha.mFlags);
    125 
    126         // Sets SSL flag
    127         ha.setConnection("imap", "server", HostAuth.PORT_UNKNOWN, HostAuth.FLAG_SSL);
    128         assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
    129 
    130         // Sets SSL+Trusted flags
    131         ha.setConnection("imap", "server", HostAuth.PORT_UNKNOWN,
    132                 HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL);
    133         assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, ha.mFlags);
    134 
    135         // Sets TLS flag
    136         ha.setConnection("imap", "server", HostAuth.PORT_UNKNOWN, HostAuth.FLAG_TLS);
    137         assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
    138 
    139         // Sets TLS+Trusted flags
    140         ha.setConnection("imap", "server", HostAuth.PORT_UNKNOWN,
    141                 HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL);
    142         assertEquals(HostAuth.FLAG_TLS | HostAuth.FLAG_TRUST_ALL, ha.mFlags);
    143 
    144         // Test other defined flags; should not affect mFlags
    145         ha.setConnection("imap", "server", HostAuth.PORT_UNKNOWN, HostAuth.FLAG_AUTHENTICATE);
    146         assertEquals(0, ha.mFlags);
    147 
    148         // Test every other bit; should not affect mFlags
    149         ha.setConnection("imap", "server", HostAuth.PORT_UNKNOWN, 0xfffffff4);
    150         assertEquals(0, ha.mFlags);
    151     }
    152 
    153     public void testSetConnectionWithCerts() {
    154         HostAuth ha = new HostAuth();
    155 
    156         ha.setConnection("eas", "server", HostAuth.PORT_UNKNOWN, HostAuth.FLAG_SSL, "client-cert");
    157         assertEquals(HostAuth.FLAG_SSL, ha.mFlags);
    158         assertEquals("client-cert", ha.mClientCertAlias);
    159 
    160         ha.setConnection("eas", "server", HostAuth.PORT_UNKNOWN, HostAuth.FLAG_TLS, "client-cert");
    161         assertEquals(HostAuth.FLAG_TLS, ha.mFlags);
    162         assertEquals("client-cert", ha.mClientCertAlias);
    163 
    164         // Note that we can still trust all server certificates, even if we present a client
    165         // user certificate.
    166         ha.setConnection("eas", "server", HostAuth.PORT_UNKNOWN,
    167                 HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, "client-cert");
    168         assertEquals(HostAuth.FLAG_SSL | HostAuth.FLAG_TRUST_ALL, ha.mFlags);
    169         assertEquals("client-cert", ha.mClientCertAlias);
    170 
    171         try {
    172             ha.setConnection(
    173                     "eas", "server", HostAuth.PORT_UNKNOWN, 0 /* no flags */, "client-cert");
    174             fail("Shouldn't be able to set a client certificate on an unsecure connection");
    175         } catch (IllegalArgumentException expected) {
    176         }
    177     }
    178 }
    179 
    180