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