testing: refactor part of TestUtils to internal

Moved the following APIs from `io.grpc.testing.TestUtils` to `io.grpc.internal.TestUtils`:

`InetSocketAddress testServerAddress(String host, int port)`
`InetSocketAddress testServerAddress(int port)`
`List<String> preferredTestCiphers()`
`File loadCert(String name)`
`X509Certificate loadX509Cert(String fileName)`
`SSLSocketFactory newSslSocketFactoryForCa(Provider provider, File certChainFile)`
`void sleepAtLeast(long millis)`

APIs not to be moved:

`ServerInterceptor recordRequestHeadersInterceptor()`
`ServerInterceptor recordServerCallInterceptor()`
This commit is contained in:
ZHANG Dapeng 2017-07-10 16:30:38 -07:00 committed by GitHub
parent 8ce0bc25b6
commit ff0ad5fac3
17 changed files with 219 additions and 16 deletions

View File

@ -28,12 +28,12 @@ import io.grpc.benchmarks.proto.Messages.Payload;
import io.grpc.benchmarks.proto.Messages.SimpleRequest;
import io.grpc.benchmarks.proto.Messages.SimpleResponse;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.okhttp.OkHttpChannelBuilder;
import io.grpc.okhttp.internal.Platform;
import io.grpc.testing.TestUtils;
import io.netty.channel.epoll.EpollDomainSocketChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;

View File

@ -36,8 +36,8 @@ import io.grpc.benchmarks.proto.BenchmarkServiceGrpc;
import io.grpc.benchmarks.proto.Control;
import io.grpc.benchmarks.proto.Messages;
import io.grpc.benchmarks.proto.Stats;
import io.grpc.internal.testing.TestUtils;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.TestUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import java.io.File;

View File

@ -22,10 +22,10 @@ import io.grpc.benchmarks.Utils;
import io.grpc.benchmarks.proto.BenchmarkServiceGrpc;
import io.grpc.benchmarks.proto.Messages.SimpleRequest;
import io.grpc.benchmarks.proto.Messages.SimpleResponse;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.TestUtils;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;

View File

@ -26,7 +26,7 @@ import io.grpc.benchmarks.Utils;
import io.grpc.benchmarks.proto.Control.RpcType;
import io.grpc.benchmarks.proto.Messages;
import io.grpc.benchmarks.proto.Messages.PayloadType;
import io.grpc.testing.TestUtils;
import io.grpc.internal.testing.TestUtils;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

View File

@ -37,11 +37,11 @@ import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.TestUtils;
import io.netty.handler.ssl.SslContext;
import java.io.IOException;
import java.net.InetAddress;

View File

@ -19,12 +19,12 @@ package io.grpc.testing.integration;
import com.google.common.io.Files;
import io.grpc.ManagedChannel;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.okhttp.OkHttpChannelBuilder;
import io.grpc.okhttp.internal.Platform;
import io.grpc.testing.TestUtils;
import io.netty.handler.ssl.SslContext;
import java.io.File;
import java.io.FileInputStream;

View File

@ -20,9 +20,9 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.Server;
import io.grpc.ServerInterceptors;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.testing.TestUtils;
import io.netty.handler.ssl.SslContext;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

View File

@ -20,12 +20,12 @@ import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.ManagedChannel;
import io.grpc.Server;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.TestUtils;
import io.grpc.testing.integration.Messages.PayloadType;
import io.grpc.testing.integration.Messages.ResponseParameters;
import io.grpc.testing.integration.Messages.StreamingOutputCallRequest;

View File

@ -17,10 +17,10 @@
package io.grpc.testing.integration;
import io.grpc.ManagedChannel;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.testing.TestUtils;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.SupportedCipherSuiteFilter;

View File

@ -26,13 +26,13 @@ import com.squareup.okhttp.ConnectionSpec;
import com.squareup.okhttp.TlsVersion;
import io.grpc.ManagedChannel;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.okhttp.OkHttpChannelBuilder;
import io.grpc.okhttp.internal.Platform;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.StreamRecorder;
import io.grpc.testing.TestUtils;
import io.netty.handler.ssl.OpenSsl;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;

View File

@ -27,11 +27,11 @@ import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.testing.TestUtils;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.OpenSsl;
import io.netty.handler.ssl.SslContext;

View File

@ -56,7 +56,7 @@ import io.grpc.internal.ServerStream;
import io.grpc.internal.ServerStreamListener;
import io.grpc.internal.ServerTransport;
import io.grpc.internal.ServerTransportListener;
import io.grpc.testing.TestUtils;
import io.grpc.internal.testing.TestUtils;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;

View File

@ -20,6 +20,7 @@ import static com.google.common.base.Charsets.UTF_8;
import static io.grpc.internal.GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
import static io.grpc.internal.GrpcUtil.DEFAULT_SERVER_KEEPALIVE_TIMEOUT_NANOS;
import static io.grpc.internal.GrpcUtil.DEFAULT_SERVER_KEEPALIVE_TIME_NANOS;
import static io.grpc.internal.testing.TestUtils.sleepAtLeast;
import static io.grpc.netty.NettyServerBuilder.MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE;
import static io.grpc.netty.NettyServerBuilder.MAX_CONNECTION_AGE_NANOS_DISABLED;
import static io.grpc.netty.NettyServerBuilder.MAX_CONNECTION_IDLE_NANOS_DISABLED;
@ -28,7 +29,6 @@ import static io.grpc.netty.Utils.CONTENT_TYPE_HEADER;
import static io.grpc.netty.Utils.HTTP_METHOD;
import static io.grpc.netty.Utils.TE_HEADER;
import static io.grpc.netty.Utils.TE_TRAILERS;
import static io.grpc.testing.TestUtils.sleepAtLeast;
import static io.netty.buffer.Unpooled.directBuffer;
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_WINDOW_SIZE;
import static org.junit.Assert.assertArrayEquals;

View File

@ -26,9 +26,9 @@ import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.ProtocolNegotiators.ServerTlsHandler;
import io.grpc.netty.ProtocolNegotiators.TlsNegotiator;
import io.grpc.testing.TestUtils;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;

View File

@ -0,0 +1,181 @@
/*
* Copyright 2014, gRPC Authors All rights reserved.
*
* Licensed 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 io.grpc.internal.testing;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import javax.security.auth.x500.X500Principal;
/**
* Internal utility functions useful for writing tests.
*/
public class TestUtils {
public static final String TEST_SERVER_HOST = "foo.test.google.fr";
/**
* Creates a new {@link InetSocketAddress} that overrides the host with {@link #TEST_SERVER_HOST}.
*/
public static InetSocketAddress testServerAddress(String host, int port) {
try {
InetAddress inetAddress = InetAddress.getByName(host);
inetAddress = InetAddress.getByAddress(TEST_SERVER_HOST, inetAddress.getAddress());
return new InetSocketAddress(inetAddress, port);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
/**
* Creates a new {@link InetSocketAddress} on localhost that overrides the host with
* {@link #TEST_SERVER_HOST}.
*/
public static InetSocketAddress testServerAddress(int port) {
try {
InetAddress inetAddress = InetAddress.getByName("::1");
inetAddress = InetAddress.getByAddress(TEST_SERVER_HOST, inetAddress.getAddress());
return new InetSocketAddress(inetAddress, port);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
/**
* Returns the ciphers preferred to use during tests. They may be chosen because they are widely
* available or because they are fast. There is no requirement that they provide confidentiality
* or integrity.
*/
public static List<String> preferredTestCiphers() {
String[] ciphers;
try {
ciphers = SSLContext.getDefault().getDefaultSSLParameters().getCipherSuites();
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
List<String> ciphersMinusGcm = new ArrayList<String>();
for (String cipher : ciphers) {
// The GCM implementation in Java is _very_ slow (~1 MB/s)
if (cipher.contains("_GCM_")) {
continue;
}
ciphersMinusGcm.add(cipher);
}
return Collections.unmodifiableList(ciphersMinusGcm);
}
/**
* Saves a file from the classpath resources in src/main/resources/certs as a file on the
* filesystem.
*
* @param name name of a file in src/main/resources/certs.
*/
public static File loadCert(String name) throws IOException {
InputStream
in = new BufferedInputStream(TestUtils.class.getResourceAsStream("/certs/" + name));
File tmpFile = File.createTempFile(name, "");
tmpFile.deleteOnExit();
OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile));
try {
int b;
while ((b = in.read()) != -1) {
os.write(b);
}
os.flush();
} finally {
in.close();
os.close();
}
return tmpFile;
}
/**
* Loads an X.509 certificate from the classpath resources in src/main/resources/certs.
*
* @param fileName name of a file in src/main/resources/certs.
*/
public static X509Certificate loadX509Cert(String fileName)
throws CertificateException, IOException {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream in = TestUtils.class.getResourceAsStream("/certs/" + fileName);
try {
return (X509Certificate) cf.generateCertificate(in);
} finally {
in.close();
}
}
/**
* Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
*/
public static SSLSocketFactory newSslSocketFactoryForCa(Provider provider,
File certChainFile) throws Exception {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(
new BufferedInputStream(new FileInputStream(certChainFile)));
X500Principal principal = cert.getSubjectX500Principal();
ks.setCertificateEntry(principal.getName("RFC2253"), cert);
// Set up trust manager factory to use our key store.
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks);
SSLContext context = SSLContext.getInstance("TLS", provider);
context.init(null, trustManagerFactory.getTrustManagers(), null);
return context.getSocketFactory();
}
/**
* Sleeps for at least the specified time. When in need of a guaranteed sleep time, use this in
* preference to {@code Thread.sleep} which might not sleep for the required time.
*/
public static void sleepAtLeast(long millis) throws InterruptedException {
long delay = TimeUnit.MILLISECONDS.toNanos(millis);
long end = System.nanoTime() + delay;
while (delay > 0) {
TimeUnit.NANOSECONDS.sleep(delay);
delay = end - System.nanoTime();
}
}
private TestUtils() {}
}

View File

@ -53,6 +53,7 @@ import javax.security.auth.x500.X500Principal;
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1791")
public class TestUtils {
@Deprecated
public static final String TEST_SERVER_HOST = "foo.test.google.fr";
/**
@ -92,7 +93,10 @@ public class TestUtils {
/**
* Creates a new {@link InetSocketAddress} that overrides the host with {@link #TEST_SERVER_HOST}.
*
* @deprecated Not for public use
*/
@Deprecated
public static InetSocketAddress testServerAddress(String host, int port) {
try {
InetAddress inetAddress = InetAddress.getByName(host);
@ -106,7 +110,10 @@ public class TestUtils {
/**
* Creates a new {@link InetSocketAddress} on localhost that overrides the host with
* {@link #TEST_SERVER_HOST}.
*
* @deprecated Not for public use
*/
@Deprecated
public static InetSocketAddress testServerAddress(int port) {
try {
InetAddress inetAddress = InetAddress.getByName("::1");
@ -121,7 +128,10 @@ public class TestUtils {
* Returns the ciphers preferred to use during tests. They may be chosen because they are widely
* available or because they are fast. There is no requirement that they provide confidentiality
* or integrity.
*
* @deprecated Not for public use
*/
@Deprecated
public static List<String> preferredTestCiphers() {
String[] ciphers;
try {
@ -145,7 +155,10 @@ public class TestUtils {
* filesystem.
*
* @param name name of a file in src/main/resources/certs.
*
* @deprecated Not for public use
*/
@Deprecated
public static File loadCert(String name) throws IOException {
InputStream in = new BufferedInputStream(TestUtils.class.getResourceAsStream("/certs/" + name));
File tmpFile = File.createTempFile(name, "");
@ -170,7 +183,10 @@ public class TestUtils {
* Loads an X.509 certificate from the classpath resources in src/main/resources/certs.
*
* @param fileName name of a file in src/main/resources/certs.
*
* @deprecated Not for public use
*/
@Deprecated
public static X509Certificate loadX509Cert(String fileName)
throws CertificateException, IOException {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
@ -185,7 +201,10 @@ public class TestUtils {
/**
* Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
*
* @deprecated Not for public use
*/
@Deprecated
public static SSLSocketFactory newSslSocketFactoryForCa(Provider provider,
File certChainFile) throws Exception {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
@ -208,7 +227,10 @@ public class TestUtils {
/**
* Sleeps for at least the specified time. When in need of a guaranteed sleep time, use this in
* preference to {@code Thread.sleep} which might not sleep for the required time.
*
* @deprecated Not for public use
*/
@Deprecated
public static void sleepAtLeast(long millis) throws InterruptedException {
long delay = TimeUnit.MILLISECONDS.toNanos(millis);
long end = System.nanoTime() + delay;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.grpc.testing;
package io.grpc.internal.testing;
import static com.google.common.truth.Truth.assertThat;
@ -24,7 +24,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Unit tests for {@link TestUtils}.
* Unit tests for {@link io.grpc.internal.testing.TestUtils}.
*/
@RunWith(JUnit4.class)
public class TestUtilsTest {