zentinel-agent-protocol 0.6.21

Agent protocol and IPC for Zentinel reverse proxy external processors
Documentation
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//! Integration tests for reverse agent connections.
//!
//! Tests the complete reverse connection flow:
//! - Listener binding and cleanup
//! - Agent registration handshake
//! - Validation (protocol version, agent ID, allowed list, auth)
//! - Pool integration after successful registration
//! - Timeout handling for slow handshakes
//! - Connection lifecycle (close, drop)

use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::UnixStream;

use zentinel_agent_protocol::v2::pool::AgentPoolConfig;
use zentinel_agent_protocol::v2::reverse::{
    RegistrationRequest, RegistrationResponse, ReverseConnectionConfig, ReverseConnectionListener,
};
use zentinel_agent_protocol::v2::uds::{MessageType, UdsCapabilities, UdsFeatures, UdsLimits};
use zentinel_agent_protocol::v2::{AgentPool, PROTOCOL_VERSION_2};
use zentinel_agent_protocol::AgentProtocolError;

// =============================================================================
// Helper Functions
// =============================================================================

fn test_capabilities(agent_id: &str) -> UdsCapabilities {
    UdsCapabilities {
        agent_id: agent_id.to_string(),
        name: format!("Test Agent {}", agent_id),
        version: "1.0.0".to_string(),
        supported_events: vec![0x10, 0x11], // RequestHeaders, RequestBodyChunk
        features: UdsFeatures {
            streaming_body: false,
            websocket: false,
            guardrails: false,
            config_push: false,
            metrics_export: false,
            concurrent_requests: 10,
            cancellation: false,
            flow_control: false,
            health_reporting: false,
        },
        limits: UdsLimits {
            max_body_size: 1024 * 1024,
            max_concurrency: 10,
            preferred_chunk_size: 64 * 1024,
        },
    }
}

fn test_registration_request(agent_id: &str) -> RegistrationRequest {
    RegistrationRequest {
        protocol_version: PROTOCOL_VERSION_2,
        agent_id: agent_id.to_string(),
        capabilities: test_capabilities(agent_id),
        auth_token: None,
        metadata: None,
    }
}

/// Write a framed message to a stream (4-byte BE length + 1-byte type + payload).
async fn write_framed_message(stream: &mut UnixStream, msg_type: MessageType, payload: &[u8]) {
    let total_len = (payload.len() + 1) as u32;
    stream.write_all(&total_len.to_be_bytes()).await.unwrap();
    stream.write_all(&[msg_type as u8]).await.unwrap();
    stream.write_all(payload).await.unwrap();
    stream.flush().await.unwrap();
}

/// Read a framed message from a stream.
async fn read_framed_message(stream: &mut UnixStream) -> (MessageType, Vec<u8>) {
    let mut len_bytes = [0u8; 4];
    stream.read_exact(&mut len_bytes).await.unwrap();
    let total_len = u32::from_be_bytes(len_bytes) as usize;

    let mut type_byte = [0u8; 1];
    stream.read_exact(&mut type_byte).await.unwrap();
    let msg_type = MessageType::try_from(type_byte[0]).unwrap();

    let payload_len = total_len - 1;
    let mut payload = vec![0u8; payload_len];
    if payload_len > 0 {
        stream.read_exact(&mut payload).await.unwrap();
    }

    (msg_type, payload)
}

/// Perform a full agent registration handshake on a raw stream.
async fn perform_handshake(
    stream: &mut UnixStream,
    request: &RegistrationRequest,
) -> RegistrationResponse {
    let payload = serde_json::to_vec(request).unwrap();
    write_framed_message(stream, MessageType::HandshakeRequest, &payload).await;

    let (msg_type, response_payload) = read_framed_message(stream).await;
    assert_eq!(msg_type, MessageType::HandshakeResponse);

    serde_json::from_slice(&response_payload).unwrap()
}

fn temp_socket_path(name: &str) -> String {
    let dir = tempfile::tempdir().unwrap();
    // Leak the dir so it isn't cleaned up before the test finishes
    let path = dir.path().join(format!("{}.sock", name));
    let path_str = path.to_string_lossy().to_string();
    std::mem::forget(dir);
    path_str
}

// =============================================================================
// Listener Binding Tests
// =============================================================================

#[tokio::test]
async fn listener_binds_to_uds_path() {
    let socket_path = temp_socket_path("bind-test");
    let config = ReverseConnectionConfig::default();

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    assert_eq!(listener.socket_path(), socket_path);
    assert!(std::path::Path::new(&socket_path).exists());
}

#[tokio::test]
async fn listener_removes_existing_socket_on_bind() {
    let socket_path = temp_socket_path("rebind-test");

    // Create a file at the socket path
    std::fs::write(&socket_path, "placeholder").unwrap();
    assert!(std::path::Path::new(&socket_path).exists());

    let config = ReverseConnectionConfig::default();
    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    // Should have replaced the file with a socket
    assert_eq!(listener.socket_path(), socket_path);
}

#[tokio::test]
async fn listener_cleans_up_socket_on_drop() {
    let socket_path = temp_socket_path("drop-test");
    let config = ReverseConnectionConfig::default();

    {
        let _listener = ReverseConnectionListener::bind_uds(&socket_path, config)
            .await
            .unwrap();
        assert!(std::path::Path::new(&socket_path).exists());
    }
    // Listener dropped — socket should be cleaned up
    assert!(!std::path::Path::new(&socket_path).exists());
}

// =============================================================================
// Registration Validation Tests
// =============================================================================

#[tokio::test]
async fn successful_registration_handshake() {
    let socket_path = temp_socket_path("success-handshake");
    let config = ReverseConnectionConfig::default();
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    // Spawn agent connection
    let socket_path_clone = socket_path.clone();
    let agent_handle = tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        let request = test_registration_request("test-agent-1");
        perform_handshake(&mut stream, &request).await
    });

    // Accept on listener side
    let result = listener.accept_one(&pool).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "test-agent-1");

    // Verify agent got success response
    let response = agent_handle.await.unwrap();
    assert!(response.success);
    assert!(response.error.is_none());
    assert_eq!(response.proxy_id, "zentinel-proxy");
    assert!(!response.connection_id.is_empty());
}

#[tokio::test]
async fn rejects_wrong_protocol_version() {
    let socket_path = temp_socket_path("wrong-version");
    let config = ReverseConnectionConfig::default();
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    let agent_handle = tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        let mut request = test_registration_request("bad-version-agent");
        request.protocol_version = 99;
        perform_handshake(&mut stream, &request).await
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentProtocolError::VersionMismatch { expected, actual } => {
            assert_eq!(expected, PROTOCOL_VERSION_2);
            assert_eq!(actual, 99);
        }
        other => panic!("Expected VersionMismatch, got: {:?}", other),
    }

    let response = agent_handle.await.unwrap();
    assert!(!response.success);
    assert!(response.error.is_some());
}

#[tokio::test]
async fn rejects_empty_agent_id() {
    let socket_path = temp_socket_path("empty-id");
    let config = ReverseConnectionConfig::default();
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    let agent_handle = tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        let mut request = test_registration_request("");
        request.agent_id = String::new();
        perform_handshake(&mut stream, &request).await
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentProtocolError::InvalidMessage(msg) => {
            assert!(
                msg.contains("empty"),
                "Error should mention empty ID: {}",
                msg
            );
        }
        other => panic!("Expected InvalidMessage, got: {:?}", other),
    }

    let response = agent_handle.await.unwrap();
    assert!(!response.success);
}

#[tokio::test]
async fn rejects_agent_not_in_allowed_list() {
    let socket_path = temp_socket_path("not-allowed");
    let mut allowed = HashSet::new();
    allowed.insert("allowed-agent".to_string());

    let config = ReverseConnectionConfig {
        allowed_agents: allowed,
        ..Default::default()
    };
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    let agent_handle = tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        let request = test_registration_request("unauthorized-agent");
        perform_handshake(&mut stream, &request).await
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentProtocolError::InvalidMessage(msg) => {
            assert!(
                msg.contains("not in the allowed list"),
                "Error should mention allowed list: {}",
                msg
            );
        }
        other => panic!("Expected InvalidMessage, got: {:?}", other),
    }

    let response = agent_handle.await.unwrap();
    assert!(!response.success);
}

#[tokio::test]
async fn accepts_agent_in_allowed_list() {
    let socket_path = temp_socket_path("in-allowed-list");
    let mut allowed = HashSet::new();
    allowed.insert("allowed-agent".to_string());

    let config = ReverseConnectionConfig {
        allowed_agents: allowed,
        ..Default::default()
    };
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    let agent_handle = tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        let request = test_registration_request("allowed-agent");
        perform_handshake(&mut stream, &request).await
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "allowed-agent");

    let response = agent_handle.await.unwrap();
    assert!(response.success);
}

#[tokio::test]
async fn rejects_missing_auth_token_when_required() {
    let socket_path = temp_socket_path("auth-required");
    let config = ReverseConnectionConfig {
        require_auth: true,
        ..Default::default()
    };
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    let agent_handle = tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        let request = test_registration_request("no-token-agent");
        // auth_token is None by default
        perform_handshake(&mut stream, &request).await
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentProtocolError::InvalidMessage(msg) => {
            assert!(
                msg.contains("Authentication required"),
                "Error should mention auth: {}",
                msg
            );
        }
        other => panic!("Expected InvalidMessage, got: {:?}", other),
    }

    let response = agent_handle.await.unwrap();
    assert!(!response.success);
}

#[tokio::test]
async fn accepts_valid_auth_token() {
    let socket_path = temp_socket_path("auth-valid");
    let config = ReverseConnectionConfig {
        require_auth: true,
        ..Default::default()
    };
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    let agent_handle = tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        let mut request = test_registration_request("authed-agent");
        request.auth_token = Some("valid-token-123".to_string());
        perform_handshake(&mut stream, &request).await
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_ok());

    let response = agent_handle.await.unwrap();
    assert!(response.success);
}

// =============================================================================
// Handshake Timeout Tests
// =============================================================================

#[tokio::test]
async fn handshake_timeout_on_slow_agent() {
    let socket_path = temp_socket_path("timeout");
    let config = ReverseConnectionConfig {
        handshake_timeout: Duration::from_millis(100),
        ..Default::default()
    };
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    // Agent connects but never sends registration
    let _agent_handle = tokio::spawn(async move {
        let _stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        // Hold connection open without sending anything
        tokio::time::sleep(Duration::from_secs(5)).await;
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentProtocolError::Timeout(duration) => {
            assert_eq!(duration, Duration::from_millis(100));
        }
        other => panic!("Expected Timeout, got: {:?}", other),
    }
}

// =============================================================================
// Protocol Error Tests
// =============================================================================

#[tokio::test]
async fn rejects_wrong_message_type() {
    let socket_path = temp_socket_path("wrong-type");
    let config = ReverseConnectionConfig::default();
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        // Send an AgentResponse instead of HandshakeRequest
        let payload = b"{}";
        write_framed_message(&mut stream, MessageType::AgentResponse, payload).await;
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentProtocolError::InvalidMessage(msg) => {
            assert!(
                msg.contains("Expected registration request"),
                "Error should mention expected type: {}",
                msg
            );
        }
        other => panic!("Expected InvalidMessage, got: {:?}", other),
    }
}

#[tokio::test]
async fn rejects_malformed_json_payload() {
    let socket_path = temp_socket_path("malformed-json");
    let config = ReverseConnectionConfig::default();
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        // Send HandshakeRequest with invalid JSON
        write_framed_message(&mut stream, MessageType::HandshakeRequest, b"not-json!!").await;
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentProtocolError::InvalidMessage(_) => {} // Expected
        other => panic!("Expected InvalidMessage, got: {:?}", other),
    }
}

#[tokio::test]
async fn handles_connection_closed_during_handshake() {
    let socket_path = temp_socket_path("conn-closed");
    let config = ReverseConnectionConfig {
        handshake_timeout: Duration::from_secs(2),
        ..Default::default()
    };
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    tokio::spawn(async move {
        let stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        // Immediately close the connection
        drop(stream);
    });

    let result = listener.accept_one(&pool).await;
    assert!(result.is_err());
}

// =============================================================================
// Accept Loop Tests
// =============================================================================

#[tokio::test]
async fn accept_loop_handles_multiple_agents() {
    let socket_path = temp_socket_path("multi-agent");
    let config = ReverseConnectionConfig::default();
    let pool = Arc::new(AgentPool::with_config(AgentPoolConfig::default()));

    let listener = Arc::new(
        ReverseConnectionListener::bind_uds(&socket_path, config)
            .await
            .unwrap(),
    );

    // Start accept loop in background
    let listener_clone = Arc::clone(&listener);
    let pool_clone = Arc::clone(&pool);
    tokio::spawn(async move {
        listener_clone.accept_loop(pool_clone).await;
    });

    // Give the accept loop time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Connect 3 agents sequentially
    for i in 0..3 {
        let socket_path_clone = socket_path.clone();
        let agent_id = format!("agent-{}", i);
        let handle = tokio::spawn(async move {
            let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
            let request = test_registration_request(&agent_id);
            let response = perform_handshake(&mut stream, &request).await;
            assert!(
                response.success,
                "Agent {} registration failed: {:?}",
                agent_id, response.error
            );
            // Keep the stream alive briefly
            tokio::time::sleep(Duration::from_millis(100)).await;
        });
        handle.await.unwrap();
    }
}

// =============================================================================
// ReverseConnectionClient Tests
// =============================================================================

#[tokio::test]
async fn reverse_client_reports_connected_after_registration() {
    let socket_path = temp_socket_path("client-connected");
    let config = ReverseConnectionConfig::default();
    let pool = AgentPool::with_config(AgentPoolConfig::default());

    let listener = ReverseConnectionListener::bind_uds(&socket_path, config)
        .await
        .unwrap();

    let socket_path_clone = socket_path.clone();
    tokio::spawn(async move {
        let mut stream = UnixStream::connect(&socket_path_clone).await.unwrap();
        let request = test_registration_request("connected-agent");
        let response = perform_handshake(&mut stream, &request).await;
        assert!(response.success);
        // Keep connection alive
        tokio::time::sleep(Duration::from_secs(2)).await;
    });

    let agent_id = listener.accept_one(&pool).await.unwrap();
    assert_eq!(agent_id, "connected-agent");

    // The agent should now be registered in the pool
    let stats = pool.stats().await;
    assert!(
        !stats.is_empty(),
        "Pool should have at least one agent after registration"
    );
    assert_eq!(stats[0].agent_id, "connected-agent");
}

// =============================================================================
// Config Default Tests
// =============================================================================

#[test]
fn config_default_values_are_reasonable() {
    let config = ReverseConnectionConfig::default();

    assert_eq!(config.backlog, 128);
    assert_eq!(config.handshake_timeout, Duration::from_secs(10));
    assert_eq!(config.max_connections_per_agent, 4);
    assert!(config.allowed_agents.is_empty());
    assert!(!config.require_auth);
    assert_eq!(config.request_timeout, Duration::from_secs(30));
}

#[test]
fn registration_request_roundtrip_with_metadata() {
    let mut request = test_registration_request("meta-agent");
    request.metadata = Some(serde_json::json!({
        "region": "us-west-2",
        "version": "2.1.0",
        "tags": ["security", "waf"]
    }));
    request.auth_token = Some("bearer-token-abc".to_string());

    let json = serde_json::to_vec(&request).unwrap();
    let parsed: RegistrationRequest = serde_json::from_slice(&json).unwrap();

    assert_eq!(parsed.agent_id, "meta-agent");
    assert_eq!(parsed.protocol_version, PROTOCOL_VERSION_2);
    assert!(parsed.auth_token.is_some());
    assert!(parsed.metadata.is_some());
    let meta = parsed.metadata.unwrap();
    assert_eq!(meta["region"], "us-west-2");
}

#[test]
fn registration_response_roundtrip_success() {
    let response = RegistrationResponse {
        success: true,
        error: None,
        proxy_id: "zentinel-proxy".to_string(),
        proxy_version: "0.5.12".to_string(),
        connection_id: "agent-1-abc123".to_string(),
    };

    let json = serde_json::to_vec(&response).unwrap();
    let parsed: RegistrationResponse = serde_json::from_slice(&json).unwrap();

    assert!(parsed.success);
    assert!(parsed.error.is_none());
    assert_eq!(parsed.proxy_id, "zentinel-proxy");
    assert_eq!(parsed.connection_id, "agent-1-abc123");
}

#[test]
fn registration_response_roundtrip_failure() {
    let response = RegistrationResponse {
        success: false,
        error: Some("Agent not in allowed list".to_string()),
        proxy_id: "zentinel-proxy".to_string(),
        proxy_version: "0.5.12".to_string(),
        connection_id: String::new(),
    };

    let json = serde_json::to_vec(&response).unwrap();
    let parsed: RegistrationResponse = serde_json::from_slice(&json).unwrap();

    assert!(!parsed.success);
    assert_eq!(parsed.error.unwrap(), "Agent not in allowed list");
}