FileDocCategorySizeDatePackage
DbSSLSessionCacheTest.javaAPI DocAndroid 1.5 API7834Wed May 06 22:42:02 BST 2009com.android.unit_tests

DbSSLSessionCacheTest

public class DbSSLSessionCacheTest extends android.test.AndroidTestCase
Unit test for SSL session caching with {@link GoogleHttpClient}. Uses network resources.

Fields Summary
Constructors Summary
Methods Summary
private voidmakeRequestInNewContext(java.lang.String url)

        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();
        }
    
protected voidsetUp()

    
protected voidtearDown()

    
public voidtestExpire()

        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();
    
public voidtestSslCacheAdd()
We want to test the actual database write - the actual hooking into low-level SSL is tested.

        // 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 = DbSSLSessionCache.getInstanceForPackage(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();
        }