Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion benchmarks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ dependencies {
project(':grpc-netty'),
project(':grpc-okhttp'),
project(':grpc-stub'),
project(':grpc-interop-testing'),
project(':grpc-protobuf'),
project(':grpc-testing'),
libraries.junit,
libraries.mockito,
libraries.hdrhistogram,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

package io.grpc.benchmarks.qps;

import static io.grpc.testing.integration.Util.loadCert;

import com.google.common.util.concurrent.MoreExecutors;
import com.google.protobuf.ByteString;

Expand All @@ -44,8 +42,10 @@
import io.grpc.testing.SimpleRequest;
import io.grpc.testing.SimpleResponse;
import io.grpc.testing.TestServiceGrpc;
import io.grpc.testing.TestUtils;
import io.grpc.transport.netty.GrpcSslContexts;
import io.grpc.transport.netty.NettyServerBuilder;

import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;
Expand Down Expand Up @@ -106,8 +106,8 @@ static ServerImpl newServer(ServerConfiguration config) throws IOException {
System.out.println("Using fake CA for TLS certificate.\n"
+ "Run the Java client with --tls --testca");

File cert = loadCert("server1.pem");
File key = loadCert("server1.key");
File cert = TestUtils.loadCert("server1.pem");
File key = TestUtils.loadCert("server1.key");
boolean useJdkSsl = config.transport == ServerConfiguration.Transport.NETTY_NIO;
sslContext = GrpcSslContexts.forServer(cert, key)
.sslProvider(useJdkSsl ? SslProvider.JDK : SslProvider.OPENSSL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
import static io.grpc.benchmarks.qps.SocketAddressValidator.INET;
import static io.grpc.benchmarks.qps.SocketAddressValidator.UDS;
import static io.grpc.benchmarks.qps.Utils.parseBoolean;
import static io.grpc.testing.integration.Util.pickUnusedPort;
import static java.lang.Integer.parseInt;

import io.grpc.testing.TestUtils;
import io.grpc.transport.netty.NettyChannelBuilder;

import java.net.InetSocketAddress;
Expand Down Expand Up @@ -160,7 +160,7 @@ protected void setServerValue(ServerConfiguration config, String value) {
SocketAddress address = Utils.parseSocketAddress(value);
if (address instanceof InetSocketAddress) {
InetSocketAddress addr = (InetSocketAddress) address;
int port = addr.getPort() == 0 ? pickUnusedPort() : addr.getPort();
int port = addr.getPort() == 0 ? TestUtils.pickUnusedPort() : addr.getPort();
// Re-create the address so that the server is available on all local addresses.
address = new InetSocketAddress(port);
}
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/src/main/java/io/grpc/benchmarks/qps/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@

package io.grpc.benchmarks.qps;

import static io.grpc.testing.integration.Util.loadCert;

import com.google.common.util.concurrent.MoreExecutors;
import com.google.protobuf.ByteString;

import io.grpc.Channel;
import io.grpc.testing.Payload;
import io.grpc.testing.SimpleRequest;
import io.grpc.testing.TestUtils;
import io.grpc.transport.netty.GrpcSslContexts;
import io.grpc.transport.netty.NegotiationType;
import io.grpc.transport.netty.NettyChannelBuilder;
import io.grpc.transport.okhttp.OkHttpChannelBuilder;

import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollDomainSocketChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
Expand Down Expand Up @@ -139,7 +139,7 @@ static Channel newClientChannel(ClientConfiguration config) throws IOException {
SslContext context = null;
NegotiationType negotiationType = config.tls ? NegotiationType.TLS : NegotiationType.PLAINTEXT;
if (config.tls && config.testca) {
File cert = loadCert("ca.pem");
File cert = TestUtils.loadCert("ca.pem");
boolean useJdkSsl = config.transport == ClientConfiguration.Transport.NETTY_NIO;
context = GrpcSslContexts.forClient().trustManager(cert)
.sslProvider(useJdkSsl ? SslProvider.JDK : SslProvider.OPENSSL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
package io.grpc.testing.integration;

import io.grpc.ChannelImpl;
import io.grpc.testing.TestUtils;
import io.grpc.transport.netty.GrpcSslContexts;
import io.grpc.transport.netty.NegotiationType;
import io.grpc.transport.netty.NettyChannelBuilder;
Expand Down Expand Up @@ -216,7 +217,8 @@ protected ChannelImpl createChannel() {
SslContext sslContext = null;
if (useTestCa) {
try {
sslContext = GrpcSslContexts.forClient().trustManager(Util.loadCert("ca.pem")).build();
sslContext = GrpcSslContexts.forClient().trustManager(
TestUtils.loadCert("ca.pem")).build();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
Expand All @@ -234,7 +236,7 @@ protected ChannelImpl createChannel() {
if (useTls) {
try {
SSLSocketFactory factory = useTestCa
? Util.getSslSocketFactoryForCertainCert(Util.loadCert("ca.pem"))
? TestUtils.getSslSocketFactoryForCertainCert(TestUtils.loadCert("ca.pem"))
: (SSLSocketFactory) SSLSocketFactory.getDefault();
builder.sslSocketFactory(factory);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void start() throws Exception {
SslContext sslContext = null;
if (useTls) {
sslContext = GrpcSslContexts.forServer(
Util.loadCert("server1.pem"), Util.loadCert("server1.key")).build();
TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key")).build();
}
server = NettyServerBuilder.forPort(port)
.sslContext(sslContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,8 @@

import org.junit.Assert;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import javax.security.auth.x500.X500Principal;

/**
* Utility methods to support integration testing.
*/
Expand All @@ -64,43 +48,6 @@ public class Util {
public static final Metadata.Key<Messages.SimpleContext> METADATA_KEY =
ProtoUtils.keyForProto(Messages.SimpleContext.getDefaultInstance());

/**
* Picks an unused port.
*/
public static int pickUnusedPort() {
try {
ServerSocket serverSocket = new ServerSocket(0);
int port = serverSocket.getLocalPort();
serverSocket.close();
return port;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Load a file from the resources folder.
*
* @param name name of a file in src/main/resources/certs.
*/
public static File loadCert(String name) throws IOException {
InputStream in = Util.class.getResourceAsStream("/certs/" + name);
File tmpFile = File.createTempFile(name, "");
tmpFile.deleteOnExit();

BufferedWriter writer = new BufferedWriter(new FileWriter(tmpFile));
try {
int b;
while ((b = in.read()) != -1) {
writer.write(b);
}
} finally {
writer.close();
}

return tmpFile;
}

/** Assert that two messages are equal, producing a useful message if not. */
public static void assertEquals(MessageLite expected, MessageLite actual) {
if (expected == null || actual == null) {
Expand Down Expand Up @@ -129,26 +76,4 @@ public static void assertEquals(List<? extends MessageLite> expected,
}
}
}

/**
* Returns a SSLSocketFactory which uses the certificate specified in certChainFile.
*/
public static SSLSocketFactory getSslSocketFactoryForCertainCert(File certChainFile)
throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
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");
context.init(null, trustManagerFactory.getTrustManagers(), null);
return context.getSocketFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
package io.grpc.testing.integration;

import io.grpc.ChannelImpl;
import io.grpc.testing.TestUtils;
import io.grpc.transport.netty.GrpcSslContexts;
import io.grpc.transport.netty.NettyChannelBuilder;
import io.grpc.transport.netty.NettyServerBuilder;
Expand All @@ -50,15 +51,15 @@
*/
@RunWith(JUnit4.class)
public class Http2NettyTest extends AbstractTransportTest {
private static int serverPort = Util.pickUnusedPort();
private static int serverPort = TestUtils.pickUnusedPort();

/** Starts the server with HTTPS. */
@BeforeClass
public static void startServer() {
try {
startStaticServer(NettyServerBuilder.forPort(serverPort)
.sslContext(GrpcSslContexts.forServer(
Util.loadCert("server1.pem"), Util.loadCert("server1.key")).build()));
TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key")).build()));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
Expand All @@ -76,7 +77,8 @@ protected ChannelImpl createChannel() {
= InetAddress.getByAddress("foo.test.google.fr", new byte[] {127, 0, 0, 1});
return NettyChannelBuilder
.forAddress(new InetSocketAddress(address, serverPort))
.sslContext(GrpcSslContexts.forClient().trustManager(Util.loadCert("ca.pem")).build())
.sslContext(GrpcSslContexts.forClient().trustManager(
TestUtils.loadCert("ca.pem")).build())
.build();
} catch (Exception ex) {
throw new RuntimeException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
package io.grpc.testing.integration;

import io.grpc.ChannelImpl;
import io.grpc.testing.TestUtils;
import io.grpc.transport.netty.GrpcSslContexts;
import io.grpc.transport.netty.NettyServerBuilder;
import io.grpc.transport.okhttp.OkHttpChannelBuilder;
Expand All @@ -48,15 +49,15 @@
*/
@RunWith(JUnit4.class)
public class Http2OkHttpTest extends AbstractTransportTest {
private static int serverPort = Util.pickUnusedPort();
private static int serverPort = TestUtils.pickUnusedPort();

/** Starts the server with HTTPS. */
@BeforeClass
public static void startServer() throws Exception {
try {
startStaticServer(NettyServerBuilder.forPort(serverPort)
.sslContext(GrpcSslContexts.forServer(
Util.loadCert("server1.pem"), Util.loadCert("server1.key")).build()));
TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key")).build()));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
Expand All @@ -72,7 +73,8 @@ protected ChannelImpl createChannel() {
OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("127.0.0.1", serverPort)
.overrideHostForAuthority("foo.test.google.fr");
try {
builder.sslSocketFactory(Util.getSslSocketFactoryForCertainCert(Util.loadCert("ca.pem")));
builder.sslSocketFactory(TestUtils.getSslSocketFactoryForCertainCert(
TestUtils.loadCert("ca.pem")));
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Loading