Repository: dragonflyoss/Dragonfly2
Pull request #1305 (2022-05-13 03:53:14)

fix: digest reader and unit tests
Signed-off-by: Jim Ma <majinjing3@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#1305](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (e3701c2) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/62cd4d2013d9e4570f7a6dc5a5a6accebf140cc3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (62cd4d2) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305/graphs/tree.svg?width=650&height=150&src=pr&token=PUICJYSAT7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@           Coverage Diff           @@
##             main    #1305   +/-   ##
=======================================
  Coverage   46.39%   46.40%           
=======================================
  Files         139      139           
  Lines       11716    11716           
=======================================
+ Hits         5436     5437    +1     
- Misses       5689     5690    +1     
+ Partials      591      589    -2     
```

| Flag | Coverage Δ | |
|---|---|---|
| Object-compatibility-e2etests | `?` | |
| e2etests | `?` | |
| unittests | `46.40% <100.00%> (+<0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [pkg/util/digestutils/digest\_reader.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL3V0aWwvZGlnZXN0dXRpbHMvZGlnZXN0X3JlYWRlci5nbw==) | `82.92% <100.00%> (+26.82%)` | :arrow_up: |
| [client/daemon/peer/peertask\_piecetask\_poller.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19wb2xsZXIuZ28=) | `47.10% <0.00%> (-3.63%)` | :arrow_down: |
| [client/daemon/peer/peertask\_conductor.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX2NvbmR1Y3Rvci5nbw==) | `57.84% <0.00%> (-1.04%)` | :arrow_down: |
| [client/daemon/peer/peertask\_file.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX2ZpbGUuZ28=) | `58.77% <0.00%> (+1.52%)` | :arrow_up: |
| [...ent/daemon/peer/peertask\_piecetask\_synchronizer.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1305/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19zeW5jaHJvbml6ZXIuZ28=) | `46.66% <0.00%> (+1.53%)` | :arrow_up: |


Patch:
diff --git a/pkg/util/digestutils/digest_reader.go b/pkg/util/digestutils/digest_reader.go
index f5682df7..da639f23 100644
--- a/pkg/util/digestutils/digest_reader.go
+++ b/pkg/util/digestutils/digest_reader.go
@@ -106,5 +106,5 @@ func (dr *digestReader) Read(p []byte) (int, error) {
 
 // Digest returns the digest of contents.
 func (dr *digestReader) Digest() string {
-	return hex.EncodeToString(dr.hash.Sum(nil)[:16])
+	return hex.EncodeToString(dr.hash.Sum(nil))
 }


Test patch:
diff --git a/pkg/util/digestutils/digest_reader_test.go b/pkg/util/digestutils/digest_reader_test.go
index 12d9b83a..b5b1d337 100644
--- a/pkg/util/digestutils/digest_reader_test.go
+++ b/pkg/util/digestutils/digest_reader_test.go
@@ -19,6 +19,9 @@ package digestutils
 import (
 	"bytes"
 	"crypto/md5"
+	"crypto/sha1"
+	"crypto/sha256"
+	"crypto/sha512"
 	"encoding/hex"
 	"io"
 	"os"
@@ -36,16 +39,67 @@ func TestMain(m *testing.M) {
 func TestNewDigestReader(t *testing.T) {
 	assert := testifyassert.New(t)
 
-	testBytes := []byte("hello world")
-	hash := md5.New()
-	hash.Write(testBytes)
-	digest := hex.EncodeToString(hash.Sum(nil)[:16])
+	testCases := []struct {
+		name   string
+		data   []byte
+		digest func(data []byte) string
+	}{
+		{
+			name: "default md5",
+			data: []byte("hello world"),
+			digest: func(data []byte) string {
+				hash := md5.New()
+				hash.Write(data)
+				return hex.EncodeToString(hash.Sum(nil))
+			},
+		},
+		{
+			name: "md5",
+			data: []byte("hello world"),
+			digest: func(data []byte) string {
+				hash := md5.New()
+				hash.Write(data)
+				return "md5:" + hex.EncodeToString(hash.Sum(nil))
+			},
+		},
+		{
+			name: "sha1",
+			data: []byte("hello world"),
+			digest: func(data []byte) string {
+				hash := sha1.New()
+				hash.Write(data)
+				return "sha1:" + hex.EncodeToString(hash.Sum(nil))
+			},
+		},
+		{
+			name: "sha256",
+			data: []byte("hello world"),
+			digest: func(data []byte) string {
+				hash := sha256.New()
+				hash.Write(data)
+				return "sha256:" + hex.EncodeToString(hash.Sum(nil))
+			},
+		},
+		{
+			name: "sha512",
+			data: []byte("hello world"),
+			digest: func(data []byte) string {
+				hash := sha512.New()
+				hash.Write(data)
+				return "sha512:" + hex.EncodeToString(hash.Sum(nil))
+			},
+		},
+	}
 
-	buf := bytes.NewBuffer(testBytes)
-	reader, err := NewDigestReader(logger.With("test", "test"), buf, digest)
-	assert.Nil(err)
-	data, err := io.ReadAll(reader)
-
-	assert.Nil(err)
-	assert.Equal(testBytes, data)
+	for _, tc := range testCases {
+		t.Run(tc.name, func(t *testing.T) {
+			digest := tc.digest(tc.data)
+			buf := bytes.NewBuffer(tc.data)
+			reader, err := NewDigestReader(logger.With("test", "test"), buf, digest)
+			assert.Nil(err)
+			data, err := io.ReadAll(reader)
+			assert.Nil(err)
+			assert.Equal(tc.data, data)
+		})
+	}
 }