Skip to content

Commit 5e767a1

Browse files
author
Zhen Li
committed
Fixed failing tests in the code
1 parent 3b6c3a1 commit 5e767a1

File tree

7 files changed

+40
-110
lines changed

7 files changed

+40
-110
lines changed

driver/src/main/java/org/neo4j/driver/internal/connector/socket/TrustOnFirstUseTrustManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* References:
4040
* http://stackoverflow.com/questions/6802421/how-to-compare-distinct-implementations-of-java-security-cert-x509certificate?answertab=votes#tab-top
4141
*/
42-
class TrustOnFirstUseTrustManager implements X509TrustManager
42+
public class TrustOnFirstUseTrustManager implements X509TrustManager
4343
{
4444
/**
4545
* A list of pairs (known_server certificate) are stored in this file.

driver/src/test/java/org/neo4j/driver/internal/ConfigTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void shouldConfigureMinIdleTime() throws Throwable
8383
Config config = Config.build().withSessionLivenessCheckTimeout( 1337 ).toConfig();
8484

8585
// then
86-
assertThat( config.idleTimeBeforeConnectionTest(), equalTo( 1337l ));
86+
assertThat( config.idleTimeBeforeConnectionTest(), equalTo( 1337l ) );
8787
}
8888

8989
public static void deleteDefaultKnownCertFileIfExists()

driver/src/test/java/org/neo4j/driver/v1/integration/TLSSocketChannelIT.java

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,32 @@
1818
*/
1919
package org.neo4j.driver.v1.integration;
2020

21+
import org.junit.BeforeClass;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
2125
import java.io.BufferedWriter;
2226
import java.io.File;
2327
import java.io.FileWriter;
2428
import java.io.IOException;
25-
import java.io.InputStream;
2629
import java.net.InetAddress;
2730
import java.net.InetSocketAddress;
2831
import java.net.URI;
2932
import java.nio.channels.SocketChannel;
3033
import java.security.cert.X509Certificate;
31-
import java.util.Scanner;
32-
import javax.net.ssl.SSLHandshakeException;
33-
import javax.xml.bind.DatatypeConverter;
3434

35-
import org.junit.Rule;
36-
import org.junit.Test;
35+
import javax.net.ssl.SSLHandshakeException;
3736

38-
import org.neo4j.driver.internal.ConfigTest;
3937
import org.neo4j.driver.internal.connector.socket.TLSSocketChannel;
38+
import org.neo4j.driver.internal.connector.socket.TrustOnFirstUseTrustManager;
4039
import org.neo4j.driver.internal.spi.Logger;
4140
import org.neo4j.driver.internal.util.CertificateTool;
4241
import org.neo4j.driver.v1.Config;
4342
import org.neo4j.driver.v1.Driver;
4443
import org.neo4j.driver.v1.GraphDatabase;
4544
import org.neo4j.driver.v1.ResultCursor;
4645
import org.neo4j.driver.v1.util.CertificateToolTest;
46+
import org.neo4j.driver.v1.util.Neo4jInstaller;
4747
import org.neo4j.driver.v1.util.Neo4jRunner;
4848
import org.neo4j.driver.v1.util.TestNeo4j;
4949

@@ -53,12 +53,20 @@
5353
import static org.mockito.Mockito.atLeastOnce;
5454
import static org.mockito.Mockito.mock;
5555
import static org.mockito.Mockito.verify;
56+
import static org.neo4j.driver.internal.connector.socket.TrustOnFirstUseTrustManager.fingerprint;
5657

5758
public class TLSSocketChannelIT
5859
{
5960
@Rule
6061
public TestNeo4j neo4j = new TestNeo4j();
6162

63+
@BeforeClass
64+
public static void setup() throws IOException, InterruptedException
65+
{
66+
/* uncomment for JSSE debugging info */
67+
// System.setProperty( "javax.net.debug", "all" );
68+
}
69+
6270
@Test
6371
public void shouldPerformTLSHandshakeWithEmptyKnownCertsFile() throws Throwable
6472
{
@@ -129,7 +137,7 @@ private void createFakeServerCertPairInKnownCerts( String host, int port, File k
129137
String serverId = ip + ":" + port;
130138

131139
X509Certificate cert = CertificateToolTest.generateSelfSignedCertificate();
132-
String certStr = DatatypeConverter.printBase64Binary( cert.getEncoded() );
140+
String certStr = fingerprint(cert);
133141

134142
BufferedWriter writer = new BufferedWriter( new FileWriter( knownCerts, true ) );
135143
writer.write( serverId + "," + certStr );
@@ -174,17 +182,15 @@ public void shouldFailTLSHandshakeDueToServerCertNotSignedByKnownCA() throws Thr
174182
@Test
175183
public void shouldPerformTLSHandshakeWithTrustedServerCert() throws Throwable
176184
{
177-
// Given
178-
TestKeys keys = testKeys();
179-
neo4j.restartServerOnEmptyDatabase( Neo4jSettings.DEFAULT.usingEncryptionKeyAndCert( keys.serverKey, keys.serverCert ) );
180185

181186
Logger logger = mock( Logger.class );
182187
SocketChannel channel = SocketChannel.open();
183188
channel.connect( new InetSocketAddress( "localhost", 7687 ) );
184189

185190
// When
186191
TLSSocketChannel sslChannel = new TLSSocketChannel( "localhost", 7687, channel, logger,
187-
Config.TrustStrategy.trustSignedBy( keys.signingCert ) );
192+
Config.TrustStrategy.trustSignedBy(
193+
new File( Neo4jInstaller.neo4jHomeDir, "conf/ssl/snakeoil.cert") ) );
188194
sslChannel.close();
189195

190196
// Then
@@ -196,7 +202,7 @@ public void shouldPerformTLSHandshakeWithTrustedServerCert() throws Throwable
196202
@Test
197203
public void shouldEstablishTLSConnection() throws Throwable
198204
{
199-
ConfigTest.deleteDefaultKnownCertFileIfExists();
205+
200206
Config config = Config.build().withEncryptionLevel( Config.EncryptionLevel.REQUIRED ).toConfig();
201207

202208
Driver driver = GraphDatabase.driver(
@@ -210,33 +216,4 @@ public void shouldEstablishTLSConnection() throws Throwable
210216

211217
driver.close();
212218
}
213-
214-
class TestKeys
215-
{
216-
final File serverKey;
217-
final File serverCert;
218-
final File signingCert;
219-
220-
TestKeys( File serverKey, File serverCert, File signingCert )
221-
{
222-
this.serverKey = serverKey;
223-
this.serverCert = serverCert;
224-
this.signingCert = signingCert;
225-
}
226-
}
227-
228-
TestKeys testKeys() throws IOException
229-
{
230-
return new TestKeys( fileFromCertResource( "server.key" ), fileFromCertResource( "server.crt" ), fileFromCertResource( "ca.crt" ) );
231-
}
232-
233-
private File fileFromCertResource( String fileName ) throws IOException
234-
{
235-
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream( "certificates/" + fileName );
236-
try ( Scanner scanner = new Scanner( resourceAsStream ).useDelimiter( "\\A" ) )
237-
{
238-
String contents = scanner.next();
239-
return new File( neo4j.putTmpFile( fileName, "", contents ).getFile() );
240-
}
241-
}
242219
}

driver/src/test/java/org/neo4j/driver/v1/util/CertificateToolTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
*/
1919
package org.neo4j.driver.v1.util;
2020

21-
import java.io.File;
2221
import java.io.IOException;
2322
import java.math.BigInteger;
2423
import java.security.GeneralSecurityException;
25-
import java.security.KeyPair;
2624
import java.security.KeyPairGenerator;
27-
import java.security.KeyStore;
28-
import java.security.SecureRandom;
2925
import java.security.Security;
3026
import java.security.cert.Certificate;
3127
import java.security.cert.X509Certificate;
3228
import java.util.Date;
3329
import java.util.Enumeration;
30+
import java.security.KeyPair;
31+
import java.security.KeyStore;
32+
import java.security.SecureRandom;
33+
import java.io.File;
3434

3535
import org.bouncycastle.asn1.x500.X500Name;
3636
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;

driver/src/test/java/org/neo4j/driver/v1/util/Neo4jRunner.java

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private enum ServerStatus
5252

5353
private Neo4jSettings currentSettings = Neo4jSettings.DEFAULT;
5454
private Driver currentDriver;
55-
private boolean staleDriver;
55+
private Config testConfig = Config.build().withEncryptionLevel( Config.EncryptionLevel.REJECTED ).toConfig();
5656

5757
private Neo4jInstaller installer = Neo4jInstaller.Neo4jInstallerFactory.create();
5858

@@ -78,9 +78,6 @@ private Neo4jRunner() throws Exception
7878
// Install default settings
7979
updateServerSettingsFile();
8080

81-
// Reset driver to match default settings
82-
resetDriver();
83-
8481
// Make sure we stop on JVM exit
8582
installShutdownHook();
8683
}
@@ -154,11 +151,7 @@ private void clear( Neo4jSettings config ) throws Exception
154151
throw new IllegalStateException( "Failed to start server" );
155152
}
156153
awaitServerStatusOrFail( ServerStatus.ONLINE );
157-
158-
if ( staleDriver )
159-
{
160-
resetDriver();
161-
}
154+
currentDriver = new Driver( serverURI(), testConfig );
162155
}
163156

164157
private boolean updateServerSettings( Neo4jSettings settingsUpdate )
@@ -173,7 +166,6 @@ private boolean updateServerSettings( Neo4jSettings settingsUpdate )
173166
currentSettings = updatedSettings;
174167
}
175168
updateServerSettingsFile();
176-
staleDriver = true;
177169
return true;
178170
}
179171

@@ -236,8 +228,7 @@ private ServerStatus serverStatus() throws IOException, InterruptedException
236228
try
237229
{
238230
URI uri = serverURI();
239-
Config config = serverConfig();
240-
SocketClient client = new SocketClient( uri.getHost(), uri.getPort(), config, new DevNullLogger() );
231+
SocketClient client = new SocketClient( uri.getHost(), uri.getPort(), testConfig, new DevNullLogger() );
241232
client.start();
242233
client.stop();
243234
return ServerStatus.ONLINE;
@@ -248,26 +239,6 @@ private ServerStatus serverStatus() throws IOException, InterruptedException
248239
}
249240
}
250241

251-
private void resetDriver() throws Exception
252-
{
253-
if( currentDriver != null )
254-
{
255-
currentDriver.close();
256-
}
257-
currentDriver = new Driver( serverURI(), serverConfig() );
258-
staleDriver = false;
259-
}
260-
261-
private Config serverConfig()
262-
{
263-
Config config = Config.defaultConfig();
264-
if( currentSettings.isUsingTLS() )
265-
{
266-
config = Config.build().withEncryptionLevel( Config.EncryptionLevel.REQUIRED ).toConfig();
267-
}
268-
return config;
269-
}
270-
271242
private URI serverURI()
272243
{
273244
return URI.create( DEFAULT_URL );

driver/src/test/java/org/neo4j/driver/v1/util/Neo4jSettings.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,26 @@
1818
*/
1919
package org.neo4j.driver.v1.util;
2020

21-
import java.io.File;
2221
import java.util.HashMap;
2322
import java.util.Map;
2423

2524
import static org.neo4j.driver.internal.util.Iterables.map;
2625

2726
public class Neo4jSettings
2827
{
29-
private static final String TLS_ENABLED_KEY = "dbms.bolt.tls.enabled";
3028
private static final String TLS_CERT_KEY = "dbms.security.tls_certificate_file";
3129
private static final String TLS_KEY_KEY = "dbms.security.tls_key_file";
3230

31+
3332
private final Map<String, String> settings;
3433

35-
public static Neo4jSettings DEFAULT = new Neo4jSettings(new HashMap<String, String>()).usingTLS( false );
34+
public static Neo4jSettings DEFAULT = new Neo4jSettings(new HashMap<String, String>());
3635

3736
private Neo4jSettings( Map<String, String> settings )
3837
{
3938
this.settings = settings;
4039
}
4140

42-
public Neo4jSettings usingTLS( boolean usingTLS )
43-
{
44-
return updateWith( map( TLS_ENABLED_KEY, Boolean.toString( usingTLS ) ) );
45-
}
46-
47-
public boolean isUsingTLS()
48-
{
49-
return "true".equals( settings.get( TLS_ENABLED_KEY ) );
50-
}
51-
52-
public Neo4jSettings usingEncryptionKeyAndCert( File key, File cert )
53-
{
54-
return updateWith( map(
55-
TLS_CERT_KEY, cert.getAbsolutePath(),
56-
TLS_KEY_KEY, key.getAbsolutePath()
57-
));
58-
}
59-
6041
public Map<String, String> propertiesMap()
6142
{
6243
return settings;

examples/src/main/java/org/neo4j/docs/driver/Examples.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import org.neo4j.driver.v1.Value;
3535
import org.neo4j.driver.v1.Values;
3636

37-
import org.neo4j.driver.v1.Config.TlsAuthenticationConfig;
38-
3937
public class Examples
4038
{
4139

@@ -182,7 +180,8 @@ public static void notifications( Session session ) throws Exception
182180
public static Driver requireEncryption() throws Exception
183181
{
184182
// tag::tls-require-encryption[]
185-
Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build().withTlsEnabled( true ).toConfig() );
183+
Driver driver = GraphDatabase.driver( "bolt://localhost",
184+
Config.build().withEncryptionLevel( Config.EncryptionLevel.REQUIRED ).toConfig() );
186185
// end::tls-require-encryption[]
187186

188187
return driver;
@@ -191,9 +190,10 @@ public static Driver requireEncryption() throws Exception
191190
public static Driver trustOnFirstUse() throws Exception
192191
{
193192
// tag::tls-trust-on-first-use[]
194-
Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build().withTlsEnabled( true )
195-
.withTlsAuthConfig( TlsAuthenticationConfig.usingKnownCerts( new File( "/path/to/neo4j_known_hosts" )
196-
) ).toConfig() );
193+
Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build()
194+
.withEncryptionLevel( Config.EncryptionLevel.REJECTED )
195+
.withTrustStrategy( Config.TrustStrategy.trustOnFirstUse( new File( "/path/to/neo4j_known_hosts" ) ) )
196+
.toConfig() );
197197
// end::tls-trust-on-first-use[]
198198

199199
return driver;
@@ -202,9 +202,10 @@ public static Driver trustOnFirstUse() throws Exception
202202
public static Driver trustSignedCertificates() throws Exception
203203
{
204204
// tag::tls-signed[]
205-
Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build().withTlsEnabled( true )
206-
.withTlsAuthConfig( TlsAuthenticationConfig.usingTrustedCert( new File( "/path/to/ca-certificate.pem"
207-
) ) ).toConfig() );
205+
Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build()
206+
.withEncryptionLevel( Config.EncryptionLevel.REJECTED )
207+
.withTrustStrategy( Config.TrustStrategy.trustSignedBy( new File( "/path/to/ca-certificate.pem") ) )
208+
.toConfig() );
208209
// end::tls-signed[]
209210

210211
return driver;

0 commit comments

Comments
 (0)