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

Function TestExtractZip

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

Source from the content-addressed store, hash-verified

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