Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/backend/build/interceptor/nydus.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func calcCrc32(ctx context.Context, r io.Reader, chunkSize int64) ([]uint32, err
hash := crc32.New(table)
n, err := io.Copy(hash, limitedReader)
if n == 0 || err == io.EOF {
// if no data read, return 0 as the crc32 value.
if len(crc32Results) == 0 {
return []uint32{0}, nil
}
return crc32Results, nil
}

Expand Down
25 changes: 25 additions & 0 deletions pkg/backend/build/interceptor/nydus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package interceptor

import (
"bytes"
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCalcCrc32_NormalExecution_ReturnsCorrectCrc32(t *testing.T) {
data := []byte("hello world")
expectedCrc32 := []uint32{0xc99465aa}

crc32Results, err := calcCrc32(context.Background(), bytes.NewReader(data), int64(len(data)))
assert.NoError(t, err)
assert.Equal(t, expectedCrc32, crc32Results)
}

func TestCalcCrc32_EmptyInput_ReturnsEmptySlice(t *testing.T) {
crc32Results, err := calcCrc32(context.Background(), bytes.NewReader([]byte{}), 10)
assert.NoError(t, err)
assert.NotEmpty(t, crc32Results)
assert.Equal(t, uint32(0x0), crc32Results[0])
}