Remove unused range parsing code and update tests (#6530)

master
Aditya Manthramurthy 6 years ago committed by Nitish Tiwari
parent 1111419d4a
commit 387584356f
  1. 28
      cmd/copy-part-range.go
  2. 23
      cmd/copy-part-range_test.go

@ -43,34 +43,6 @@ func writeCopyPartErr(w http.ResponseWriter, err error, url *url.URL) {
// http://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadUploadPartCopy.html
// for full details. This function treats an empty rangeString as
// referring to the whole resource.
//
// In addition to parsing the range string, it also validates the
// specified range against the given object size, so that Copy API
// specific error can be returned.
func parseCopyPartRange(rangeString string, resourceSize int64) (offset, length int64, err error) {
var hrange *HTTPRangeSpec
if rangeString != "" {
hrange, err = parseRequestRangeSpec(rangeString)
if err != nil {
return -1, -1, err
}
// Require that both start and end are specified.
if hrange.IsSuffixLength || hrange.Start == -1 || hrange.End == -1 {
return -1, -1, errInvalidRange
}
// Validate specified range against object size.
if hrange.Start >= resourceSize || hrange.End >= resourceSize {
return -1, -1, errInvalidRangeSource
}
}
return hrange.GetOffsetLength(resourceSize)
}
// parseCopyPartRangeSpec transforms a range string (e.g. bytes=3-4) to HTTPRangeSpec
// and returns errors if weird values
func parseCopyPartRangeSpec(rangeString string) (hrange *HTTPRangeSpec, err error) {
hrange, err = parseRequestRangeSpec(rangeString)
if err != nil {

@ -19,7 +19,7 @@ package cmd
import "testing"
// Test parseCopyPartRange()
func TestParseCopyPartRange(t *testing.T) {
func TestParseCopyPartRangeSpec(t *testing.T) {
// Test success cases.
successCases := []struct {
rangeString string
@ -29,16 +29,21 @@ func TestParseCopyPartRange(t *testing.T) {
{"bytes=2-5", 2, 5},
{"bytes=2-9", 2, 9},
{"bytes=2-2", 2, 2},
{"", 0, 9},
{"bytes=0000-0006", 0, 6},
}
objectSize := int64(10)
for _, successCase := range successCases {
start, length, err := parseCopyPartRange(successCase.rangeString, 10)
rs, err := parseCopyPartRangeSpec(successCase.rangeString)
if err != nil {
t.Fatalf("expected: <nil>, got: %s", err)
}
start, length, err1 := rs.GetOffsetLength(objectSize)
if err1 != nil {
t.Fatalf("expected: <nil>, got: %s", err1)
}
if start != successCase.offsetBegin {
t.Fatalf("expected: %d, got: %d", successCase.offsetBegin, start)
}
@ -61,9 +66,11 @@ func TestParseCopyPartRange(t *testing.T) {
"bytes=2 - 5",
"bytes=0-0,-1",
"bytes=2-5 ",
"bytes=-1",
"bytes=1-",
}
for _, rangeString := range invalidRangeStrings {
if _, _, err := parseCopyPartRange(rangeString, 10); err == nil {
if _, err := parseCopyPartRangeSpec(rangeString); err == nil {
t.Fatalf("expected: an error, got: <nil> for range %s", rangeString)
}
}
@ -74,8 +81,14 @@ func TestParseCopyPartRange(t *testing.T) {
"bytes=20-30",
}
for _, rangeString := range errorRangeString {
if _, _, err := parseCopyPartRange(rangeString, 10); err != errInvalidRangeSource {
rs, err := parseCopyPartRangeSpec(rangeString)
if err == nil {
err1 := checkCopyPartRangeWithSize(rs, objectSize)
if err1 != errInvalidRangeSource {
t.Fatalf("expected: %s, got: %s", errInvalidRangeSource, err)
}
} else {
t.Fatalf("expected: %s, got: <nil>", errInvalidRangeSource)
}
}
}

Loading…
Cancel
Save