Skip to content

Commit 5bc1907

Browse files
jdhenkeadg
authored andcommitted
godoc/vfs/zipfs: add tests; fix handling of "/"
- add tests for Open, ReadDir, and Stat funcs - add tests for Seek of Open() - simplify internal handling of absolute vs. "zip" paths - fix handling of "/" The fix special cases this scenario, leaving the codepath for all other file paths the same. Specifically, - Exported funcs call stat(), so stat("/") is handled by simply returning 0 to indicate all entries are (effectively) prefixed by "/" and zipFI{"", nil} because "/" has no name and nil indicates it is a directory. - ReadDir("/") is further handled by seeding the existing lookup logic with "" instead of what would have been "/". This is necessary because, per the zipfs spec, the zip file entries MUST NOT start with "/", so using "/" would incorrectly match nothing. This works because seeding lookup with "" (correctly) matches all files and then the following, existing logic (correctly) pares things down to just the files in the root directory; not in any subdirectories. Verified that godoc -zip still works. Fixes #12743 Change-Id: Icb5f01b8a29cefa4e2820135f318894042970301 Reviewed-on: https://go-review.googlesource.com/16925 Reviewed-by: Andrew Gerrand <[email protected]>
1 parent d94e6fe commit 5bc1907

File tree

2 files changed

+219
-13
lines changed

2 files changed

+219
-13
lines changed

godoc/vfs/zipfs/zipfs.go

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,35 @@ func (fs *zipFS) Close() error {
8686
return fs.ReadCloser.Close()
8787
}
8888

89-
func zipPath(name string) string {
89+
func zipPath(name string) (string, error) {
9090
name = path.Clean(name)
9191
if !path.IsAbs(name) {
92-
panic(fmt.Sprintf("stat: not an absolute path: %s", name))
92+
return "", fmt.Errorf("stat: not an absolute path: %s", name)
9393
}
94-
return name[1:] // strip leading '/'
94+
return name[1:], nil // strip leading '/'
95+
}
96+
97+
func isRoot(abspath string) bool {
98+
return path.Clean(abspath) == "/"
9599
}
96100

97101
func (fs *zipFS) stat(abspath string) (int, zipFI, error) {
98-
i, exact := fs.list.lookup(abspath)
102+
if isRoot(abspath) {
103+
return 0, zipFI{
104+
name: "",
105+
file: nil,
106+
}, nil
107+
}
108+
zippath, err := zipPath(abspath)
109+
if err != nil {
110+
return 0, zipFI{}, err
111+
}
112+
i, exact := fs.list.lookup(zippath)
99113
if i < 0 {
100-
// abspath has leading '/' stripped - print it explicitly
101-
return -1, zipFI{}, fmt.Errorf("file not found: /%s", abspath)
114+
// zippath has leading '/' stripped - print it explicitly
115+
return -1, zipFI{}, fmt.Errorf("file not found: /%s", zippath)
102116
}
103-
_, name := path.Split(abspath)
117+
_, name := path.Split(zippath)
104118
var file *zip.File
105119
if exact {
106120
file = fs.list[i] // exact match found - must be a file
@@ -109,7 +123,7 @@ func (fs *zipFS) stat(abspath string) (int, zipFI, error) {
109123
}
110124

111125
func (fs *zipFS) Open(abspath string) (vfs.ReadSeekCloser, error) {
112-
_, fi, err := fs.stat(zipPath(abspath))
126+
_, fi, err := fs.stat(abspath)
113127
if err != nil {
114128
return nil, err
115129
}
@@ -142,18 +156,17 @@ func (f *zipSeek) Seek(offset int64, whence int) (int64, error) {
142156
}
143157

144158
func (fs *zipFS) Lstat(abspath string) (os.FileInfo, error) {
145-
_, fi, err := fs.stat(zipPath(abspath))
159+
_, fi, err := fs.stat(abspath)
146160
return fi, err
147161
}
148162

149163
func (fs *zipFS) Stat(abspath string) (os.FileInfo, error) {
150-
_, fi, err := fs.stat(zipPath(abspath))
164+
_, fi, err := fs.stat(abspath)
151165
return fi, err
152166
}
153167

154168
func (fs *zipFS) ReadDir(abspath string) ([]os.FileInfo, error) {
155-
path := zipPath(abspath)
156-
i, fi, err := fs.stat(path)
169+
i, fi, err := fs.stat(abspath)
157170
if err != nil {
158171
return nil, err
159172
}
@@ -162,7 +175,21 @@ func (fs *zipFS) ReadDir(abspath string) ([]os.FileInfo, error) {
162175
}
163176

164177
var list []os.FileInfo
165-
dirname := path + "/"
178+
179+
// make dirname the prefix that file names must start with to be considered
180+
// in this directory. we must special case the root directory because, per
181+
// the spec of this package, zip file entries MUST NOT start with /, so we
182+
// should not append /, as we would in every other case.
183+
var dirname string
184+
if isRoot(abspath) {
185+
dirname = ""
186+
} else {
187+
zippath, err := zipPath(abspath)
188+
if err != nil {
189+
return nil, err
190+
}
191+
dirname = zippath + "/"
192+
}
166193
prevname := ""
167194
for _, e := range fs.list[i:] {
168195
if !strings.HasPrefix(e.Name, dirname) {

godoc/vfs/zipfs/zipfs_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright 2015 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.package zipfs
4+
package zipfs
5+
6+
import (
7+
"archive/zip"
8+
"bytes"
9+
"fmt"
10+
"io"
11+
"io/ioutil"
12+
"os"
13+
"reflect"
14+
"testing"
15+
16+
"golang.org/x/tools/godoc/vfs"
17+
)
18+
19+
var (
20+
21+
// files to use to build zip used by zipfs in testing; maps path : contents
22+
files = map[string]string{"foo": "foo", "bar/baz": "baz", "a/b/c": "c"}
23+
24+
// expected info for each entry in a file system described by files
25+
tests = []struct {
26+
Path string
27+
IsDir bool
28+
IsRegular bool
29+
Name string
30+
Contents string
31+
Files map[string]bool
32+
}{
33+
{"/", true, false, "", "", map[string]bool{"foo": true, "bar": true, "a": true}},
34+
{"//", true, false, "", "", map[string]bool{"foo": true, "bar": true, "a": true}},
35+
{"/foo", false, true, "foo", "foo", nil},
36+
{"/foo/", false, true, "foo", "foo", nil},
37+
{"/foo//", false, true, "foo", "foo", nil},
38+
{"/bar", true, false, "bar", "", map[string]bool{"baz": true}},
39+
{"/bar/", true, false, "bar", "", map[string]bool{"baz": true}},
40+
{"/bar/baz", false, true, "baz", "baz", nil},
41+
{"//bar//baz", false, true, "baz", "baz", nil},
42+
{"/a/b", true, false, "b", "", map[string]bool{"c": true}},
43+
}
44+
45+
// to be initialized in setup()
46+
fs vfs.FileSystem
47+
statFuncs []statFunc
48+
)
49+
50+
type statFunc struct {
51+
Name string
52+
Func func(string) (os.FileInfo, error)
53+
}
54+
55+
func TestMain(t *testing.M) {
56+
if err := setup(); err != nil {
57+
fmt.Fprintf(os.Stderr, "Error setting up zipfs testing state: %v.\n", err)
58+
os.Exit(1)
59+
}
60+
os.Exit(t.Run())
61+
}
62+
63+
// setups state each of the tests uses
64+
func setup() error {
65+
// create zipfs
66+
b := new(bytes.Buffer)
67+
zw := zip.NewWriter(b)
68+
for file, contents := range files {
69+
w, err := zw.Create(file)
70+
if err != nil {
71+
return err
72+
}
73+
_, err = io.WriteString(w, contents)
74+
if err != nil {
75+
return err
76+
}
77+
}
78+
zw.Close()
79+
zr, err := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))
80+
if err != nil {
81+
return err
82+
}
83+
rc := &zip.ReadCloser{
84+
Reader: *zr,
85+
}
86+
fs = New(rc, "foo")
87+
88+
// pull out different stat functions
89+
statFuncs = []statFunc{
90+
{"Stat", fs.Stat},
91+
{"Lstat", fs.Lstat},
92+
}
93+
94+
return nil
95+
}
96+
97+
func TestZipFSReadDir(t *testing.T) {
98+
for _, test := range tests {
99+
if test.IsDir {
100+
infos, err := fs.ReadDir(test.Path)
101+
if err != nil {
102+
t.Errorf("Failed to read directory %v\n", test.Path)
103+
continue
104+
}
105+
got := make(map[string]bool)
106+
for _, info := range infos {
107+
got[info.Name()] = true
108+
}
109+
if want := test.Files; !reflect.DeepEqual(got, want) {
110+
t.Errorf("ReadDir %v got %v\nwanted %v\n", test.Path, got, want)
111+
}
112+
}
113+
}
114+
}
115+
116+
func TestZipFSStatFuncs(t *testing.T) {
117+
for _, test := range tests {
118+
for _, statFunc := range statFuncs {
119+
120+
// test can stat
121+
info, err := statFunc.Func(test.Path)
122+
if err != nil {
123+
t.Errorf("Unexpected error using %v for %v: %v\n", statFunc.Name, test.Path, err)
124+
continue
125+
}
126+
127+
// test info.Name()
128+
if got, want := info.Name(), test.Name; got != want {
129+
t.Errorf("Using %v for %v info.Name() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
130+
}
131+
// test info.IsDir()
132+
if got, want := info.IsDir(), test.IsDir; got != want {
133+
t.Errorf("Using %v for %v info.IsDir() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
134+
}
135+
// test info.Mode().IsDir()
136+
if got, want := info.Mode().IsDir(), test.IsDir; got != want {
137+
t.Errorf("Using %v for %v info.Mode().IsDir() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
138+
}
139+
// test info.Mode().IsRegular()
140+
if got, want := info.Mode().IsRegular(), test.IsRegular; got != want {
141+
t.Errorf("Using %v for %v info.Mode().IsRegular() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
142+
}
143+
// test info.Size()
144+
if test.IsRegular {
145+
if got, want := info.Size(), int64(len(test.Contents)); got != want {
146+
t.Errorf("Using %v for %v inf.Size() got %v wanted %v", statFunc.Name, test.Path, got, want)
147+
}
148+
}
149+
}
150+
}
151+
}
152+
153+
func TestZipFSOpenSeek(t *testing.T) {
154+
for _, test := range tests {
155+
if test.IsRegular {
156+
157+
// test Open()
158+
f, err := fs.Open(test.Path)
159+
if err != nil {
160+
t.Error(err)
161+
return
162+
}
163+
defer f.Close()
164+
165+
// test Seek() multiple times
166+
for i := 0; i < 3; i++ {
167+
all, err := ioutil.ReadAll(f)
168+
if err != nil {
169+
t.Error(err)
170+
return
171+
}
172+
if got, want := string(all), test.Contents; got != want {
173+
t.Errorf("File contents for %v got %v wanted %v\n", test.Path, got, want)
174+
}
175+
f.Seek(0, 0)
176+
}
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)