Home | History | Annotate | Download | only in ssl
      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 org.apache.harmony.xnet.tests.javax.net.ssl;
     19 
     20 import javax.net.ssl.SSLEngineResult;
     21 
     22 import junit.framework.TestCase;
     23 
     24 /**
     25  * Tests for SSLEngineResult class
     26  *
     27  */
     28 public class SSLEngineResultTest extends TestCase {
     29 
     30     /**
     31      * Test for <code>SSLEngineResult(SSLEngineResult.Status status,
     32      *              SSLEngineResult.HandshakeStatus handshakeStatus,
     33      *              int bytesConsumed,
     34      *              int bytesProduced) </code> constructor and
     35      * <code>getHandshakeStatus()</code>
     36      * <code>getStatus()</code>
     37      * <code>bytesConsumed()</code>
     38      * <code>bytesProduced()</code>
     39      * <code>toString()</code>
     40      * methods
     41      * Assertions:
     42      * constructor throws IllegalArgumentException when bytesConsumed
     43      * or bytesProduced is negative or when status or handshakeStatus
     44      * is null
     45      *
     46      */
     47     public void testSSLEngineResultConstructor() {
     48 
     49         int[] neg = { -1, -10, -1000, Integer.MIN_VALUE,
     50                 (Integer.MIN_VALUE + 1) };
     51         int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) };
     52         try {
     53             new SSLEngineResult(null, SSLEngineResult.HandshakeStatus.FINISHED,
     54                     1, 1);
     55             fail("IllegalArgumentException must be thrown");
     56         } catch (IllegalArgumentException e) {
     57         }
     58         try {
     59             new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, null,
     60                     1, 1);
     61             fail("IllegalArgumentException must be thrown");
     62         } catch (IllegalArgumentException e) {
     63         }
     64         for (int i = 0; i < neg.length; i++) {
     65             try {
     66                 new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW,
     67                         SSLEngineResult.HandshakeStatus.FINISHED, neg[i], 1);
     68                 fail("IllegalArgumentException must be thrown");
     69             } catch (IllegalArgumentException e) {
     70             }
     71         }
     72         for (int i = 0; i < neg.length; i++) {
     73             try {
     74                 new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW,
     75                         SSLEngineResult.HandshakeStatus.FINISHED, 1, neg[i]);
     76                 fail("IllegalArgumentException must be thrown");
     77             } catch (IllegalArgumentException e) {
     78             }
     79         }
     80         SSLEngineResult.Status [] enS = SSLEngineResult.Status.values();
     81         SSLEngineResult.HandshakeStatus [] enHS = SSLEngineResult.HandshakeStatus
     82                 .values();
     83         SSLEngineResult res;
     84         String toS;
     85         for (int i = 0; i < enS.length; i++) {
     86             for (int j = 0; j < enHS.length; j++) {
     87                 for (int n = 0; n < pos.length; n++) {
     88                     for (int l = 0; l < pos.length; l++) {
     89                         res = new SSLEngineResult(enS[i], enHS[j], pos[n],
     90                                 pos[l]);
     91 
     92                         assertEquals("Incorrect bytesProduced", res
     93                                 .bytesProduced(), pos[l]);
     94                         assertEquals("Incorrect bytesConsumed", res
     95                                 .bytesConsumed(), pos[n]);
     96                         assertEquals("Incorrect HandshakeStatus", res
     97                                 .getHandshakeStatus(), enHS[j]);
     98                         assertEquals("Incorrect Status", res.getStatus(),
     99                                 enS[i]);
    100                         toS = res.toString();
    101                         assertNotNull("Result of toSring() method is null", toS);
    102                     }
    103                 }
    104             }
    105         }
    106 
    107     }
    108     /**
    109      * Test for <code>SSLEngineResult.Status.values()</code> method
    110      */
    111 
    112     public void testStatus01() {
    113         SSLEngineResult.Status [] enS = SSLEngineResult.Status.values();
    114         assertTrue("Incorrect array of Status objects", enS.length > 0);
    115         assertTrue("OK object does not define", findEl(enS,
    116                 SSLEngineResult.Status.OK));
    117         assertTrue("CLOSED object does not define", findEl(enS,
    118                 SSLEngineResult.Status.CLOSED));
    119         assertTrue("BUFFER_OVERFLOW object does not define", findEl(enS,
    120                 SSLEngineResult.Status.BUFFER_OVERFLOW));
    121         assertTrue("BUFFER_UNDERFLOW object does not define", findEl(enS,
    122                 SSLEngineResult.Status.BUFFER_UNDERFLOW));
    123     }
    124 
    125     /**
    126      * Test for <code>SSLEngineResult.Status.valueOf(String name)</code> method
    127      * Assertion:
    128      * throws IllegalArgumentException when there is no constan with specified
    129      * name
    130      */
    131 
    132     public void testStatus02() {
    133         String [] invalid = {"", "OK1", "BUFFER_overflow", "BUFFER_UND",
    134                 "CLOSED_CLOSED", "Bad string for verification valueOf method"
    135         };
    136         assertEquals(SSLEngineResult.Status.valueOf("BUFFER_OVERFLOW"),
    137                 SSLEngineResult.Status.BUFFER_OVERFLOW);
    138         assertEquals(SSLEngineResult.Status.valueOf("BUFFER_UNDERFLOW"),
    139                 SSLEngineResult.Status.BUFFER_UNDERFLOW);
    140         assertEquals(SSLEngineResult.Status.valueOf("CLOSED"),
    141                 SSLEngineResult.Status.CLOSED);
    142         assertEquals(SSLEngineResult.Status.valueOf("OK"),
    143                 SSLEngineResult.Status.OK);
    144         for (int i = 0; i < invalid.length; i++) {
    145             try {
    146                 SSLEngineResult.Status.valueOf(invalid[i]);
    147                 fail("IllegalArgumentException must be thrown for name: " + invalid[i]);
    148             } catch (IllegalArgumentException e) {
    149             }
    150         }
    151     }
    152 
    153     /**
    154      * Test for <code>SSLEngineResult.HandshakeStatus.values()</code> method
    155      */
    156 
    157     public void testHandshakeStatus01() {
    158         SSLEngineResult.HandshakeStatus [] enHS = SSLEngineResult.HandshakeStatus
    159                 .values();
    160         assertTrue("Incorrect array of HandshakeStatus objects",
    161                 enHS.length > 0);
    162         assertTrue("FINISHED object does not define", findEl(enHS,
    163                 SSLEngineResult.HandshakeStatus.FINISHED));
    164         assertTrue("NEED_UNWRAP object does not define", findEl(enHS,
    165                 SSLEngineResult.HandshakeStatus.NEED_UNWRAP));
    166         assertTrue("NEED_WRAP object does not define", findEl(enHS,
    167                 SSLEngineResult.HandshakeStatus.NEED_WRAP));
    168         assertTrue("NEED_TASK object does not define", findEl(enHS,
    169                 SSLEngineResult.HandshakeStatus.NEED_TASK));
    170         assertTrue("NOT_HANDSHAKING object does not define", findEl(enHS,
    171                 SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING));
    172     }
    173 
    174     /**
    175      * Test for <code>SSLEngineResult.HandshakeStatus.valueOf(String name)</code> method
    176      * Assertion:
    177      * throws IllegalArgumentException when there is no constan with specified
    178      * name
    179      */
    180 
    181     public void testHandshakeStatus02() {
    182         String [] invalid = {"", "FINISHED1", "NEED_task", "NEED_UN",
    183                 "NEED_WRAP_WRAP", "not_HANDSHAKING", "Bad string for verification valueOf method"
    184         };
    185         assertEquals(SSLEngineResult.HandshakeStatus.valueOf("NOT_HANDSHAKING"),
    186                 SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING);
    187         assertEquals(SSLEngineResult.HandshakeStatus.valueOf("NEED_WRAP"),
    188                 SSLEngineResult.HandshakeStatus.NEED_WRAP);
    189         assertEquals(SSLEngineResult.HandshakeStatus.valueOf("NEED_UNWRAP"),
    190                 SSLEngineResult.HandshakeStatus.NEED_UNWRAP);
    191         assertEquals(SSLEngineResult.HandshakeStatus.valueOf("FINISHED"),
    192                 SSLEngineResult.HandshakeStatus.FINISHED);
    193         assertEquals(SSLEngineResult.HandshakeStatus.valueOf("NEED_TASK"),
    194                 SSLEngineResult.HandshakeStatus.NEED_TASK);
    195         for (int i = 0; i < invalid.length; i++) {
    196             try {
    197                 SSLEngineResult.HandshakeStatus.valueOf(invalid[i]);
    198                 fail("IllegalArgumentException must be thrown for name: " + invalid[i]);
    199             } catch (IllegalArgumentException e) {
    200             }
    201         }
    202     }
    203 
    204     private boolean findEl(Object[] arr, Object el) {
    205         boolean ok = false;
    206         for (int i = 0; i < arr.length; i++) {
    207             if (arr[i].equals(el)) {
    208                 ok = true;
    209                 break;
    210             }
    211         }
    212         return ok;
    213     }
    214 
    215 }
    216