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

Function TestExtractZip

pkg/cmd/copilot/copilot_test.go:273–333  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

271}
272
273func TestExtractZip(t *testing.T) {
274 t.Run("extracts files correctly", func(t *testing.T) {
275 zipDir := t.TempDir()
276 zipPath := filepath.Join(zipDir, "archive.zip")
277 content := []byte("hello world")
278 archive := createZipBuffer(t, map[string][]byte{
279 "copilot.exe": content,
280 })
281 require.NoError(t, os.WriteFile(zipPath, archive, 0x755))
282
283 destDir := t.TempDir()
284
285 err := extractZip(zipPath, destDir)
286 require.NoError(t, err, "extractZip() error")
287
288 extracted, err := os.ReadFile(filepath.Join(destDir, "copilot.exe"))
289 require.NoError(t, err, "failed to read extracted file")
290 require.Equal(t, content, extracted, "extracted content mismatch")
291 })
292
293 t.Run("extracts nested files", func(t *testing.T) {
294 zipDir := t.TempDir()
295 zipPath := filepath.Join(zipDir, "archive.zip")
296 content := []byte("hello world")
297 archive := createZipBuffer(t, map[string][]byte{
298 "subdir/file.txt": content,
299 })
300 require.NoError(t, os.WriteFile(zipPath, archive, 0x755))
301
302 destDir := t.TempDir()
303
304 err := extractZip(zipPath, destDir)
305 require.NoError(t, err, "extractZip() error")
306
307 extracted, err := os.ReadFile(filepath.Join(destDir, "subdir", "file.txt"))
308 require.NoError(t, err, "failed to read extracted file")
309 require.Equal(t, content, extracted, "extracted content mismatch")
310 })
311
312 t.Run("rejects path traversal", func(t *testing.T) {
313 zipDir := t.TempDir()
314 zipPath := filepath.Join(zipDir, "archive.zip")
315
316 var buf bytes.Buffer
317 zw := zip.NewWriter(&buf)
318
319 fh := &zip.FileHeader{
320 Name: "../evil.txt",
321 Method: zip.Store,
322 }
323 fw, _ := zw.CreateHeader(fh)
324 _, _ = fw.Write([]byte("evil"))
325 _ = zw.Close()
326
327 require.NoError(t, os.WriteFile(zipPath, buf.Bytes(), 0x755))
328 destDir := t.TempDir()
329
330 err := extractZip(zipPath, destDir)

Callers

nothing calls this directly

Calls 8

createZipBufferFunction · 0.85
extractZipFunction · 0.85
JoinMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected