Skip to content

Commit 28c12d6

Browse files
authored
fix: fix the nydus implementation failed when input an empty file (#156)
Signed-off-by: Yang Kaiyong <[email protected]> Co-authored-by: Yang Kaiyong <[email protected]>
1 parent bd61f57 commit 28c12d6

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pkg/backend/build/interceptor/nydus.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ func calcCrc32(ctx context.Context, r io.Reader, chunkSize int64) ([]uint32, err
152152
hash := crc32.New(table)
153153
n, err := io.Copy(hash, limitedReader)
154154
if n == 0 || err == io.EOF {
155+
// if no data read, return 0 as the crc32 value.
156+
if len(crc32Results) == 0 {
157+
return []uint32{0}, nil
158+
}
155159
return crc32Results, nil
156160
}
157161

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package interceptor
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestCalcCrc32_NormalExecution_ReturnsCorrectCrc32(t *testing.T) {
12+
data := []byte("hello world")
13+
expectedCrc32 := []uint32{0xc99465aa}
14+
15+
crc32Results, err := calcCrc32(context.Background(), bytes.NewReader(data), int64(len(data)))
16+
assert.NoError(t, err)
17+
assert.Equal(t, expectedCrc32, crc32Results)
18+
}
19+
20+
func TestCalcCrc32_EmptyInput_ReturnsEmptySlice(t *testing.T) {
21+
crc32Results, err := calcCrc32(context.Background(), bytes.NewReader([]byte{}), 10)
22+
assert.NoError(t, err)
23+
assert.NotEmpty(t, crc32Results)
24+
assert.Equal(t, uint32(0x0), crc32Results[0])
25+
}

0 commit comments

Comments
 (0)