-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter_fuzz_test.go
More file actions
424 lines (358 loc) · 13.2 KB
/
Copy pathadapter_fuzz_test.go
File metadata and controls
424 lines (358 loc) · 13.2 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package tooladapter
import (
"encoding/json"
"log/slog"
"strings"
"testing"
"github.com/openai/openai-go/v3"
)
func FuzzTransformCompletionsResponse(f *testing.F) {
// Seed with valid responses
f.Add(`{"name": "test_function", "parameters": {}}`)
f.Add(`[{"name": "get_weather", "parameters": {"city": "London"}}]`)
f.Add(`I'll help you with that. [{"name": "calculate", "parameters": {"x": 10, "y": 20}}]`)
f.Add("```json\n{\"name\": \"search\", \"parameters\": {\"query\": \"test\"}}\n```")
// Seed with edge cases
f.Add(``)
f.Add(`Just a regular response without function calls`)
f.Add(`Here's some JSON: {"not": "a function call"}`)
f.Add(`Multiple functions: [{"name": "func1"}, {"name": "func2", "parameters": null}]`)
// Seed with malformed content
f.Add(`{"name": "broken_json"`)
f.Add(`[{"name": "test", "parameters": invalid}]`)
f.Add(`{"name": "", "parameters": {}}`)
f.Add(`{"name": "test with spaces", "parameters": {}}`)
f.Add(`{"name": "test@invalid", "parameters": {}}`)
// Seed with mixed content
f.Add(`Let me process that. {"name": "process", "parameters": {"data": "value"}} Done!`)
f.Add("Response with `{\"name\": \"inline\", \"parameters\": {}}` function call.")
f.Add(`Multiple: [{"name": "f1"}] and {"name": "f2", "parameters": {"x": 1}}`)
f.Fuzz(func(t *testing.T, content string) {
// Transform response should never panic
defer func() {
if r := recover(); r != nil {
t.Errorf("TransformCompletionsResponse panicked on content %q: %v", content, r)
}
}()
performFuzzTransformCompletionsResponse(t, content)
})
}
// performFuzzTransformCompletionsResponse executes the main fuzzing logic for response transformation
func performFuzzTransformCompletionsResponse(t *testing.T, content string) {
adapter := New(WithLogLevel(slog.LevelError))
// Create a mock response with the fuzzed content
resp := openai.ChatCompletion{
Choices: []openai.ChatCompletionChoice{
{
Message: openai.ChatCompletionMessage{
Content: content,
},
},
},
}
result, err := adapter.TransformCompletionsResponse(resp)
if err != nil {
// Errors are acceptable, but should be proper error types
if err.Error() == "" {
t.Errorf("TransformCompletionsResponse returned empty error message for content %q", content)
}
return
}
// If transformation succeeded, validate the result
if len(result.Choices) > 0 {
validateFuzzResponseChoice(t, result.Choices[0], content)
}
}
// validateFuzzResponseChoice validates a single response choice from fuzzing
func validateFuzzResponseChoice(t *testing.T, choice openai.ChatCompletionChoice, originalContent string) {
// If tool calls were detected, validate them
if len(choice.Message.ToolCalls) > 0 {
validateFuzzToolCallsDetected(t, choice)
} else {
validateFuzzNoToolCalls(t, choice, originalContent)
}
}
// validateFuzzToolCallsDetected validates when tool calls are detected
func validateFuzzToolCallsDetected(t *testing.T, choice openai.ChatCompletionChoice) {
// Content should be empty when tool calls are present
if choice.Message.Content != "" {
t.Errorf("TransformCompletionsResponse left content %q when tool calls were detected", choice.Message.Content)
}
// Finish reason should be tool_calls
if choice.FinishReason != "tool_calls" {
t.Errorf("TransformCompletionsResponse set finish reason to %q instead of 'tool_calls'", choice.FinishReason)
}
// All tool calls should be valid
for i, toolCall := range choice.Message.ToolCalls {
validateFuzzSingleToolCall(t, toolCall, i)
}
}
// validateFuzzSingleToolCall validates a single tool call from fuzzing
func validateFuzzSingleToolCall(t *testing.T, toolCall openai.ChatCompletionMessageToolCallUnion, index int) {
// Basic field validation
if toolCall.Function.Name == "" {
t.Errorf("TransformCompletionsResponse produced tool call %d with empty function name", index)
}
if toolCall.ID == "" {
t.Errorf("TransformCompletionsResponse produced tool call %d with empty ID", index)
}
if !strings.HasPrefix(toolCall.ID, "call_") {
t.Errorf("TransformCompletionsResponse produced tool call %d with invalid ID format: %q", index, toolCall.ID)
}
if toolCall.Type != "function" {
t.Errorf("TransformCompletionsResponse produced tool call %d with invalid type: %q", index, toolCall.Type)
}
// Validate function name
if err := ValidateFunctionName(toolCall.Function.Name); err != nil {
t.Errorf("TransformCompletionsResponse produced invalid function name %q: %v", toolCall.Function.Name, err)
}
// Arguments should be valid JSON
validateFuzzToolCallArguments(t, toolCall.Function.Arguments)
}
// validateFuzzToolCallArguments validates tool call arguments JSON
func validateFuzzToolCallArguments(t *testing.T, arguments string) {
if arguments != "" {
var temp interface{}
if err := json.Unmarshal([]byte(arguments), &temp); err != nil {
t.Errorf("TransformCompletionsResponse produced invalid JSON arguments %q: %v", arguments, err)
}
}
}
// validateFuzzNoToolCalls validates when no tool calls are detected
func validateFuzzNoToolCalls(t *testing.T, choice openai.ChatCompletionChoice, originalContent string) {
if choice.Message.Content != originalContent {
t.Errorf("TransformCompletionsResponse modified content when no tool calls detected: got %q, want %q",
choice.Message.Content, originalContent)
}
}
// buildFuzzedTool creates a tool from fuzzed parameters
func buildFuzzedTool(funcName, description string, hasParams bool) openai.ChatCompletionToolUnionParam {
functionDef := openai.FunctionDefinitionParam{
Name: funcName,
}
if description != "" {
functionDef.Description = openai.String(description)
}
if hasParams {
functionDef.Parameters = openai.FunctionParameters{
"type": "object",
"properties": map[string]interface{}{
"test": map[string]interface{}{
"type": "string",
},
},
}
}
return openai.ChatCompletionFunctionTool(functionDef)
}
// validateToolsRemoved checks that tools were properly removed from the result
func validateToolsRemoved(t *testing.T, result openai.ChatCompletionNewParams) {
if len(result.Tools) > 0 {
t.Errorf("TransformCompletionsRequest did not remove tools from result")
}
}
// validateMessageCount validates the message count after transformation
func validateMessageCount(t *testing.T, req, result openai.ChatCompletionNewParams) {
if len(req.Messages) == 0 {
// Should have created a new instruction message (user by default)
if len(result.Messages) != 1 {
t.Errorf("TransformCompletionsRequest did not create instruction message for empty messages: got %d messages, want 1",
len(result.Messages))
}
return
}
// Should always preserve message count (modify existing, not add new)
if len(result.Messages) != len(req.Messages) {
t.Errorf("TransformCompletionsRequest changed message count: got %d messages, want %d",
len(result.Messages), len(req.Messages))
}
}
// validateNoToolsCase validates the case when no tools are provided
func validateNoToolsCase(t *testing.T, req, result openai.ChatCompletionNewParams) {
if len(result.Tools) != 0 || len(result.Messages) != len(req.Messages) {
t.Errorf("TransformCompletionsRequest modified request when no valid tools were provided")
}
}
// FuzzTransformCompletionsRequest fuzzes the request transformation pipeline
func FuzzTransformCompletionsRequest(f *testing.F) {
// Seed with various tool configurations
f.Add("get_weather", "Get current weather", true)
f.Add("", "Empty name", true)
f.Add("test_function", "", true)
f.Add("function with spaces", "Invalid name", true)
f.Add("valid_function", "Normal description", false)
f.Fuzz(func(t *testing.T, funcName, description string, hasParams bool) {
// Transform request should never panic
defer func() {
if r := recover(); r != nil {
t.Errorf("TransformCompletionsRequest panicked with function name %q, description %q: %v",
funcName, description, r)
}
}()
adapter := New(WithLogLevel(slog.LevelError))
// Build a request with the fuzzed function definition
tools := []openai.ChatCompletionToolUnionParam{}
// Only add the tool if the function name is not empty (to test various scenarios)
if funcName != "" {
tool := buildFuzzedTool(funcName, description, hasParams)
tools = append(tools, tool)
}
req := openai.ChatCompletionNewParams{
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("Test message"),
},
Tools: tools,
}
result, err := adapter.TransformCompletionsRequest(req)
if err != nil {
// Errors are acceptable for invalid input, but should have proper error messages
if err.Error() == "" {
t.Errorf("TransformCompletionsRequest returned empty error message for function %q", funcName)
}
return
}
// If transformation succeeded, validate the result
if len(tools) > 0 {
validateToolsRemoved(t, result)
validateMessageCount(t, req, result)
// First message should contain tool information
// (either modified user message or system message)
if len(result.Messages) > 0 {
// We can't easily check the content without accessing internal fields,
// but we know the tool prompt should be injected somewhere
_ = result.Messages[0]
}
} else {
// No tools - should pass through unchanged
validateNoToolsCase(t, req, result)
}
})
}
// FuzzStreamingBuffering fuzzes the streaming buffer logic
func FuzzStreamingBuffering(f *testing.F) {
// Seed with various streaming scenarios
f.Add(`[{"name": "test"}]`, true) // Complete function call
f.Add(`[{"name": "test"`, false) // Incomplete JSON
f.Add(`{"name": "func", "parameters": `, false) // Partial parameters
f.Add(`Let me help`, true) // Regular text
f.Add(``, true) // Empty content
f.Add("```json\n{\"name\":", false) // Incomplete markdown block
f.Add("`{\"name\": \"test\"}`", true) // Complete inline code
f.Fuzz(func(t *testing.T, content string, expectComplete bool) {
// Streaming operations should never panic
defer func() {
if r := recover(); r != nil {
t.Errorf("Streaming operations panicked on content %q: %v", content, r)
}
}()
// Test HasCompleteJSON directly
_ = HasCompleteJSON(content)
// Test shouldStartBuffering heuristic
adapter := New(WithLogLevel(slog.LevelError))
stream := adapter.TransformStreamingResponse(&mockStreamingResponse{
chunks: []string{content},
})
defer func() {
if err := stream.Close(); err != nil {
// Use simple log since we don't have access to f *testing.F here
// This is acceptable for fuzzing where we want minimal overhead
_ = err // Fuzzing tests prioritize speed over logging
}
}()
// This should not panic or hang
hasNext := stream.Next()
if hasNext {
chunk := stream.Current()
// Basic validation - should have valid structure
_ = chunk
}
// Check for errors
if err := stream.Err(); err != nil {
// Errors are acceptable, but should have messages
if err.Error() == "" {
t.Errorf("Stream returned empty error message for content %q", content)
}
}
})
}
// mockStreamingResponse is a simple mock for testing streaming functionality
type mockStreamingResponse struct {
chunks []string
index int
}
func (m *mockStreamingResponse) Next() bool {
return m.index < len(m.chunks)
}
func (m *mockStreamingResponse) Current() openai.ChatCompletionChunk {
if m.index >= len(m.chunks) {
return openai.ChatCompletionChunk{}
}
chunk := openai.ChatCompletionChunk{
Choices: []openai.ChatCompletionChunkChoice{
{
Delta: openai.ChatCompletionChunkChoiceDelta{
Content: m.chunks[m.index],
Role: "assistant",
},
},
},
}
m.index++
// Last chunk should have finish reason
if m.index >= len(m.chunks) {
chunk.Choices[0].FinishReason = "stop"
}
return chunk
}
func (m *mockStreamingResponse) Err() error {
return nil
}
func (m *mockStreamingResponse) Close() error {
return nil
}
// FuzzIDGeneration fuzzes the ID generation to ensure it never panics or produces invalid IDs
func FuzzIDGeneration(f *testing.F) {
// This is more of a stress test since ID generation doesn't take input
// But we can test it under various conditions
f.Add(100) // Generate multiple IDs
f.Add(1000) // Stress test
f.Add(0) // Edge case
f.Add(1) // Single ID
f.Fuzz(func(t *testing.T, count int) {
adapter := New()
// ID generation should never panic
defer func() {
if r := recover(); r != nil {
t.Errorf("GenerateToolCallID panicked when generating %d IDs: %v", count, r)
}
}()
// Limit the count to prevent excessive resource usage
if count < 0 || count > 10000 {
count = 100
}
ids := make(map[string]bool)
for i := 0; i < count; i++ {
id := adapter.GenerateToolCallID()
// Basic validation
if id == "" {
t.Errorf("GenerateToolCallID returned empty ID")
return
}
if !strings.HasPrefix(id, "call_") {
t.Errorf("GenerateToolCallID returned ID without proper prefix: %q", id)
return
}
// Check for duplicates (should be extremely rare)
if ids[id] {
t.Errorf("GenerateToolCallID returned duplicate ID: %q", id)
return
}
ids[id] = true
// ID should have reasonable length (call_ + UUID)
if len(id) < 10 || len(id) > 50 {
t.Errorf("GenerateToolCallID returned ID with unusual length %d: %q", len(id), id)
return
}
}
})
}