From 20df6d35608c6568e7647c7a1a4d8209dc5b4a4e Mon Sep 17 00:00:00 2001 From: Brandon Weeks Date: Wed, 21 Feb 2024 10:06:59 -0800 Subject: Relax LocalDate parsing (#69) --- .../android/attestation/AuthorizationList.java | 23 ++++++++++------------ .../android/attestation/AuthorizationListTest.java | 9 +++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/server/src/main/java/com/google/android/attestation/AuthorizationList.java b/server/src/main/java/com/google/android/attestation/AuthorizationList.java index 8dc2738..e5927c8 100644 --- a/server/src/main/java/com/google/android/attestation/AuthorizationList.java +++ b/server/src/main/java/com/google/android/attestation/AuthorizationList.java @@ -771,20 +771,17 @@ public abstract class AuthorizationList { 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) { diff --git a/server/src/test/java/com/google/android/attestation/AuthorizationListTest.java b/server/src/test/java/com/google/android/attestation/AuthorizationListTest.java index f589ca2..4a6f3b2 100644 --- a/server/src/test/java/com/google/android/attestation/AuthorizationListTest.java +++ b/server/src/test/java/com/google/android/attestation/AuthorizationListTest.java @@ -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; @@ -269,4 +270,12 @@ public class AuthorizationListTest { 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)); + } } -- cgit v1.2.3