summaryrefslogtreecommitdiffstats
path: root/tests/AndroidTests/src/com/android/unit_tests/DbSSLSessionCacheTest.java
blob: 77460beed6c74214a8c2c5beb395a6fd35fd67ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.unit_tests;

import android.content.ContentResolver;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.Settings;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.Suppress;

import com.google.android.net.GoogleHttpClient;

import com.android.internal.net.DbSSLSessionCache;
import com.android.internal.net.SSLSessionCache;
import com.android.internal.net.DbSSLSessionCache.DatabaseHelper;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;

import java.io.IOException;
import java.security.Principal;
import java.security.cert.Certificate;

import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;
import javax.security.cert.X509Certificate;

/** Unit test for SSL session caching with {@link GoogleHttpClient}.
 *  Uses network resources. 
 */
@Suppress
public class DbSSLSessionCacheTest extends AndroidTestCase {
    
    protected void setUp() throws Exception {
    }

    protected void tearDown() throws Exception {
    }
    
    @LargeTest
    public void testSslCacheSettings() throws Exception {
        ContentResolver resolver = getContext().getContentResolver();
        Settings.Gservices.putString(resolver, Settings.Gservices.SSL_SESSION_CACHE, 
                "0");
        assertFalse(SSLSessionCache.isEnabled(getContext().getContentResolver()));
        
        resolver = getContext().getContentResolver();
        Settings.Gservices.putString(resolver, Settings.Gservices.SSL_SESSION_CACHE, 
                "db");
        assertTrue(SSLSessionCache.isEnabled(getContext().getContentResolver()));
    }

    /** 
     * We want to test the actual database write - the actual hooking into 
     * low-level SSL is tested. 
     */
    @LargeTest
    public void testSslCacheAdd() throws Exception {
        // Let's verify the database has the rows.
        // Use internal details of the implementation - could make the field
        // visible for testing, but it's same.
        
        // Use default database
        DbSSLSessionCache cache = new DbSSLSessionCache(getContext());
        cache.clear();
        
        
        makeRequestInNewContext("https://www.google.com");

        // Verify the key was inserted
        SQLiteOpenHelper helper = new DatabaseHelper(getContext());
        Cursor query = null;
        try {
            query = helper.getReadableDatabase().query(DbSSLSessionCache.SSL_CACHE_TABLE, 
                    new String[] {"hostport"}, null, 
                    null, null, null, null);

            assertTrue(query.moveToFirst()); // one row inserted
            String hostPort = query.getString(0);
            assertEquals(hostPort, "www.google.com:443");
        } finally {
            query.close();
        }
    }
    
    @LargeTest
    public void testExpire() throws Exception {
        DatabaseHelper helper = new DatabaseHelper(getContext());
        // clean up
        DbSSLSessionCache cache = new DbSSLSessionCache(helper);
        cache.clear();
        
        long t0 = System.currentTimeMillis();
        for (int i = 0; i < DbSSLSessionCache.MAX_CACHE_SIZE + 2; i++) {
            final int port = i;
            cache.putSessionData(new MockSession() {
                
                public String getPeerHost() {
                    return "test.host.com";
                }
                
                public int getPeerPort() {
                    return port;
                }
            }, new byte[256]);
        }
        long t1 = System.currentTimeMillis();

        System.err.println("Time to insert " + 
                (DbSSLSessionCache.MAX_CACHE_SIZE + 2) + " " + (t1 - t0));
        
        // first entry should have port 1.
        Cursor query = helper.getReadableDatabase().query(DbSSLSessionCache.SSL_CACHE_TABLE, 
                new String[] {"hostport", "session"}, null, 
                null, null, null, null);

        int cnt = query.getCount();
        
        assertTrue(query.moveToFirst()); // one row inserted
        String hostPort = query.getString(0);
        assertEquals("test.host.com:2", hostPort);
        while (query.moveToNext()) {
            hostPort = query.getString(0);
            String session = query.getString(1);
        }
        long t2 = System.currentTimeMillis();
        System.err.println("Time to load " + cnt + " " + (t2 - t1));
        
        query.close();
    }
    
    private void makeRequestInNewContext(String url) throws IOException {
        GoogleHttpClient client = new GoogleHttpClient(getContext(), "Test",
                false /* no gzip */);

        try {
            // Note: we must test against a real server, because the connection
            // gets established before the interceptor can crash the request.
            HttpGet method = new HttpGet(url);
            HttpResponse response = client.execute(method);
        } finally {
            client.close();
        }
    }
    
    private static class MockSession implements SSLSession {
        
        public String getPeerHost() {
            throw new UnsupportedOperationException();
        }

        
        public int getPeerPort() {
            throw new UnsupportedOperationException();
        }


        
        public int getApplicationBufferSize() {
            throw new UnsupportedOperationException();
        }

        
        public String getCipherSuite() {
            throw new UnsupportedOperationException();
        }

        
        public long getCreationTime() {
            throw new UnsupportedOperationException();
        }

        
        public byte[] getId() {
            throw new UnsupportedOperationException();
        }

        
        public long getLastAccessedTime() {
            throw new UnsupportedOperationException();
        }

        
        public Certificate[] getLocalCertificates() {
            throw new UnsupportedOperationException();
        }

        
        public Principal getLocalPrincipal() {
            throw new UnsupportedOperationException();
        }

        
        public int getPacketBufferSize() {
            throw new UnsupportedOperationException();
        }

        
        public X509Certificate[] getPeerCertificateChain()
                throws SSLPeerUnverifiedException {
            throw new UnsupportedOperationException();
        }

        
        public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
            throw new UnsupportedOperationException();
        }

        
        public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
            throw new UnsupportedOperationException();
        }

        
        public String getProtocol() {
            throw new UnsupportedOperationException();
        }

        
        public SSLSessionContext getSessionContext() {
            throw new UnsupportedOperationException();
        }

        
        public Object getValue(String name) {
            throw new UnsupportedOperationException();
        }

        
        public String[] getValueNames() {
            throw new UnsupportedOperationException();
        }

        
        public void invalidate() {
            throw new UnsupportedOperationException();
        }

        
        public boolean isValid() {
            throw new UnsupportedOperationException();
        }

        
        public void putValue(String name, Object value) {
            throw new UnsupportedOperationException();
        }

        
        public void removeValue(String name) {
            throw new UnsupportedOperationException();
        }
    }

    
}