forked from smart-mcp-proxy/mcpproxy-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_test.go
More file actions
366 lines (353 loc) · 8.99 KB
/
validation_test.go
File metadata and controls
366 lines (353 loc) · 8.99 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package config
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidateDetailed(t *testing.T) {
tests := []struct {
name string
config *Config
expectedErrors int
errorFields []string
}{
{
name: "valid config",
config: &Config{
Listen: "127.0.0.1:8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000), // 1 minute
Servers: []*ServerConfig{},
},
expectedErrors: 0,
errorFields: []string{},
},
{
name: "ToolsLimit out of range",
config: &Config{
Listen: ":8080",
ToolsLimit: 0, // Too low
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000), // Add valid timeout
},
expectedErrors: 1,
errorFields: []string{"tools_limit"},
},
{
name: "negative ToolResponseLimit",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: -100, // Negative
CallToolTimeout: Duration(60000000000), // Add valid timeout
},
expectedErrors: 1,
errorFields: []string{"tool_response_limit"},
},
{
name: "invalid timeout",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(0), // Zero
},
expectedErrors: 1,
errorFields: []string{"call_tool_timeout"},
},
{
name: "server missing name",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "", // Missing
Protocol: "stdio",
Command: "echo",
},
},
},
expectedErrors: 1,
errorFields: []string{"mcpServers[0].name"},
},
{
name: "duplicate server names",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test",
Protocol: "stdio",
Command: "echo",
},
{
Name: "test", // Duplicate
Protocol: "stdio",
Command: "cat",
},
},
},
expectedErrors: 1,
errorFields: []string{"mcpServers[1].name"},
},
{
name: "invalid protocol",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test",
Protocol: "invalid", // Invalid
Command: "echo",
},
},
},
expectedErrors: 1,
errorFields: []string{"mcpServers[0].protocol"},
},
{
name: "stdio server missing command",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test",
Protocol: "stdio",
Command: "", // Missing
},
},
},
expectedErrors: 1,
errorFields: []string{"mcpServers[0].command"},
},
{
name: "http server missing url",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test",
Protocol: "http",
URL: "", // Missing
},
},
},
expectedErrors: 1,
errorFields: []string{"mcpServers[0].url"},
},
{
name: "invalid log level",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Logging: &LogConfig{
Level: "invalid", // Invalid
},
},
expectedErrors: 1,
errorFields: []string{"logging.level"},
},
{
name: "oauth with empty client_id (DCR mode)",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test-oauth",
Protocol: "http",
URL: "https://api.example.com/mcp",
OAuth: &OAuthConfig{
ClientID: "", // Empty - should use DCR
Scopes: []string{"mcp.read", "mcp.write"},
},
},
},
},
expectedErrors: 0,
errorFields: []string{},
},
{
name: "oauth with only scopes and pkce",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test-oauth-dcr",
Protocol: "http",
URL: "https://api.example.com/mcp",
OAuth: &OAuthConfig{
Scopes: []string{"mcp.read"},
PKCEEnabled: true,
},
},
},
},
expectedErrors: 0,
errorFields: []string{},
},
{
name: "oauth with client_id provided",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test-oauth-client",
Protocol: "http",
URL: "https://api.example.com/mcp",
OAuth: &OAuthConfig{
ClientID: "my-client-id",
ClientSecret: "my-secret",
Scopes: []string{"mcp.read", "mcp.write"},
PKCEEnabled: true,
},
},
},
},
expectedErrors: 0,
errorFields: []string{},
},
{
name: "enabled_tools and disabled_tools are mutually exclusive",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test",
Protocol: "http",
URL: "https://api.example.com/mcp",
EnabledTools: []string{"read_file"},
DisabledTools: []string{"write_file"},
},
},
},
expectedErrors: 1,
errorFields: []string{"mcpServers[0].enabled_tools"},
},
{
name: "enabled_tools alone is valid",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test",
Protocol: "http",
URL: "https://api.example.com/mcp",
EnabledTools: []string{"read_file", "list_dir"},
},
},
},
expectedErrors: 0,
errorFields: []string{},
},
{
name: "disabled_tools alone is valid",
config: &Config{
Listen: ":8080",
ToolsLimit: 15,
ToolResponseLimit: 1000,
CallToolTimeout: Duration(60000000000),
Servers: []*ServerConfig{
{
Name: "test",
Protocol: "http",
URL: "https://api.example.com/mcp",
DisabledTools: []string{"delete_file", "execute_code"},
},
},
},
expectedErrors: 0,
errorFields: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
errors := tt.config.ValidateDetailed()
assert.Equal(t, tt.expectedErrors, len(errors), "Expected %d errors, got %d: %v", tt.expectedErrors, len(errors), errors)
if tt.expectedErrors > 0 {
// Check that expected fields are in error list
errorFieldMap := make(map[string]bool)
for _, err := range errors {
errorFieldMap[err.Field] = true
}
for _, expectedField := range tt.errorFields {
assert.True(t, errorFieldMap[expectedField], "Expected error for field %s", expectedField)
}
}
})
}
}
func TestValidationError(t *testing.T) {
err := ValidationError{
Field: "test_field",
Message: "test message",
}
assert.Equal(t, "test_field: test message", err.Error())
}
func TestIsValidListenAddr(t *testing.T) {
tests := []struct {
name string
addr string
valid bool
}{
{"empty", "", false},
{"port only", ":8080", true},
{"host and port", "127.0.0.1:8080", true},
{"localhost", "localhost:8080", true},
{"just colon", ":", true}, // Edge case: technically valid for port 0
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isValidListenAddr(tt.addr)
assert.Equal(t, tt.valid, result, "Expected %s to be valid=%v", tt.addr, tt.valid)
})
}
}
func TestValidateWithDefaults(t *testing.T) {
// Test that Validate applies defaults before validation
cfg := &Config{
Listen: "", // Should default to 127.0.0.1:8080
ToolsLimit: 0, // Should default to 15
ToolResponseLimit: -1, // Should default to 0
CallToolTimeout: 0, // Should default to 2 minutes
Servers: []*ServerConfig{},
}
err := cfg.Validate()
require.NoError(t, err, "Validation should succeed after applying defaults")
assert.Equal(t, "127.0.0.1:8080", cfg.Listen)
assert.Equal(t, 15, cfg.ToolsLimit)
assert.Equal(t, 0, cfg.ToolResponseLimit)
assert.Greater(t, cfg.CallToolTimeout.Duration().Seconds(), 0.0)
}