@@ -2408,5 +2408,202 @@ public Void call() throws Exception {
24082408
24092409 System .out .println ("\t ... passed" );
24102410 }
2411+
2412+ @ Test
2413+ public void test_WolfSSLSession_readByteBuffer () throws Exception {
2414+ int ret = 0 ;
2415+ int err = 0 ;
2416+ int bytesRead = 0 ;
2417+ Socket cliSock = null ;
2418+ WolfSSLSession cliSes = null ;
2419+ ByteBuffer readBuffer = ByteBuffer .allocate (128 );
2420+ byte [] testData = "Hello ByteBuffer read test" .getBytes ();
2421+
2422+ /* Create client/server WolfSSLContext objects */
2423+ final WolfSSLContext srvCtx ;
2424+ WolfSSLContext cliCtx ;
2425+
2426+ System .out .print ("\t Testing ByteBuffer read() method" );
2427+
2428+ /* Create ServerSocket first to get ephemeral port */
2429+ final ServerSocket srvSocket = new ServerSocket (0 );
2430+
2431+ srvCtx = createAndSetupWolfSSLContext (srvCert , srvKey ,
2432+ WolfSSL .SSL_FILETYPE_PEM , cliCert ,
2433+ WolfSSL .SSLv23_ServerMethod ());
2434+ cliCtx = createAndSetupWolfSSLContext (cliCert , cliKey ,
2435+ WolfSSL .SSL_FILETYPE_PEM , caCert ,
2436+ WolfSSL .SSLv23_ClientMethod ());
2437+
2438+ ExecutorService es = Executors .newSingleThreadExecutor ();
2439+
2440+ /* Start server */
2441+ try {
2442+ es .submit (new Callable <Void >() {
2443+ @ Override
2444+ public Void call () throws Exception {
2445+ int ret ;
2446+ int err ;
2447+ Socket server = null ;
2448+ WolfSSLSession srvSes = null ;
2449+
2450+ try {
2451+ server = srvSocket .accept ();
2452+ srvSes = new WolfSSLSession (srvCtx );
2453+
2454+ ret = srvSes .setFd (server );
2455+ if (ret != WolfSSL .SSL_SUCCESS ) {
2456+ throw new Exception (
2457+ "WolfSSLSession.setFd() failed: " + ret );
2458+ }
2459+
2460+ do {
2461+ ret = srvSes .accept ();
2462+ err = srvSes .getError (ret );
2463+ } while (ret != WolfSSL .SSL_SUCCESS &&
2464+ (err == WolfSSL .SSL_ERROR_WANT_READ ||
2465+ err == WolfSSL .SSL_ERROR_WANT_WRITE ));
2466+
2467+ if (ret != WolfSSL .SSL_SUCCESS ) {
2468+ throw new Exception (
2469+ "WolfSSLSession.accept() failed: " + ret );
2470+ }
2471+
2472+ /* Send test data to client */
2473+ do {
2474+ ret = srvSes .write (testData , testData .length , 0 );
2475+ err = srvSes .getError (ret );
2476+ } while ((ret < 0 ) &&
2477+ (err == WolfSSL .SSL_ERROR_WANT_READ ||
2478+ err == WolfSSL .SSL_ERROR_WANT_WRITE ));
2479+
2480+ if (ret != testData .length ) {
2481+ throw new Exception ("Server write failed: " + ret );
2482+ }
2483+
2484+ srvSes .shutdownSSL ();
2485+ srvSes .freeSSL ();
2486+ srvSes = null ;
2487+ server .close ();
2488+ server = null ;
2489+
2490+ } finally {
2491+ if (srvSes != null ) {
2492+ srvSes .freeSSL ();
2493+ }
2494+ if (server != null ) {
2495+ server .close ();
2496+ }
2497+ }
2498+
2499+ return null ;
2500+ }
2501+ });
2502+
2503+ } catch (Exception e ) {
2504+ System .out .println ("\t ... failed" );
2505+ e .printStackTrace ();
2506+ fail ();
2507+ }
2508+
2509+ try {
2510+ /* Client connection */
2511+ cliSock = new Socket (InetAddress .getLocalHost (),
2512+ srvSocket .getLocalPort ());
2513+
2514+ cliSes = new WolfSSLSession (cliCtx );
2515+
2516+ ret = cliSes .setFd (cliSock );
2517+ if (ret != WolfSSL .SSL_SUCCESS ) {
2518+ throw new Exception (
2519+ "WolfSSLSession.setFd() failed, ret = " + ret );
2520+ }
2521+
2522+ /* Do handshake */
2523+ do {
2524+ ret = cliSes .connect ();
2525+ err = cliSes .getError (ret );
2526+ } while (ret != WolfSSL .SSL_SUCCESS &&
2527+ (err == WolfSSL .SSL_ERROR_WANT_READ ||
2528+ err == WolfSSL .SSL_ERROR_WANT_WRITE ));
2529+
2530+ if (ret != WolfSSL .SSL_SUCCESS ) {
2531+ throw new Exception (
2532+ "WolfSSLSession.connect() failed: " + err );
2533+ }
2534+
2535+ /* Test ByteBuffer read */
2536+ int initialPosition = readBuffer .position ();
2537+
2538+ do {
2539+ bytesRead = cliSes .read (readBuffer , testData .length , 5000 );
2540+ err = cliSes .getError (bytesRead );
2541+ } while ((bytesRead < 0 ) &&
2542+ (err == WolfSSL .SSL_ERROR_WANT_READ ||
2543+ err == WolfSSL .SSL_ERROR_WANT_WRITE ));
2544+
2545+ if (bytesRead != testData .length ) {
2546+ throw new Exception (
2547+ "Client ByteBuffer read failed: " + bytesRead );
2548+ }
2549+
2550+ /* Verify ByteBuffer position was updated correctly */
2551+ int expectedPosition = initialPosition + bytesRead ;
2552+ if (readBuffer .position () != expectedPosition ) {
2553+ throw new Exception (
2554+ "ByteBuffer position not updated correctly. Expected: " +
2555+ expectedPosition + ", Got: " + readBuffer .position ());
2556+ }
2557+
2558+ /* Verify received data matches sent data */
2559+ readBuffer .flip ();
2560+ byte [] receivedData = new byte [bytesRead ];
2561+ readBuffer .get (receivedData );
2562+
2563+ boolean arraysMatch = true ;
2564+ if (testData .length != receivedData .length ) {
2565+ arraysMatch = false ;
2566+ } else {
2567+ for (int i = 0 ; i < testData .length ; i ++) {
2568+ if (testData [i ] != receivedData [i ]) {
2569+ arraysMatch = false ;
2570+ break ;
2571+ }
2572+ }
2573+ }
2574+ if (!arraysMatch ) {
2575+ throw new Exception ("Received data does not match sent data" );
2576+ }
2577+
2578+ cliSes .shutdownSSL ();
2579+ cliSes .freeSSL ();
2580+ cliSes = null ;
2581+ cliSock .close ();
2582+ cliSock = null ;
2583+
2584+ } catch (Exception e ) {
2585+ System .out .println ("\t ... failed" );
2586+ e .printStackTrace ();
2587+ fail ();
2588+
2589+ } finally {
2590+ /* Free resources */
2591+ if (cliSes != null ) {
2592+ cliSes .freeSSL ();
2593+ }
2594+ if (cliSock != null ) {
2595+ cliSock .close ();
2596+ }
2597+ if (srvSocket != null ) {
2598+ srvSocket .close ();
2599+ }
2600+ if (srvCtx != null ) {
2601+ srvCtx .free ();
2602+ }
2603+ es .shutdown ();
2604+ }
2605+
2606+ System .out .println ("\t ... passed" );
2607+ }
24112608}
24122609
0 commit comments