|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +package com.google.api.gax.httpjson; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 35 | +import static org.junit.jupiter.api.Assertions.fail; |
| 36 | + |
| 37 | +import com.google.api.gax.core.NoCredentialsProvider; |
| 38 | +import com.google.cloud.translate.v3.LocationName; |
| 39 | +import com.google.cloud.translate.v3.TranslationServiceClient; |
| 40 | +import com.google.cloud.translate.v3.TranslationServiceSettings; |
| 41 | +import java.io.InputStream; |
| 42 | +import java.security.Provider; |
| 43 | +import java.security.Security; |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.TreeSet; |
| 46 | +import org.junit.jupiter.api.AfterAll; |
| 47 | +import org.junit.jupiter.api.BeforeAll; |
| 48 | +import org.junit.jupiter.api.Test; |
| 49 | + |
| 50 | +public abstract class PqcConnectivityTest { |
| 51 | + |
| 52 | + private static Process serverProcess; |
| 53 | + protected static int grpcPqcPort; |
| 54 | + protected static int grpcClassicalPort; |
| 55 | + |
| 56 | + protected boolean clientSupportsPqc() { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + protected abstract boolean grpcTestShouldSucceed(); |
| 61 | + |
| 62 | + @BeforeAll |
| 63 | + public static void setup() throws Exception { |
| 64 | + |
| 65 | + // 6. Spawn PqcTestServer in a separate background process to ensure physical |
| 66 | + // JVM runtime isolation! |
| 67 | + ProcessBuilder pb = |
| 68 | + new ProcessBuilder( |
| 69 | + "java", |
| 70 | + "-cp", |
| 71 | + System.getProperty("java.class.path"), |
| 72 | + "com.google.api.gax.pqc.PqcTestServer"); |
| 73 | + |
| 74 | + // Force merging of error stream to ease debugging in test output |
| 75 | + pb.redirectErrorStream(true); |
| 76 | + serverProcess = pb.start(); |
| 77 | + |
| 78 | + // Read server's stdout to dynamically capture the allocated ephemeral ports |
| 79 | + java.io.BufferedReader reader = |
| 80 | + new java.io.BufferedReader( |
| 81 | + new java.io.InputStreamReader( |
| 82 | + serverProcess.getInputStream(), java.nio.charset.StandardCharsets.UTF_8)); |
| 83 | + |
| 84 | + String line; |
| 85 | + boolean grpcPqcFound = false; |
| 86 | + boolean grpcClassicalFound = false; |
| 87 | + |
| 88 | + // Wait for the server process to output its HTTP and gRPC ports |
| 89 | + long startTime = System.currentTimeMillis(); |
| 90 | + while ((line = reader.readLine()) != null) { |
| 91 | + System.out.println("[SERVER-OUT] " + line); |
| 92 | + if (line.startsWith("GRPC_PQC_PORT: ")) { |
| 93 | + grpcPqcPort = Integer.parseInt(line.substring(15).trim()); |
| 94 | + grpcPqcFound = true; |
| 95 | + } else if (line.startsWith("GRPC_CLASSICAL_PORT: ")) { |
| 96 | + grpcClassicalPort = Integer.parseInt(line.substring(21).trim()); |
| 97 | + grpcClassicalFound = true; |
| 98 | + } |
| 99 | + |
| 100 | + if (grpcPqcFound && grpcClassicalFound) { |
| 101 | + break; |
| 102 | + } |
| 103 | + |
| 104 | + // Ephemeral port detection timeout (10 seconds) to fail-fast on server startup |
| 105 | + // errors |
| 106 | + if (System.currentTimeMillis() - startTime > 10000) { |
| 107 | + throw new RuntimeException( |
| 108 | + "Timeout waiting for PqcTestServer ephemeral ports to be printed!"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (!grpcPqcFound || !grpcClassicalFound) { |
| 113 | + throw new RuntimeException("PqcTestServer failed to initialize ephemeral ports!"); |
| 114 | + } |
| 115 | + |
| 116 | + // Start a background thread to continuously drain the server's stdout |
| 117 | + Thread drainThread = |
| 118 | + new Thread( |
| 119 | + () -> { |
| 120 | + try { |
| 121 | + String l; |
| 122 | + while ((l = reader.readLine()) != null) { |
| 123 | + System.out.println("[SERVER-OUT] " + l); |
| 124 | + } |
| 125 | + } catch (java.io.IOException e) { |
| 126 | + // Ignore stream closed |
| 127 | + } |
| 128 | + }); |
| 129 | + drainThread.setDaemon(true); |
| 130 | + drainThread.start(); |
| 131 | + } |
| 132 | + |
| 133 | + @AfterAll |
| 134 | + public static void teardown() { |
| 135 | + if (serverProcess != null) { |
| 136 | + // Forcibly destroy the background process and close standard streams to allow |
| 137 | + // clean exit |
| 138 | + serverProcess.destroyForcibly(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void runGrpcTest(int port, boolean shouldSucceed) throws Exception { |
| 143 | + TranslationServiceSettings settings = |
| 144 | + TranslationServiceSettings.newBuilder() |
| 145 | + .setEndpoint("localhost:" + port) |
| 146 | + .setCredentialsProvider(NoCredentialsProvider.create()) |
| 147 | + .build(); |
| 148 | + |
| 149 | + try (TranslationServiceClient client = TranslationServiceClient.create(settings)) { |
| 150 | + LocationName parent = LocationName.of("test-project", "global"); |
| 151 | + client.translateText(parent, "es", Collections.singletonList("hello")); |
| 152 | + if (!shouldSucceed) { |
| 153 | + fail("Expected gRPC call to fail!"); |
| 154 | + } |
| 155 | + } catch (Exception e) { |
| 156 | + if (shouldSucceed) { |
| 157 | + fail("Expected gRPC call to succeed, but failed: " + e.getMessage(), e); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testGrpcPqcServerEnforced() throws Exception { |
| 166 | + runGrpcTest(grpcPqcPort, grpcTestShouldSucceed()); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testGrpcClassicalServer() throws Exception { |
| 171 | + runGrpcTest(grpcClassicalPort, true); |
| 172 | + } |
| 173 | +} |
0 commit comments