Repository: fsouza/fake-gcs-server
Pull request #504 (2021-05-27 15:18:48)

Add CRC32 and MD5 hashes for preseeded files
This patch adds CRC32 and MD5 hashes for files preloaded from the seed directory. I believe this closes the issue https://github.com/fsouza/fake-gcs-server/issues/313

I haven't noticed any preexisting tests for this functionality so I only tested the patch manually. Probably it makes sense to move CRC32 and MD5 functions out of upload.go into a separate file as they are now used in multiple places but I didn't do this to keep the patch small.


Patch:
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index ecf37fd..e3b03e4 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -62,7 +62,7 @@ jobs:
 
       - name: staticcheck cache
         id: staticcheck-cache
-        uses: actions/cache@v2.1.5
+        uses: actions/cache@v2.1.6
         with:
           path: ~/.cache/staticcheck
           key: "${{ steps.get-staticcheck-version.outputs.version }}"
diff --git a/fakestorage/upload.go b/fakestorage/upload.go
index 223733e..26d8bd7 100644
--- a/fakestorage/upload.go
+++ b/fakestorage/upload.go
@@ -119,8 +119,8 @@ func (s *Server) simpleUpload(bucketName string, r *http.Request) jsonResponse {
 		Content:         data,
 		ContentType:     r.Header.Get(contentTypeHeader),
 		ContentEncoding: contentEncoding,
-		Crc32c:          encodedCrc32cChecksum(data),
-		Md5Hash:         encodedMd5Hash(data),
+		Crc32c:          EncodedCrc32cChecksum(data),
+		Md5Hash:         EncodedMd5Hash(data),
 		ACL:             getObjectACL(predefinedACL),
 	}
 	obj, err = s.createObject(obj)
@@ -159,8 +159,8 @@ func (s *Server) signedUpload(bucketName string, r *http.Request) jsonResponse {
 		Content:         data,
 		ContentType:     r.Header.Get(contentTypeHeader),
 		ContentEncoding: contentEncoding,
-		Crc32c:          encodedCrc32cChecksum(data),
-		Md5Hash:         encodedMd5Hash(data),
+		Crc32c:          EncodedCrc32cChecksum(data),
+		Md5Hash:         EncodedMd5Hash(data),
 		ACL:             getObjectACL(predefinedACL),
 		Metadata:        metaData,
 	}
@@ -201,7 +201,7 @@ func encodedChecksum(checksum []byte) string {
 	return base64.StdEncoding.EncodeToString(checksum)
 }
 
-func encodedCrc32cChecksum(content []byte) string {
+func EncodedCrc32cChecksum(content []byte) string {
 	return encodedChecksum(crc32cChecksum(content))
 }
 
@@ -216,7 +216,7 @@ func encodedHash(hash []byte) string {
 	return base64.StdEncoding.EncodeToString(hash)
 }
 
-func encodedMd5Hash(content []byte) string {
+func EncodedMd5Hash(content []byte) string {
 	return encodedHash(md5Hash(content))
 }
 
@@ -268,8 +268,8 @@ func (s *Server) multipartUpload(bucketName string, r *http.Request) jsonRespons
 		Content:         content,
 		ContentType:     contentType,
 		ContentEncoding: metadata.ContentEncoding,
-		Crc32c:          encodedCrc32cChecksum(content),
-		Md5Hash:         encodedMd5Hash(content),
+		Crc32c:          EncodedCrc32cChecksum(content),
+		Md5Hash:         EncodedMd5Hash(content),
 		ACL:             getObjectACL(predefinedACL),
 		Metadata:        metadata.Metadata,
 	}
@@ -364,8 +364,8 @@ func (s *Server) uploadFileContent(r *http.Request) jsonResponse {
 	commit := true
 	status := http.StatusOK
 	obj.Content = append(obj.Content, content...)
-	obj.Crc32c = encodedCrc32cChecksum(obj.Content)
-	obj.Md5Hash = encodedMd5Hash(obj.Content)
+	obj.Crc32c = EncodedCrc32cChecksum(obj.Content)
+	obj.Md5Hash = EncodedMd5Hash(obj.Content)
 	obj.ContentType = r.Header.Get(contentTypeHeader)
 	responseHeader := make(http.Header)
 	if contentRange := r.Header.Get("Content-Range"); contentRange != "" {
diff --git a/main.go b/main.go
index 2726950..0996538 100644
--- a/main.go
+++ b/main.go
@@ -93,6 +93,8 @@ func objectsFromBucket(localBucketPath, bucketName string) ([]fakestorage.Object
 				BucketName: bucketName,
 				Name:       objectKey,
 				Content:    fileContent,
+				Crc32c:     fakestorage.EncodedCrc32cChecksum(fileContent),
+				Md5Hash:    fakestorage.EncodedMd5Hash(fileContent),
 			})
 		}
 		return nil


Test patch:
diff --git a/fakestorage/upload_test.go b/fakestorage/upload_test.go
index 94a0cb0..0d7c370 100644
--- a/fakestorage/upload_test.go
+++ b/fakestorage/upload_test.go
@@ -120,7 +120,7 @@ func TestServerClientObjectWriter(t *testing.T) {
 
 func checkChecksum(t *testing.T, content []byte, obj Object) {
 	t.Helper()
-	if expect := encodedCrc32cChecksum(content); expect != obj.Crc32c {
+	if expect := EncodedCrc32cChecksum(content); expect != obj.Crc32c {
 		t.Errorf("wrong checksum in the object\nwant %s\ngot  %s", expect, obj.Crc32c)
 	}
 }