-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.corfudb.util; | ||
|
||
import com.google.common.io.BaseEncoding; | ||
import java.nio.BufferUnderflowException; | ||
import java.nio.ByteBuffer; | ||
import java.util.UUID; | ||
import javax.annotation.Nonnull; | ||
|
||
/** A collection of utilities to manage and maniupulate UUIDs. */ | ||
public class UuidUtils { | ||
|
||
/** Generate a base64 URL-safe string from a given UUID. | ||
* | ||
* @param uuid The UUID to convert. | ||
* @return A base64 URL-safe string for the UUID. | ||
*/ | ||
public static String asBase64(@Nonnull UUID uuid) { | ||
ByteBuffer bb = ByteBuffer.wrap(new byte[16]); | ||
bb.putLong(uuid.getMostSignificantBits()); | ||
bb.putLong(uuid.getLeastSignificantBits()); | ||
return BaseEncoding.base64Url().omitPadding().encode(bb.array()); | ||
} | ||
|
||
/** Generate a UUID from a base64 URL-safe string. | ||
* | ||
* @param uuidString The base64 URL-safe string to convert. | ||
* | ||
* @throws IllegalArgumentException If decoding fails due to a malformed string. | ||
* @return A UUID from the string. | ||
*/ | ||
public static UUID fromBase64(@Nonnull String uuidString) { | ||
ByteBuffer bb = ByteBuffer.wrap(BaseEncoding.base64Url().decode(uuidString)); | ||
if (bb.remaining() < 16) { | ||
throw new IllegalArgumentException("Input too short: must be 16 bytes"); | ||
} else if (bb.remaining() > 16) { | ||
throw new IllegalArgumentException("Input too long: must be 16 bytes"); | ||
} | ||
return new UUID(bb.getLong(), bb.getLong()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.corfudb.util; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.util.UUID; | ||
import org.junit.Test; | ||
|
||
public class UuidUtilsTest { | ||
|
||
/** Test that a UUID can be converted back and forth between base64 and UUID. */ | ||
@Test | ||
public void base64StringEqualsUuid() { | ||
final UUID testId = UUID.nameUUIDFromBytes("test".getBytes()); | ||
final String base64 = UuidUtils.asBase64(testId); | ||
|
||
assertThat(UuidUtils.fromBase64(base64)) | ||
.isEqualTo(testId); | ||
} | ||
|
||
/** Test that a invalid string throws {@link java.lang.IllegalArgumentException}. */ | ||
@Test | ||
public void nonBase64StringThrowsException() { | ||
assertThatThrownBy(() -> UuidUtils.fromBase64("!!!!!!!!!!!")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
/** Test that a short base64 string, which is insufficient (< 16 bytes) to reconstruct | ||
* an UUID throws an exception. | ||
*/ | ||
@Test | ||
public void shortBase64StringThrowsException() { | ||
assertThatThrownBy(() -> UuidUtils.fromBase64("AAAA")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
/** Test that a long base64 string, which is too long (> 16 bytes) to reconstruct | ||
* an UUID throws an exception. | ||
*/ | ||
@Test | ||
public void longBase64StringThrowsException() { | ||
assertThatThrownBy(() -> UuidUtils.fromBase64("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |