|
| 1 | +package org.neo4j.driver.v1.integration; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.ByteBuffer; |
| 8 | +import java.nio.channels.ByteChannel; |
| 9 | +import java.security.GeneralSecurityException; |
| 10 | +import java.security.KeyManagementException; |
| 11 | +import java.security.KeyStore; |
| 12 | +import java.security.KeyStoreException; |
| 13 | +import java.security.NoSuchAlgorithmException; |
| 14 | +import java.security.UnrecoverableKeyException; |
| 15 | +import java.security.cert.CertificateException; |
| 16 | +import java.security.cert.X509Certificate; |
| 17 | +import javax.net.ssl.KeyManagerFactory; |
| 18 | +import javax.net.ssl.SSLContext; |
| 19 | +import javax.net.ssl.TrustManager; |
| 20 | +import javax.net.ssl.X509TrustManager; |
| 21 | + |
| 22 | +public abstract class TLSSocketChannelFragmentation |
| 23 | +{ |
| 24 | + protected SSLContext sslCtx; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setup() throws Throwable |
| 28 | + { |
| 29 | + createSSLContext(); |
| 30 | + createServer(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void shouldHandleFuzziness() throws Throwable |
| 35 | + { |
| 36 | + // Given |
| 37 | + int networkFrameSize, userBufferSize, blobOfDataSize; |
| 38 | + |
| 39 | + for(int dataBlobMagnitude = 1; dataBlobMagnitude < 16; dataBlobMagnitude+=2 ) |
| 40 | + { |
| 41 | + blobOfDataSize = (int) Math.pow( 2, dataBlobMagnitude ); |
| 42 | + |
| 43 | + for ( int frameSizeMagnitude = 1; frameSizeMagnitude < 16; frameSizeMagnitude+=2 ) |
| 44 | + { |
| 45 | + networkFrameSize = (int) Math.pow( 2, frameSizeMagnitude ); |
| 46 | + for ( int userBufferMagnitude = 1; userBufferMagnitude < 16; userBufferMagnitude+=2 ) |
| 47 | + { |
| 48 | + userBufferSize = (int) Math.pow( 2, userBufferMagnitude ); |
| 49 | + testForBufferSizes( blobOfDataSize, networkFrameSize, userBufferSize ); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + protected void createSSLContext() |
| 56 | + throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, |
| 57 | + UnrecoverableKeyException, KeyManagementException |
| 58 | + { |
| 59 | + KeyStore ks = KeyStore.getInstance("JKS"); |
| 60 | + char[] password = "password".toCharArray(); |
| 61 | + ks.load( getClass().getResourceAsStream( "/keystore.jks" ), password ); |
| 62 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
| 63 | + kmf.init(ks, password); |
| 64 | + |
| 65 | + sslCtx = SSLContext.getInstance("TLS"); |
| 66 | + sslCtx.init( kmf.getKeyManagers(), new TrustManager[]{new X509TrustManager() { |
| 67 | + public void checkClientTrusted( X509Certificate[] chain, String authType) throws CertificateException |
| 68 | + { |
| 69 | + } |
| 70 | + |
| 71 | + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 72 | + } |
| 73 | + |
| 74 | + public X509Certificate[] getAcceptedIssuers() { |
| 75 | + return null; |
| 76 | + } |
| 77 | + }}, null ); |
| 78 | + } |
| 79 | + |
| 80 | + protected abstract void testForBufferSizes( int blobOfDataSize, int networkFrameSize, int userBufferSize ) throws IOException, |
| 81 | + GeneralSecurityException; |
| 82 | + |
| 83 | + protected abstract void createServer() throws IOException; |
| 84 | + |
| 85 | + /** |
| 86 | + * Delegates to underlying channel, but only reads up to the set amount at a time, used to emulate |
| 87 | + * different network frame sizes in this test. |
| 88 | + */ |
| 89 | + protected static class LittleAtATimeChannel implements ByteChannel |
| 90 | + { |
| 91 | + private final ByteChannel delegate; |
| 92 | + private final int maxFrameSize; |
| 93 | + |
| 94 | + public LittleAtATimeChannel( ByteChannel delegate, int maxFrameSize ) |
| 95 | + { |
| 96 | + |
| 97 | + this.delegate = delegate; |
| 98 | + this.maxFrameSize = maxFrameSize; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean isOpen() |
| 103 | + { |
| 104 | + return delegate.isOpen(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void close() throws IOException |
| 109 | + { |
| 110 | + delegate.close(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public int write( ByteBuffer src ) throws IOException |
| 115 | + { |
| 116 | + int originalLimit = src.limit(); |
| 117 | + try |
| 118 | + { |
| 119 | + src.limit( Math.min( src.limit(), src.position() + maxFrameSize ) ); |
| 120 | + return delegate.write( src ); |
| 121 | + } |
| 122 | + finally |
| 123 | + { |
| 124 | + src.limit(originalLimit); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public int read( ByteBuffer dst ) throws IOException |
| 130 | + { |
| 131 | + int originalLimit = dst.limit(); |
| 132 | + try |
| 133 | + { |
| 134 | + dst.limit( Math.min( dst.limit(), dst.position() + maxFrameSize ) ); |
| 135 | + return delegate.read( dst ); |
| 136 | + } |
| 137 | + finally |
| 138 | + { |
| 139 | + dst.limit(originalLimit); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments