@@ -32,7 +32,7 @@ func TestExtractZip_Success(t *testing.T) {
3232 t .Errorf ("failed to read extracted file %s: %v" , name , err )
3333 }
3434 if string (data ) != content {
35- t .Errorf ("content mismatch for %s: expected %q, got %q " , name , content , string ( data ) )
35+ t .Errorf ("content mismatch for %s" , name )
3636 }
3737 }
3838}
@@ -50,10 +50,11 @@ func TestExtractZip_FailureScenarios(t *testing.T) {
5050
5151 t .Run ("corrupted zip file" , func (t * testing.T ) {
5252 badZipPath := filepath .Join (tempDir , "bad.zip" )
53- os .WriteFile (badZipPath , []byte ("not a real zip content" ), 0644 )
53+ if err := os .WriteFile (badZipPath , []byte ("not a real zip content" ), 0644 ); err != nil {
54+ t .Fatalf ("failed to write corrupted zip: %v" , err )
55+ }
5456 extractDir := filepath .Join (tempDir , "extracted2" )
55- err := ExtractZip (badZipPath , extractDir )
56- if err == nil {
57+ if err := ExtractZip (badZipPath , extractDir ); err == nil {
5758 t .Errorf ("expected error for corrupted zip" )
5859 }
5960 })
@@ -69,7 +70,9 @@ func TestExtractZip_FailureScenarios(t *testing.T) {
6970 t .Run ("empty destination" , func (t * testing.T ) {
7071 zipPath := filepath .Join (tempDir , "test.zip" )
7172 testFiles := map [string ]string {"file.txt" : "data" }
72- createTestZip (t , zipPath , testFiles )
73+ if err := createTestZip (t , zipPath , testFiles ); err != nil {
74+ t .Fatalf ("failed to create test zip: %v" , err )
75+ }
7376 err := ExtractZip (zipPath , "" )
7477 if err == nil || ! strings .Contains (err .Error (), "empty destination path" ) {
7578 t .Errorf ("expected empty destination path error" )
@@ -79,24 +82,32 @@ func TestExtractZip_FailureScenarios(t *testing.T) {
7982 t .Run ("permission denied on destination" , func (t * testing.T ) {
8083 zipPath := filepath .Join (tempDir , "test2.zip" )
8184 testFiles := map [string ]string {"file.txt" : "data" }
82- createTestZip (t , zipPath , testFiles )
85+ if err := createTestZip (t , zipPath , testFiles ); err != nil {
86+ t .Fatalf ("failed to create test zip: %v" , err )
87+ }
8388
8489 extractDir := filepath .Join (tempDir , "extracted3" )
85- os .MkdirAll (extractDir , 0500 )
86- defer os .Chmod (extractDir , 0700 )
90+ if err := os .MkdirAll (extractDir , 0500 ); err != nil {
91+ t .Fatalf ("failed to mkdir: %v" , err )
92+ }
93+ defer func () {
94+ if err := os .Chmod (extractDir , 0700 ); err != nil {
95+ t .Errorf ("failed to restore permissions: %v" , err )
96+ }
97+ }()
8798
88- err := ExtractZip (zipPath , extractDir )
89- if err == nil {
99+ if err := ExtractZip (zipPath , extractDir ); err == nil {
90100 t .Errorf ("expected permission error" )
91101 }
92102 })
93103
94104 t .Run ("invalid path traversal" , func (t * testing.T ) {
95105 zipPath := filepath .Join (tempDir , "evil.zip" )
96- createTestZip (t , zipPath , map [string ]string {"../evil.txt" : "attack" })
106+ if err := createTestZip (t , zipPath , map [string ]string {"../evil.txt" : "attack" }); err != nil {
107+ t .Fatalf ("failed to create evil zip: %v" , err )
108+ }
97109 extractDir := filepath .Join (tempDir , "extracted4" )
98- err := ExtractZip (zipPath , extractDir )
99- if err == nil || ! strings .Contains (err .Error (), "invalid file path detected" ) {
110+ if err := ExtractZip (zipPath , extractDir ); err == nil || ! strings .Contains (err .Error (), "invalid file path detected" ) {
100111 t .Errorf ("expected invalid path detection" )
101112 }
102113 })
@@ -107,18 +118,25 @@ func createTestZip(t *testing.T, zipPath string, files map[string]string) error
107118 if err != nil {
108119 t .Fatalf ("failed to create zip file: %v" , err )
109120 }
110- defer zipFile .Close ()
121+ defer func () {
122+ if cerr := zipFile .Close (); cerr != nil {
123+ t .Errorf ("failed to close zip file: %v" , cerr )
124+ }
125+ }()
111126
112127 zipWriter := zip .NewWriter (zipFile )
113- defer zipWriter .Close ()
128+ defer func () {
129+ if cerr := zipWriter .Close (); cerr != nil {
130+ t .Errorf ("failed to close zip writer: %v" , cerr )
131+ }
132+ }()
114133
115134 for name , content := range files {
116135 writer , err := zipWriter .Create (name )
117136 if err != nil {
118137 t .Fatalf ("failed to create entry in zip: %v" , err )
119138 }
120- _ , err = writer .Write ([]byte (content ))
121- if err != nil {
139+ if _ , err := writer .Write ([]byte (content )); err != nil {
122140 t .Fatalf ("failed to write zip content: %v" , err )
123141 }
124142 }
0 commit comments