Skip to content

Commit 14655b1

Browse files
committed
ttrpc: Add extension framework unit tests and integration tests
The extension framework introduces 10 injection points for wire-format transforms across the client-server lifecycle. Without dedicated tests, regressions in transform ordering (e.g., double-transform on the streaming_client=false path) or missing injection points would silently produce incorrect payload bytes. Add comprehensive test coverage for all 10 injection points: Unit tests (src/extension.rs, 20 tests): - PayloadTransform::transform_inbound / transform_outbound with XOR-0xA5A5 - transform_inbound_msg / transform_outbound_msg on GenMessage - AcceptHook::on_accept with mock TCP socket - ConnectHook::on_connect with mock TCP connection - ConnectionContext helpers (transform_*_msg delegation) - File descriptor passthrough via ConnectionContext - Asymmetric transform (distinct inbound/outbound keys) - Empty payload edge case Integration tests (tests/hook_integration_unix.rs, 19 tests): - Symmetric client/server hooks with data and transform - XOR transform on the wire (end-to-end encryption) - Streaming with XOR transform (chunked encrypt/decrypt) - streaming_client=false path (server creates faked DATA) - streaming_server=false path (rejects DATA, unary only) - Multiple concurrent streams on one connection - Multiple concurrent connections with transform - Unary request timeout - Server-initiated stream close - Server shutdown during active stream - Connect hook called and receives valid raw fd - Connect hook rejection fails connection - Accept hook called on connection - Accept hook rejects connection - Connection data propagated to handler - No-hook plaintext passthrough (baseline) Also adds a unit test in src/asynchronous/client.rs verifying that Client::with_hook fails when the Socket has no raw fd, preventing a silent downgrade to an untransformed connection. Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
1 parent 1638836 commit 14655b1

3 files changed

Lines changed: 1301 additions & 9 deletions

File tree

src/extension.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@
101101
//! The fd is captured in the platform-specific `From` impls (vsock, unix, tcp)
102102
//! before the socket is type-erased into the inner `Box<dyn AsyncReadWrite>`.
103103
104+
#[cfg(all(unix, feature = "async"))]
105+
use crate::asynchronous::transport::Socket;
104106
use std::any::Any;
105107
use std::collections::HashMap;
106108
use std::fmt;
107-
use std::sync::Arc;
108109
#[cfg(unix)]
109110
use std::os::unix::io::RawFd;
110-
#[cfg(all(unix, feature = "async"))]
111-
use crate::asynchronous::transport::Socket;
111+
use std::sync::Arc;
112112

113113
/// Type-erased per-connection data store.
114114
///
@@ -372,7 +372,6 @@ impl ConnectionContext {
372372
Ok(None)
373373
}
374374

375-
376375
/// Apply inbound payload transform (if any).
377376
/// Returns the transformed payload, or the original if no transform is set.
378377
pub fn transform_inbound(&self, payload: Vec<u8>) -> Result<Vec<u8>, String> {

src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,13 @@ pub use self::proto::{Code, MessageHeader, Request, Response, Status};
6262
#[doc(inline)]
6363
pub use crate::error::{get_status, Error, Result};
6464

65-
#[doc(inline)]
66-
pub use crate::extension::{
67-
HookError, HookOutput, ConnectionContext, ConnectionData, ConnectionDataExt,
68-
PayloadTransform,
69-
};
7065
#[cfg(unix)]
7166
#[doc(inline)]
7267
pub use crate::extension::{AcceptHook, ConnectHook};
68+
#[doc(inline)]
69+
pub use crate::extension::{
70+
ConnectionContext, ConnectionData, ConnectionDataExt, HookError, HookOutput, PayloadTransform,
71+
};
7372

7473
cfg_sync! {
7574
pub mod sync;

0 commit comments

Comments
 (0)