From fecd81118dc7850e99a04c426273cf10b41acf88 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Sat, 12 Jul 2025 16:03:23 +0200 Subject: [PATCH] fix: various unit tests fixes for windows --- caplets/caplet_test.go | 53 ++++++++++++++++++++++----- caplets/manager_test.go | 2 +- js/fs_test.go | 13 ++++++- modules/http_proxy/http_proxy_test.go | 6 +++ 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/caplets/caplet_test.go b/caplets/caplet_test.go index 167579fc..dee5d9ff 100644 --- a/caplets/caplet_test.go +++ b/caplets/caplet_test.go @@ -104,11 +104,18 @@ func TestCapletEval(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cap := NewCaplet("test", "/tmp/test.cap", 100) + tempFile, err := ioutil.TempFile("", "test-*.cap") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tempFile.Name()) + tempFile.Close() + + cap := NewCaplet("test", tempFile.Name(), 100) cap.Code = tt.code var gotLines []string - err := cap.Eval(tt.argv, func(line string) error { + err = cap.Eval(tt.argv, func(line string) error { gotLines = append(gotLines, line) return nil }) @@ -133,7 +140,14 @@ func TestCapletEval(t *testing.T) { } func TestCapletEvalError(t *testing.T) { - cap := NewCaplet("test", "/tmp/test.cap", 100) + tempFile, err := ioutil.TempFile("", "test-*.cap") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tempFile.Name()) + tempFile.Close() + + cap := NewCaplet("test", tempFile.Name(), 100) cap.Code = []string{ "first line", "error line", @@ -143,7 +157,7 @@ func TestCapletEvalError(t *testing.T) { expectedErr := errors.New("test error") var executedLines []string - err := cap.Eval(nil, func(line string) error { + err = cap.Eval(nil, func(line string) error { executedLines = append(executedLines, line) if line == "error line" { return expectedErr @@ -208,7 +222,14 @@ func TestNewScript(t *testing.T) { } func TestCapletEvalCommentAtStartOfLine(t *testing.T) { - cap := NewCaplet("test", "/tmp/test.cap", 100) + tempFile, err := ioutil.TempFile("", "test-*.cap") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tempFile.Name()) + tempFile.Close() + + cap := NewCaplet("test", tempFile.Name(), 100) cap.Code = []string{ "# comment", " # not a comment (has space before #)", @@ -217,7 +238,7 @@ func TestCapletEvalCommentAtStartOfLine(t *testing.T) { } var gotLines []string - err := cap.Eval(nil, func(line string) error { + err = cap.Eval(nil, func(line string) error { gotLines = append(gotLines, line) return nil }) @@ -273,11 +294,18 @@ func TestCapletEvalArgvSubstitutionEdgeCases(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cap := NewCaplet("test", "/tmp/test.cap", 100) + tempFile, err := ioutil.TempFile("", "test-*.cap") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tempFile.Name()) + tempFile.Close() + + cap := NewCaplet("test", tempFile.Name(), 100) cap.Code = []string{tt.code} var gotLine string - err := cap.Eval(tt.argv, func(line string) error { + err = cap.Eval(tt.argv, func(line string) error { gotLine = line return nil }) @@ -295,7 +323,14 @@ func TestCapletEvalArgvSubstitutionEdgeCases(t *testing.T) { func TestCapletStructFields(t *testing.T) { // Test that Caplet properly embeds Script - cap := NewCaplet("test", "/tmp/test.cap", 100) + tempFile, err := ioutil.TempFile("", "test-*.cap") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tempFile.Name()) + tempFile.Close() + + cap := NewCaplet("test", tempFile.Name(), 100) // These fields should be accessible due to embedding _ = cap.Path diff --git a/caplets/manager_test.go b/caplets/manager_test.go index 8f90de72..0392a12b 100644 --- a/caplets/manager_test.go +++ b/caplets/manager_test.go @@ -64,7 +64,7 @@ func TestList(t *testing.T) { } // Check names (should be sorted) - expectedNames := []string{"subdir/nested", "test1", "test2", "test3"} + expectedNames := []string{filepath.Join("subdir", "nested"), "test1", "test2", "test3"} sort.Strings(expectedNames) gotNames := make([]string, len(caplets)) diff --git a/js/fs_test.go b/js/fs_test.go index 0f5880bc..fd089d28 100644 --- a/js/fs_test.go +++ b/js/fs_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "strings" "testing" @@ -383,8 +384,16 @@ func TestWriteFile(t *testing.T) { // Check file permissions info, _ := os.Stat(testFile) - if info.Mode().Perm() != 0644 { - t.Errorf("expected permissions 0644, got %v", info.Mode().Perm()) + if runtime.GOOS == "windows" { + // On Windows, permissions are different - just check that file exists and is readable + if info.Mode()&0400 == 0 { + t.Error("expected file to be readable on Windows") + } + } else { + // On Unix-like systems, check exact permissions + if info.Mode().Perm() != 0644 { + t.Errorf("expected permissions 0644, got %v", info.Mode().Perm()) + } } }) diff --git a/modules/http_proxy/http_proxy_test.go b/modules/http_proxy/http_proxy_test.go index 2ffc2b99..d05d046e 100644 --- a/modules/http_proxy/http_proxy_test.go +++ b/modules/http_proxy/http_proxy_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "os" + "runtime" "strings" "testing" "time" @@ -597,6 +598,11 @@ func TestHTTPProxyJavaScriptInjection(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + // Skip test with invalid filename characters on Windows + if runtime.GOOS == "windows" && strings.ContainsAny(tt.jsToInject, "<>:\"|?*") { + t.Skip("Skipping test with invalid filename characters on Windows") + } + err := proxy.Configure("127.0.0.1", 8080, 80, false, "", tt.jsToInject, false) if err != nil { t.Fatalf("Configure failed: %v", err)