Skip to content

Commit

Permalink
4117: Added Crc32cString() method and updated tests. Added to snippets.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-qlogic committed Jan 23, 2019
1 parent 26ff7c6 commit 8862ddf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,26 @@ public String getCrc32c() {
return Data.isNull(crc32c) ? null : crc32c;
}

/**
* Returns the CRC32C checksum of blob's data as described in <a
* href="http://tools.ietf.org/html/rfc4960#appendix-B">RFC 4960, Appendix B;</a> decoded to
* string.
*
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
* Best Practices</a>
*/
public String getCrc32cString() {
if (crc32c == null) {
return null;
}
byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32c);
StringBuilder stringBuilder = new StringBuilder();
for (byte b : decodeCrc32c) {
stringBuilder.append(String.format("%02x", b & 0xff));
}
return stringBuilder.toString();
}

/** Returns the blob's media download link. */
public String getMediaLink() {
return mediaLink;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public class BlobInfoTest {
private static final String CONTENT_ENCODING = "UTF-8";
private static final String CONTENT_LANGUAGE = "En";
private static final String CRC32 = "0xFF00";
private static final String CRC32_STRING = "d31145d3";
private static final Long DELETE_TIME = System.currentTimeMillis();
private static final String ETAG = "0xFF00";
private static final Long GENERATION = 1L;
private static final String GENERATED_ID = "B/N:1";
private static final String MD5 = "0xFF00";
private static final String MD5_STRING = "d31145d3";
private static final String MEDIA_LINK = "http://media/b/n";
private static final Map<String, String> METADATA = ImmutableMap.of("n1", "v1", "n2", "v2");
private static final Long META_GENERATION = 10L;
Expand Down Expand Up @@ -142,11 +144,13 @@ public void testBuilder() {
assertEquals(CONTENT_LANGUAGE, BLOB_INFO.getContentLanguage());
assertEquals(CUSTOMER_ENCRYPTION, BLOB_INFO.getCustomerEncryption());
assertEquals(CRC32, BLOB_INFO.getCrc32c());
assertEquals(CRC32_STRING, BLOB_INFO.getCrc32cString());
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(MD5_STRING, BLOB_INFO.getMd5String());
assertEquals(MEDIA_LINK, BLOB_INFO.getMediaLink());
assertEquals(METADATA, BLOB_INFO.getMetadata());
assertEquals(META_GENERATION, BLOB_INFO.getMetageneration());
Expand All @@ -172,12 +176,14 @@ public void testBuilder() {
assertNull(DIRECTORY_INFO.getContentLanguage());
assertNull(DIRECTORY_INFO.getCustomerEncryption());
assertNull(DIRECTORY_INFO.getCrc32c());
assertNull(DIRECTORY_INFO.getCrc32cString());
assertNull(DIRECTORY_INFO.getCreateTime());
assertNull(DIRECTORY_INFO.getDeleteTime());
assertNull(DIRECTORY_INFO.getEtag());
assertNull(DIRECTORY_INFO.getGeneration());
assertNull(DIRECTORY_INFO.getGeneratedId());
assertNull(DIRECTORY_INFO.getMd5());
assertNull(DIRECTORY_INFO.getMd5String());
assertNull(DIRECTORY_INFO.getMediaLink());
assertNull(DIRECTORY_INFO.getMetadata());
assertNull(DIRECTORY_INFO.getMetageneration());
Expand All @@ -201,12 +207,14 @@ private void compareBlobs(BlobInfo expected, BlobInfo value) {
assertEquals(expected.getContentLanguage(), value.getContentLanguage());
assertEquals(expected.getCustomerEncryption(), value.getCustomerEncryption());
assertEquals(expected.getCrc32c(), value.getCrc32c());
assertEquals(expected.getCrc32cString(), value.getCrc32cString());
assertEquals(expected.getCreateTime(), value.getCreateTime());
assertEquals(expected.getDeleteTime(), value.getDeleteTime());
assertEquals(expected.getEtag(), value.getEtag());
assertEquals(expected.getGeneration(), value.getGeneration());
assertEquals(expected.getGeneratedId(), value.getGeneratedId());
assertEquals(expected.getMd5(), value.getMd5());
assertEquals(expected.getMd5String(), value.getMd5String());
assertEquals(expected.getMediaLink(), value.getMediaLink());
assertEquals(expected.getMetadata(), value.getMetadata());
assertEquals(expected.getMetageneration(), value.getMetageneration());
Expand Down Expand Up @@ -253,12 +261,14 @@ public void testToPbAndFromPb() {
assertNull(blobInfo.getContentLanguage());
assertNull(blobInfo.getCustomerEncryption());
assertNull(blobInfo.getCrc32c());
assertNull(blobInfo.getCrc32cString());
assertNull(blobInfo.getCreateTime());
assertNull(blobInfo.getDeleteTime());
assertNull(blobInfo.getEtag());
assertNull(blobInfo.getGeneration());
assertNull(blobInfo.getGeneratedId());
assertNull(blobInfo.getMd5());
assertNull(blobInfo.getMd5String());
assertNull(blobInfo.getMediaLink());
assertNull(blobInfo.getMetadata());
assertNull(blobInfo.getMetageneration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class BlobTest {
private static final String CONTENT_ENCODING = "UTF-8";
private static final String CONTENT_LANGUAGE = "En";
private static final String CRC32 = "0xFF00";
private static final String CRC32_STRING = "d31145d3";
private static final Long DELETE_TIME = System.currentTimeMillis();
private static final String ETAG = "0xFF00";
private static final Long GENERATION = 1L;
Expand Down Expand Up @@ -507,6 +508,7 @@ public void testBuilder() {
assertEquals(CONTENT_ENCODING, blob.getContentEncoding());
assertEquals(CONTENT_LANGUAGE, blob.getContentLanguage());
assertEquals(CRC32, blob.getCrc32c());
assertEquals(CRC32_STRING, blob.getCrc32cString());
assertEquals(CREATE_TIME, blob.getCreateTime());
assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption());
assertEquals(KMS_KEY_NAME, blob.getKmsKeyName());
Expand Down Expand Up @@ -539,6 +541,7 @@ public void testBuilder() {
assertNull(blob.getContentEncoding());
assertNull(blob.getContentLanguage());
assertNull(blob.getCrc32c());
assertNull(blob.getCrc32cString());
assertNull(blob.getCreateTime());
assertNull(blob.getCustomerEncryption());
assertNull(blob.getKmsKeyName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ public void getBlobMetadata(String bucketName, String blobName) throws StorageEx
System.out.println("ContentLanguage: " + blob.getContentLanguage());
System.out.println("ContentType: " + blob.getContentType());
System.out.println("Crc32c: " + blob.getCrc32c());
System.out.println("Crc32cString: " + blob.getCrc32cString());
System.out.println("ETag: " + blob.getEtag());
System.out.println("Generation: " + blob.getGeneration());
System.out.println("Id: " + blob.getBlobId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,13 @@ public void testGetBlobMetadata() {
assertTrue(snippetOutput.contains("ContentLanguage: " + remoteBlob.getContentLanguage()));
assertTrue(snippetOutput.contains("ContentType: " + remoteBlob.getContentType()));
assertTrue(snippetOutput.contains("Crc32c: " + remoteBlob.getCrc32c()));
assertTrue(snippetOutput.contains("Crc32cString: " + remoteBlob.getCrc32cString()));
assertTrue(snippetOutput.contains("ETag: " + remoteBlob.getEtag()));
assertTrue(snippetOutput.contains("Generation: " + remoteBlob.getGeneration()));
assertTrue(snippetOutput.contains("Id: " + remoteBlob.getBlobId()));
assertTrue(snippetOutput.contains("KmsKeyName: " + remoteBlob.getKmsKeyName()));
assertTrue(snippetOutput.contains("Md5Hash: " + remoteBlob.getMd5()));
assertTrue(snippetOutput.contains("Md5HashString: " + remoteBlob.getMd5String()));
assertTrue(snippetOutput.contains("MediaLink: " + remoteBlob.getMediaLink()));
assertTrue(snippetOutput.contains("Metageneration: " + remoteBlob.getMetageneration()));
assertTrue(snippetOutput.contains("Name: " + remoteBlob.getName()));
Expand Down

0 comments on commit 8862ddf

Please sign in to comment.