backend: add tests for IsPermanentError

This commit is contained in:
Michael Eischer 2024-05-11 00:13:23 +02:00
parent 6a85df7297
commit 4740528a0b

View file

@ -99,6 +99,7 @@ func (s *Suite[C]) TestConfig(t *testing.T) {
t.Fatalf("did not get expected error for non-existing config") t.Fatalf("did not get expected error for non-existing config")
} }
test.Assert(t, b.IsNotExist(err), "IsNotExist() did not recognize error from LoadAll(): %v", err) test.Assert(t, b.IsNotExist(err), "IsNotExist() did not recognize error from LoadAll(): %v", err)
test.Assert(t, b.IsPermanentError(err), "IsPermanentError() did not recognize error from LoadAll(): %v", err)
err = b.Save(context.TODO(), backend.Handle{Type: backend.ConfigFile}, backend.NewByteReader([]byte(testString), b.Hasher())) err = b.Save(context.TODO(), backend.Handle{Type: backend.ConfigFile}, backend.NewByteReader([]byte(testString), b.Hasher()))
if err != nil { if err != nil {
@ -135,6 +136,7 @@ func (s *Suite[C]) TestLoad(t *testing.T) {
t.Fatalf("Load() did not return an error for non-existing blob") t.Fatalf("Load() did not return an error for non-existing blob")
} }
test.Assert(t, b.IsNotExist(err), "IsNotExist() did not recognize non-existing blob: %v", err) test.Assert(t, b.IsNotExist(err), "IsNotExist() did not recognize non-existing blob: %v", err)
test.Assert(t, b.IsPermanentError(err), "IsPermanentError() did not recognize non-existing blob: %v", err)
length := rand.Intn(1<<24) + 2000 length := rand.Intn(1<<24) + 2000
@ -181,8 +183,12 @@ func (s *Suite[C]) TestLoad(t *testing.T) {
} }
getlen := l getlen := l
if l >= len(d) && rand.Float32() >= 0.5 { if l >= len(d) {
getlen = 0 if rand.Float32() >= 0.5 {
getlen = 0
} else {
getlen = len(d)
}
} }
if l > 0 && l < len(d) { if l > 0 && l < len(d) {
@ -225,6 +231,18 @@ func (s *Suite[C]) TestLoad(t *testing.T) {
} }
} }
// test error checking for partial and fully out of bounds read
// only test for length > 0 as we currently do not need strict out of bounds handling for length==0
for _, offset := range []int{length - 99, length - 50, length, length + 100} {
err = b.Load(context.TODO(), handle, 100, int64(offset), func(rd io.Reader) (ierr error) {
_, ierr = io.ReadAll(rd)
return ierr
})
test.Assert(t, err != nil, "Load() did not return error on out of bounds read! o %v, l %v, filelength %v", offset, 100, length)
test.Assert(t, b.IsPermanentError(err), "IsPermanentError() did not recognize out of range read: %v", err)
test.Assert(t, !b.IsNotExist(err), "IsNotExist() must not recognize out of range read: %v", err)
}
test.OK(t, b.Remove(context.TODO(), handle)) test.OK(t, b.Remove(context.TODO(), handle))
} }
@ -762,6 +780,7 @@ func (s *Suite[C]) TestBackend(t *testing.T) {
defer s.close(t, b) defer s.close(t, b)
test.Assert(t, !b.IsNotExist(nil), "IsNotExist() recognized nil error") test.Assert(t, !b.IsNotExist(nil), "IsNotExist() recognized nil error")
test.Assert(t, !b.IsPermanentError(nil), "IsPermanentError() recognized nil error")
for _, tpe := range []backend.FileType{ for _, tpe := range []backend.FileType{
backend.PackFile, backend.KeyFile, backend.LockFile, backend.PackFile, backend.KeyFile, backend.LockFile,
@ -782,11 +801,13 @@ func (s *Suite[C]) TestBackend(t *testing.T) {
_, err = b.Stat(context.TODO(), h) _, err = b.Stat(context.TODO(), h)
test.Assert(t, err != nil, "blob data could be extracted before creation") test.Assert(t, err != nil, "blob data could be extracted before creation")
test.Assert(t, b.IsNotExist(err), "IsNotExist() did not recognize Stat() error: %v", err) test.Assert(t, b.IsNotExist(err), "IsNotExist() did not recognize Stat() error: %v", err)
test.Assert(t, b.IsPermanentError(err), "IsPermanentError() did not recognize Stat() error: %v", err)
// try to read not existing blob // try to read not existing blob
err = testLoad(b, h) err = testLoad(b, h)
test.Assert(t, err != nil, "blob could be read before creation") test.Assert(t, err != nil, "blob could be read before creation")
test.Assert(t, b.IsNotExist(err), "IsNotExist() did not recognize Load() error: %v", err) test.Assert(t, b.IsNotExist(err), "IsNotExist() did not recognize Load() error: %v", err)
test.Assert(t, b.IsPermanentError(err), "IsPermanentError() did not recognize Load() error: %v", err)
// try to get string out, should fail // try to get string out, should fail
ret, err = beTest(context.TODO(), b, h) ret, err = beTest(context.TODO(), b, h)