From d59c8874b75d019a9aee1528c35b893d161a8e80 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 5 Oct 2018 02:39:00 -0400 Subject: [PATCH] Fix issues with UUIDs with leading zeroes. Fixes #109 --- .../velocitypowered/api/util/UuidUtils.java | 4 +- .../api/util/UuidUtilsTest.java | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/com/velocitypowered/api/util/UuidUtils.java b/api/src/main/java/com/velocitypowered/api/util/UuidUtils.java index 9cec7f6c4..488ae7449 100644 --- a/api/src/main/java/com/velocitypowered/api/util/UuidUtils.java +++ b/api/src/main/java/com/velocitypowered/api/util/UuidUtils.java @@ -1,6 +1,7 @@ package com.velocitypowered.api.util; import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import org.checkerframework.checker.nullness.qual.NonNull; import java.nio.charset.StandardCharsets; @@ -36,7 +37,8 @@ public final class UuidUtils { */ public static @NonNull String toUndashed(final @NonNull UUID uuid) { Preconditions.checkNotNull(uuid, "uuid"); - return Long.toUnsignedString(uuid.getMostSignificantBits(), 16) + Long.toUnsignedString(uuid.getLeastSignificantBits(), 16); + return Strings.padStart(Long.toHexString(uuid.getMostSignificantBits()), 16, '0') + + Strings.padStart(Long.toHexString(uuid.getLeastSignificantBits()), 16, '0'); } /** diff --git a/api/src/test/java/com/velocitypowered/api/util/UuidUtilsTest.java b/api/src/test/java/com/velocitypowered/api/util/UuidUtilsTest.java index 31d20bd91..0638b41ff 100644 --- a/api/src/test/java/com/velocitypowered/api/util/UuidUtilsTest.java +++ b/api/src/test/java/com/velocitypowered/api/util/UuidUtilsTest.java @@ -5,11 +5,24 @@ import org.junit.jupiter.api.Test; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class UuidUtilsTest { private static final UUID EXPECTED_DASHED_UUID = UUID.fromString("6b501978-d3be-4f33-bcf6-6e7808f37a0d"); private static final String ACTUAL_UNDASHED_UUID = EXPECTED_DASHED_UUID.toString().replace("-", ""); + private static final UUID ISSUE_109_ZERO_UUID = new UUID(0, 0); + private static final String ISSUE_109_ZERO_UUID_UNDASHED = "00000000000000000000000000000000"; + + private static final UUID ISSUE_109_ONE_LSB_UUID = new UUID(0, 1); + private static final String ISSUE_109_ONE_LSB_UUID_UNDASHED = "00000000000000000000000000000001"; + + private static final UUID ISSUE_109_ONE_MLSB_UUID = new UUID(1, 1); + private static final String ISSUE_109_ONE_MLSB_UUID_UNDASHED = "00000000000000010000000000000001"; + + private static final UUID ISSUE_109_LEADING_ZERO_UUID = UUID.fromString("0d470a25-0416-48a1-b7a6-2a27aa5eb251"); + private static final String ISSUE_109_LEADING_ZERO_UNDASHED = "0d470a25041648a1b7a62a27aa5eb251"; + private static final UUID TEST_OFFLINE_PLAYER_UUID = UUID.fromString("708f6260-183d-3912-bbde-5e279a5e739a"); private static final String TEST_OFFLINE_PLAYER = "tuxed"; @@ -27,4 +40,28 @@ class UuidUtilsTest { void toUndashed() { assertEquals(ACTUAL_UNDASHED_UUID, UuidUtils.toUndashed(EXPECTED_DASHED_UUID), "UUIDs do not match"); } + + @Test + void zeroUuidIssue109() { + assertEquals(ISSUE_109_ZERO_UUID, UuidUtils.fromUndashed(ISSUE_109_ZERO_UUID_UNDASHED), "UUIDs do not match"); + assertEquals(ISSUE_109_ZERO_UUID_UNDASHED, UuidUtils.toUndashed(ISSUE_109_ZERO_UUID), "UUIDs do not match"); + } + + @Test + void leadingZeroUuidIssue109() { + assertEquals(ISSUE_109_LEADING_ZERO_UUID, UuidUtils.fromUndashed(ISSUE_109_LEADING_ZERO_UNDASHED), "UUIDs do not match"); + assertEquals(ISSUE_109_LEADING_ZERO_UNDASHED, UuidUtils.toUndashed(ISSUE_109_LEADING_ZERO_UUID), "UUIDs do not match"); + } + + @Test + void oneUuidLsbIssue109() { + assertEquals(ISSUE_109_ONE_LSB_UUID, UuidUtils.fromUndashed(ISSUE_109_ONE_LSB_UUID_UNDASHED), "UUIDs do not match"); + assertEquals(ISSUE_109_ONE_LSB_UUID_UNDASHED, UuidUtils.toUndashed(ISSUE_109_ONE_LSB_UUID), "UUIDs do not match"); + } + + @Test + void oneUuidMsbAndLsbIssue109() { + assertEquals(ISSUE_109_ONE_MLSB_UUID, UuidUtils.fromUndashed(ISSUE_109_ONE_MLSB_UUID_UNDASHED), "UUIDs do not match"); + assertEquals(ISSUE_109_ONE_MLSB_UUID_UNDASHED, UuidUtils.toUndashed(ISSUE_109_ONE_MLSB_UUID), "UUIDs do not match"); + } }