Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/ArgCheck.java
41152 views
/*1* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 501909626* @summary Add scatter/gather APIs for SSLEngine27*28* Check to see if the args are being parsed properly.29*30*/3132import javax.net.ssl.*;33import javax.net.ssl.SSLEngineResult.*;34import java.io.*;35import java.security.*;36import java.nio.*;3738public class ArgCheck {3940private static boolean debug = false;4142private static String pathToStores = "../etc";43private static String keyStoreFile = "keystore";44private static String trustStoreFile = "truststore";45private static String passwd = "passphrase";4647private static String keyFilename =48System.getProperty("test.src", "./") + "/" + pathToStores +49"/" + keyStoreFile;50private static String trustFilename =51System.getProperty("test.src", "./") + "/" + pathToStores +52"/" + trustStoreFile;5354private static void tryNull(SSLEngine ssle, ByteBuffer appData,55ByteBuffer netData) throws Exception {56try {57ssle.wrap(appData, netData);58throw new Exception();59} catch (IllegalArgumentException e) {60System.out.println("Caught right exception");61}6263try {64ssle.unwrap(netData, appData);65throw new Exception();66} catch (IllegalArgumentException e) {67System.out.println("Caught right exception");68}69}7071private static void tryNullArray(SSLEngine ssle, ByteBuffer [] appData,72ByteBuffer netData) throws Exception {73try {74ssle.wrap(appData, netData);75throw new Exception();76} catch (IllegalArgumentException e) {77System.out.println("Caught right exception");78}7980try {81ssle.unwrap(netData, appData);82throw new Exception();83} catch (IllegalArgumentException e) {84System.out.println("Caught right exception");85}86}8788private static void tryNullArrayLen(SSLEngine ssle, ByteBuffer [] appData,89int offset, int len, ByteBuffer netData) throws Exception {90try {91ssle.wrap(appData, offset, len, netData);92throw new Exception();93} catch (IllegalArgumentException e) {94System.out.println("Caught right exception");95}9697try {98ssle.unwrap(netData, appData, offset, len);99throw new Exception();100} catch (IllegalArgumentException e) {101System.out.println("Caught right exception");102}103}104105private static void tryReadOnly(SSLEngine ssle, ByteBuffer [] appData,106int offset, int len, ByteBuffer netData) throws Exception {107try {108if (netData.isReadOnly()) {109ssle.wrap(appData, offset, len, netData);110throw new Exception();111}112} catch (ReadOnlyBufferException e) {113System.out.println("Caught right exception");114}115116try {117if (!netData.isReadOnly()) {118ssle.unwrap(netData, appData, offset, len);119throw new Exception();120}121} catch (ReadOnlyBufferException e) {122System.out.println("Caught right exception");123}124}125126private static void tryOutOfBounds(SSLEngine ssle, ByteBuffer [] appData,127int offset, int len, ByteBuffer netData) throws Exception {128try {129ssle.wrap(appData, offset, len, netData);130throw new Exception();131} catch (IndexOutOfBoundsException e) {132System.out.println("Caught right exception");133}134135try {136ssle.unwrap(netData, appData, offset, len);137throw new Exception();138} catch (IndexOutOfBoundsException e) {139System.out.println("Caught right exception");140}141}142143private static void trySmallBufs(SSLEngine ssle,144ByteBuffer appBB, ByteBuffer smallNetBB,145ByteBuffer smallAppBB, ByteBuffer netBB) throws Exception {146147SSLEngineResult res = ssle.wrap(appBB, smallNetBB);148if (res.getStatus() != Status.BUFFER_OVERFLOW) {149throw new Exception();150}151152// For unwrap(), the BUFFER_OVERFLOW will not be generated153// until received SSL/TLS application data.154// Test test/jdk/javax/net/ssl/SSLEngine/LargePacket.java will check155// BUFFER_OVERFLOW/UNDERFLOW for both wrap() and unwrap().156//157//res = ssle.unwrap(netBB, smallAppBB);158//if (res.getStatus() != Status.BUFFER_OVERFLOW) {159// throw new Exception();160//}161}162163private static void trySmallBufsArray(SSLEngine ssle,164ByteBuffer [] appBB, ByteBuffer smallNetBB,165ByteBuffer [] smallAppBB, ByteBuffer netBB) throws Exception {166167SSLEngineResult res = ssle.wrap(appBB, 0, appBB.length, smallNetBB);168if (res.getStatus() != Status.BUFFER_OVERFLOW) {169throw new Exception();170}171172// For unwrap(), the BUFFER_OVERFLOW will not be generated173// until received SSL/TLS application data.174// Test test/jdk/javax/net/ssl/SSLEngine/LargePacket.java will check175// BUFFER_OVERFLOW/UNDERFLOW for both wrap() and unwrap().176//177//res = ssle.unwrap(netBB, smallAppBB, 0, appBB.length);178//if (res.getStatus() != Status.BUFFER_OVERFLOW) {179// throw new Exception();180//}181}182183private static void runTest(SSLEngine ssle) throws Exception {184ByteBuffer [] bufs;185186ByteBuffer roBB = ByteBuffer.allocate(40).asReadOnlyBuffer();187188ByteBuffer bb1K = ByteBuffer.allocate(1024);189ByteBuffer bb2K = ByteBuffer.allocate(2048);190ByteBuffer bb4K = ByteBuffer.allocate(5096);191ByteBuffer bb8K = ByteBuffer.allocate(10192);192193SSLSession ssls = ssle.getSession();194195ByteBuffer netBBMinus1 = ByteBuffer.allocate(196ssls.getPacketBufferSize() - 1);197ByteBuffer appBBMinus1 = ByteBuffer.allocate(198ssls.getApplicationBufferSize() - 1);199200ByteBuffer bbNet = ByteBuffer.allocate(201ssls.getPacketBufferSize());202ByteBuffer bbApp = ByteBuffer.allocate(203ssls.getApplicationBufferSize());204205bufs = new ByteBuffer [] { bb1K, bb2K, bb4K, bb8K};206207tryNull(ssle, null, null);208tryNull(ssle, bb1K, null);209tryNull(ssle, null, bb1K);210211tryNullArray(ssle, null, null);212tryNullArray(ssle, bufs, null);213tryNullArray(ssle, null, bb1K);214215tryNullArrayLen(ssle, null, 0, 4, null);216tryNullArrayLen(ssle, bufs, 0, 4, null);217tryNullArrayLen(ssle, null, 0, 4, bb1K);218219bufs[2] = null;220tryNullArray(ssle, bufs, bb1K);221222bufs[2] = bb4K;223tryReadOnly(ssle, bufs, 0, 4, roBB);224225bufs[2] = roBB;226tryReadOnly(ssle, bufs, 0, 4, bb4K);227228bufs[2] = bb4K;229tryOutOfBounds(ssle, bufs, -1, 0, bb1K);230tryOutOfBounds(ssle, bufs, 0, -1, bb1K);231tryOutOfBounds(ssle, bufs, 0, bufs.length + 1, bb1K);232tryOutOfBounds(ssle, bufs, bufs.length, 1, bb1K);233234bufs[3].position(bufs[3].limit());235trySmallBufs(ssle, bb1K, netBBMinus1, appBBMinus1, bb1K);236trySmallBufsArray(ssle, bufs, netBBMinus1, bufs, bb1K);237bufs[3].rewind();238239}240241public static void main(String args[]) throws Exception {242243SSLEngine ssle = createSSLEngine(keyFilename, trustFilename);244runTest(ssle);245246System.out.println("Test Passed.");247}248249/*250* Create an initialized SSLContext to use for this test.251*/252static private SSLEngine createSSLEngine(String keyFile, String trustFile)253throws Exception {254255SSLEngine ssle;256257KeyStore ks = KeyStore.getInstance("JKS");258KeyStore ts = KeyStore.getInstance("JKS");259260char[] passphrase = "passphrase".toCharArray();261262ks.load(new FileInputStream(keyFile), passphrase);263ts.load(new FileInputStream(trustFile), passphrase);264265KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");266kmf.init(ks, passphrase);267268TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");269tmf.init(ts);270271SSLContext sslCtx = SSLContext.getInstance("TLS");272273sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);274275ssle = sslCtx.createSSLEngine("client", 1001);276ssle.setUseClientMode(true);277278return ssle;279}280}281282283