Skip to content

Commit

Permalink
Throw NPE when setCredentials is called with null
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Nov 7, 2016
1 parent c3d396d commit faa4476
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,7 @@ protected Serializable[] serializableObjects() {
.setProjectId("p1")
.setNoCredentials()
.build();
BigQueryOptions otherOptions = options.toBuilder()
.setProjectId("p2")
.setCredentials(null)
.build();
BigQueryOptions otherOptions = options.toBuilder().setProjectId("p2").build();
return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID,
DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, TABLE_DEFINITION,
EXTERNAL_TABLE_DEFINITION, VIEW_DEFINITION, TABLE_SCHEMA, TABLE_INFO, VIEW_INFO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ protected Serializable[] serializableObjects() {
ComputeOptions otherOptions = options.toBuilder()
.setProjectId("p2")
.setRetryParams(RetryParams.getDefaultInstance())
.setCredentials(null)
.build();
return new Serializable[]{DISK_TYPE_ID, DISK_TYPE, MACHINE_TYPE_ID, MACHINE_TYPE, REGION_ID,
REGION, ZONE_ID, ZONE, LICENSE_ID, LICENSE, DEPRECATION_STATUS, GLOBAL_OPERATION_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.auth.Credentials;
Expand Down Expand Up @@ -213,13 +214,18 @@ public B setHost(String host) {
}

/**
* Sets the service authentication credentials.
* Sets the service authentication credentials. If this method or {@link #setNoCredentials() are
* not used on the builder, {@link GoogleCredentials#getApplicationDefault()} will be used to
* attempt getting credentials from the environment.
*
* @param credentials authentication credentials, should not be {@code null}
* @return the builder
* @throws NullPointerException if {@code credentials} is {@code null}. To disable
* authentication use {@link Builder#setNoCredentials()}
*/
public B setCredentials(Credentials credentials) {
this.credentials = checkNotNull(credentials);
this.noCredentials = false;
this.credentials = credentials;
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.spi.ServiceRpcFactory;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -105,6 +107,9 @@ public class ServiceOptionsTest {
private static final Pattern APPLICATION_NAME_PATTERN =
Pattern.compile(LIBRARY_NAME + "(/[0-9]+.[0-9]+.[0-9]+)?");

@Rule
public ExpectedException thrown = ExpectedException.none();

private static class TestClock extends Clock {
@Override
public long millis() {
Expand Down Expand Up @@ -226,6 +231,12 @@ public void testBuilderNoCredentials() {
assertSame(RetryParams.noRetries(), OPTIONS_NO_CREDENTIALS.getRetryParams());
}

@Test
public void testBuilderNullCredentials() {
thrown.expect(NullPointerException.class);
TestServiceOptions.newBuilder().setCredentials(null).build();
}

@Test
public void testBuilderDeprecated() {
assertSame(credentials, DEPRECATED_OPTIONS.getCredentials());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ protected java.io.Serializable[] serializableObjects() {
.setNoCredentials()
.setProjectId("ds1")
.build();
DatastoreOptions otherOptions = options.toBuilder()
.setNamespace("ns1")
.setCredentials(null)
.build();
DatastoreOptions otherOptions = options.toBuilder().setNamespace("ns1").build();
return new java.io.Serializable[]{KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1,
ENTITY2, ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1,
GQL2, QUERY1, QUERY2, QUERY3, NULL_VALUE, KEY_VALUE, STRING_VALUE, EMBEDDED_ENTITY_VALUE1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ protected Serializable[] serializableObjects() {
.setNoCredentials()
.setProjectId("id1")
.build();
DnsOptions otherOptions = options.toBuilder()
.setCredentials(null)
.build();
DnsOptions otherOptions = options.toBuilder().build();
return new Serializable[]{FULL_ZONE_INFO, PARTIAL_ZONE_INFO, ZONE_LIST_OPTION,
RECORD_SET_LIST_OPTION, CHANGE_REQUEST_LIST_OPTION, ZONE_OPTION, CHANGE_REQUEST_OPTION,
PROJECT_OPTION, PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, OPTIONS, FULL_ZONE, PARTIAL_ZONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ protected Serializable[] serializableObjects() {
.setProjectId("p1")
.setNoCredentials()
.build();
StorageOptions otherOptions = options.toBuilder()
.setProjectId("p2")
.setCredentials(null)
.build();
StorageOptions otherOptions = options.toBuilder().setProjectId("p2").build();
return new Serializable[]{ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL,
BLOB_INFO, BLOB, BUCKET_INFO, BUCKET, ORIGIN, CORS, PAGE_RESULT, BLOB_LIST_OPTIONS,
BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ protected Serializable[] serializableObjects() {
.setApiKey(API_KEY)
.setNoCredentials()
.build();
TranslateOptions otherOptions = options.toBuilder()
.setCredentials(null)
.build();
TranslateOptions otherOptions = options.toBuilder().build();
return new Serializable[]{DETECTION, TRANSLATION, TRANSLATE_EXCEPTION, LANGUAGE_LIST_OPTION,
TRANSLATE_OPTION, options, otherOptions};
}
Expand Down

0 comments on commit faa4476

Please sign in to comment.