Skip to content

Commit d99c21d

Browse files
author
Zhen
committed
Added cucumber files for tls tck
1 parent 0209325 commit d99c21d

File tree

7 files changed

+589
-46
lines changed

7 files changed

+589
-46
lines changed

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
import org.neo4j.driver.v1.util.CertificateToolTest;
4444
import org.neo4j.driver.v1.util.Neo4jInstaller;
4545
import org.neo4j.driver.v1.util.Neo4jRunner;
46+
import org.neo4j.driver.v1.util.Neo4jSettings;
4647
import org.neo4j.driver.v1.util.TestNeo4j;
4748

49+
import static java.io.File.createTempFile;
4850
import static org.junit.Assert.assertEquals;
4951
import static org.junit.Assert.assertFalse;
5052
import static org.junit.Assert.assertTrue;
@@ -92,6 +94,65 @@ private void performTLSHandshakeUsingKnownCerts( File knownCerts ) throws Throwa
9294
verify( logger, atLeastOnce() ).debug( "TLS connection closed" );
9395
}
9496

97+
private File tempFile(String prefix, String suffix) throws Throwable
98+
{
99+
File file = createTempFile( prefix, suffix );
100+
file.deleteOnExit();
101+
return file;
102+
}
103+
104+
@Test
105+
public void shouldPerformTLSHandshakeWithTrustedCert() throws Throwable
106+
{
107+
try
108+
{
109+
// Given
110+
// Create root certificate
111+
File rootCert = tempFile( "temp_root_cert", ".cert" );
112+
File rootKey = tempFile( "temp_root_key", ".key" );
113+
114+
CertificateToolTest.SelfSignedCertificateGenerator
115+
certGenerator = new CertificateToolTest.SelfSignedCertificateGenerator();
116+
certGenerator.saveSelfSignedCertificate( rootCert );
117+
certGenerator.savePrivateKey( rootKey );
118+
119+
// Generate certificate signing request and get a certificate signed by the root private key
120+
File cert = tempFile( "temp_cert", ".cert" );
121+
File key = tempFile( "temp_key", ".key" );
122+
CertificateToolTest.CertificateSigningRequestGenerator
123+
csrGenerator = new CertificateToolTest.CertificateSigningRequestGenerator();
124+
X509Certificate signedCert = certGenerator.sign(
125+
csrGenerator.certificateSigningRequest(), csrGenerator.publicKey() );
126+
csrGenerator.savePrivateKey( key );
127+
CertificateTool.saveX509Cert( signedCert, cert );
128+
129+
// Give the server certs to database
130+
neo4j.restartServerOnEmptyDatabase(
131+
Neo4jSettings.DEFAULT
132+
.usingEncryptionKeyAndCert( key, cert ) );
133+
134+
Logger logger = mock( Logger.class );
135+
SocketChannel channel = SocketChannel.open();
136+
channel.connect( new InetSocketAddress( "localhost", 7687 ) );
137+
138+
// When
139+
TLSSocketChannel sslChannel =
140+
new TLSSocketChannel( "localhost", 7687, channel, logger,
141+
Config.TrustStrategy.trustSignedBy( rootCert ) );
142+
sslChannel.close();
143+
144+
// Then
145+
verify( logger, atLeastOnce() ).debug( "TLS connection enabled" );
146+
verify( logger, atLeastOnce() ).debug( "TLS connection established" );
147+
verify( logger, atLeastOnce() ).debug( "TLS connection closed" );
148+
}
149+
finally
150+
{
151+
// always restore the db default settings
152+
neo4j.restartServerOnEmptyDatabase( Neo4jSettings.DEFAULT );
153+
}
154+
}
155+
95156
@Test
96157
public void shouldFailTLSHandshakeDueToWrongCertInKnownCertsFile() throws Throwable
97158
{
@@ -178,7 +239,7 @@ public void shouldFailTLSHandshakeDueToServerCertNotSignedByKnownCA() throws Thr
178239
}
179240

180241
@Test
181-
public void shouldPerformTLSHandshakeWithTrustedServerCert() throws Throwable
242+
public void shouldPerformTLSHandshakeWithTheSameTrustedServerCert() throws Throwable
182243
{
183244

184245
Logger logger = mock( Logger.class );
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright (c) 2002-2016 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.v1.tck;
20+
21+
import cucumber.api.CucumberOptions;
22+
import org.junit.ClassRule;
23+
import org.junit.runner.RunWith;
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
28+
import org.neo4j.driver.v1.util.Neo4jSettings;
29+
import org.neo4j.driver.v1.util.TestNeo4j;
30+
31+
/**
32+
* The tls tck needs to access the server rather than a session, therefore we pull tls tck outside specially
33+
*/
34+
@RunWith( DriverCucumberAdapter.class )
35+
@CucumberOptions(
36+
features = {"target/resources/features/TransportLayerSecurity.feature"},
37+
tags={"@tls"},
38+
format = {"pretty"})
39+
public class DriverSecurityComplianceIT
40+
{
41+
@ClassRule
42+
public static TestNeo4j neo4j = new TestNeo4j();
43+
44+
public DriverSecurityComplianceIT() throws IOException {}
45+
46+
public static void updateEncryptionKeyAndCert( File key, File cert ) throws Exception
47+
{
48+
neo4j.restartServerOnEmptyDatabase(
49+
Neo4jSettings.DEFAULT.usingEncryptionKeyAndCert( key, cert ) );
50+
}
51+
52+
public static void useDefaultEncryptionKeyAndCert() throws Exception
53+
{
54+
neo4j.restartServerOnEmptyDatabase( Neo4jSettings.DEFAULT );
55+
}
56+
}

0 commit comments

Comments
 (0)