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 18package tests.api.javax.net.ssl; 19 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetNew; 24 25import javax.net.ssl.SSLEngineResult; 26import junit.framework.TestCase; 27 28 29/** 30 * Tests for SSLEngineResult class 31 * 32 */ 33@TestTargetClass(SSLEngineResult.class) 34public class SSLEngineResultTest extends TestCase { 35 36 /** 37 * Test for <code>SSLEngineResult(SSLEngineResult.Status status, 38 * SSLEngineResult.HandshakeStatus handshakeStatus, 39 * int bytesConsumed, 40 * int bytesProduced) </code> constructor and 41 * <code>getHandshakeStatus()</code> 42 * <code>getStatus()</code> 43 * <code>bytesConsumed()</code> 44 * <code>bytesProduced()</code> 45 * <code>toString()</code> 46 * methods 47 * Assertions: 48 * constructor throws IllegalArgumentException when bytesConsumed 49 * or bytesProduced is negative or when status or handshakeStatus 50 * is null 51 * 52 */ 53 @TestTargetNew( 54 level = TestLevel.COMPLETE, 55 notes = "", 56 method = "SSLEngineResult", 57 args = {javax.net.ssl.SSLEngineResult.Status.class, javax.net.ssl.SSLEngineResult.HandshakeStatus.class, int.class, int.class} 58 ) 59 public void test_ConstructorLjavax_net_ssl_SSLEngineResult_StatusLjavax_net_ssl_SSLEngineResult_HandshakeStatusII() { 60 61 int[] neg = { -1, -10, -1000, Integer.MIN_VALUE, 62 (Integer.MIN_VALUE + 1) }; 63 try { 64 new SSLEngineResult(null, SSLEngineResult.HandshakeStatus.FINISHED, 65 1, 1); 66 fail("IllegalArgumentException must be thrown"); 67 } catch (IllegalArgumentException e) { 68 } 69 try { 70 new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, null, 71 1, 1); 72 fail("IllegalArgumentException must be thrown"); 73 } catch (IllegalArgumentException e) { 74 } 75 for (int i = 0; i < neg.length; i++) { 76 try { 77 new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, 78 SSLEngineResult.HandshakeStatus.FINISHED, neg[i], 1); 79 fail("IllegalArgumentException must be thrown"); 80 } catch (IllegalArgumentException e) { 81 } 82 } 83 for (int i = 0; i < neg.length; i++) { 84 try { 85 new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, 86 SSLEngineResult.HandshakeStatus.FINISHED, 1, neg[i]); 87 fail("IllegalArgumentException must be thrown"); 88 } catch (IllegalArgumentException e) { 89 } 90 } 91 92 try { 93 SSLEngineResult res = new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, 94 SSLEngineResult.HandshakeStatus.FINISHED, 1, 2); 95 assertNotNull("Null object", res); 96 assertEquals(1, res.bytesConsumed()); 97 assertEquals(2, res.bytesProduced()); 98 } catch (Exception e) { 99 fail("Unexpected exception: " + e); 100 } 101 } 102 103 /** 104 * Test for <code>bytesConsumed()</code> method 105 */ 106 @TestTargetNew( 107 level = TestLevel.COMPLETE, 108 notes = "", 109 method = "bytesConsumed", 110 args = {} 111 ) 112 public void test_bytesConsumed() { 113 int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) }; 114 SSLEngineResult.Status [] enS = 115 SSLEngineResult.Status.values(); 116 SSLEngineResult.HandshakeStatus [] enHS = 117 SSLEngineResult.HandshakeStatus.values(); 118 for (int i = 0; i < enS.length; i++) { 119 for (int j = 0; j < enHS.length; j++) { 120 for (int n = 0; n < pos.length; n++) { 121 for (int l = 0; l < pos.length; l++) { 122 SSLEngineResult res = new SSLEngineResult(enS[i], 123 enHS[j], pos[n], pos[l]); 124 assertEquals("Incorrect bytesConsumed", pos[n], 125 res.bytesConsumed()); 126 } 127 } 128 } 129 } 130 } 131 132 /** 133 * Test for <code>bytesProduced()</code> method 134 */ 135 @TestTargetNew( 136 level = TestLevel.COMPLETE, 137 notes = "", 138 method = "bytesProduced", 139 args = {} 140 ) 141 public void test_bytesProduced() { 142 int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) }; 143 SSLEngineResult.Status [] enS = 144 SSLEngineResult.Status.values(); 145 SSLEngineResult.HandshakeStatus [] enHS = 146 SSLEngineResult.HandshakeStatus.values(); 147 for (int i = 0; i < enS.length; i++) { 148 for (int j = 0; j < enHS.length; j++) { 149 for (int n = 0; n < pos.length; n++) { 150 for (int l = 0; l < pos.length; ++l) { 151 SSLEngineResult res = new SSLEngineResult(enS[i], 152 enHS[j], pos[n], pos[l]); 153 assertEquals("Incorrect bytesProduced", pos[l], 154 res.bytesProduced()); 155 } 156 } 157 } 158 } 159 } 160 161 /** 162 * Test for <code>getHandshakeStatus()</code> method 163 */ 164 @TestTargetNew( 165 level = TestLevel.COMPLETE, 166 notes = "", 167 method = "getHandshakeStatus", 168 args = {} 169 ) 170 public void test_getHandshakeStatus() { 171 int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) }; 172 SSLEngineResult.Status [] enS = 173 SSLEngineResult.Status.values(); 174 SSLEngineResult.HandshakeStatus [] enHS = 175 SSLEngineResult.HandshakeStatus.values(); 176 for (int i = 0; i < enS.length; i++) { 177 for (int j = 0; j < enHS.length; j++) { 178 for (int n = 0; n < pos.length; n++) { 179 for (int l = 0; l < pos.length; ++l) { 180 SSLEngineResult res = new SSLEngineResult(enS[i], 181 enHS[j], pos[n], pos[l]); 182 assertEquals("Incorrect HandshakeStatus", enHS[j], 183 res.getHandshakeStatus()); 184 } 185 } 186 } 187 } 188 } 189 190 /** 191 * Test for <code>getStatus()</code> method 192 */ 193 @TestTargetNew( 194 level = TestLevel.COMPLETE, 195 notes = "", 196 method = "getStatus", 197 args = {} 198 ) 199 public void test_getStatus() { 200 int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) }; 201 SSLEngineResult.Status [] enS = 202 SSLEngineResult.Status.values(); 203 SSLEngineResult.HandshakeStatus [] enHS = 204 SSLEngineResult.HandshakeStatus.values(); 205 for (int i = 0; i < enS.length; i++) { 206 for (int j = 0; j < enHS.length; j++) { 207 for (int n = 0; n < pos.length; n++) { 208 for (int l = 0; l < pos.length; ++l) { 209 SSLEngineResult res = new SSLEngineResult(enS[i], 210 enHS[j], pos[n], pos[l]); 211 assertEquals("Incorrect Status", enS[i], 212 res.getStatus()); 213 } 214 } 215 } 216 } 217 } 218 219 /** 220 * Test for <code>toString()</code> method 221 */ 222 @TestTargetNew( 223 level = TestLevel.COMPLETE, 224 notes = "", 225 method = "toString", 226 args = {} 227 ) 228 public void test_toString() { 229 int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) }; 230 SSLEngineResult.Status [] enS = 231 SSLEngineResult.Status.values(); 232 SSLEngineResult.HandshakeStatus [] enHS = 233 SSLEngineResult.HandshakeStatus.values(); 234 for (int i = 0; i < enS.length; i++) { 235 for (int j = 0; j < enHS.length; j++) { 236 for (int n = 0; n < pos.length; n++) { 237 for (int l = 0; l < pos.length; ++l) { 238 SSLEngineResult res = new SSLEngineResult(enS[i], 239 enHS[j], pos[n], pos[l]); 240 assertNotNull("Result of toSring() method is null", 241 res.toString()); 242 } 243 } 244 } 245 } 246 } 247 248 private boolean findEl(Object[] arr, Object el) { 249 boolean ok = false; 250 for (int i = 0; i < arr.length; i++) { 251 if (arr[i].equals(el)) { 252 ok = true; 253 break; 254 } 255 } 256 return ok; 257 } 258 259} 260