Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public boolean equals(Object obj) {

private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
executorFactory = ServiceOptions.newInstance(executorFactoryClassName);
executorFactory = ServiceOptions.newInstance(executorFactoryClassName, ExecutorFactory.class);
}

public static Builder newBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

import com.google.cloud.grpc.GrpcTransportOptions.DefaultExecutorFactory;
import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.ScheduledExecutorService;
import org.easymock.EasyMock;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -67,4 +71,19 @@ void testDefaultExecutorFactory() {
ScheduledExecutorService executorService = executorFactory.get();
assertSame(executorService, executorFactory.get());
}

@Test
void testSerializationRoundTrip() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(DEFAULT_OPTIONS);
}
GrpcTransportOptions deserialized;
try (ObjectInputStream ois =
new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
deserialized = (GrpcTransportOptions) ois.readObject();
}
assertEquals(DEFAULT_OPTIONS, deserialized);
assertTrue(deserialized.getExecutorFactory() instanceof DefaultExecutorFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ public boolean equals(Object obj) {

private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
httpTransportFactory = ServiceOptions.newInstance(httpTransportFactoryClassName);
httpTransportFactory =
ServiceOptions.newInstance(httpTransportFactoryClassName, HttpTransportFactory.class);
}

public static Builder newBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
import com.google.cloud.TransportOptions;
import com.google.cloud.http.HttpTransportOptions.DefaultHttpTransportFactory;
import com.google.cloud.spi.ServiceRpcFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Set;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -342,4 +346,19 @@ public int hashCode() {
return baseHashCode();
}
}

@Test
void testSerializationRoundTrip() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(DEFAULT_OPTIONS);
}
HttpTransportOptions deserialized;
try (ObjectInputStream ois =
new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
deserialized = (HttpTransportOptions) ois.readObject();
}
assertEquals(DEFAULT_OPTIONS, deserialized);
assertTrue(deserialized.getHttpTransportFactory() instanceof DefaultHttpTransportFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -783,16 +783,22 @@ protected boolean baseEquals(ServiceOptions<?, ?> other) {

private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
serviceFactory = newInstance(serviceFactoryClassName);
serviceRpcFactory = newInstance(serviceRpcFactoryClassName);
serviceFactory = newInstance(serviceFactoryClassName, ServiceFactory.class);
serviceRpcFactory = newInstance(serviceRpcFactoryClassName, ServiceRpcFactory.class);
}

@SuppressWarnings("unchecked")
@InternalApi
public static <T> T newInstance(String className) throws IOException, ClassNotFoundException {
public static <T> T newInstance(String className, Class<?> expected)
throws IOException, ClassNotFoundException {
// Avoid running static initializers before verifying the class type.
Class<?> clazz = Class.forName(className, false, ServiceOptions.class.getClassLoader());
if (!expected.isAssignableFrom(clazz)) {
throw new IOException("Class " + className + " is not assignable to " + expected.getName());
}
try {
return (T) Class.forName(className).newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return (T) clazz.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new IOException(e);
}
}
Comment thread
whowes marked this conversation as resolved.
Resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@
import com.google.common.collect.Multimap;
import com.google.common.io.Files;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -670,6 +674,70 @@ void testGetScopedCredentials_useJwtAccessWithScope_overridesCredentials() {
assertThat(((ServiceAccountCredentials) scoped).getUseJwtAccessWithScope()).isFalse();
}

private static final AtomicBoolean DISALLOWED_CLASS_INITIALIZED = new AtomicBoolean(false);

public static class DisallowedClass {
static {
DISALLOWED_CLASS_INITIALIZED.set(true);
}
}

public static class PublicTestServiceFactory implements TestServiceFactory {
@Override
public TestService create(TestServiceOptions options) {
return new TestServiceImpl(options);
}
}

public static class PublicTestServiceRpcFactory implements TestServiceRpcFactory {
@Override
public TestServiceRpc create(TestServiceOptions options) {
return new DefaultTestServiceRpc(options);
}
}

private static class SerializableTransportOptions implements TransportOptions {
private static final long serialVersionUID = 1L;
}

@Test
void testNewInstance_disallowedClassDoesNotInitialize() {
IOException exception =
assertThrows(
IOException.class,
() ->
ServiceOptions.newInstance(DisallowedClass.class.getName(), ServiceFactory.class));
assertThat(exception).hasMessageThat().contains("is not assignable to");
assertThat(DISALLOWED_CLASS_INITIALIZED.get()).isFalse();
}

@Test
void testReadObject_validSerializationRoundTrip() throws Exception {
TestServiceOptions options =
TestServiceOptions.newBuilder()
.setProjectId("project-id")
.setCredentials(NoCredentials.getInstance())
.setClock(CurrentMillisClock.getDefaultClock())
.setServiceFactory(new PublicTestServiceFactory())
.setServiceRpcFactory(new PublicTestServiceRpcFactory())
.setTransportOptions(new SerializableTransportOptions())
.build();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(options);
}
TestServiceOptions deserialized;
try (ObjectInputStream ois =
new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
deserialized = (TestServiceOptions) ois.readObject();
}

assertThat(deserialized).isEqualTo(options);
assertThat(deserialized.getService()).isInstanceOf(TestServiceImpl.class);
assertThat(deserialized.getRpc()).isInstanceOf(DefaultTestServiceRpc.class);
}

private HttpResponse createHttpResponseWithHeader(final Multimap<String, String> headers)
throws Exception {
HttpTransport mockHttpTransport =
Expand Down
Loading