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