Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

fix size-0 chunker bug #9

Merged
merged 2 commits into from
Oct 1, 2018
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
7 changes: 6 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"strings"
)

var ErrRabinMin = errors.New("rabin min must be greater than 16")
var (
ErrRabinMin = errors.New("rabin min must be greater than 16")
ErrSize = errors.New("chunker size muster greater than 0")
)

// FromString returns a Splitter depending on the given string:
// it supports "default" (""), "size-{size}", "rabin", "rabin-{blocksize}" and
Expand All @@ -23,6 +26,8 @@ func FromString(r io.Reader, chunker string) (Splitter, error) {
size, err := strconv.Atoi(sizeStr)
if err != nil {
return nil, err
} else if size <= 0 {
return nil, ErrSize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine. I think it would be better though to have NewSizeSplitter verify its arguments are sane and return an error. But since NewRabinMinMax does not return an error it is not that big of a deal.

}
return NewSizeSplitter(r, int64(size)), nil

Expand Down
17 changes: 16 additions & 1 deletion parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestParse(t *testing.T) {
func TestParseRabin(t *testing.T) {
max := 1000
r := bytes.NewReader(randBuf(t, max))
chk1 := "rabin-18-25-32"
Expand All @@ -19,3 +19,18 @@ func TestParse(t *testing.T) {
t.Log("it should be ErrRabinMin here.")
}
}

func TestParseSize(t *testing.T) {
max := 1000
r := bytes.NewReader(randBuf(t, max))
size1 := "size-0"
size2 := "size-32"
_, err := FromString(r, size1)
if err == ErrSize {
t.Log("it should be ErrSize here.")
}
_, err = FromString(r, size2)
if err == ErrSize {
t.Fatal(err)
}
}