Commit c592c057 authored by Joe Tsai's avatar Joe Tsai Committed by Joe Tsai

archive/tar: expand TestPartialRead to cover sparse files

Given that sparse file logic is not trivial, there should be a test
in TestPartialRead to ensure that partial reads work.

Change-Id: I913da3e331da06dca6758a8be3f5099abba233a6
Reviewed-on: https://go-review.googlesource.com/54430
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent e17405d7
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"io/ioutil" "io/ioutil"
"math" "math"
"os" "os"
"path"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
...@@ -431,38 +432,57 @@ func TestReader(t *testing.T) { ...@@ -431,38 +432,57 @@ func TestReader(t *testing.T) {
} }
func TestPartialRead(t *testing.T) { func TestPartialRead(t *testing.T) {
f, err := os.Open("testdata/gnu.tar") type testCase struct {
if err != nil { cnt int // Number of bytes to read
t.Fatalf("Unexpected error: %v", err) output string // Expected value of string read
} }
defer f.Close() vectors := []struct {
file string
cases []testCase
}{{
file: "testdata/gnu.tar",
cases: []testCase{
{4, "Kilt"},
{6, "Google"},
},
}, {
file: "testdata/sparse-formats.tar",
cases: []testCase{
{2, "\x00G"},
{4, "\x00G\x00o"},
{6, "\x00G\x00o\x00G"},
{8, "\x00G\x00o\x00G\x00o"},
{4, "end\n"},
},
}}
tr := NewReader(f) for _, v := range vectors {
t.Run(path.Base(v.file), func(t *testing.T) {
f, err := os.Open(v.file)
if err != nil {
t.Fatalf("Open() error: %v", err)
}
defer f.Close()
// Read the first four bytes; Next() should skip the last byte. tr := NewReader(f)
hdr, err := tr.Next() for i, tc := range v.cases {
if err != nil || hdr == nil { hdr, err := tr.Next()
t.Fatalf("Didn't get first file: %v", err) if err != nil || hdr == nil {
} t.Fatalf("entry %d, Next(): got %v, want %v", i, err, nil)
buf := make([]byte, 4) }
if _, err := io.ReadFull(tr, buf); err != nil { buf := make([]byte, tc.cnt)
t.Fatalf("Unexpected error: %v", err) if _, err := io.ReadFull(tr, buf); err != nil {
} t.Fatalf("entry %d, ReadFull(): got %v, want %v", i, err, nil)
if expected := []byte("Kilt"); !bytes.Equal(buf, expected) { }
t.Errorf("Contents = %v, want %v", buf, expected) if string(buf) != tc.output {
} t.Fatalf("entry %d, ReadFull(): got %q, want %q", i, string(buf), tc.output)
}
}
// Second file if _, err := tr.Next(); err != io.EOF {
hdr, err = tr.Next() t.Fatalf("Next(): got %v, want EOF", err)
if err != nil || hdr == nil { }
t.Fatalf("Didn't get second file: %v", err) })
}
buf = make([]byte, 6)
if _, err := io.ReadFull(tr, buf); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if expected := []byte("Google"); !bytes.Equal(buf, expected) {
t.Errorf("Contents = %v, want %v", buf, expected)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment