From c50754f82ae0dce647a4b72c4e5bbd20fce00a6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Sautter?= Date: Tue, 5 Nov 2024 20:10:17 +0100 Subject: [PATCH] [java] start the secure server only when needed in unit tests --- .../selenium/CookieImplementationTest.java | 2 ++ .../org/openqa/selenium/PageLoadingTest.java | 2 ++ .../environment/InProcessTestEnvironment.java | 6 ++-- .../environment/webserver/NettyAppServer.java | 26 ++++++++-------- .../webserver/NettyAppServerTest.java | 2 +- .../FederatedCredentialManagementTest.java | 2 ++ .../grid/router/RemoteWebDriverBiDiTest.java | 2 +- .../router/RemoteWebDriverDownloadTest.java | 2 +- .../javascript/JavaScriptTestSuite.java | 2 +- .../selenium/safari/CrossDomainTest.java | 2 +- .../org/openqa/selenium/testing/BUILD.bazel | 1 + .../selenium/testing/JupiterTestBase.java | 24 ++++++++++++++- .../selenium/testing/NeedsSecureServer.java | 30 +++++++++++++++++++ 13 files changed, 82 insertions(+), 21 deletions(-) create mode 100644 java/test/org/openqa/selenium/testing/NeedsSecureServer.java diff --git a/java/test/org/openqa/selenium/CookieImplementationTest.java b/java/test/org/openqa/selenium/CookieImplementationTest.java index 46f3d4b9d7f15..5da0b5f408991 100644 --- a/java/test/org/openqa/selenium/CookieImplementationTest.java +++ b/java/test/org/openqa/selenium/CookieImplementationTest.java @@ -35,10 +35,12 @@ import org.openqa.selenium.environment.DomainHelper; import org.openqa.selenium.testing.Ignore; import org.openqa.selenium.testing.JupiterTestBase; +import org.openqa.selenium.testing.NeedsSecureServer; import org.openqa.selenium.testing.NotWorkingInRemoteBazelBuilds; import org.openqa.selenium.testing.NotYetImplemented; import org.openqa.selenium.testing.SwitchToTopAfterTest; +@NeedsSecureServer class CookieImplementationTest extends JupiterTestBase { private DomainHelper domainHelper; diff --git a/java/test/org/openqa/selenium/PageLoadingTest.java b/java/test/org/openqa/selenium/PageLoadingTest.java index 545d236248f9a..e5a792b8dcc87 100644 --- a/java/test/org/openqa/selenium/PageLoadingTest.java +++ b/java/test/org/openqa/selenium/PageLoadingTest.java @@ -36,10 +36,12 @@ import org.openqa.selenium.testing.Ignore; import org.openqa.selenium.testing.JupiterTestBase; import org.openqa.selenium.testing.NeedsFreshDriver; +import org.openqa.selenium.testing.NeedsSecureServer; import org.openqa.selenium.testing.NoDriverAfterTest; import org.openqa.selenium.testing.NotYetImplemented; import org.openqa.selenium.testing.SwitchToTopAfterTest; +@NeedsSecureServer class PageLoadingTest extends JupiterTestBase { @Test diff --git a/java/test/org/openqa/selenium/environment/InProcessTestEnvironment.java b/java/test/org/openqa/selenium/environment/InProcessTestEnvironment.java index 150f0a2685a5a..70b29dfd27a70 100644 --- a/java/test/org/openqa/selenium/environment/InProcessTestEnvironment.java +++ b/java/test/org/openqa/selenium/environment/InProcessTestEnvironment.java @@ -24,8 +24,8 @@ public class InProcessTestEnvironment implements TestEnvironment { private final AppServer appServer; - public InProcessTestEnvironment() { - appServer = new NettyAppServer(); + public InProcessTestEnvironment(boolean secureServer) { + appServer = new NettyAppServer(secureServer); appServer.start(); } @@ -40,6 +40,6 @@ public void stop() { } public static void main(String[] args) { - new InProcessTestEnvironment(); + new InProcessTestEnvironment(true); } } diff --git a/java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java b/java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java index d477bb4f1a2fd..04316b0e451d8 100644 --- a/java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java +++ b/java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java @@ -68,13 +68,13 @@ public class NettyAppServer implements AppServer { LOG.log( Level.WARNING, String.format("NettyAppServer retry #%s. ", e.getAttemptCount())); - initValues(); + initValues(secure != null); }) .onRetriesExceeded(e -> LOG.log(Level.WARNING, "NettyAppServer start aborted.")) .build(); - public NettyAppServer() { - initValues(); + public NettyAppServer(boolean secureServer) { + initValues(secureServer); } public NettyAppServer(HttpHandler handler) { @@ -116,7 +116,7 @@ public static void main(String[] args) { System.out.printf("Server started. Root URL: %s%n", server.whereIs("/")); } - private void initValues() { + private void initValues(boolean secureServer) { Config config = createDefaultConfig(); BaseServerOptions options = new BaseServerOptions(config); @@ -128,16 +128,18 @@ private void initValues() { server = new NettyServer(options, handler); - Config secureConfig = new CompoundConfig(sslConfig, createDefaultConfig()); - BaseServerOptions secureOptions = new BaseServerOptions(secureConfig); + if (secureServer) { + Config secureConfig = new CompoundConfig(sslConfig, createDefaultConfig()); + BaseServerOptions secureOptions = new BaseServerOptions(secureConfig); - HttpHandler secureHandler = - new HandlersForTests( - secureOptions.getHostname().orElse("localhost"), - secureOptions.getPort(), - tempDir.toPath()); + HttpHandler secureHandler = + new HandlersForTests( + secureOptions.getHostname().orElse("localhost"), + secureOptions.getPort(), + tempDir.toPath()); - secure = new NettyServer(secureOptions, secureHandler); + secure = new NettyServer(secureOptions, secureHandler); + } } @Override diff --git a/java/test/org/openqa/selenium/environment/webserver/NettyAppServerTest.java b/java/test/org/openqa/selenium/environment/webserver/NettyAppServerTest.java index 86b40e0282449..3574739763528 100644 --- a/java/test/org/openqa/selenium/environment/webserver/NettyAppServerTest.java +++ b/java/test/org/openqa/selenium/environment/webserver/NettyAppServerTest.java @@ -21,6 +21,6 @@ class NettyAppServerTest extends AppServerTestBase { @Override protected AppServer createAppServer() { - return new NettyAppServer(); + return new NettyAppServer(false); } } diff --git a/java/test/org/openqa/selenium/federatedcredentialmanagement/FederatedCredentialManagementTest.java b/java/test/org/openqa/selenium/federatedcredentialmanagement/FederatedCredentialManagementTest.java index 8bf947f4ca584..ddb9b5b462f45 100644 --- a/java/test/org/openqa/selenium/federatedcredentialmanagement/FederatedCredentialManagementTest.java +++ b/java/test/org/openqa/selenium/federatedcredentialmanagement/FederatedCredentialManagementTest.java @@ -36,8 +36,10 @@ import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.testing.JupiterTestBase; +import org.openqa.selenium.testing.NeedsSecureServer; import org.openqa.selenium.testing.NotYetImplemented; +@NeedsSecureServer class FederatedCredentialManagementTest extends JupiterTestBase { private JavascriptExecutor jsAwareDriver; diff --git a/java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java b/java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java index a9debd364b8f1..8269f5028cfc9 100644 --- a/java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java +++ b/java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java @@ -61,7 +61,7 @@ class RemoteWebDriverBiDiTest { @BeforeAll static void serverSetup() { - server = new NettyAppServer(); + server = new NettyAppServer(false); server.start(); } diff --git a/java/test/org/openqa/selenium/grid/router/RemoteWebDriverDownloadTest.java b/java/test/org/openqa/selenium/grid/router/RemoteWebDriverDownloadTest.java index 96f9ba64b23bf..6ebc1752b6e12 100644 --- a/java/test/org/openqa/selenium/grid/router/RemoteWebDriverDownloadTest.java +++ b/java/test/org/openqa/selenium/grid/router/RemoteWebDriverDownloadTest.java @@ -88,7 +88,7 @@ public void setupServers() { tearDowns.add(deployment); server = deployment.getServer(); - appServer = new NettyAppServer(); + appServer = new NettyAppServer(false); tearDowns.add(() -> appServer.stop()); appServer.start(); } diff --git a/java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java b/java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java index 002bfd5d205b8..cbb5f660b959a 100644 --- a/java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java +++ b/java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java @@ -62,7 +62,7 @@ private static boolean isBazel() { @BeforeEach public void setup() { // this field is actually in use, javascript test do access it - testEnvironment = GlobalTestEnvironment.getOrCreate(InProcessTestEnvironment::new); + testEnvironment = GlobalTestEnvironment.getOrCreate(() -> new InProcessTestEnvironment(true)); } @AfterEach diff --git a/java/test/org/openqa/selenium/safari/CrossDomainTest.java b/java/test/org/openqa/selenium/safari/CrossDomainTest.java index 2f9e5c4a51724..842f0fc7db4c4 100644 --- a/java/test/org/openqa/selenium/safari/CrossDomainTest.java +++ b/java/test/org/openqa/selenium/safari/CrossDomainTest.java @@ -41,7 +41,7 @@ class CrossDomainTest extends JupiterTestBase { @BeforeAll public static void startSecondServer() { - otherServer = new NettyAppServer(); + otherServer = new NettyAppServer(false); otherServer.start(); otherPages = new Pages(otherServer); diff --git a/java/test/org/openqa/selenium/testing/BUILD.bazel b/java/test/org/openqa/selenium/testing/BUILD.bazel index 9bc398860502a..68d6d6a2f675d 100644 --- a/java/test/org/openqa/selenium/testing/BUILD.bazel +++ b/java/test/org/openqa/selenium/testing/BUILD.bazel @@ -20,6 +20,7 @@ java_library( "Ignore.java", "IgnoreList.java", "NeedsFreshDriver.java", + "NeedsSecureServer.java", "NoDriverAfterTest.java", "NoDriverBeforeTest.java", "NotWorkingInRemoteBazelBuilds.java", diff --git a/java/test/org/openqa/selenium/testing/JupiterTestBase.java b/java/test/org/openqa/selenium/testing/JupiterTestBase.java index bd42009600ba5..a540bafb7c849 100644 --- a/java/test/org/openqa/selenium/testing/JupiterTestBase.java +++ b/java/test/org/openqa/selenium/testing/JupiterTestBase.java @@ -22,6 +22,8 @@ import java.net.MalformedURLException; import java.net.URL; import java.time.Duration; +import java.util.Optional; +import java.util.logging.Logger; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -37,6 +39,8 @@ public abstract class JupiterTestBase { + private static final Logger LOG = Logger.getLogger(JupiterTestBase.class.getName()); + @RegisterExtension protected static SeleniumExtension seleniumExtension = new SeleniumExtension(); protected TestEnvironment environment; @@ -54,9 +58,27 @@ public static void shouldTestBeRunAtAll() { @BeforeEach public void prepareEnvironment() { - environment = GlobalTestEnvironment.getOrCreate(InProcessTestEnvironment::new); + boolean needsSecureServer = + Optional.ofNullable(this.getClass().getAnnotation(NeedsSecureServer.class)) + .map(NeedsSecureServer::value) + .orElse(false); + + environment = + GlobalTestEnvironment.getOrCreate(() -> new InProcessTestEnvironment(needsSecureServer)); appServer = environment.getAppServer(); + if (needsSecureServer) { + try { + appServer.whereIsSecure("/"); + } catch (IllegalStateException ex) { + // this should not happen with bazel, a new JVM is used for each class + // the annotation is on class level, so we should never see this + LOG.info("appServer is restarted with secureServer=true"); + environment.stop(); + environment = new InProcessTestEnvironment(true); + } + } + pages = new Pages(appServer); driver = seleniumExtension.getDriver(); diff --git a/java/test/org/openqa/selenium/testing/NeedsSecureServer.java b/java/test/org/openqa/selenium/testing/NeedsSecureServer.java new file mode 100644 index 0000000000000..beda0b16d666b --- /dev/null +++ b/java/test/org/openqa/selenium/testing/NeedsSecureServer.java @@ -0,0 +1,30 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.testing; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface NeedsSecureServer { + + boolean value() default true; +}