diff --git a/driver/src/test/java/org/neo4j/driver/v1/integration/TrustCustomCertificateIT.java b/driver/src/test/java/org/neo4j/driver/v1/integration/TrustCustomCertificateIT.java index b0ddc8ac9c..bff08aeeef 100644 --- a/driver/src/test/java/org/neo4j/driver/v1/integration/TrustCustomCertificateIT.java +++ b/driver/src/test/java/org/neo4j/driver/v1/integration/TrustCustomCertificateIT.java @@ -18,16 +18,10 @@ */ package org.neo4j.driver.v1.integration; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; import java.util.function.Supplier; import org.neo4j.driver.v1.Config; @@ -36,6 +30,7 @@ import org.neo4j.driver.v1.Session; import org.neo4j.driver.v1.StatementResult; import org.neo4j.driver.v1.exceptions.SecurityException; +import org.neo4j.driver.v1.util.CertificateExtension; import org.neo4j.driver.v1.util.CertificateToolUtil.CertificateKeyPair; import org.neo4j.driver.v1.util.DatabaseExtension; import org.neo4j.driver.v1.util.ParallelizableIT; @@ -51,28 +46,7 @@ class TrustCustomCertificateIT { @RegisterExtension - static final DatabaseExtension neo4j = new DatabaseExtension(); - - private static Path originalKeyFile; - private static Path originalCertFile; - - @BeforeAll - static void beforeAll() throws IOException - { - originalKeyFile = Files.createTempFile( "key-file-", "" ); - originalCertFile = Files.createTempFile( "cert-file-", "" ); - - Files.copy( neo4j.tlsKeyFile().toPath(), originalKeyFile , StandardCopyOption.REPLACE_EXISTING); - Files.copy( neo4j.tlsCertFile().toPath(), originalCertFile, StandardCopyOption.REPLACE_EXISTING ); - } - - @AfterAll - static void afterAll() throws Exception - { - neo4j.updateEncryptionKeyAndCert( originalKeyFile.toFile(), originalCertFile.toFile() ); - Files.deleteIfExists( originalKeyFile ); - Files.deleteIfExists( originalCertFile ); - } + static final DatabaseExtension neo4j = new CertificateExtension(); @Test void shouldAcceptServerWithCertificateSignedByDriverCertificate() throws Throwable diff --git a/driver/src/test/java/org/neo4j/driver/v1/integration/TrustOnFirstUseIT.java b/driver/src/test/java/org/neo4j/driver/v1/integration/TrustOnFirstUseIT.java index b447542097..3feb72807a 100644 --- a/driver/src/test/java/org/neo4j/driver/v1/integration/TrustOnFirstUseIT.java +++ b/driver/src/test/java/org/neo4j/driver/v1/integration/TrustOnFirstUseIT.java @@ -30,6 +30,7 @@ import org.neo4j.driver.v1.Session; import org.neo4j.driver.v1.StatementResult; import org.neo4j.driver.v1.exceptions.SecurityException; +import org.neo4j.driver.v1.util.CertificateExtension; import org.neo4j.driver.v1.util.CertificateToolUtil; import org.neo4j.driver.v1.util.DatabaseExtension; import org.neo4j.driver.v1.util.ParallelizableIT; @@ -46,7 +47,7 @@ class TrustOnFirstUseIT { @RegisterExtension - static final DatabaseExtension neo4j = new DatabaseExtension(); + static final DatabaseExtension neo4j = new CertificateExtension(); @Test void shouldTrustOnFirstUse() throws Throwable diff --git a/driver/src/test/java/org/neo4j/driver/v1/util/CertificateExtension.java b/driver/src/test/java/org/neo4j/driver/v1/util/CertificateExtension.java new file mode 100644 index 0000000000..e19baade4b --- /dev/null +++ b/driver/src/test/java/org/neo4j/driver/v1/util/CertificateExtension.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2002-2019 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.driver.v1.util; + +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; + +public class CertificateExtension extends DatabaseExtension implements BeforeAllCallback, AfterAllCallback +{ + private static Path originalKeyFile; + private static Path originalCertFile; + + @Override + public void beforeAll( ExtensionContext extensionContext ) throws Exception + { + originalKeyFile = Files.createTempFile( "key-file-", "" ); + originalCertFile = Files.createTempFile( "cert-file-", "" ); + + Files.copy( tlsKeyFile().toPath(), originalKeyFile , StandardCopyOption.REPLACE_EXISTING); + Files.copy( tlsCertFile().toPath(), originalCertFile, StandardCopyOption.REPLACE_EXISTING ); + } + + @Override + public void afterAll( ExtensionContext extensionContext ) throws Exception + { + updateEncryptionKeyAndCert( originalKeyFile.toFile(), originalCertFile.toFile() ); + Files.deleteIfExists( originalKeyFile ); + Files.deleteIfExists( originalCertFile ); + } +}