more passing lazy union reader tests

Signed-off-by: Will Murphy <will.murphy@anchore.com>
This commit is contained in:
Will Murphy 2024-04-24 11:00:50 -04:00
parent 20a26a0dfe
commit bad5cf2af8
2 changed files with 23 additions and 2 deletions

View File

@ -49,8 +49,12 @@ func (c *lazyUnionReader) ReadAt(p []byte, off int64) (n int, err error) {
needUntil := int64(len(p)) + off
err = c.ensureReadUntil(needUntil)
end := min(off+int64(len(p)), int64(len(c.buf)))
copy(p, c.buf[off:end])
return int(end - off), err
start := min(off, c.maxRead())
if off > start {
return 0, io.EOF
}
copy(p, c.buf[start:end])
return int(end - start), err
}
func (c *lazyUnionReader) Seek(offset int64, whence int) (int64, error) {
@ -63,6 +67,7 @@ func (c *lazyUnionReader) Seek(offset int64, whence int) (int64, error) {
trueOffset = offset
case io.SeekCurrent:
trueOffset = offset + c.cursor
err = c.ensureReadUntil(trueOffset)
case io.SeekEnd:
err = c.readAll()
trueOffset = c.maxRead() + offset
@ -73,6 +78,7 @@ func (c *lazyUnionReader) Seek(offset int64, whence int) (int64, error) {
if trueOffset < 0 {
return 0, fmt.Errorf("request to read negative offset impossible %v", trueOffset)
}
trueOffset = min(c.maxRead(), trueOffset)
c.cursor = trueOffset
return c.cursor, nil
}

View File

@ -101,6 +101,21 @@ func Test_lazyUnionReader_ReadAt(t *testing.T) {
wantBytes: []byte("ef"),
wantEOF: true,
},
{
name: "read way out of bounds",
dst: make([]byte, 4),
off: 512,
wantN: 0,
wantEOF: true,
},
{
name: "buffer more than available",
dst: make([]byte, 512),
off: 0,
wantN: 16,
wantBytes: []byte("0123456789abcdef"),
wantEOF: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {