MCPcopy Create free account
hub / github.com/cli/cli / Test_extractZip

Function Test_extractZip

internal/zip/zip_test.go:15–108  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func Test_extractZip(t *testing.T) {
16 tests := []struct {
17 name string
18 directories []string
19 entries []string
20 setup func(t *testing.T, extractPath, outside string)
21 wantExtract []string
22 wantDirs []string
23 wantErr string
24 }{
25 {
26 name: "extracts nested file",
27 entries: []string{"src/main.go"},
28 wantExtract: []string{"src/main.go"},
29 },
30 {
31 name: "skips lexical escape",
32 entries: []string{"../outside.txt"},
33 },
34 {
35 name: "skips escapes among safe entries",
36 entries: []string{
37 "../outside-before.txt",
38 "safe-one.txt",
39 "../../outside-between.txt",
40 "nested/safe-two.txt",
41 },
42 wantExtract: []string{"safe-one.txt", "nested/safe-two.txt"},
43 },
44 {
45 name: "extracts explicit directory",
46 directories: []string{"nested/"},
47 wantDirs: []string{"nested"},
48 },
49 {
50 name: "explicit directory rejects planted link",
51 directories: []string{"nested/"},
52 setup: func(t *testing.T, extractPath, outside string) {
53 t.Helper()
54 require.NoError(t, os.Symlink(outside, filepath.Join(extractPath, "nested")))
55 },
56 wantErr: "symbolic link",
57 },
58 }
59
60 for _, tt := range tests {
61 t.Run(tt.name, func(t *testing.T) {
62 var archive bytes.Buffer
63 writer := zip.NewWriter(&archive)
64 for _, name := range tt.directories {
65 header := &zip.FileHeader{Name: name}
66 header.SetMode(os.ModeDir | 0o755)
67 _, err := writer.CreateHeader(header)
68 require.NoError(t, err)
69 }
70 for _, name := range tt.entries {
71 file, err := writer.Create(name)
72 require.NoError(t, err)

Callers

nothing calls this directly

Calls 12

OpenRootFunction · 0.92
ExtractZipFunction · 0.85
mustReadDirFunction · 0.85
JoinMethod · 0.80
EmptyMethod · 0.80
EqualMethod · 0.80
HelperMethod · 0.65
RunMethod · 0.65
CreateMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected