Skip to content

Commit 8e23033

Browse files
committed
Fixes
1 parent 089250a commit 8e23033

2 files changed

Lines changed: 43 additions & 19 deletions

File tree

internal/downloader/downloader_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,21 @@ func TestDownloadPhpMyAdmin_ServerReturns500(t *testing.T) {
101101
func TestDownloadPhpMyAdmin_DirectoryNotWritable(t *testing.T) {
102102
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
103103
w.Header().Set("Content-Type", "application/zip")
104-
w.Write([]byte("PK\x03\x04 dummy zip content"))
104+
if _, err := w.Write([]byte("PK\x03\x04 dummy zip content")); err != nil {
105+
t.Fatalf("failed to write mock zip content: %v", err)
106+
}
105107
}))
106108
defer server.Close()
107109

108110
tempDir := t.TempDir()
109111
if err := os.Chmod(tempDir, 0500); err != nil {
110112
t.Fatalf("failed to chmod: %v", err)
111113
}
112-
defer os.Chmod(tempDir, 0700)
114+
defer func() {
115+
if err := os.Chmod(tempDir, 0700); err != nil {
116+
t.Errorf("failed to restore permissions: %v", err)
117+
}
118+
}()
113119

114120
version := "5.2.2"
115121
mockDownloadURL := fmt.Sprintf("%s/phpMyAdmin-%s-all-languages.zip", server.URL, version)

internal/extractor/extractor_test.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)