fix: various unit tests fixes for windows

This commit is contained in:
evilsocket 2025-07-12 16:03:23 +02:00
commit fecd81118d
4 changed files with 62 additions and 12 deletions

View file

@ -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

View file

@ -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))

View file

@ -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())
}
}
})

View file

@ -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)