1/* 2 * Copyright (C) 2008 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 tests.api.java.net; 18 19import dalvik.annotation.TestTargets; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTargetNew; 22import dalvik.annotation.TestTargetClass; 23 24import junit.framework.TestCase; 25 26import tests.api.java.net.ResponseCacheTest.TestCacheRequest; 27import tests.api.java.net.ResponseCacheTest.TestCacheResponse; 28 29import java.io.IOException; 30import java.io.InputStream; 31import java.net.CacheRequest; 32import java.net.CacheResponse; 33import java.net.HttpURLConnection; 34import java.net.ResponseCache; 35import java.net.SecureCacheResponse; 36import java.net.URI; 37import java.net.URISyntaxException; 38import java.net.URL; 39import java.net.URLConnection; 40import java.security.Principal; 41import java.security.cert.Certificate; 42import java.util.List; 43import java.util.Map; 44 45import javax.net.ssl.SSLPeerUnverifiedException; 46 47@TestTargetClass(SecureCacheResponse.class) 48public class SecureCacheResponseTest extends TestCase { 49 50 @TestTargets({ 51 @TestTargetNew( 52 level = TestLevel.COMPLETE, 53 notes = "", 54 method = "SecureCacheResponse", 55 args = {} 56 ), 57 @TestTargetNew( 58 level = TestLevel.COMPLETE, 59 notes = "", 60 method = "getCipherSuite", 61 args = {} 62 ), 63 @TestTargetNew( 64 level = TestLevel.COMPLETE, 65 notes = "", 66 method = "getLocalCertificateChain", 67 args = {} 68 ), 69 @TestTargetNew( 70 level = TestLevel.COMPLETE, 71 notes = "", 72 method = "getLocalPrincipal", 73 args = {} 74 ), 75 @TestTargetNew( 76 level = TestLevel.COMPLETE, 77 notes = "", 78 method = "getPeerPrincipal", 79 args = {} 80 ), 81 @TestTargetNew( 82 level = TestLevel.COMPLETE, 83 notes = "", 84 method = "getServerCertificateChain", 85 args = {} 86 ) 87 }) 88 public void test_Constructor() throws Exception { 89 TestSecureCacheResponse scr = new TestSecureCacheResponse(); 90 assertNull(scr.getCipherSuite()); 91 assertNull(scr.getLocalCertificateChain()); 92 assertNull(scr.getLocalPrincipal()); 93 assertNull(scr.getPeerPrincipal()); 94 assertNull(scr.getServerCertificateChain()); 95 assertNull(scr.getBody()); 96 assertNull(scr.getHeaders()); 97 } 98 99 @TestTargetNew( 100 level = TestLevel.ADDITIONAL, 101 notes = "", 102 method = "SecureCacheResponse", 103 args = {} 104 ) 105 public void test_additional() throws Exception { 106 107 URL url = new URL("http://google.com"); 108 ResponseCache.setDefault(new TestResponseCache()); 109 HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 110 httpCon.setUseCaches(true); 111 httpCon.connect(); 112 try { 113 Thread.sleep(5000); 114 } catch(Exception e) {} 115 httpCon.disconnect(); 116 117 } 118 119 class TestSecureCacheResponse extends SecureCacheResponse { 120 121 @Override 122 public String getCipherSuite() { 123 return null; 124 } 125 126 @Override 127 public List<Certificate> getLocalCertificateChain() { 128 return null; 129 } 130 131 @Override 132 public Principal getLocalPrincipal() { 133 return null; 134 } 135 136 @Override 137 public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { 138 return null; 139 } 140 141 @Override 142 public List<Certificate> getServerCertificateChain() throws SSLPeerUnverifiedException { 143 return null; 144 } 145 146 @Override 147 public InputStream getBody() throws IOException { 148 return null; 149 } 150 151 @Override 152 public Map<String, List<String>> getHeaders() throws IOException { 153 return null; 154 } 155 156 } 157 158 class TestResponseCache extends ResponseCache { 159 160 URI uri1 = null; 161 162 public TestSecureCacheResponse get(URI uri, String rqstMethod, Map rqstHeaders) 163 throws IOException { 164 165 166 try { 167 uri1 = new URI("http://google.com"); 168 } catch (URISyntaxException e) { 169 } 170 if (uri.equals(uri)) { 171 return new TestSecureCacheResponse(); 172 } 173 return null; 174 } 175 176 public CacheRequest put(URI uri, URLConnection conn) 177 throws IOException { 178 return null; 179 } 180 } 181} 182