Files
gofakes3/range_test.go
John Woffindin d11e83aaf8 Incorrect range handling for some edge cases
Why:

Incorrect range handling cases causes s3manager downloads to fail.

* When end extends beyond available data it should return data from
  "start" to the end of the available data. Currently it returns
  invalid range error.

  From RFC-7233:

  > If the last-byte-pos value is.. greater than or equal to the current
  > length of the representation data, the byte range is interpreted as
  > the remainder of the representation (i.e., the server replaces the
  > value of last-byte-pos with a value that is one less than the current
  > length of the selected representation).

* A "-0" suffix byte range is "not satisfiable" according to RFC and
  should return an invalid range error. Currently it just returns an
  empty data set.

  Also from RFC-7233:

  > If a valid byte-range-set includes ... or at least one
  > suffix-byte-range-spec with a non-zero suffix-length, then the
  > byte-range-set is satisfiable.  Otherwise, the byte-range-set is
  > unsatisfiable.
2019-07-29 11:11:24 +12:00

52 lines
1.5 KiB
Go

package gofakes3
import (
"fmt"
"testing"
)
func TestRangeRequest(t *testing.T) {
for idx, tc := range []struct {
inst, inend int64
rev bool
sz int64
outst, outln int64
fail bool
}{
{inst: 0, inend: RangeNoEnd, sz: 5, outst: 0, outln: 5},
{inst: 0, inend: 5, sz: 10, outst: 0, outln: 6},
{inst: 0, inend: 0, sz: 4, outst: 0, outln: 1},
{inst: 1, inend: 5, sz: 10, outst: 1, outln: 5},
{inst: 1, inend: 5, sz: 3, outst: 1, outln: 2},
{inst: 5, inend: 7, sz: 6, outst: 5, outln: 1},
{rev: true, inend: 10, sz: 10, outst: 0, outln: 10},
{rev: true, inend: 5, sz: 10, outst: 5, outln: 5},
{fail: true, inst: 0, inend: 0, sz: 0},
{fail: true, inst: 1, inend: 1, sz: 1},
{fail: true, inst: 10, inend: 15, sz: 10},
{fail: true, inst: 40, inend: 50, sz: 11},
{fail: true, rev: true, inend: 20, sz: 10},
{fail: true, rev: true, inend: 11, sz: 10},
{fail: true, rev: true, inend: 0, sz: 10}, // zero suffix-length is not satisfiable
} {
t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
orr := ObjectRangeRequest{Start: tc.inst, End: tc.inend, FromEnd: tc.rev}
rng, err := orr.Range(tc.sz)
if tc.fail != (err != nil) {
t.Fatal("failure expected:", tc.fail, "found:", err)
}
if !tc.fail {
if rng.Start != tc.outst {
t.Fatal("unexpected start:", rng.Start, "expected:", tc.outst)
}
if rng.Length != tc.outln {
t.Fatal("unexpected length:", rng.Length, "expected:", tc.outln)
}
}
})
}
}