utils_test.go - fingered - Fingerd protocol daemon, allowing custom responses.
(HTM) git clone git://jay.scot/fingered
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) 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 characters
23 " ", // Empty string
24 "username=", // Potential injection attempt
25 "../", // Directory traversal
26 "/etc/passwd", // Absolute path traversal
27 "\\windows\\system32\\", // Windows path traversal
28 "ROOT", // Uppercase letters
29 "john_doe_", // Underscore at the end
30 "john_doe_doe_doe_doe_doe_doe_doe_doe", // Too long
31 "foo|bar", // Shell pipe
32 "filename>.txt", // Shell redirection
33 "cmd &", // Background execution
34 "`ls -l`", // Command substitution
35 "$(echo hi)", // Command substitution
36 "{malicious}", // Command grouping
37 "evil$word", // Dollar sign
38 "abc;def", // Command chaining
39 "[evil]", // Command grouping
40 "nasty~word", // Tilde expansion
41 "question?", // Question mark
42 "exclamation!", // Exclamation mark
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, expected true", word)
51 }
52 }
53
54 for _, word := range invalidWords {
55 if utils.IsValidWord(word) {
56 t.Errorf("isValidWord(%s) returned true, expected false", word)
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", err)
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", err)
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', but got '%s'", test.filePath, test.expected, actual)
90 }
91 }
92
93 if actual != test.expected {
94 t.Errorf("For file %s, expected '%s', but got '%s'", test.filePath, test.expected, actual)
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 message. Got: %s, Expected: %s", logOutput, expectedLogMessage)
112 }
113 }