@@ -4,68 +4,52 @@ import (
44 "bytes"
55 "context"
66 "fmt"
7- "io/ioutil"
87 "os"
98 "path/filepath"
109 "runtime"
1110 "testing"
1211
12+ "github.com/arduino/go-paths-helper"
1313 "github.com/codeclysm/extract/v3"
1414 "github.com/stretchr/testify/require"
1515)
1616
17- func TestExtractor_Tar (t * testing.T ) {
18- tmp , _ := ioutil .TempDir ("" , "" )
19-
20- extractor := extract.Extractor {
21- FS : MockDisk {
22- Base : tmp ,
23- },
17+ func TestExtractors (t * testing.T ) {
18+ type archiveTest struct {
19+ name string
20+ file * paths.Path
2421 }
25-
26- data , err := ioutil .ReadFile ("testdata/archive.tar.gz" )
27- if err != nil {
28- t .Fatal (err )
22+ testCases := []archiveTest {
23+ {"TarGz" , paths .New ("testdata/archive.tar.gz" )},
24+ {"TarBz2" , paths .New ("testdata/archive.tar.bz2" )},
25+ {"TarXz" , paths .New ("testdata/archive.tar.xz" )},
26+ {"TarZstd" , paths .New ("testdata/archive.tar.zst" )},
27+ {"Zip" , paths .New ("testdata/archive.zip" )},
2928 }
30- buffer := bytes .NewBuffer (data )
31-
32- err = extractor .Gz (context .Background (), buffer , "/" , nil )
33- if err != nil {
34- t .Error (err .Error ())
35- }
36-
37- files := Files {
38- "" : "dir" ,
39- "/archive" : "dir" ,
40- "/archive/folder" : "dir" ,
41- "/archive/folderlink" : "link" ,
42- "/archive/folder/file1.txt" : "folder/File1" ,
43- "/archive/file1.txt" : "File1" ,
44- "/archive/file2.txt" : "File2" ,
45- "/archive/link.txt" : "File1" ,
29+ for _ , test := range testCases {
30+ t .Run (test .name , func (t * testing.T ) {
31+ testArchive (t , test .file )
32+ })
4633 }
47- testWalk (t , tmp , files )
4834}
4935
50- func TestExtractor_Zip (t * testing.T ) {
51- tmp , _ := ioutil .TempDir ("" , "" )
36+ func testArchive (t * testing.T , archivePath * paths.Path ) {
37+ tmp , err := paths .MkTempDir ("" , "" )
38+ require .NoError (t , err )
39+ defer tmp .RemoveAll ()
5240
53- extractor := extract.Extractor {
54- FS : MockDisk {
55- Base : tmp ,
56- },
57- }
41+ data , err := archivePath .ReadFile ()
42+ require .NoError (t , err )
5843
59- data , err := ioutil .ReadFile ("testdata/archive.zip" )
60- if err != nil {
61- t .Fatal (err )
62- }
6344 buffer := bytes .NewBuffer (data )
6445
65- err = extractor .Zip (context .Background (), buffer , "/" , nil )
66- if err != nil {
67- t .Error (err .Error ())
46+ extractor := extract.Extractor {
47+ FS : MockDisk {
48+ Base : tmp .String (),
49+ },
6850 }
51+ err = extractor .Archive (context .Background (), buffer , "/" , nil )
52+ require .NoError (t , err )
6953
7054 files := Files {
7155 "" : "dir" ,
@@ -77,7 +61,7 @@ func TestExtractor_Zip(t *testing.T) {
7761 "/archive/file2.txt" : "File2" ,
7862 "/archive/link.txt" : "File1" ,
7963 }
80- testWalk (t , tmp , files )
64+ testWalk (t , tmp . String () , files )
8165}
8266
8367func TestZipSlipHardening (t * testing.T ) {
0 commit comments