1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package tests.api.javax.net.ssl; 19 20 import dalvik.annotation.TestTargetClass; 21 import dalvik.annotation.TestTargets; 22 import dalvik.annotation.TestLevel; 23 import dalvik.annotation.TestTargetNew; 24 25 import java.security.Principal; 26 import java.security.cert.Certificate; 27 28 import javax.net.ssl.SSLPeerUnverifiedException; 29 import javax.net.ssl.SSLSession; 30 import javax.net.ssl.SSLSessionContext; 31 import javax.net.ssl.SSLSessionBindingEvent; 32 import javax.security.cert.X509Certificate; 33 34 import junit.framework.TestCase; 35 36 /** 37 * Tests for <code>SSLSessionBindingEvent</code> class constructors and methods. 38 * 39 */ 40 @TestTargetClass(SSLSessionBindingEvent.class) 41 public class SSLSessionBindingEventTest extends TestCase { 42 43 @TestTargetNew( 44 level = TestLevel.COMPLETE, 45 notes = "", 46 method = "SSLSessionBindingEvent", 47 args = {javax.net.ssl.SSLSession.class, java.lang.String.class} 48 ) 49 public final void test_ConstructorLjavax_net_ssl_SSLSessionLjava_lang_String() { 50 SSLSession ses = new MySSLSession(); 51 52 try { 53 SSLSessionBindingEvent event = new SSLSessionBindingEvent(ses, "test"); 54 if (!"test".equals(event.getName())) { 55 fail("incorrect name"); 56 } 57 if (!event.getSession().equals(ses)) { 58 fail("incorrect session"); 59 } 60 } catch (Exception e) { 61 fail("Unexpected exception " + e); 62 } 63 64 try { 65 SSLSessionBindingEvent event = new SSLSessionBindingEvent(null, "test"); 66 fail("IllegalArgumentException expected"); 67 } catch (IllegalArgumentException e) { 68 // expected 69 } 70 71 try { 72 SSLSessionBindingEvent event = new SSLSessionBindingEvent(ses, null); 73 } catch (IllegalArgumentException e) { 74 fail("Unexpected IllegalArgumentException: " + e); 75 } 76 } 77 78 /** 79 * @tests javax.net.ssl.SSLSessionBindingEvent#getName() 80 */ 81 @TestTargetNew( 82 level = TestLevel.COMPLETE, 83 notes = "", 84 method = "getName", 85 args = {} 86 ) 87 public void test_getName() { 88 SSLSession ses = new MySSLSession(); 89 SSLSessionBindingEvent event = new SSLSessionBindingEvent(ses, "test"); 90 assertEquals("Incorrect session name", "test", event.getName()); 91 event = new SSLSessionBindingEvent(ses, null); 92 assertEquals("Incorrect session name", null, event.getName()); 93 } 94 95 /** 96 * @tests javax.net.ssl.SSLSessionBindingEvent#getSession() 97 */ 98 @TestTargetNew( 99 level = TestLevel.COMPLETE, 100 notes = "", 101 method = "getSession", 102 args = {} 103 ) 104 public void test_getSession() { 105 SSLSession ses = new MySSLSession(); 106 SSLSessionBindingEvent event = new SSLSessionBindingEvent(ses, "test"); 107 assertEquals("Incorrect session", ses, event.getSession()); 108 } 109 } 110 111 class MySSLSession implements SSLSession { 112 /* 113 * @see javax.net.ssl.SSLSession#getApplicationBufferSize() 114 */ 115 public int getApplicationBufferSize() { 116 return 0; 117 } 118 119 /* 120 * @see javax.net.ssl.SSLSession#getCipherSuite() 121 */ 122 public String getCipherSuite() { 123 return "MyTestCipherSuite"; 124 } 125 126 /* 127 * @see javax.net.ssl.SSLSession#getCreationTime() 128 */ 129 public long getCreationTime() { 130 return 0; 131 } 132 133 /* 134 * @see javax.net.ssl.SSLSession#getId() 135 */ 136 public byte[] getId() { 137 return null; 138 } 139 140 /* 141 * @see javax.net.ssl.SSLSession#getLastAccessedTime() 142 */ 143 public long getLastAccessedTime() { 144 return 0; 145 } 146 147 /* 148 * @see javax.net.ssl.SSLSession#getLocalCertificates() 149 */ 150 public Certificate[] getLocalCertificates() { 151 return null; 152 } 153 154 /* 155 * @see javax.net.ssl.SSLSession#getLocalPrincipal() 156 */ 157 public Principal getLocalPrincipal() { 158 return null; 159 } 160 161 /* 162 * @see javax.net.ssl.SSLSession#getPacketBufferSize() 163 */ 164 public int getPacketBufferSize() { 165 return 0; 166 } 167 168 /* 169 * @see javax.net.ssl.SSLSession#getPeerCertificateChain() 170 */ 171 public X509Certificate[] getPeerCertificateChain() 172 throws SSLPeerUnverifiedException { 173 throw new SSLPeerUnverifiedException("test exception"); 174 } 175 176 /* 177 * @see javax.net.ssl.SSLSession#getPeerCertificates() 178 */ 179 public Certificate[] getPeerCertificates() 180 throws SSLPeerUnverifiedException { 181 throw new SSLPeerUnverifiedException("test exception"); 182 } 183 184 /* 185 * @see javax.net.ssl.SSLSession#getPeerHost() 186 */ 187 public String getPeerHost() { 188 return null; 189 } 190 191 /* 192 * @see javax.net.ssl.SSLSession#getPeerPort() 193 */ 194 public int getPeerPort() { 195 return 0; 196 } 197 198 /* 199 * @see javax.net.ssl.SSLSession#getPeerPrincipal() 200 */ 201 public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { 202 return null; 203 } 204 205 /* 206 * @see javax.net.ssl.SSLSession#getProtocol() 207 */ 208 public String getProtocol() { 209 return null; 210 } 211 212 /* 213 * @see javax.net.ssl.SSLSession#getSessionContext() 214 */ 215 public SSLSessionContext getSessionContext() { 216 return null; 217 } 218 219 /* 220 * @see javax.net.ssl.SSLSession#getValue(java.lang.String) 221 */ 222 public Object getValue(String name) { 223 return null; 224 } 225 226 /* 227 * @see javax.net.ssl.SSLSession#getValueNames() 228 */ 229 public String[] getValueNames() { 230 return null; 231 } 232 233 /* 234 * @see javax.net.ssl.SSLSession#invalidate() 235 */ 236 public void invalidate() { 237 } 238 239 /* 240 * @see javax.net.ssl.SSLSession#isValid() 241 */ 242 public boolean isValid() { 243 return false; 244 } 245 246 /* 247 * @see javax.net.ssl.SSLSession#putValue(java.lang.String, 248 * java.lang.Object) 249 */ 250 public void putValue(String name, Object value) { 251 } 252 253 /* 254 * @see javax.net.ssl.SSLSession#removeValue(java.lang.String) 255 */ 256 public void removeValue(String name) { 257 } 258 259 } 260 261