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 tests.api.javax.net.ssl;
     19 
     20 import java.io.IOException;
     21 import java.nio.ByteBuffer;
     22 import java.nio.ReadOnlyBufferException;
     23 import java.nio.channels.Pipe;
     24 import java.nio.channels.Pipe.SinkChannel;
     25 import java.nio.channels.Pipe.SourceChannel;
     26 import java.security.KeyManagementException;
     27 import java.security.NoSuchAlgorithmException;
     28 import java.util.Arrays;
     29 import java.util.HashSet;
     30 import java.util.Set;
     31 
     32 import javax.net.ssl.SSLContext;
     33 import javax.net.ssl.SSLEngine;
     34 import javax.net.ssl.SSLEngineResult;
     35 import javax.net.ssl.SSLException;
     36 import javax.net.ssl.SSLEngineResult.HandshakeStatus;
     37 
     38 import junit.framework.TestCase;
     39 import dalvik.annotation.AndroidOnly;
     40 import dalvik.annotation.KnownFailure;
     41 
     42 
     43 /**
     44  * Tests for SSLEngine class
     45  *
     46  */
     47 public class SSLEngineTest extends TestCase {
     48 
     49     private HandshakeHandler clientEngine;
     50     private HandshakeHandler serverEngine;
     51 
     52     @Override protected void setUp() throws Exception {
     53         super.setUp();
     54     }
     55 
     56     /**
     57      * Test for <code>SSLEngine()</code> constructor Assertion: creates
     58      * SSLEngine object with null host and -1 port
     59      * @throws NoSuchAlgorithmException
     60      */
     61     public void test_Constructor() throws NoSuchAlgorithmException {
     62         SSLEngine e = getEngine();
     63         assertNull(e.getPeerHost());
     64         assertEquals(-1, e.getPeerPort());
     65         String[] suites = e.getSupportedCipherSuites();
     66         e.setEnabledCipherSuites(suites);
     67         assertEquals(e.getEnabledCipherSuites().length, suites.length);
     68     }
     69 
     70     /**
     71      * Test for <code>SSLEngine(String host, int port)</code> constructor
     72      * @throws NoSuchAlgorithmException
     73      */
     74     public void test_ConstructorLjava_lang_StringI01() throws NoSuchAlgorithmException {
     75         int port = 1010;
     76         SSLEngine e = getEngine(null, port);
     77         assertNull(e.getPeerHost());
     78         assertEquals(e.getPeerPort(), port);
     79         try {
     80             e.beginHandshake();
     81         } catch (IllegalStateException ex) {
     82             // expected
     83         } catch (SSLException ex) {
     84             fail("unexpected SSLException was thrown.");
     85         }
     86         e = getEngine(null, port);
     87         e.setUseClientMode(true);
     88         try {
     89             e.beginHandshake();
     90         } catch (SSLException ex) {
     91             // expected
     92         }
     93         e = getEngine(null, port);
     94         e.setUseClientMode(false);
     95         try {
     96             e.beginHandshake();
     97         } catch (SSLException ex) {
     98             // expected
     99         }
    100     }
    101 
    102     /**
    103      * Test for <code>SSLEngine(String host, int port)</code> constructor
    104      * @throws NoSuchAlgorithmException
    105      */
    106     public void test_ConstructorLjava_lang_StringI02() throws NoSuchAlgorithmException {
    107         String host = "new host";
    108         int port = 8080;
    109         SSLEngine e = getEngine(host, port);
    110         assertEquals(e.getPeerHost(), host);
    111         assertEquals(e.getPeerPort(), port);
    112         String[] suites = e.getSupportedCipherSuites();
    113         e.setEnabledCipherSuites(suites);
    114         assertEquals(e.getEnabledCipherSuites().length, suites.length);
    115         e.setUseClientMode(true);
    116         assertTrue(e.getUseClientMode());
    117     }
    118 
    119     /**
    120      * Test for <code>getPeerHost()</code> method
    121      * @throws NoSuchAlgorithmException
    122      */
    123     public void test_getPeerHost() throws NoSuchAlgorithmException {
    124         SSLEngine e = getEngine();
    125         assertNull(e.getPeerHost());
    126         e = getEngine("www.fortify.net", 80);
    127         assertEquals("Incorrect host name", "www.fortify.net", e.getPeerHost());
    128     }
    129 
    130     /**
    131      * Test for <code>getPeerPort()</code> method
    132      * @throws NoSuchAlgorithmException
    133      */
    134     public void test_getPeerPort() throws NoSuchAlgorithmException {
    135         SSLEngine e = getEngine();
    136         assertEquals("Incorrect default value of peer port",
    137                 -1 ,e.getPeerPort());
    138         e = getEngine("www.fortify.net", 80);
    139         assertEquals("Incorrect peer port", 80, e.getPeerPort());
    140     }
    141 
    142     /**
    143      * @throws NoSuchAlgorithmException
    144      * javax.net.ssl.SSLEngine#getSupportedProtocols()
    145      */
    146     public void test_getSupportedProtocols() throws NoSuchAlgorithmException {
    147         SSLEngine sse = getEngine();
    148         try {
    149             String[] res = sse.getSupportedProtocols();
    150             assertNotNull(res);
    151             assertTrue(res.length > 0);
    152         } catch (Exception ex) {
    153             fail("Unexpected exception " + ex);
    154         }
    155     }
    156 
    157     /**
    158      * @throws NoSuchAlgorithmException
    159      * javax.net.ssl.SSLEngine#setEnabledProtocols(String[] protocols)
    160      * javax.net.ssl.SSLEngine#getEnabledProtocols()
    161      */
    162     public void test_EnabledProtocols() throws NoSuchAlgorithmException {
    163         SSLEngine sse = getEngine();
    164         String[] pr = sse.getSupportedProtocols();
    165         try {
    166             sse.setEnabledProtocols(pr);
    167             String[] res = sse.getEnabledProtocols();
    168             assertNotNull("Null array was returned", res);
    169             assertEquals("Incorrect array length", res.length, pr.length);
    170             assertTrue("Incorrect array was returned", Arrays.equals(res, pr));
    171         } catch (Exception ex) {
    172             fail("Unexpected exception " + ex);
    173         }
    174         try {
    175             sse.setEnabledProtocols(null);
    176             fail("IllegalArgumentException wasn't thrown");
    177         } catch (IllegalArgumentException iae) {
    178             //expected
    179         }
    180     }
    181 
    182     /**
    183      * @throws NoSuchAlgorithmException
    184      * javax.net.ssl.SSLEngine#getSupportedCipherSuites()
    185      */
    186     public void test_getSupportedCipherSuites() throws NoSuchAlgorithmException {
    187         SSLEngine sse = getEngine();
    188         try {
    189             String[] res = sse.getSupportedCipherSuites();
    190             assertNotNull(res);
    191             assertTrue(res.length > 0);
    192         } catch (Exception ex) {
    193             fail("Unexpected exception " + ex);
    194         }
    195     }
    196 
    197     /**
    198      * @throws NoSuchAlgorithmException
    199      * javax.net.ssl.SSLEngine#setEnabledCipherSuites(String[] suites)
    200      * javax.net.ssl.SSLEngine#getEnabledCipherSuites()
    201      */
    202     public void test_EnabledCipherSuites() throws NoSuchAlgorithmException {
    203         SSLEngine sse = getEngine();
    204         String[] st = sse.getSupportedCipherSuites();
    205         try {
    206             sse.setEnabledCipherSuites(st);
    207             String[] res = sse.getEnabledCipherSuites();
    208             assertNotNull("Null array was returned", res);
    209             assertEquals("Incorrect array length", res.length, st.length);
    210             assertTrue("Incorrect array was returned", Arrays.equals(res, st));
    211         } catch (Exception ex) {
    212             fail("Unexpected exception " + ex);
    213         }
    214         try {
    215             sse.setEnabledCipherSuites(null);
    216             fail("IllegalArgumentException wasn't thrown");
    217         } catch (IllegalArgumentException iae) {
    218             //expected
    219         }
    220     }
    221 
    222     /**
    223      * @throws NoSuchAlgorithmException
    224      * javax.net.ssl.SSLEngine#setEnableSessionCreation(boolean flag)
    225      * javax.net.ssl.SSLEngine#getEnableSessionCreation()
    226      */
    227     public void test_EnableSessionCreation() throws NoSuchAlgorithmException {
    228         SSLEngine sse = getEngine();
    229         try {
    230             assertTrue(sse.getEnableSessionCreation());
    231             sse.setEnableSessionCreation(false);
    232             assertFalse(sse.getEnableSessionCreation());
    233             sse.setEnableSessionCreation(true);
    234             assertTrue(sse.getEnableSessionCreation());
    235         } catch (Exception ex) {
    236             fail("Unexpected exception " + ex);
    237         }
    238     }
    239 
    240     /**
    241      * @throws NoSuchAlgorithmException
    242      * javax.net.ssl.SSLEngine#setNeedClientAuth(boolean need)
    243      * javax.net.ssl.SSLEngine#getNeedClientAuth()
    244      */
    245     public void test_NeedClientAuth() throws NoSuchAlgorithmException {
    246         SSLEngine sse = getEngine();
    247         try {
    248             sse.setNeedClientAuth(false);
    249             assertFalse(sse.getNeedClientAuth());
    250             sse.setNeedClientAuth(true);
    251             assertTrue(sse.getNeedClientAuth());
    252         } catch (Exception ex) {
    253             fail("Unexpected exception " + ex);
    254         }
    255     }
    256 
    257     /**
    258      * @throws NoSuchAlgorithmException
    259      * javax.net.ssl.SSLEngine#setWantClientAuth(boolean want)
    260      * javax.net.ssl.SSLEngine#getWantClientAuth()
    261      */
    262     public void test_WantClientAuth() throws NoSuchAlgorithmException {
    263         SSLEngine sse = getEngine();
    264         try {
    265             sse.setWantClientAuth(false);
    266             assertFalse(sse.getWantClientAuth());
    267             sse.setWantClientAuth(true);
    268             assertTrue(sse.getWantClientAuth());
    269         } catch (Exception ex) {
    270             fail("Unexpected exception " + ex);
    271         }
    272     }
    273 
    274     /**
    275      * @throws NoSuchAlgorithmException
    276      * javax.net.ssl.SSLEngine#beginHandshake()
    277      */
    278     public void test_beginHandshake() throws NoSuchAlgorithmException {
    279         SSLEngine sse = getEngine();
    280         try {
    281             sse.beginHandshake();
    282             fail("IllegalStateException wasn't thrown");
    283         } catch (IllegalStateException se) {
    284             //expected
    285         } catch (Exception e) {
    286             fail(e + " was thrown instead of IllegalStateException");
    287         }
    288         sse = getEngine("new host", 1080);
    289         try {
    290             sse.beginHandshake();
    291             fail("IllegalStateException wasn't thrown");
    292         } catch (IllegalStateException ise) {
    293             //expected
    294         } catch (Exception e) {
    295             fail(e + " was thrown instead of IllegalStateException");
    296         }
    297         sse = getEngine();
    298         try {
    299             sse.setUseClientMode(true);
    300             sse.beginHandshake();
    301         } catch (Exception ex) {
    302             fail("Unexpected exception " + ex);
    303         }
    304     }
    305 
    306     /**
    307      * @throws NoSuchAlgorithmException
    308      * javax.net.ssl.SSLEngine#setUseClientMode(boolean mode)
    309      * javax.net.ssl.SSLEngine#getUseClientMode()
    310      */
    311     @AndroidOnly("The RI doesn't throw the expected IllegalStateException.")
    312     public void test_UseClientMode() throws NoSuchAlgorithmException {
    313         SSLEngine sse = getEngine();
    314         try {
    315             sse.setUseClientMode(false);
    316             assertFalse(sse.getUseClientMode());
    317             sse.setUseClientMode(true);
    318             assertTrue(sse.getUseClientMode());
    319         } catch (Exception ex) {
    320             fail("Unexpected exception " + ex);
    321         }
    322 
    323         try {
    324             sse = getEngine(null, 1080);
    325             sse.setUseClientMode(true);
    326             sse.beginHandshake();
    327             try {
    328                 sse.setUseClientMode(false);
    329                 fail("IllegalArgumentException was not thrown");
    330             } catch (IllegalArgumentException iae) {
    331                 //expected
    332             }
    333         } catch (Exception ex) {
    334             fail("Unexpected exception " + ex);
    335         }
    336     }
    337 
    338     /**
    339      * @throws NoSuchAlgorithmException
    340      * javax.net.ssl.SSLEngine#getSession()
    341      */
    342     public void test_getSession() throws NoSuchAlgorithmException {
    343         SSLEngine sse = getEngine();
    344         try {
    345             assertNotNull(sse.getSession());
    346         } catch (Exception ex) {
    347             fail("Unexpected exception " + ex);
    348         }
    349     }
    350 
    351     /**
    352      * @throws NoSuchAlgorithmException
    353      * javax.net.ssl.SSLEngine#getHandshakeStatus()
    354      */
    355     public void test_getHandshakeStatus() throws NoSuchAlgorithmException {
    356         SSLEngine sse = getEngine();
    357         try {
    358             assertEquals(sse.getHandshakeStatus().toString(), "NOT_HANDSHAKING");
    359             sse.setUseClientMode(true);
    360             sse.beginHandshake();
    361             assertEquals(sse.getHandshakeStatus().toString(), "NEED_WRAP");
    362         } catch (Exception ex) {
    363             fail("Unexpected exception " + ex);
    364         }
    365     }
    366 
    367     /**
    368      * @throws NoSuchAlgorithmException
    369      * javax.net.ssl.SSLEngine#getDelegatedTask()
    370      */
    371     @KnownFailure("org.apache.harmony.xnet.provider.jsse.SSLEngineImpl#getDelegatedTask() throws NPE instead of returning null")
    372     public void test_getDelegatedTask() throws NoSuchAlgorithmException {
    373         SSLEngine sse = getEngine();
    374         try {
    375             assertNull(sse.getDelegatedTask());
    376         } catch (Exception ex) {
    377             fail("Unexpected exception " + ex);
    378         }
    379     }
    380 
    381     /**
    382      * @throws IOException
    383      * @throws InterruptedException
    384      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
    385      *                                       int offset, int length)
    386      * Exception case: SSLException should be thrown.
    387      */
    388     public void test_unwrap_01() throws IOException, InterruptedException {
    389         prepareEngines();
    390         doHandshake();
    391 
    392         ByteBuffer bbs = ByteBuffer.wrap(new byte[] {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,31,2,3,1,2,3,1,2,3,1,2,3});
    393         ByteBuffer bbd = ByteBuffer.allocate(100);
    394         try {
    395             clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }, 0, 1);
    396             fail("SSLException wasn't thrown");
    397         } catch (SSLException ex) {
    398             //expected
    399         }
    400     }
    401 
    402     /**
    403      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
    404      *                                       int offset, int length)
    405      * Exception case: IndexOutOfBoundsException should be thrown.
    406      */
    407     @KnownFailure("Fixed in DonutBurger, boundary checks missing")
    408     public void test_unwrap_02() throws SSLException {
    409         String host = "new host";
    410         int port = 8080;
    411         ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
    412 
    413         ByteBuffer bb = ByteBuffer.allocate(10);
    414         SSLEngine sse = getEngine(host, port);
    415         sse.setUseClientMode(true);
    416 
    417         try {
    418             sse.unwrap(bb, bbA, -1, 3);
    419             fail("IndexOutOfBoundsException wasn't thrown");
    420         } catch (IndexOutOfBoundsException iobe) {
    421             //expected
    422         }
    423         try {
    424             sse.unwrap(bb, bbA, 0, -3);
    425             fail("IndexOutOfBoundsException wasn't thrown");
    426         } catch (IndexOutOfBoundsException iobe) {
    427             //expected
    428         }
    429         try {
    430             sse.unwrap(bb, bbA, bbA.length + 1, bbA.length);
    431             fail("IndexOutOfBoundsException wasn't thrown");
    432         } catch (IndexOutOfBoundsException iobe) {
    433             //expected
    434         }
    435         try {
    436             sse.unwrap(bb, bbA, 0, bbA.length + 1);
    437             fail("IndexOutOfBoundsException wasn't thrown");
    438         } catch (IndexOutOfBoundsException iobe) {
    439             //expected
    440         }
    441     }
    442 
    443     /**
    444      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
    445      *                                       int offset, int length)
    446      * Exception case: ReadOnlyBufferException should be thrown.
    447      */
    448     @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
    449     public void test_unwrap_03() {
    450         String host = "new host";
    451         int port = 8080;
    452         ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer();
    453         ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
    454 
    455         ByteBuffer bb = ByteBuffer.allocate(10);
    456         SSLEngine sse = getEngine(host, port);
    457         sse.setUseClientMode(true);
    458 
    459         try {
    460             sse.unwrap(bb, bbA, 0, bbA.length);
    461             fail("ReadOnlyBufferException wasn't thrown");
    462         } catch (ReadOnlyBufferException iobe) {
    463             //expected
    464         } catch (Exception e) {
    465             fail(e + " was thrown instead of ReadOnlyBufferException");
    466         }
    467     }
    468 
    469     /**
    470      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
    471      *                                       int offset, int length)
    472      * Exception case: IllegalArgumentException should be thrown.
    473      */
    474     @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
    475     public void test_unwrap_04() {
    476         String host = "new host";
    477         int port = 8080;
    478         ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)};
    479         ByteBuffer[] bbAN = {ByteBuffer.allocate(100), null, ByteBuffer.allocate(100)};
    480         ByteBuffer[] bbN = null;
    481         ByteBuffer bb = ByteBuffer.allocate(10);
    482         ByteBuffer bN = null;
    483         SSLEngine sse = getEngine(host, port);
    484         sse.setUseClientMode(true);
    485 
    486         try {
    487             sse.unwrap(bN, bbA, 0, 3);
    488             fail("IllegalArgumentException wasn't thrown");
    489         } catch (IllegalArgumentException iobe) {
    490             //expected
    491         } catch (NullPointerException npe) {
    492         } catch (Exception e) {
    493             fail(e + " was thrown instead of IllegalArgumentException");
    494         }
    495         try {
    496             sse.unwrap(bb, bbAN, 0, 3);
    497             fail("IllegalArgumentException wasn't thrown");
    498         } catch (IllegalArgumentException iobe) {
    499             //expected
    500         } catch (NullPointerException npe) {
    501         } catch (Exception e) {
    502             fail(e + " was thrown instead of IllegalArgumentException");
    503         }
    504         try {
    505             sse.unwrap(bb, bbN, 0, 0);
    506             fail("IllegalArgumentException wasn't thrown");
    507         } catch (IllegalArgumentException iobe) {
    508             //expected
    509         } catch (NullPointerException npe) {
    510         } catch (Exception e) {
    511             fail(e + " was thrown instead of IllegalArgumentException");
    512         }
    513         try {
    514             sse.unwrap(bN, bbN, 0, 0);
    515             fail("IllegalArgumentException wasn't thrown");
    516         } catch (IllegalArgumentException iobe) {
    517             //expected
    518         } catch (NullPointerException npe) {
    519         } catch (Exception e) {
    520             fail(e + " was thrown instead of IllegalArgumentException");
    521         }
    522 
    523     }
    524 
    525     /**
    526      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
    527      *                                       int offset, int length)
    528      * Exception case: IllegalStateException should be thrown.
    529      */
    530     @AndroidOnly("The RI doesn't throw the IllegalStateException.")
    531     public void test_unwrap_05() {
    532         String host = "new host";
    533         int port = 8080;
    534         ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
    535 
    536         ByteBuffer bb = ByteBuffer.allocate(10);
    537         SSLEngine sse = getEngine(host, port);
    538 
    539         try {
    540             sse.unwrap(bb, bbA, 0, bbA.length);
    541             fail("IllegalStateException wasn't thrown");
    542         } catch (IllegalStateException iobe) {
    543             //expected
    544         } catch (Exception e) {
    545             fail(e + " was thrown instead of IllegalStateException");
    546         }
    547     }
    548 
    549     /**
    550      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
    551      *                                       int offset, int length)
    552      */
    553     public void test_unwrap_06() {
    554         String host = "new host";
    555         int port = 8080;
    556         ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
    557 
    558         ByteBuffer bb = ByteBuffer.allocate(10);
    559         SSLEngine sse = getEngine(host, port);
    560         sse.setUseClientMode(true);
    561 
    562         try {
    563             SSLEngineResult res = sse.unwrap(bb, bbA, 0, bbA.length);
    564             assertEquals(0, res.bytesConsumed());
    565             assertEquals(0, res.bytesProduced());
    566         } catch (Exception ex) {
    567             fail("Unexpected exception: " + ex);
    568         }
    569     }
    570 
    571     public void test_wrap_01() throws IOException, InterruptedException {
    572         prepareEngines();
    573         doHandshake();
    574         ByteBuffer bbs = ByteBuffer.allocate(100);
    575         ByteBuffer bbd = ByteBuffer.allocate(20000);
    576         clientEngine.engine.wrap(new ByteBuffer[] { bbs }, 0, 1, bbd);
    577     }
    578 
    579     /**
    580      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
    581      *                                     int length, ByteBuffer dst)
    582      * Exception case: IndexOutOfBoundsException should be thrown.
    583      */
    584     @KnownFailure("Fixed in DonutBurger, boundary checks missing")
    585     public void test_wrap_02() throws SSLException {
    586         String host = "new host";
    587         int port = 8080;
    588         ByteBuffer bb = ByteBuffer.allocate(10);
    589         ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
    590         SSLEngine sse = getEngine(host, port);
    591         sse.setUseClientMode(true);
    592 
    593         try {
    594             sse.wrap(bbA, -1, 3, bb);
    595             fail("IndexOutOfBoundsException wasn't thrown");
    596         } catch (IndexOutOfBoundsException iobe) {
    597             //expected
    598         }
    599         try {
    600             sse.wrap(bbA, 0, -3, bb);
    601             fail("IndexOutOfBoundsException wasn't thrown");
    602         } catch (IndexOutOfBoundsException iobe) {
    603             //expected
    604         }
    605         try {
    606             sse.wrap(bbA, bbA.length + 1, bbA.length, bb);
    607             fail("IndexOutOfBoundsException wasn't thrown");
    608         } catch (IndexOutOfBoundsException iobe) {
    609             //expected
    610         }
    611         try {
    612             sse.wrap(bbA, 0, bbA.length + 1, bb);
    613             fail("IndexOutOfBoundsException wasn't thrown");
    614         } catch (IndexOutOfBoundsException iobe) {
    615             //expected
    616         }
    617     }
    618 
    619     /**
    620      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
    621      *                                     int length, ByteBuffer dst)
    622      * Exception case: ReadOnlyBufferException should be thrown.
    623      */
    624     public void test_wrap_03() throws SSLException {
    625         String host = "new host";
    626         int port = 8080;
    627         ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer();
    628         ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
    629         SSLEngine sse = getEngine(host, port);
    630         sse.setUseClientMode(true);
    631 
    632         try {
    633             sse.wrap(bbA, 0, bbA.length, bb);
    634             fail("ReadOnlyBufferException wasn't thrown");
    635         } catch (ReadOnlyBufferException iobe) {
    636             //expected
    637         }
    638     }
    639 
    640     /**
    641      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
    642      *                                     int length, ByteBuffer dst)
    643      * Exception case: IllegalArgumentException should be thrown.
    644      */
    645     @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
    646     public void test_wrap_04() {
    647         String host = "new host";
    648         int port = 8080;
    649         ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)};
    650         ByteBuffer[] bbN = null;
    651         ByteBuffer bN = null;
    652         SSLEngine e = getEngine(host, port);
    653         e.setUseClientMode(true);
    654 
    655         try {
    656             e.wrap(bbA, 0, 3, bN);
    657             fail("IllegalArgumentException must be thrown for null srcs byte buffer array");
    658         } catch (NullPointerException npe) {
    659         } catch (IllegalArgumentException ex) {
    660         } catch (Exception ex) {
    661             fail(ex + " was thrown instead of IllegalArgumentException");
    662         }
    663 
    664         try {
    665             e.wrap(bbN, 0, 0, bN);
    666             fail("IllegalArgumentException wasn't thrown");
    667         } catch (IllegalArgumentException ex) {
    668         } catch (NullPointerException npe) {
    669         } catch (Exception ex) {
    670             fail(ex + " was thrown instead of IllegalArgumentException");
    671         }
    672     }
    673 
    674     /**
    675      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
    676      *                                     int length, ByteBuffer dst)
    677      * Exception case: IllegalStateException should be thrown.
    678      */
    679     @AndroidOnly("The RI doesn't throw the IllegalStateException.")
    680     public void test_wrap_05() throws SSLException {
    681         String host = "new host";
    682         int port = 8080;
    683         ByteBuffer bb = ByteBuffer.allocate(10);
    684         ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
    685         SSLEngine sse = getEngine(host, port);
    686 
    687         try {
    688             sse.wrap(bbA, 0, bbA.length, bb);
    689             fail("IllegalStateException wasn't thrown");
    690         } catch (IllegalStateException iobe) {
    691             //expected
    692         }
    693     }
    694 
    695     /**
    696      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
    697      *                                     int length, ByteBuffer dst)
    698      */
    699     public void test_wrap_06() {
    700         String host = "new host";
    701         int port = 8080;
    702         ByteBuffer bb = ByteBuffer.allocate(10);
    703         ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
    704         SSLEngine sse = getEngine(host, port);
    705         sse.setUseClientMode(true);
    706 
    707         try {
    708             sse.wrap(bbA, 0, bbA.length, bb);
    709         } catch (Exception ex) {
    710             fail("Unexpected exception: " + ex);
    711         }
    712     }
    713 
    714     /**
    715      * @throws NoSuchAlgorithmException
    716      * javax.net.ssl.SSLEngine#closeOutbound()
    717      * javax.net.ssl.SSLEngine#isOutboundDone()
    718      */
    719     public void test_closeOutbound() throws NoSuchAlgorithmException {
    720         SSLEngine sse = getEngine();
    721 
    722         try {
    723             assertFalse(sse.isOutboundDone());
    724             sse.closeOutbound();
    725             assertTrue(sse.isOutboundDone());
    726         } catch (Exception ex) {
    727             fail("Unexpected exception: " + ex);
    728         }
    729     }
    730 
    731     /**
    732      * @throws NoSuchAlgorithmException
    733      * javax.net.ssl.SSLEngine#closeInbound()
    734      * javax.net.ssl.SSLEngine#isInboundDone()
    735      */
    736     public void test_closeInbound() throws NoSuchAlgorithmException {
    737         SSLEngine sse = getEngine();
    738 
    739         try {
    740             assertFalse(sse.isInboundDone());
    741             sse.closeInbound();
    742             assertTrue(sse.isInboundDone());
    743         } catch (Exception ex) {
    744             fail("Unexpected exception: " + ex);
    745         }
    746     }
    747 
    748     /**
    749      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
    750      * SSLException should be thrown.
    751      */
    752     public void test_unwrap_ByteBuffer_ByteBuffer_01() throws InterruptedException, IOException {
    753         prepareEngines();
    754         doHandshake();
    755         ByteBuffer bbs = ByteBuffer.allocate(100);
    756         ByteBuffer bbd = ByteBuffer.allocate(100);
    757 
    758         try {
    759             SSLEngineResult unwrap = clientEngine.engine.unwrap(bbs, bbd);
    760             fail("SSLException wasn't thrown");
    761         } catch (SSLException ex) {
    762             //expected
    763         }
    764     }
    765 
    766     /**
    767      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
    768      * ReadOnlyBufferException should be thrown.
    769      */
    770     @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
    771     public void test_unwrap_ByteBuffer_ByteBuffer_02() {
    772         String host = "new host";
    773         int port = 8080;
    774         ByteBuffer bbs = ByteBuffer.allocate(10);
    775         ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer();
    776         SSLEngine sse = getEngine(host, port);
    777         sse.setUseClientMode(true);
    778 
    779         try {
    780             sse.unwrap(bbs, bbd);
    781             fail("ReadOnlyBufferException wasn't thrown");
    782         } catch (ReadOnlyBufferException iobe) {
    783             //expected
    784         } catch (Exception e) {
    785             fail(e + " was thrown instead of ReadOnlyBufferException");
    786         }
    787     }
    788 
    789     /**
    790      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
    791      * IllegalArgumentException should be thrown.
    792      */
    793     @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
    794     public void test_unwrap_ByteBuffer_ByteBuffer_03() {
    795         String host = "new host";
    796         int port = 8080;
    797         ByteBuffer bbsN = null;
    798         ByteBuffer bbdN = null;
    799         ByteBuffer bbs = ByteBuffer.allocate(10);
    800         ByteBuffer bbd = ByteBuffer.allocate(100);
    801         SSLEngine sse = getEngine(host, port);
    802         sse.setUseClientMode(true);
    803 
    804         try {
    805             sse.unwrap(bbsN, bbd);
    806             fail("IllegalArgumentException wasn't thrown");
    807         } catch (IllegalArgumentException iae) {
    808             //expected
    809         } catch (NullPointerException npe) {
    810         } catch (Exception e) {
    811             fail(e + " was thrown instead of IllegalArgumentException");
    812         }
    813 
    814         try {
    815             sse.unwrap(bbs, bbdN);
    816             fail("IllegalArgumentException wasn't thrown");
    817         } catch (IllegalArgumentException iae) {
    818             //expected
    819         } catch (NullPointerException npe) {
    820         } catch (Exception e) {
    821             fail(e + " was thrown instead of IllegalArgumentException");
    822         }
    823 
    824         try {
    825             sse.unwrap(bbsN, bbdN);
    826             fail("IllegalArgumentException wasn't thrown");
    827         } catch (IllegalArgumentException iae) {
    828             //expected
    829         } catch (NullPointerException npe) {
    830         } catch (Exception e) {
    831             fail(e + " was thrown instead of IllegalArgumentException");
    832         }
    833     }
    834 
    835     /**
    836      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
    837      * IllegalStateException should be thrown.
    838      */
    839     @AndroidOnly("The RI doesn't throw the IllegalStateException.")
    840     public void test_unwrap_ByteBuffer_ByteBuffer_04() {
    841         String host = "new host";
    842         int port = 8080;
    843         ByteBuffer bbs = ByteBuffer.allocate(10);
    844         ByteBuffer bbd = ByteBuffer.allocate(100);
    845         SSLEngine sse = getEngine(host, port);
    846 
    847         try {
    848             sse.unwrap(bbs, bbd);
    849             fail("IllegalStateException wasn't thrown");
    850         } catch (IllegalStateException iobe) {
    851             //expected
    852         } catch (Exception e) {
    853             fail(e + " was thrown instead of IllegalStateException");
    854         }
    855     }
    856 
    857     /**
    858      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
    859      */
    860     public void test_unwrap_ByteBuffer_ByteBuffer_05() {
    861         String host = "new host";
    862         int port = 8080;
    863         ByteBuffer bbs = ByteBuffer.allocate(10);
    864         ByteBuffer bbd = ByteBuffer.allocate(100);
    865         SSLEngine sse = getEngine(host, port);
    866         sse.setUseClientMode(true);
    867 
    868         try {
    869             SSLEngineResult res = sse.unwrap(bbs, bbd);
    870             assertEquals(0, res.bytesConsumed());
    871             assertEquals(0, res.bytesProduced());
    872         } catch (Exception e) {
    873             fail("Unexpected exception: " + e);
    874         }
    875     }
    876 
    877     /**
    878      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
    879      * SSLException should be thrown.
    880      */
    881     public void test_unwrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException {
    882         prepareEngines();
    883         doHandshake();
    884 
    885         ByteBuffer bbs = ByteBuffer.allocate(100);
    886         ByteBuffer bbd = ByteBuffer.allocate(100);
    887 
    888         try {
    889             clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd });
    890             fail("SSLException wasn't thrown");
    891         } catch (SSLException ex) {
    892             //expected
    893         }
    894     }
    895 
    896     /**
    897      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
    898      * ReadOnlyBufferException should be thrown.
    899      */
    900     @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
    901     public void test_unwrap_ByteBuffer$ByteBuffer_02() {
    902         String host = "new host";
    903         int port = 8080;
    904         ByteBuffer bbs = ByteBuffer.allocate(10);
    905         ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer();
    906         ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
    907         SSLEngine sse = getEngine(host, port);
    908         sse.setUseClientMode(true);
    909 
    910         try {
    911             sse.unwrap(bbs, bbA);
    912             fail("ReadOnlyBufferException wasn't thrown");
    913         } catch (ReadOnlyBufferException iobe) {
    914             //expected
    915         } catch (Exception e) {
    916             fail(e + " was thrown instead of ReadOnlyBufferException");
    917         }
    918     }
    919 
    920     /**
    921      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
    922      * IllegalArgumentException should be thrown.
    923      */
    924     @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
    925     public void test_unwrap_ByteBuffer$ByteBuffer_03() {
    926         String host = "new host";
    927         int port = 8080;
    928         ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
    929         ByteBuffer[] bbN = { ByteBuffer.allocate(100), null, ByteBuffer.allocate(100) };
    930         ByteBuffer[] bbAN = null;
    931         ByteBuffer bb = ByteBuffer.allocate(10);
    932         ByteBuffer bN = null;
    933         SSLEngine sse = getEngine(host, port);
    934         sse.setUseClientMode(true);
    935 
    936         try {
    937             sse.unwrap(bN, bbA);
    938             fail("IllegalArgumentException wasn't thrown");
    939         } catch (IllegalArgumentException iobe) {
    940             //expected
    941         } catch (NullPointerException npe) {
    942         } catch (Exception e) {
    943             fail(e + " was thrown instead of IllegalArgumentException");
    944         }
    945 
    946         try {
    947             sse.unwrap(bb, bbAN);
    948             fail("IllegalArgumentException wasn't thrown");
    949         } catch (IllegalArgumentException iobe) {
    950             //expected
    951         } catch (NullPointerException npe) {
    952         } catch (Exception e) {
    953             fail(e + " was thrown instead of IllegalArgumentException");
    954         }
    955 
    956         try {
    957             sse.unwrap(bb, bbN);
    958             fail("IllegalArgumentException wasn't thrown");
    959         } catch (IllegalArgumentException iobe) {
    960             //expected
    961         } catch (NullPointerException npe) {
    962         } catch (Exception e) {
    963             fail(e + " was thrown instead of IllegalArgumentException");
    964         }
    965 
    966         try {
    967             sse.unwrap(bN, bbAN);
    968             fail("IllegalArgumentException wasn't thrown");
    969         } catch (IllegalArgumentException iobe) {
    970             //expected
    971         } catch (NullPointerException npe) {
    972         } catch (Exception e) {
    973             fail(e + " was thrown instead of IllegalArgumentException");
    974         }
    975     }
    976 
    977     /**
    978      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
    979      * IllegalStateException should be thrown.
    980      */
    981     @AndroidOnly("The RI doesn't throw the IllegalStateException.")
    982     public void test_unwrap_ByteBuffer$ByteBuffer_04() {
    983         String host = "new host";
    984         int port = 8080;
    985         ByteBuffer bbs = ByteBuffer.allocate(10);
    986         ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
    987         SSLEngine sse = getEngine(host, port);
    988 
    989         try {
    990             sse.unwrap(bbs, bbd);
    991             fail("IllegalStateException wasn't thrown");
    992         } catch (IllegalStateException iobe) {
    993             //expected
    994         } catch (Exception e) {
    995             fail(e + " was thrown instead of IllegalStateException");
    996         }
    997     }
    998 
    999     /**
   1000      * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
   1001      */
   1002     public void test_unwrap_ByteBuffer$ByteBuffer_05() {
   1003         String host = "new host";
   1004         int port = 8080;
   1005         ByteBuffer bbs = ByteBuffer.allocate(10);
   1006         ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
   1007         SSLEngine sse = getEngine(host, port);
   1008         sse.setUseClientMode(true);
   1009 
   1010         try {
   1011             SSLEngineResult res = sse.unwrap(bbs, bbd);
   1012             assertEquals(0, res.bytesConsumed());
   1013             assertEquals(0, res.bytesProduced());
   1014         } catch (Exception ex) {
   1015             fail("Unexpected exception: " + ex);
   1016         }
   1017     }
   1018 
   1019     public void test_wrap_ByteBuffer_ByteBuffer_01() throws IOException, InterruptedException {
   1020         prepareEngines();
   1021         doHandshake();
   1022         ByteBuffer bbs = ByteBuffer.allocate(20);
   1023         ByteBuffer bbd = ByteBuffer.allocate(20000);
   1024         clientEngine.engine.wrap(bbs, bbd);
   1025     }
   1026 
   1027     /**
   1028      * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst)
   1029      * ReadOnlyBufferException should be thrown.
   1030      */
   1031     public void test_wrap_ByteBuffer_ByteBuffer_02() {
   1032         String host = "new host";
   1033         int port = 8080;
   1034         ByteBuffer bbs = ByteBuffer.allocate(10);
   1035         ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer();
   1036         SSLEngine sse = getEngine(host, port);
   1037         sse.setUseClientMode(true);
   1038 
   1039         try {
   1040             sse.wrap(bbs, bbd);
   1041             fail("ReadOnlyBufferException wasn't thrown");
   1042         } catch (ReadOnlyBufferException iobe) {
   1043             //expected
   1044         } catch (Exception e) {
   1045             fail(e + " was thrown instead of ReadOnlyBufferException");
   1046         }
   1047     }
   1048 
   1049     /**
   1050      * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst)
   1051      * IllegalArgumentException should be thrown.
   1052      */
   1053     @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
   1054     public void test_wrap_ByteBuffer_ByteBuffer_03() {
   1055         String host = "new host";
   1056         int port = 8080;
   1057         ByteBuffer bbsN = null;
   1058         ByteBuffer bbdN = null;
   1059         ByteBuffer bbs = ByteBuffer.allocate(10);
   1060         ByteBuffer bbd = ByteBuffer.allocate(100);
   1061         SSLEngine sse = getEngine(host, port);
   1062         sse.setUseClientMode(true);
   1063 
   1064         try {
   1065             sse.wrap(bbsN, bbd);
   1066             fail("IllegalArgumentException wasn't thrown");
   1067         } catch (IllegalArgumentException iae) {
   1068             //expected
   1069         } catch (NullPointerException npe) {
   1070         } catch (Exception e) {
   1071             fail(e + " was thrown instead of IllegalArgumentException");
   1072         }
   1073 
   1074         try {
   1075             sse.wrap(bbs, bbdN);
   1076             fail("IllegalArgumentException wasn't thrown");
   1077         } catch (IllegalArgumentException iae) {
   1078             //expected
   1079         } catch (NullPointerException npe) {
   1080         } catch (Exception e) {
   1081             fail(e + " was thrown instead of IllegalArgumentException");
   1082         }
   1083 
   1084         try {
   1085             sse.wrap(bbsN, bbdN);
   1086             fail("IllegalArgumentException wasn't thrown");
   1087         } catch (IllegalArgumentException iae) {
   1088             //expected
   1089         } catch (NullPointerException npe) {
   1090         } catch (Exception e) {
   1091             fail(e + " was thrown instead of IllegalArgumentException");
   1092         }
   1093     }
   1094 
   1095     /**
   1096      * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst)
   1097      * IllegalStateException should be thrown.
   1098      */
   1099     @AndroidOnly("The RI doesn't throw the IllegalStateException.")
   1100     public void test_wrap_ByteBuffer_ByteBuffer_04() {
   1101         String host = "new host";
   1102         int port = 8080;
   1103         ByteBuffer bbs = ByteBuffer.allocate(10);
   1104         ByteBuffer bbd = ByteBuffer.allocate(10);
   1105         SSLEngine sse = getEngine(host, port);
   1106 
   1107         try {
   1108             sse.wrap(bbs, bbd);
   1109             fail("IllegalStateException wasn't thrown");
   1110         } catch (IllegalStateException iobe) {
   1111             //expected
   1112         } catch (Exception e) {
   1113             fail(e + " was thrown instead of IllegalStateException");
   1114         }
   1115     }
   1116 
   1117     /**
   1118      * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst)
   1119      */
   1120     public void test_wrap_ByteBuffer_ByteBuffer_05() {
   1121         String host = "new host";
   1122         int port = 8080;
   1123         ByteBuffer bb = ByteBuffer.allocate(10);
   1124         SSLEngine sse = getEngine(host, port);
   1125         sse.setUseClientMode(true);
   1126 
   1127         try {
   1128             SSLEngineResult res = sse.wrap(bb, ByteBuffer.allocate(10));
   1129             assertEquals(0, res.bytesConsumed());
   1130             assertEquals(0, res.bytesProduced());
   1131         } catch (Exception e) {
   1132             fail("Unexpected exception: " + e);
   1133         }
   1134     }
   1135 
   1136     /**
   1137      * @throws IOException
   1138      * @throws InterruptedException
   1139      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
   1140      * SSLException should be thrown.
   1141      */
   1142     public void test_wrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException {
   1143         prepareEngines();
   1144         doHandshake();
   1145         ByteBuffer bbs = ByteBuffer.allocate(100);
   1146         ByteBuffer bbd = ByteBuffer.allocate(20000);
   1147 
   1148         try {
   1149             clientEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd);
   1150             serverEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd);
   1151             //fail("SSLException wasn't thrown");
   1152         } catch (SSLException ex) {
   1153             //expected
   1154         }
   1155     }
   1156 
   1157     /**
   1158      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
   1159      * ReadOnlyBufferException should be thrown.
   1160      */
   1161     public void test_wrap_ByteBuffer$ByteBuffer_02() {
   1162         String host = "new host";
   1163         int port = 8080;
   1164         ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer();
   1165         ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
   1166         SSLEngine sse = getEngine(host, port);
   1167         sse.setUseClientMode(true);
   1168 
   1169         try {
   1170             sse.wrap(bbA, bb);
   1171             fail("ReadOnlyBufferException wasn't thrown");
   1172         } catch (ReadOnlyBufferException iobe) {
   1173             //expected
   1174         } catch (Exception e) {
   1175             fail(e + " was thrown instead of ReadOnlyBufferException");
   1176         }
   1177     }
   1178 
   1179     /**
   1180      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
   1181      * IllegalArgumentException should be thrown.
   1182      */
   1183     @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
   1184     public void test_wrap_ByteBuffer$ByteBuffer_03() {
   1185         String host = "new host";
   1186         int port = 8080;
   1187         ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)};
   1188         ByteBuffer[] bbAN = null;
   1189         ByteBuffer bb = ByteBuffer.allocate(10);
   1190         ByteBuffer bN = null;
   1191         SSLEngine sse = getEngine(host, port);
   1192         sse.setUseClientMode(true);
   1193 
   1194         try {
   1195             sse.wrap(bbA, bN);
   1196             fail("IllegalArgumentException wasn't thrown");
   1197         } catch (IllegalArgumentException iobe) {
   1198             //expected
   1199         } catch (NullPointerException npe) {
   1200         } catch (Exception e) {
   1201             fail(e + " was thrown instead of IllegalArgumentException");
   1202         }
   1203 
   1204         try {
   1205             sse.wrap(bbAN, bb);
   1206             fail("IllegalArgumentException wasn't thrown");
   1207         } catch (IllegalArgumentException iobe) {
   1208             //expected
   1209         } catch (NullPointerException npe) {
   1210         } catch (Exception e) {
   1211             fail(e + " was thrown instead of IllegalArgumentException");
   1212         }
   1213 
   1214         try {
   1215             sse.wrap(bbAN, bN);
   1216             fail("IllegalArgumentException wasn't thrown");
   1217         } catch (IllegalArgumentException iobe) {
   1218             //expected
   1219         } catch (NullPointerException npe) {
   1220         } catch (Exception e) {
   1221             fail(e + " was thrown instead of IllegalArgumentException");
   1222         }
   1223     }
   1224 
   1225     /**
   1226      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
   1227      * IllegalStateException should be thrown.
   1228      */
   1229     @AndroidOnly("The RI doesn't throw the IllegalStateException.")
   1230     public void test_wrap_ByteBuffer$ByteBuffer_04() {
   1231         String host = "new host";
   1232         int port = 8080;
   1233         ByteBuffer bb = ByteBuffer.allocate(10);
   1234         ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) };
   1235         SSLEngine sse = getEngine(host, port);
   1236 
   1237         try {
   1238             sse.wrap(bbA, bb);
   1239             fail("IllegalStateException wasn't thrown");
   1240         } catch (IllegalStateException iobe) {
   1241             //expected
   1242         } catch (Exception e) {
   1243             fail(e + " was thrown instead of IllegalStateException");
   1244         }
   1245     }
   1246 
   1247     /**
   1248      * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
   1249      */
   1250     public void test_wrap_ByteBuffer$ByteBuffer_05() {
   1251         String host = "new host";
   1252         int port = 8080;
   1253         ByteBuffer bb = ByteBuffer.allocate(10);
   1254         ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) };
   1255         SSLEngine sse = getEngine(host, port);
   1256         sse.setUseClientMode(true);
   1257 
   1258         try {
   1259             SSLEngineResult res = sse.wrap(bbA, bb);
   1260             assertEquals(0, res.bytesConsumed());
   1261             assertEquals(0, res.bytesProduced());
   1262         } catch (Exception ex) {
   1263             fail("Unexpected exception: " + ex);
   1264         }
   1265     }
   1266 
   1267     private SSLEngine getEngine() {
   1268         SSLContext context = null;
   1269         try {
   1270             context = SSLContext.getInstance("TLS");
   1271             context.init(null, null, null);
   1272         } catch (KeyManagementException e) {
   1273             fail("Could not get SSLEngine: key management exception "
   1274                     + e.getMessage());
   1275         } catch (NoSuchAlgorithmException e) {
   1276             fail("Could not get SSLEngine: no such algorithm " + e.getMessage());
   1277         }
   1278         return context.createSSLEngine();
   1279     }
   1280 
   1281     private SSLEngine getEngine(String host, int port) {
   1282         SSLContext context = null;
   1283         try {
   1284             context = SSLContext.getInstance("TLS");
   1285             context.init(null, null, null);
   1286         } catch (KeyManagementException e) {
   1287             fail("Could not get SSLEngine: key management exception "
   1288                     + e.getMessage());
   1289         } catch (NoSuchAlgorithmException e) {
   1290             fail("Could not get SSLEngine: no such algorithm " + e.getMessage());
   1291         }
   1292         return context.createSSLEngine(host, port);
   1293     }
   1294 
   1295     class HandshakeHandler implements Runnable {
   1296 
   1297         private final SSLEngine engine;
   1298 
   1299         private final SourceChannel in;
   1300 
   1301         private final SinkChannel out;
   1302 
   1303         private final ByteBuffer EMPTY = ByteBuffer.allocate(0);
   1304 
   1305         @SuppressWarnings("unused")
   1306         private final String LOGTAG;
   1307 
   1308         private SSLEngineResult.HandshakeStatus status;
   1309 
   1310         private ByteBuffer readBuffer;
   1311 
   1312         private ByteBuffer writeBuffer;
   1313 
   1314         HandshakeHandler(boolean clientMode, SourceChannel in, SinkChannel out)
   1315                 throws SSLException {
   1316             this.in = in;
   1317             this.out = out;
   1318             engine = getEngine();
   1319             engine.setUseClientMode(clientMode);
   1320             String[] cipherSuites = engine.getSupportedCipherSuites();
   1321             Set<String> enabledSuites = new HashSet<String>();
   1322             for (String cipherSuite : cipherSuites) {
   1323                 if (cipherSuite.contains("anon")) {
   1324                     enabledSuites.add(cipherSuite);
   1325                 }
   1326             }
   1327             engine.setEnabledCipherSuites((String[]) enabledSuites.toArray(
   1328                     new String[enabledSuites.size()]));
   1329 
   1330             engine.beginHandshake();
   1331             status = engine.getHandshakeStatus();
   1332 
   1333             if (clientMode) {
   1334                 LOGTAG = "CLIENT: ";
   1335             } else {
   1336                 LOGTAG = "SERVER: ";
   1337             }
   1338 
   1339             log("CipherSuites: " + Arrays.toString(engine.getEnabledCipherSuites()));
   1340             log(status);
   1341 
   1342             readBuffer = ByteBuffer.allocate(200000);
   1343             writeBuffer = ByteBuffer.allocate(20000);
   1344         }
   1345 
   1346         public SSLEngineResult.HandshakeStatus getStatus() {
   1347             return status;
   1348         }
   1349 
   1350         private void log(Object o) {
   1351             //System.out.print(LOGTAG);
   1352             //System.out.println(o);
   1353         }
   1354 
   1355         private ByteBuffer read() throws IOException {
   1356             if (readBuffer == null || readBuffer.remaining() == 0 || readBuffer.position() == 0) {
   1357                 readBuffer.clear();
   1358                 int read = in.read(readBuffer);
   1359                 log("read: " + read);
   1360                 readBuffer.rewind();
   1361                 readBuffer.limit(read);
   1362             }
   1363             return readBuffer;
   1364         }
   1365 
   1366         public void run() {
   1367             try {
   1368                 while (true) {
   1369                     switch (status) {
   1370                         case FINISHED: {
   1371                             log(status);
   1372                             return;
   1373                         }
   1374                         case NEED_TASK: {
   1375                             log(status);
   1376                             Runnable task;
   1377                             while ((task = engine.getDelegatedTask()) != null) {
   1378                                 task.run();
   1379                             }
   1380                             status = engine.getHandshakeStatus();
   1381                             break;
   1382                         }
   1383                         case NEED_UNWRAP: {
   1384                             log(status);
   1385                             ByteBuffer source = read();
   1386                             writeBuffer.clear();
   1387 
   1388                             while (status == HandshakeStatus.NEED_UNWRAP) {
   1389                                 SSLEngineResult result = engine.unwrap(source, writeBuffer);
   1390                                 status = result.getHandshakeStatus();
   1391                                 log(result);
   1392                             }
   1393                             break;
   1394                         }
   1395                         case NEED_WRAP: {
   1396                             log(status);
   1397                             writeBuffer.clear();
   1398 
   1399                             int produced = 0;
   1400                             SSLEngineResult result = null;
   1401                             while (status == HandshakeStatus.NEED_WRAP) {
   1402                                 result = engine.wrap(EMPTY, writeBuffer);
   1403                                 status = result.getHandshakeStatus();
   1404                                 produced += result.bytesProduced();
   1405                                 log(result);
   1406                             }
   1407                             writeBuffer.rewind();
   1408                             writeBuffer.limit(produced);
   1409                             log("write: " + produced);
   1410                             out.write(writeBuffer);
   1411                             break;
   1412                         }
   1413                         case NOT_HANDSHAKING: {
   1414                             log("Not Handshaking");
   1415                             return;
   1416                         }
   1417                     }
   1418                 }
   1419             } catch (IOException e) {
   1420                 log(e);
   1421             } catch (RuntimeException e) {
   1422                 // ignore;
   1423             }
   1424         }
   1425     }
   1426 
   1427     @KnownFailure("Handshake Status is never finished. NPE in "
   1428             + "ClientSessionContext$HostAndPort.hashCode() when host is null")
   1429     public void testHandshake() throws IOException, InterruptedException {
   1430 
   1431         prepareEngines();
   1432 
   1433         assertTrue("handshake failed", doHandshake());
   1434 
   1435         System.out.println(clientEngine.engine.getSession().getCipherSuite());
   1436 
   1437         assertEquals("Handshake not finished",
   1438                 SSLEngineResult.HandshakeStatus.FINISHED,
   1439                 clientEngine.getStatus());
   1440         assertEquals("Handshake not finished",
   1441                 SSLEngineResult.HandshakeStatus.FINISHED,
   1442                 serverEngine.getStatus());
   1443     }
   1444 
   1445     void prepareEngines() throws IOException {
   1446         Pipe clientSendPipe = Pipe.open();
   1447         Pipe serverSendPipe = Pipe.open();
   1448 
   1449         SinkChannel clientSink = clientSendPipe.sink();
   1450         SourceChannel serverSource = clientSendPipe.source();
   1451         SinkChannel serverSink = serverSendPipe.sink();
   1452         SourceChannel clientSource = serverSendPipe.source();
   1453 
   1454         clientEngine = new HandshakeHandler(true, clientSource, clientSink);
   1455         serverEngine = new HandshakeHandler(false, serverSource, serverSink);
   1456     }
   1457 
   1458     boolean doHandshake() throws InterruptedException {
   1459         Thread clientThread = new Thread(clientEngine);
   1460         clientThread.start();
   1461 
   1462         Thread serverThread = new Thread(serverEngine);
   1463         serverThread.start();
   1464 
   1465         int i = 0;
   1466         while (clientThread.isAlive() && serverThread.isAlive() && i < 20) {
   1467             Thread.sleep(500);
   1468             i++;
   1469         }
   1470 
   1471         if (clientThread.isAlive()) {
   1472             clientThread.interrupt();
   1473         }
   1474 
   1475         if (serverThread.isAlive()) {
   1476             serverThread.interrupt();
   1477         }
   1478 
   1479         return clientEngine.getStatus() == HandshakeStatus.FINISHED && serverEngine.getStatus() == HandshakeStatus.FINISHED;
   1480     }
   1481 
   1482 }
   1483