Skip to content

Commit

Permalink
Relax LocalDate parsing (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonweeks authored Feb 21, 2024
1 parent 9ddd01c commit 20df6d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -771,20 +771,17 @@ private static ParsedAuthorizationMap getAuthorizationMap(ASN1Encodable[] author
return new ParsedAuthorizationMap(authorizationMap, ImmutableList.copyOf(unorderedTags));
}

private static LocalDate toLocalDate(String value) {
@VisibleForTesting
static LocalDate toLocalDate(String value) {
checkArgument(value.length() == 6 || value.length() == 8);
if (value.length() == 6) {
// Workaround for dates incorrectly encoded as yyyyMM.
value = value.concat("01");
} else if (value.length() == 8 && value.substring(6, 8).equals("00")) {
// Workaround for dates incorrectly encoded with a day of '00'.
value = value.substring(0, 6).concat("01");
}
try {
return LocalDate.parse(value, DateTimeFormatter.ofPattern("yyyyMMdd"));
} catch (DateTimeParseException e) {
throw new IllegalArgumentException(e);
}
int year = Integer.parseInt(value.substring(0, 4));
int month =
Integer.parseInt(value.substring(4, 6)) == 0 ? 1 : Integer.parseInt(value.substring(4, 6));
int day =
value.length() == 8 && !value.substring(6, 8).equals("00")
? Integer.parseInt(value.substring(6, 8))
: 1;
return LocalDate.of(year, month, day);
}

private static YearMonth toYearMonth(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.google.android.attestation;

import static com.google.android.attestation.AuthorizationList.toLocalDate;
import static com.google.android.attestation.AuthorizationList.DigestMode.SHA_2_256;
import static com.google.android.attestation.AuthorizationList.OperationPurpose.SIGN;
import static com.google.android.attestation.AuthorizationList.OperationPurpose.VERIFY;
Expand Down Expand Up @@ -269,4 +270,12 @@ public void testOperationPurposeMap(@TestParameter AuthorizationList.OperationPu
AuthorizationList.OPERATION_PURPOSE_TO_ASN1.get(purpose)))
.isEqualTo(purpose);
}

@Test
public void toLocalDate_conversionSucceeds() {
assertThat(toLocalDate("20240205")).isEqualTo(LocalDate.of(2024, 02, 05));
assertThat(toLocalDate("20240200")).isEqualTo(LocalDate.of(2024, 02, 01));
assertThat(toLocalDate("20240000")).isEqualTo(LocalDate.of(2024, 01, 01));
assertThat(toLocalDate("202402")).isEqualTo(LocalDate.of(2024, 02, 01));
}
}

0 comments on commit 20df6d3

Please sign in to comment.