-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_test.go
More file actions
230 lines (208 loc) · 5.24 KB
/
websocket_test.go
File metadata and controls
230 lines (208 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"net/http"
"os"
"path/filepath"
"testing"
)
func TestReadPID(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "websocket-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
tests := []struct {
name string
pidValue string
want int
}{
{
name: "valid PID",
pidValue: "12345",
want: 12345,
},
{
name: "PID with whitespace",
pidValue: " 67890 \n",
want: 67890,
},
{
name: "empty file",
pidValue: "",
want: 0,
},
{
name: "invalid PID",
pidValue: "not-a-number",
want: 0,
},
{
name: "zero PID",
pidValue: "0",
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pidPath := filepath.Join(tmpDir, "pid-"+tt.name)
if tt.pidValue != "" {
if err := os.WriteFile(pidPath, []byte(tt.pidValue), 0644); err != nil {
t.Fatalf("Failed to write PID file: %v", err)
}
}
got := readPID(pidPath)
if got != tt.want {
t.Errorf("readPID(%q) = %d; want %d", pidPath, got, tt.want)
}
})
}
}
func TestReadPIDNonExistentFile(t *testing.T) {
got := readPID("/nonexistent/pid/file")
if got != 0 {
t.Errorf("readPID(nonexistent) = %d; want 0", got)
}
}
func TestReadExitCode(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "websocket-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
tests := []struct {
name string
exitCode string
want int
}{
{
name: "success exit code",
exitCode: "0",
want: 0,
},
{
name: "error exit code",
exitCode: "1",
want: 1,
},
{
name: "exit code with whitespace",
exitCode: " 2 \n",
want: 2,
},
{
name: "empty file",
exitCode: "",
want: -1,
},
{
name: "invalid exit code",
exitCode: "not-a-number",
want: -1,
},
{
name: "negative exit code",
exitCode: "-1",
want: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exitCodePath := filepath.Join(tmpDir, "exitcode-"+tt.name)
if tt.exitCode != "" {
if err := os.WriteFile(exitCodePath, []byte(tt.exitCode), 0644); err != nil {
t.Fatalf("Failed to write exit code file: %v", err)
}
}
got := readExitCode(exitCodePath)
if got != tt.want {
t.Errorf("readExitCode(%q) = %d; want %d", exitCodePath, got, tt.want)
}
})
}
}
func TestReadExitCodeNonExistentFile(t *testing.T) {
got := readExitCode("/nonexistent/exitcode/file")
if got != -1 {
t.Errorf("readExitCode(nonexistent) = %d; want -1", got)
}
}
func TestIsProcessRunning(t *testing.T) {
// Test with current process (should be running)
currentPID := os.Getpid()
if !isProcessRunning(currentPID) {
t.Errorf("isProcessRunning(%d) = false; want true (current process)", currentPID)
}
// Test with invalid PID (very high number, unlikely to exist)
invalidPID := 999999999
if isProcessRunning(invalidPID) {
t.Errorf("isProcessRunning(%d) = true; want false (invalid PID)", invalidPID)
}
// Test with PID 1 (init process, should exist on Linux)
if !isProcessRunning(1) {
t.Log("isProcessRunning(1) = false; init process may not exist in test environment")
}
}
func TestCreateUpgrader(t *testing.T) {
tests := []struct {
name string
allowedOrigins []string
wantAllowAll bool
}{
{
name: "empty origins - allow all",
allowedOrigins: []string{},
wantAllowAll: true,
},
{
name: "nil origins - allow all",
allowedOrigins: nil,
wantAllowAll: true,
},
{
name: "single origin",
allowedOrigins: []string{"http://localhost:8080"},
wantAllowAll: false,
},
{
name: "multiple origins",
allowedOrigins: []string{"http://localhost:8080", "https://example.com"},
wantAllowAll: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
upgrader := createUpgrader(tt.allowedOrigins)
// Test CheckOrigin function
req := &http.Request{
Header: make(http.Header),
}
// Test with no origin (should be allowed if allowAll)
req.Header.Set("Origin", "")
result := upgrader.CheckOrigin(req)
if tt.wantAllowAll && !result {
t.Errorf("createUpgrader() CheckOrigin(no origin) = false; want true (allow all)")
}
// Test with matching origin
if len(tt.allowedOrigins) > 0 {
req.Header.Set("Origin", tt.allowedOrigins[0])
result = upgrader.CheckOrigin(req)
if !result {
t.Errorf("createUpgrader() CheckOrigin(matching origin) = false; want true")
}
// Test with non-matching origin
req.Header.Set("Origin", "http://evil.com")
result = upgrader.CheckOrigin(req)
if result {
t.Errorf("createUpgrader() CheckOrigin(non-matching origin) = true; want false")
}
}
})
}
}
func TestSendSystemMessage(t *testing.T) {
// Note: sendSystemMessage requires a real WebSocket connection
// For unit testing, we skip this test as it would panic with nil connection
// Full testing would require integration tests with real WebSocket connections
// This function is tested indirectly through integration tests
t.Skip("sendSystemMessage requires real WebSocket connection - tested via integration tests")
}