| utils_test.go - fingered - Fingerd protocol daemon, allowing custom responses. | |
| git clone git://jay.scot/fingered | |
| Log | |
| Files | |
| Refs | |
| README | |
| LICENSE | |
| --- | |
| utils_test.go (3568B) | |
| --- | |
| 1 package tests | |
| 2 | |
| 3 import ( | |
| 4 "bytes" | |
| 5 "log" | |
| 6 "os" | |
| 7 "strings" | |
| 8 "testing" | |
| 9 | |
| 10 "fingered/utils" | |
| 11 ) | |
| 12 | |
| 13 func TestIsValidWord(t *testing.T) { | |
| 14 validWords := []string{ | |
| 15 "john", "alice", "bob", "dave", | |
| 16 "john123", "alice456", "dave789", | |
| 17 "john_doe", "alice_smith", "dave_jones", | |
| 18 } | |
| 19 | |
| 20 invalidWords := []string{ | |
| 21 "123", // Numbers only | |
| 22 "foo.bar", // Special chara… | |
| 23 " ", // Empty string | |
| 24 "username=", // Potential inj… | |
| 25 "../", // Directory tra… | |
| 26 "/etc/passwd", // Absolute path… | |
| 27 "\\windows\\system32\\", // Windows path … | |
| 28 "ROOT", // Uppercase let… | |
| 29 "john_doe_", // Underscore at… | |
| 30 "john_doe_doe_doe_doe_doe_doe_doe_doe", // Too long | |
| 31 "foo|bar", // Shell pipe | |
| 32 "filename>.txt", // Shell redirec… | |
| 33 "cmd &", // Background ex… | |
| 34 "`ls -l`", // Command subst… | |
| 35 "$(echo hi)", // Command subst… | |
| 36 "{malicious}", // Command group… | |
| 37 "evil$word", // Dollar sign | |
| 38 "abc;def", // Command chain… | |
| 39 "[evil]", // Command group… | |
| 40 "nasty~word", // Tilde expansi… | |
| 41 "question?", // Question mark | |
| 42 "exclamation!", // Exclamation m… | |
| 43 "\"quoted\"", // Double quotes | |
| 44 "'quoted'", // Single quotes | |
| 45 "escaped\\", // Backslash | |
| 46 } | |
| 47 | |
| 48 for _, word := range validWords { | |
| 49 if !utils.IsValidWord(word) { | |
| 50 t.Errorf("isValidWord(%s) returned false, expect… | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 for _, word := range invalidWords { | |
| 55 if utils.IsValidWord(word) { | |
| 56 t.Errorf("isValidWord(%s) returned true, expecte… | |
| 57 } | |
| 58 | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 func TestGetContent(t *testing.T) { | |
| 63 // Create a temporary shell script file for testing | |
| 64 scriptContent := "#!/bin/sh\n\necho 'Hello, World!'" | |
| 65 scriptFile, err := os.CreateTemp("", "test_script_*.sh") | |
| 66 if err != nil { | |
| 67 t.Fatalf("Failed to create temporary script file: %v", e… | |
| 68 } | |
| 69 defer os.Remove(scriptFile.Name()) | |
| 70 defer scriptFile.Close() | |
| 71 | |
| 72 _, err = scriptFile.WriteString(scriptContent) | |
| 73 if err != nil { | |
| 74 t.Fatalf("Failed to write to temporary script file: %v",… | |
| 75 } | |
| 76 | |
| 77 tests := []struct { | |
| 78 filePath string | |
| 79 expected string | |
| 80 }{ | |
| 81 {"nonexistent.txt", "file not found"}, | |
| 82 {scriptFile.Name(), "Hello, World!\n"}, | |
| 83 } | |
| 84 | |
| 85 for _, test := range tests { | |
| 86 actual, err := utils.GetContent(test.filePath) | |
| 87 if err != nil { | |
| 88 if actual != test.expected { | |
| 89 t.Errorf("For file %s, expected '%s', bu… | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 if actual != test.expected { | |
| 94 t.Errorf("For file %s, expected '%s', but got '%… | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 func TestLogMsg(t *testing.T) { | |
| 100 var buf bytes.Buffer | |
| 101 log.SetOutput(&buf) | |
| 102 defer func() { | |
| 103 log.SetOutput(os.Stderr) | |
| 104 }() | |
| 105 | |
| 106 utils.LogMsg("Test message: %s %d", "Hello", 123) | |
| 107 logOutput := buf.String() | |
| 108 expectedLogMessage := "Test message: Hello 123" | |
| 109 | |
| 110 if !strings.Contains(logOutput, expectedLogMessage) { | |
| 111 t.Errorf("Log message does not contain the expected mess… | |
| 112 } | |
| 113 } |