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.tests.javax.net.ssl;
     19 
     20 import javax.net.ssl.SSLEngineResult;
     21 import junit.framework.TestCase;
     22 
     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 test_ConstructorLjavax_net_ssl_SSLEngineResult_StatusLjavax_net_ssl_SSLEngineResult_HandshakeStatusII() {
     48 
     49         int[] neg = { -1, -10, -1000, Integer.MIN_VALUE,
     50                 (Integer.MIN_VALUE + 1) };
     51         try {
     52             new SSLEngineResult(null, SSLEngineResult.HandshakeStatus.FINISHED,
     53                     1, 1);
     54             fail("IllegalArgumentException must be thrown");
     55         } catch (IllegalArgumentException e) {
     56         }
     57         try {
     58             new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, null,
     59                     1, 1);
     60             fail("IllegalArgumentException must be thrown");
     61         } catch (IllegalArgumentException e) {
     62         }
     63         for (int i = 0; i < neg.length; i++) {
     64             try {
     65                 new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW,
     66                         SSLEngineResult.HandshakeStatus.FINISHED, neg[i], 1);
     67                 fail("IllegalArgumentException must be thrown");
     68             } catch (IllegalArgumentException e) {
     69             }
     70         }
     71         for (int i = 0; i < neg.length; i++) {
     72             try {
     73                 new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW,
     74                         SSLEngineResult.HandshakeStatus.FINISHED, 1, neg[i]);
     75                 fail("IllegalArgumentException must be thrown");
     76             } catch (IllegalArgumentException e) {
     77             }
     78         }
     79 
     80         try {
     81             SSLEngineResult res = new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW,
     82                     SSLEngineResult.HandshakeStatus.FINISHED, 1, 2);
     83             assertNotNull("Null object", res);
     84             assertEquals(1, res.bytesConsumed());
     85             assertEquals(2, res.bytesProduced());
     86         } catch (Exception e) {
     87             fail("Unexpected exception: " + e);
     88         }
     89     }
     90 
     91     /**
     92      * Test for <code>bytesConsumed()</code> method
     93      */
     94     public void test_bytesConsumed() {
     95         int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) };
     96         SSLEngineResult.Status [] enS =
     97             SSLEngineResult.Status.values();
     98         SSLEngineResult.HandshakeStatus [] enHS =
     99             SSLEngineResult.HandshakeStatus.values();
    100         for (int i = 0; i < enS.length; i++) {
    101             for (int j = 0; j < enHS.length; j++) {
    102                 for (int n = 0; n < pos.length; n++) {
    103                     for (int l = 0; l < pos.length; l++) {
    104                         SSLEngineResult res = new SSLEngineResult(enS[i],
    105                                 enHS[j], pos[n], pos[l]);
    106                         assertEquals("Incorrect bytesConsumed", pos[n],
    107                                 res.bytesConsumed());
    108                     }
    109                 }
    110             }
    111         }
    112     }
    113 
    114     /**
    115      * Test for <code>bytesProduced()</code> method
    116      */
    117     public void test_bytesProduced() {
    118         int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) };
    119         SSLEngineResult.Status [] enS =
    120             SSLEngineResult.Status.values();
    121         SSLEngineResult.HandshakeStatus [] enHS =
    122             SSLEngineResult.HandshakeStatus.values();
    123         for (int i = 0; i < enS.length; i++) {
    124             for (int j = 0; j < enHS.length; j++) {
    125                 for (int n = 0; n < pos.length; n++) {
    126                     for (int l = 0; l < pos.length; ++l) {
    127                         SSLEngineResult res = new SSLEngineResult(enS[i],
    128                                 enHS[j], pos[n], pos[l]);
    129                         assertEquals("Incorrect bytesProduced", pos[l],
    130                                 res.bytesProduced());
    131                     }
    132                 }
    133             }
    134         }
    135     }
    136 
    137     /**
    138      * Test for <code>getHandshakeStatus()</code> method
    139      */
    140     public void test_getHandshakeStatus() {
    141         int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) };
    142         SSLEngineResult.Status [] enS =
    143             SSLEngineResult.Status.values();
    144         SSLEngineResult.HandshakeStatus [] enHS =
    145             SSLEngineResult.HandshakeStatus.values();
    146         for (int i = 0; i < enS.length; i++) {
    147             for (int j = 0; j < enHS.length; j++) {
    148                 for (int n = 0; n < pos.length; n++) {
    149                     for (int l = 0; l < pos.length; ++l) {
    150                         SSLEngineResult res = new SSLEngineResult(enS[i],
    151                                 enHS[j], pos[n], pos[l]);
    152                         assertEquals("Incorrect HandshakeStatus", enHS[j],
    153                                 res.getHandshakeStatus());
    154                     }
    155                 }
    156             }
    157         }
    158     }
    159 
    160     /**
    161      * Test for <code>getStatus()</code> method
    162      */
    163     public void test_getStatus() {
    164         int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) };
    165         SSLEngineResult.Status [] enS =
    166             SSLEngineResult.Status.values();
    167         SSLEngineResult.HandshakeStatus [] enHS =
    168             SSLEngineResult.HandshakeStatus.values();
    169         for (int i = 0; i < enS.length; i++) {
    170             for (int j = 0; j < enHS.length; j++) {
    171                 for (int n = 0; n < pos.length; n++) {
    172                     for (int l = 0; l < pos.length; ++l) {
    173                         SSLEngineResult res = new SSLEngineResult(enS[i],
    174                                 enHS[j], pos[n], pos[l]);
    175                         assertEquals("Incorrect Status", enS[i],
    176                                 res.getStatus());
    177                     }
    178                 }
    179             }
    180         }
    181     }
    182 
    183     /**
    184      * Test for <code>toString()</code> method
    185      */
    186     public void test_toString() {
    187         int[] pos = { 0, 1, 1000, Integer.MAX_VALUE, (Integer.MAX_VALUE - 1) };
    188         SSLEngineResult.Status [] enS =
    189             SSLEngineResult.Status.values();
    190         SSLEngineResult.HandshakeStatus [] enHS =
    191             SSLEngineResult.HandshakeStatus.values();
    192         for (int i = 0; i < enS.length; i++) {
    193             for (int j = 0; j < enHS.length; j++) {
    194                 for (int n = 0; n < pos.length; n++) {
    195                     for (int l = 0; l < pos.length; ++l) {
    196                         SSLEngineResult res = new SSLEngineResult(enS[i],
    197                                 enHS[j], pos[n], pos[l]);
    198                         assertNotNull("Result of toSring() method is null",
    199                                 res.toString());
    200                     }
    201                 }
    202             }
    203         }
    204     }
    205 
    206     private boolean findEl(Object[] arr, Object el) {
    207         boolean ok = false;
    208         for (int i = 0; i < arr.length; i++) {
    209             if (arr[i].equals(el)) {
    210                 ok = true;
    211                 break;
    212             }
    213         }
    214         return ok;
    215     }
    216 
    217 }
    218