Skip to content

Commit

Permalink
4117: Added method to remove 0x prefix from hex string. Added integra…
Browse files Browse the repository at this point in the history
…tion tests,
  • Loading branch information
andrey-qlogic committed Feb 6, 2019
1 parent bbdf088 commit 7f19e33
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public StorageObject apply(BlobInfo blobInfo) {
private final Boolean temporaryHold;
private final Long retentionExpirationTime;

/** Helper method to remove 0x prefix from the hex string */
public static String remove0xPrefixFromHexString(String hex) {
return hex != null && hex.startsWith("0x") ? hex.replaceFirst("0x", "") : hex;
}

/** This class is meant for internal use only. Users are discouraged from using this class. */
public static final class ImmutableEmptyMap<K, V> extends AbstractMap<K, V> {

Expand Down Expand Up @@ -437,25 +442,23 @@ Builder setSelfLink(String selfLink) {

@Override
public Builder setMd5(String md5) {
this.md5 = firstNonNull(md5, Data.<String>nullOf(String.class));
this.md5 = firstNonNull(remove0xPrefixFromHexString(md5), Data.<String>nullOf(String.class));
return this;
}

public Builder setMd5FromHexString(String md5HexString) {
if (md5HexString == null) {
return this;
}
if (md5HexString.startsWith("0x")) {
md5HexString = md5HexString.replaceFirst("0x", "");
}
byte[] bytes = new BigInteger(md5HexString, 16).toByteArray();
this.md5 = "0x" + BaseEncoding.base64().omitPadding().encode(bytes);
byte[] bytes = new BigInteger(remove0xPrefixFromHexString(md5HexString), 16).toByteArray();
this.md5 = BaseEncoding.base64().encode(bytes);
return this;
}

@Override
public Builder setCrc32c(String crc32c) {
this.crc32c = firstNonNull(crc32c, Data.<String>nullOf(String.class));
this.crc32c =
firstNonNull(remove0xPrefixFromHexString(crc32c), Data.<String>nullOf(String.class));
return this;
}

Expand All @@ -464,11 +467,8 @@ public Builder setCrc32cFromHexString(String crc32cHexString) {
if (crc32cHexString == null) {
return this;
}
if (crc32cHexString.startsWith("0x")) {
crc32cHexString = crc32cHexString.replaceFirst("0x", "");
}
byte[] bytes = new BigInteger(crc32cHexString, 16).toByteArray();
this.crc32c = "0x" + BaseEncoding.base64().omitPadding().encode(bytes);
byte[] bytes = new BigInteger(remove0xPrefixFromHexString(crc32cHexString), 16).toByteArray();
this.crc32c = BaseEncoding.base64().encode(bytes);
return this;
}

Expand Down Expand Up @@ -727,8 +727,7 @@ public String getMd5ToHexString() {
if (md5 == null) {
return null;
}
String md5withoutPrefix = md5.startsWith("0x") ? md5.replaceFirst("0x", "") : md5;
byte[] decodedMd5 = BaseEncoding.base64().decode(md5withoutPrefix);
byte[] decodedMd5 = BaseEncoding.base64().decode(remove0xPrefixFromHexString(md5));
StringBuilder stringBuilder = new StringBuilder();
for (byte b : decodedMd5) {
stringBuilder.append(String.format("%02x", b & 0xff));
Expand Down Expand Up @@ -760,8 +759,7 @@ public String getCrc32cToHexString() {
if (crc32c == null) {
return null;
}
String crc32cWithoutPrefix = crc32c.startsWith("0x") ? crc32c.replaceFirst("0x", "") : crc32c;
byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32cWithoutPrefix);
byte[] decodeCrc32c = BaseEncoding.base64().decode(remove0xPrefixFromHexString(crc32c));
StringBuilder stringBuilder = new StringBuilder();
for (byte b : decodeCrc32c) {
stringBuilder.append(String.format("%02x", b & 0xff));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ public void testToBuilder() {
public void testToBuilderSetMd5FromHexString() {
BlobInfo blobInfo =
BlobInfo.newBuilder(BlobId.of("b2", "n2")).setMd5FromHexString(MD5_HEX_STRING).build();
assertEquals(MD5, blobInfo.getMd5());
assertEquals(BlobInfo.remove0xPrefixFromHexString(MD5), blobInfo.getMd5());
}

@Test
public void testToBuilderSetCrc32cFromHexString() {
BlobInfo blobInfo =
BlobInfo.newBuilder(BlobId.of("b2", "n2")).setCrc32cFromHexString(CRC32_HEX_STRING).build();
assertEquals(CRC32, blobInfo.getCrc32c());
assertEquals(BlobInfo.remove0xPrefixFromHexString(CRC32), blobInfo.getCrc32c());
}

@Test
Expand All @@ -157,13 +157,13 @@ public void testBuilder() {
assertEquals(CONTENT_ENCODING, BLOB_INFO.getContentEncoding());
assertEquals(CONTENT_LANGUAGE, BLOB_INFO.getContentLanguage());
assertEquals(CUSTOMER_ENCRYPTION, BLOB_INFO.getCustomerEncryption());
assertEquals(CRC32, BLOB_INFO.getCrc32c());
assertEquals(BlobInfo.remove0xPrefixFromHexString(CRC32), BLOB_INFO.getCrc32c());
assertEquals(CRC32_HEX_STRING, BLOB_INFO.getCrc32cToHexString());
assertEquals(DELETE_TIME, BLOB_INFO.getDeleteTime());
assertEquals(ETAG, BLOB_INFO.getEtag());
assertEquals(GENERATION, BLOB_INFO.getGeneration());
assertEquals(GENERATED_ID, BLOB_INFO.getGeneratedId());
assertEquals(MD5, BLOB_INFO.getMd5());
assertEquals(BlobInfo.remove0xPrefixFromHexString(MD5), BLOB_INFO.getMd5());
assertEquals(MD5_HEX_STRING, BLOB_INFO.getMd5ToHexString());
assertEquals(MEDIA_LINK, BLOB_INFO.getMediaLink());
assertEquals(METADATA, BLOB_INFO.getMetadata());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public void testBuilder() {
assertEquals(CONTENT_DISPOSITION, blob.getContentDisposition());
assertEquals(CONTENT_ENCODING, blob.getContentEncoding());
assertEquals(CONTENT_LANGUAGE, blob.getContentLanguage());
assertEquals(CRC32, blob.getCrc32c());
assertEquals(BlobInfo.remove0xPrefixFromHexString(CRC32), blob.getCrc32c());
assertEquals(CRC32_HEX_STRING, blob.getCrc32cToHexString());
assertEquals(CREATE_TIME, blob.getCreateTime());
assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption());
Expand All @@ -518,7 +518,7 @@ public void testBuilder() {
assertEquals(DELETE_TIME, blob.getDeleteTime());
assertEquals(ETAG, blob.getEtag());
assertEquals(GENERATED_ID, blob.getGeneratedId());
assertEquals(MD5, blob.getMd5());
assertEquals(BlobInfo.remove0xPrefixFromHexString(MD5), blob.getMd5());
assertEquals(MD5_HEX_STRING, blob.getMd5ToHexString());
assertEquals(MEDIA_LINK, blob.getMediaLink());
assertEquals(METADATA, blob.getMetadata());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,40 @@ public void testCreateBlob() {
assertTrue(remoteBlob.delete());
}

@Test
public void testCreateBlobMd5FromHexString() {
String blobName = "test-create-blob-md5-from-hex-string";
BlobInfo blob =
BlobInfo.newBuilder(BUCKET, blobName)
.setContentType(CONTENT_TYPE)
.setMd5FromHexString("3b54781b51c94835084898e821899585")
.build();
Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT);
assertNotNull(remoteBlob);
assertEquals(blob.getBucket(), remoteBlob.getBucket());
assertEquals(blob.getName(), remoteBlob.getName());
byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
assertTrue(remoteBlob.delete());
}

@Test
public void testCreateBlobCrc32cFromHexString() {
String blobName = "test-create-blob-crc32c-from-hex-string";
BlobInfo blob =
BlobInfo.newBuilder(BUCKET, blobName)
.setContentType(CONTENT_TYPE)
.setCrc32cFromHexString("f4ddc43d")
.build();
Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT);
assertNotNull(remoteBlob);
assertEquals(blob.getBucket(), remoteBlob.getBucket());
assertEquals(blob.getName(), remoteBlob.getName());
byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
assertTrue(remoteBlob.delete());
}

@Test
public void testCreateBlobWithEncryptionKey() {
String blobName = "test-create-with-customer-key-blob";
Expand Down

0 comments on commit 7f19e33

Please sign in to comment.