Skip to content

Commit 9f9226c

Browse files
committed
fix: 测试
1 parent aa6bcf0 commit 9f9226c

7 files changed

Lines changed: 27 additions & 25 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Add msbuild to PATH
2727
uses: microsoft/setup-msbuild@v1.1
2828
- name: Set up go
29-
uses: actions/setup-go@v2
29+
uses: actions/setup-go@v6
3030
with:
3131
go-version: "1.23"
3232
- name: Go test

cmd/wutils/zip/crack.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818
passwordFile = "password-dict.txt"
1919
)
2020

21-
func getPasswordFilePath() (string, error) {
21+
func getHomePasswordFilePath() (string, error) {
2222
home, err := os.UserHomeDir()
2323
if err != nil {
2424
return "", fmt.Errorf("failed to get home directory: %w", err)
@@ -42,7 +42,7 @@ func getPasswordFilePath() (string, error) {
4242
}
4343

4444
func CrackPassword(archivePath string) string {
45-
passwordFilePath, err := getPasswordFilePath()
45+
passwordFilePath, err := getHomePasswordFilePath()
4646
if err != nil {
4747
fmt.Printf("Error: %v\n", err)
4848
return ""

cmd/wutils/zip/crack_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
)
88

99
func TestCrackPassword(t *testing.T) {
10-
home, _ := os.UserHomeDir()
11-
configDir := filepath.Join(home, ".config", "wutils")
12-
passwordDictPath := filepath.Join(configDir, "password-dict.txt")
10+
localDictPath := "./password-dict.txt"
11+
os.WriteFile(localDictPath, []byte("test\nwrong\n123456"), 0644)
12+
defer os.Remove(localDictPath)
1313

14-
os.MkdirAll(configDir, 0755)
15-
os.WriteFile(passwordDictPath, []byte("test\nwrong\n123456"), 0644)
14+
home, _ := os.UserHomeDir()
15+
homeDictPath := filepath.Join(home, ".config", "wutils", "password-dict.txt")
16+
os.WriteFile(homeDictPath, []byte("test\nwrong\n123456"), 0644)
1617

1718
type args struct {
1819
archivePath string
@@ -25,7 +26,7 @@ func TestCrackPassword(t *testing.T) {
2526
{
2627
name: "test",
2728
args: args{
28-
archivePath: "test.zip",
29+
archivePath: "test/test.zip",
2930
},
3031
want: "test",
3132
},

cmd/wutils/zip/password.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

cmd/wutils/zip/test.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/zip/crack.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (a *Archive) Unzip(destDir string) Result {
5353
}
5454
}
5555

56-
// TryUnzip attempts to unzip the archive with the given password
57-
// Returns true if successful, false otherwise (deprecated: use Unzip instead)
56+
// TryUnzip verifies if the password is correct without extracting files
57+
// Returns true if successful, false otherwise
5858
func (a *Archive) TryUnzip() bool {
5959
ext := strings.ToLower(filepath.Ext(a.archivePath))
6060
switch ext {
@@ -65,26 +65,32 @@ func (a *Archive) TryUnzip() bool {
6565
}
6666
defer reader.Close()
6767

68+
if len(reader.File) == 0 {
69+
return false
70+
}
71+
6872
for _, file := range reader.File {
69-
if err := a.extractZipFile(file, "."); err != nil {
73+
file.SetPassword(a.password)
74+
rc, err := file.Open()
75+
if err != nil {
76+
return false
77+
}
78+
buf := make([]byte, 1)
79+
_, err = rc.Read(buf)
80+
rc.Close()
81+
if err != nil {
7082
return false
7183
}
72-
return true
7384
}
85+
return true
7486

7587
case ".7z":
7688
reader, err := sevenzip.OpenReaderWithPassword(a.archivePath, a.password)
7789
if err != nil {
7890
return false
7991
}
8092
defer reader.Close()
81-
82-
for _, file := range reader.File {
83-
if err := a.extract7zFile(file, "."); err != nil {
84-
return false
85-
}
86-
return true
87-
}
93+
return true
8894
}
8995

9096
return false

0 commit comments

Comments
 (0)