MCPcopy
hub / github.com/cli/cli / TestExtractZip

Function TestExtractZip

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

Source from the content-addressed store, hash-verified

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