Skip to content

Commit 06982d4

Browse files
committed
test: add null-checks to builder and corresponding tests
1 parent 3fe0ca5 commit 06982d4

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

oauth2_http/java/com/google/auth/oauth2/GdchCredentials.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,17 @@ public Builder setCaCertPath(String caCertPath) {
553553

554554
@CanIgnoreReturnValue
555555
public Builder setGdchAudience(String apiAudience) {
556-
this.apiAudience = apiAudience;
556+
this.apiAudience =
557+
Preconditions.checkNotNull(
558+
apiAudience, "Audience cannot be null or empty for GDCH service account credentials.");
557559
return this;
558560
}
559561

560562
@CanIgnoreReturnValue
561563
@ObsoleteApi("Use setGdchAudience(String) instead")
562564
public Builder setGdchAudience(URI apiAudience) {
565+
Preconditions.checkNotNull(
566+
apiAudience, "Audience cannot be null or empty for GDCH service account credentials.");
563567
this.apiAudience = apiAudience.toString();
564568
return this;
565569
}

oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,24 @@ void signUsingEsSha256_validStructure() throws Exception {
12321232
assertEquals(64, signatureBytes.length);
12331233
}
12341234

1235+
@Test
1236+
void builder_setGdchAudience_nullString() {
1237+
GdchCredentials.Builder builder = GdchCredentials.newBuilder();
1238+
NullPointerException ex =
1239+
assertThrows(NullPointerException.class, () -> builder.setGdchAudience((String) null));
1240+
assertEquals(
1241+
"Audience cannot be null or empty for GDCH service account credentials.", ex.getMessage());
1242+
}
1243+
1244+
@Test
1245+
void builder_setGdchAudience_nullUri() {
1246+
GdchCredentials.Builder builder = GdchCredentials.newBuilder();
1247+
NullPointerException ex =
1248+
assertThrows(NullPointerException.class, () -> builder.setGdchAudience((URI) null));
1249+
assertEquals(
1250+
"Audience cannot be null or empty for GDCH service account credentials.", ex.getMessage());
1251+
}
1252+
12351253
static GenericJson writeGdchServiceAccountJson(
12361254
String formatVersion,
12371255
String project,

0 commit comments

Comments
 (0)