Compare commits
25
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b61386abd6 | ||
|
|
38e912632d | ||
|
|
39cbf7b6b5 | ||
|
|
eaa37d7568 | ||
|
|
da7472e10a | ||
|
|
9e6c10c083 | ||
|
|
988a360fef | ||
|
|
c28e63e67a | ||
|
|
c0a4f7a6db | ||
|
|
5bc27ef894 | ||
|
|
4c941a7d78 | ||
|
|
b98d1001de | ||
|
|
8e4424764d | ||
|
|
1e61f4168d | ||
|
|
22cead20dc | ||
|
|
e8de15d60e | ||
|
|
0a928dcf8e | ||
|
|
e13802ccdb | ||
|
|
d81784f0f6 | ||
|
|
b1b0b62848 | ||
|
|
5067ceeb1c | ||
|
|
66abb792ce | ||
|
|
1f7df1ebc9 | ||
|
|
1eed942423 | ||
|
|
0e32c76c78 |
No files matched your search
@@ -36,7 +36,7 @@ jobs:
|
||||
# It remains here while portable actionlint runs with Gitea CI, but scans
|
||||
# both workflow directories through its GitHub Actions rule set.
|
||||
- name: Run zizmor
|
||||
uses: zizmorcore/zizmor-action@cc914d7f3750a2d13d75c7f184a1060aa0e9d482 # v0.6.4
|
||||
uses: zizmorcore/zizmor-action@85b2b9fb8e3325e7a23d7d0a9d7ba5ae0f999e3e # v0.7.1
|
||||
with:
|
||||
persona: regular
|
||||
inputs: |
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
# separate terms of service, privacy policy, and support
|
||||
# documentation.
|
||||
|
||||
# A sample workflow which sets up periodic OSV-Scanner scanning for vulnerabilities,
|
||||
# in addition to a PR check which fails if new vulnerabilities are introduced.
|
||||
# Scan all dependencies on main and compare newly introduced vulnerabilities on PRs.
|
||||
#
|
||||
# For more examples and options, including how to ignore specific vulnerabilities,
|
||||
# see https://google.github.io/osv-scanner/github-action/
|
||||
@@ -20,6 +19,7 @@ on:
|
||||
- cron: "45 8 * * 1"
|
||||
push:
|
||||
branches: ["main"]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
# Require writing security events to upload SARIF file to security tab
|
||||
@@ -29,21 +29,12 @@ permissions:
|
||||
actions: read
|
||||
|
||||
jobs:
|
||||
# Both pinned workflows default to a recursive scan of ./, including fuzz/.
|
||||
# OSV v2 excludes the root Git repository unless --include-git-root is set;
|
||||
# the old --skip-git flag is no longer supported.
|
||||
scan-scheduled:
|
||||
if: ${{ github.event_name == 'push' || github.event_name == 'schedule' }}
|
||||
uses: "google/osv-scanner-action/.github/workflows/osv-scanner-reusable.yml@a345acffa64b0eaede81a3d9aae6141214d9c8fc" # v2.6.0
|
||||
with:
|
||||
# Example of specifying custom arguments
|
||||
scan-args: |-
|
||||
-r
|
||||
--skip-git
|
||||
./
|
||||
if: ${{ github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}
|
||||
uses: "google/osv-scanner-action/.github/workflows/osv-scanner-reusable.yml@b1d267aea5211fa648d9d442fa64b90dbdcbdd36" # v2.7.0
|
||||
scan-pr:
|
||||
if: ${{ github.event_name == 'pull_request' || github.event_name == 'merge_group' }}
|
||||
uses: "google/osv-scanner-action/.github/workflows/osv-scanner-reusable-pr.yml@a345acffa64b0eaede81a3d9aae6141214d9c8fc" # v2.6.0
|
||||
with:
|
||||
# Example of specifying custom arguments
|
||||
scan-args: |-
|
||||
-r
|
||||
--skip-git
|
||||
./
|
||||
uses: "google/osv-scanner-action/.github/workflows/osv-scanner-reusable-pr.yml@b1d267aea5211fa648d9d442fa64b90dbdcbdd36" # v2.7.0
|
||||
@@ -60,7 +60,7 @@ jobs:
|
||||
exit 0
|
||||
|
||||
- name: Upload Clippy SARIF
|
||||
uses: github/codeql-action/upload-sarif@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4
|
||||
uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4
|
||||
if: always()
|
||||
with:
|
||||
sarif_file: rust-clippy-results.sarif
|
||||
|
||||
@@ -34,6 +34,37 @@ release. The format is loosely based on
|
||||
|
||||
## irtt-client
|
||||
|
||||
### 0.6.0
|
||||
|
||||
#### Added
|
||||
|
||||
- Added `ManagedClientHandle::subscribe_status()` and `ManagedStatusSubscription` for observing durable lifecycle and completion state independently of the lossy event stream. The current snapshot is immediately readable, change notifications track subsequent updates, and terminal target status includes its completed outcome.
|
||||
- Managed targets can override the shared address-family policy while retaining the original hostname for resolution on each new generation.
|
||||
|
||||
#### Changed
|
||||
|
||||
- `ClientConfig` is now reusable across endpoints: `Client::connect` and `AsyncClient::connect` take the endpoint separately, with configuration grouped into `socket`, `request` (`SessionRequest`), and `open` (`OpenPolicy`). `AddressFamily` replaces the separate socket family flags, and blocking receive timeouts are configured through `Client::set_recv_timeout`.
|
||||
- Explicit `Authentication`, `HmacKey`, and managed `TargetAuth` replace `ClientConfig.hmac_key` and `ClientAuthConfig`. Key bytes are shared across clones and redacted from debug output. Switching between inherited and explicit authentication or address-family settings starts a new target generation, even when the effective settings are equal.
|
||||
- `NegotiationResult` replaces `NegotiatedParams`, separating typed `AcceptedSessionParameters` from exact peer-returned wire parameters and accepted `NegotiationChange` records (formerly `NegotiationRestriction`). Open outcomes and lifecycle events now use tuple variants carrying `SessionStarted` or `NoTestCompleted` payloads.
|
||||
- Low-level clients now leave probe cadence and run duration entirely to the caller. `next_send_deadline` and `is_run_complete` were removed; managed clients continue to provide scheduling and finite-run completion. Low-level callers can inspect `negotiation()`, `has_pending_probes()`, and `next_probe_timeout_deadline()` to drive their own event loop.
|
||||
- Probe sends now return `Result<SendReceipt, SendProbeError>`, distinguishing failures before socket acceptance from failures after a probe was committed. Receipts convert to `ClientEvent::EchoSent`; its `scheduled_at` and `timer_error` fields are now optional and absent for low-level sends.
|
||||
- Stop receipts resolve when the stop request is durably observed or the task becomes terminal; callers must still await task completion to observe finished cleanup.
|
||||
|
||||
#### Fixed
|
||||
|
||||
- Finite managed runs now reach their negotiated end under socket backpressure, even when no pending probe timeout remains to wake the driver. Staggered pacing no longer repeatedly wakes idle or draining targets that have no send deadline.
|
||||
- Blocking receive operations now return lifecycle errors immediately when no session is open, instead of blocking for a datagram or returning empty success after a timeout.
|
||||
|
||||
#### Performance
|
||||
|
||||
- Managed drain-deadline lookup no longer scans retained timed-out probes, keeping lookup work bounded as retained probe state grows.
|
||||
|
||||
#### Compatibility
|
||||
|
||||
- The configuration, authentication, negotiation, lifecycle-event, and send APIs above are breaking changes without compatibility aliases. Low-level callers must enforce their own cadence and duration or use a managed client.
|
||||
- `AcceptedSessionParameters::dscp` is a six-bit codepoint; `NegotiationResult::peer_params.dscp` remains the raw traffic-class byte. Accepted durations and intervals use Rust duration types rather than wire nanosecond integers.
|
||||
- Tokio remains optional, and the default client build remains runtime-free. These API changes do not introduce a wire-protocol migration.
|
||||
|
||||
### 0.5.3
|
||||
|
||||
#### Added
|
||||
@@ -119,6 +150,29 @@ release. The format is loosely based on
|
||||
|
||||
## irtt-stats
|
||||
|
||||
### 0.5.2
|
||||
|
||||
#### Changed
|
||||
|
||||
- `TimeStats` is now re-exported from `measurement-stats`, retaining the existing `irtt_stats::TimeStats` import path, fields, and methods.
|
||||
- Timer-error statistics now omit sends without scheduling metadata instead of recording an artificial measurement.
|
||||
|
||||
#### Fixed
|
||||
|
||||
- Time-based rolling windows now expire backdated events correctly, including expired events inside retained arrival history. Older timestamps cannot move the expiry anchor backwards or restore expired history.
|
||||
- Rolling upstream and downstream loss now use the server receive-count increase over the retained packet-event interval instead of comparing window counts against a cumulative server count.
|
||||
- Rolling directional-loss estimates are unavailable when counter observations are missing, stale, discontinuous, or separated by an interior gap in the time window, rather than reporting misleading values.
|
||||
|
||||
#### Performance
|
||||
|
||||
- Indexed rolling-window expiry avoids full-window scans on insertion and reclaims expired entries without accumulating tombstones.
|
||||
|
||||
#### Compatibility
|
||||
|
||||
- This release consumes `irtt-client` 0.6 events; applications using both crates must upgrade them together.
|
||||
- Unavailable rolling directional-loss packet estimates are `None`; their percentage fields remain `0.0`. Valid signed estimates remain possible, and raw server receive counts retain their cumulative meaning.
|
||||
- Time-based rolling windows expire when new events arrive, retain events exactly at the cutoff, and have no event-count cap. Use `rolling_count` when event storage needs a fixed bound.
|
||||
|
||||
### 0.5.1
|
||||
|
||||
#### Added
|
||||
@@ -138,6 +192,44 @@ release. The format is loosely based on
|
||||
|
||||
## irtt-rs
|
||||
|
||||
### 0.8.0
|
||||
|
||||
#### Added
|
||||
|
||||
- Added mutually exclusive `-4` / `--ipv4`, `-6` / `--ipv6`, and `--dual-stack` options to the client and TUI. Dual-stack hostnames expand into independent `/v4` and `/v6` targets with separate sessions, statistics, and failures; explicit IP literals remain single targets.
|
||||
- Continuous TUI runs now reconnect independently after target failures, peer closure, or server-limited session completion while healthy targets continue uninterrupted. The TUI remains running even when every target fails.
|
||||
- Added target selection and a scrollable details sheet showing negotiation, counters, timing statistics, warnings, and recent events.
|
||||
- Graph axes now display local wall-clock timestamps, including fractional seconds or dates where appropriate.
|
||||
|
||||
#### Changed
|
||||
|
||||
- Replaced separate Graph and Dashboard views with one dashboard comparing all targets. Target rows show effective RTT, cumulative loss, jitter, and the age of the last primary reply. `Tab` / `Shift-Tab` selects a target; `d` opens details, with `g` retained as an alias.
|
||||
- Reconnecting targets retain their logical row and graph history, reset session statistics for each new generation, and leave gaps between generations.
|
||||
- Increased graph retention from 100,000 to 500,000 samples per target and extended the maximum graph window from one hour to 24 hours.
|
||||
- Clearing graph history now clears all targets and returns to live view while preserving latest samples and statistics.
|
||||
- Client and TUI frontends now react asynchronously to measurements, status changes, input, and shutdown, removing periodic polling delays.
|
||||
- The application library's `run_stream`, `run_tui`, and `prepare_managed_run` entry points are now asynchronous.
|
||||
|
||||
#### Performance
|
||||
|
||||
- Graph rendering selects samples within the visible viewport instead of scanning the full retained history, with interpolation at viewport boundaries.
|
||||
- Reduced graph-history memory usage per sample substantially, allowing retention to increase from 100,000 to 500,000 samples per target without proportional memory growth.
|
||||
- Increased regular TUI refresh frequency to 100 ms for smoother updates.
|
||||
|
||||
#### Compatibility
|
||||
|
||||
- Automatic reconnect applies to continuous TUI runs, not ordinary `irtt-client` runs or finite TUI runs. Explicit stops and no-test completion do not trigger retries.
|
||||
- Dual-stack expansion changes target labels and can double probe traffic per hostname. The stdin limit of 128 desired targets applies after expansion; generated labels must remain unique.
|
||||
- Address families are discovered when a declaration is added. Each new session resolves the original hostname within its assigned family; newly appearing families are not added automatically. Unchanged stdin declarations retain their discovered families, while removal and re-addition performs fresh discovery.
|
||||
- New stdin declarations are prepared asynchronously while existing measurements continue. Preparation errors stop the stream gracefully without applying a partial target set.
|
||||
- The larger graph cap permits higher eventual memory use: approximately 42 MiB per target for retained graph samples at the cap on 64-bit macOS, excluding statistics and rendering buffers.
|
||||
- Application-library callers must await client/TUI run and preparation functions and supply the new shutdown argument to the run functions.
|
||||
- `ClientArgs::prepare` and `TuiArgs::prepare` are also asynchronous; `TargetPreparation::prepare` replaces `prepare_managed_targets`, and `parse_stdin_target_set` now returns `TargetSpec` values for preparation.
|
||||
- The application's former `TargetAuth` type was replaced by `irtt_client::managed::TargetAuth`.
|
||||
- The public output wrappers `EventRenderStats` and `IpdvPair` were removed in favor of `irtt_stats::EventStatsUpdate` and `IpdvPairUpdate`.
|
||||
- `DEFAULT_RECV_TIMEOUT`, `is_shutdown_requested`, and `TuiArgs::timestamp_mode` were removed. `install_signal_handler` is now available only with the `server` feature; client/TUI embedding uses `install_async_signal_handler`.
|
||||
- MSRV remains Rust 1.88. Existing output column names and formats are preserved.
|
||||
|
||||
### 0.7.1
|
||||
|
||||
#### Fixed
|
||||
|
||||
Generated
+144
-15
@@ -17,6 +17,15 @@ version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
|
||||
|
||||
[[package]]
|
||||
name = "android_system_properties"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "1.0.0"
|
||||
@@ -199,6 +208,16 @@ dependencies = [
|
||||
"rustversion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f74872d07caf508b30a21f6836e7d7016a2eaf7d9ff4f48deaa58cd8a0407630"
|
||||
dependencies = [
|
||||
"find-msvc-tools",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.5"
|
||||
@@ -211,6 +230,17 @@ version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327"
|
||||
dependencies = [
|
||||
"iana-time-zone",
|
||||
"num-traits",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.6.7"
|
||||
@@ -292,6 +322,12 @@ dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation-sys"
|
||||
version = "0.8.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||
|
||||
[[package]]
|
||||
name = "cpufeatures"
|
||||
version = "0.2.17"
|
||||
@@ -317,6 +353,7 @@ dependencies = [
|
||||
"crossterm_winapi",
|
||||
"derive_more",
|
||||
"document-features",
|
||||
"futures-core",
|
||||
"mio",
|
||||
"parking_lot",
|
||||
"rustix",
|
||||
@@ -376,9 +413,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ctutils"
|
||||
version = "0.4.2"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7d5515a3834141de9eafb9717ad39eea8247b5674e6066c404e8c4b365d2a29e"
|
||||
checksum = "03bb0e1cc970d482d121d9a1744999169b69a07470b3d644a7894e53fcaf4574"
|
||||
dependencies = [
|
||||
"cmov",
|
||||
]
|
||||
@@ -496,9 +533,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.18.0"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34"
|
||||
checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
@@ -552,6 +589,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "find-msvc-tools"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484"
|
||||
|
||||
[[package]]
|
||||
name = "finl_unicode"
|
||||
version = "1.5.0"
|
||||
@@ -685,6 +728,30 @@ dependencies = [
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.65"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
|
||||
dependencies = [
|
||||
"android_system_properties",
|
||||
"core-foundation-sys",
|
||||
"iana-time-zone-haiku",
|
||||
"js-sys",
|
||||
"log",
|
||||
"wasm-bindgen",
|
||||
"windows-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone-haiku"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ident_case"
|
||||
version = "1.0.1"
|
||||
@@ -715,7 +782,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "irtt-client"
|
||||
version = "0.5.3"
|
||||
version = "0.6.0"
|
||||
dependencies = [
|
||||
"irtt-proto",
|
||||
"irtt-server",
|
||||
@@ -748,15 +815,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "irtt-rs"
|
||||
version = "0.7.1"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
"crossterm",
|
||||
"ctrlc",
|
||||
"futures-core",
|
||||
"irtt-client",
|
||||
"irtt-proto",
|
||||
"irtt-server",
|
||||
"irtt-stats",
|
||||
"nix 0.31.3",
|
||||
"ratatui",
|
||||
"tokio",
|
||||
]
|
||||
@@ -775,7 +845,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "irtt-stats"
|
||||
version = "0.5.1"
|
||||
version = "0.5.2"
|
||||
dependencies = [
|
||||
"irtt-client",
|
||||
"measurement-stats",
|
||||
@@ -1044,9 +1114,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "objc2"
|
||||
version = "0.6.4"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3a12a8ed07aefc768292f076dc3ac8c48f3781c8f2d5851dd3d98950e8c5a89f"
|
||||
checksum = "08849bbd4767dfae9457696856ae1c84fe4e0281bbe4a7abff2d0e06fb7981f8"
|
||||
dependencies = [
|
||||
"objc2-encode",
|
||||
]
|
||||
@@ -1242,9 +1312,9 @@ checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
|
||||
|
||||
[[package]]
|
||||
name = "powerfmt"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||
checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
@@ -1601,6 +1671,12 @@ dependencies = [
|
||||
"digest 0.10.7",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook"
|
||||
version = "0.3.18"
|
||||
@@ -2150,12 +2226,65 @@ version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.62.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
|
||||
dependencies = [
|
||||
"windows-implement",
|
||||
"windows-interface",
|
||||
"windows-link",
|
||||
"windows-result",
|
||||
"windows-strings",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-implement"
|
||||
version = "0.60.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-interface"
|
||||
version = "0.59.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-result"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-strings"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
@@ -2173,18 +2302,18 @@ checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.59"
|
||||
version = "0.8.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6df92bf3d9227be3d53173901ddbffac2babc27ae50f397776ffd6dc33f800cb"
|
||||
checksum = "879fb705ce98c32e41ebdb970fbe1204f8492423b314c6ab0354c3e7b5542866"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.59"
|
||||
version = "0.8.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac4f328cf2f05d084e496c3e9c3f33ed0a183656a16e1fcec4d464d8373aec82"
|
||||
checksum = "708882a28301d604fa039cc7727607a98d04c4b86dc76ec9cc683f805d709759"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
||||
+2
-2
@@ -18,9 +18,9 @@ homepage = "https://github.com/Lochnair/irtt-rs"
|
||||
|
||||
[workspace.dependencies]
|
||||
irtt-proto = { version = "0.5.0", path = "crates/irtt-proto" }
|
||||
irtt-client = { version = "0.5.1", path = "crates/irtt-client" }
|
||||
irtt-client = { version = "0.6.0", path = "crates/irtt-client" }
|
||||
irtt-server = { version = "0.5.2", path = "crates/irtt-server" }
|
||||
irtt-stats = { version = "0.5.0", path = "crates/irtt-stats" }
|
||||
irtt-stats = { version = "0.5.2", path = "crates/irtt-stats" }
|
||||
|
||||
[profile.release]
|
||||
opt-level = "z"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "irtt-rs"
|
||||
version = "0.7.1"
|
||||
version = "0.8.0"
|
||||
description = "IRTT-compatible client, TUI, and server applications"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
@@ -42,25 +42,30 @@ doc = false
|
||||
|
||||
[features]
|
||||
default = ["client", "server", "tui"]
|
||||
client = ["dep:irtt-client", "irtt-client/ancillary", "irtt-client/tokio", "dep:irtt-proto", "dep:irtt-stats"]
|
||||
client = ["dep:tokio", "dep:irtt-client", "irtt-client/ancillary", "irtt-client/tokio", "dep:irtt-proto", "dep:irtt-stats"]
|
||||
server = ["dep:irtt-server", "dep:tokio"]
|
||||
tui =["dep:irtt-client", "irtt-client/ancillary", "irtt-client/tokio", "dep:irtt-proto", "dep:irtt-stats", "dep:ratatui", "dep:crossterm"]
|
||||
tui =["dep:tokio", "dep:futures-core", "dep:irtt-client", "irtt-client/ancillary", "irtt-client/tokio", "dep:irtt-proto", "dep:irtt-stats", "dep:ratatui", "dep:crossterm", "dep:chrono"]
|
||||
|
||||
[dependencies]
|
||||
chrono = { version = "0.4.45", default-features = false, features = ["clock"], optional = true }
|
||||
clap = { version = "4.6.7", features = ["derive", "env"] }
|
||||
crossterm = { version = "0.29.0", optional = true }
|
||||
crossterm = { version = "0.29.0", features = ["event-stream"], optional = true }
|
||||
futures-core = { version = "0.3.34", default-features = false, optional = true }
|
||||
ctrlc = { version = "3.5.2", features = ["termination"] }
|
||||
irtt-client = { workspace = true, optional = true }
|
||||
irtt-proto = { workspace = true, optional = true }
|
||||
irtt-server = { workspace = true, optional = true }
|
||||
irtt-stats = { workspace = true, optional = true }
|
||||
ratatui = { version = "0.30.2", optional = true }
|
||||
tokio = { version = "1.53.2", default-features = false, features = ["rt", "time"], optional = true }
|
||||
tokio = { version = "1.53.2", default-features = false, features = ["rt", "net", "time", "sync", "macros"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
irtt-server.workspace = true
|
||||
tokio = { version = "1.53.2", default-features = false, features = ["rt", "sync"] }
|
||||
|
||||
[target.'cfg(unix)'.dev-dependencies]
|
||||
nix = { version = "0.31.3", features = ["term", "poll", "signal", "process"] }
|
||||
|
||||
[profile.release]
|
||||
opt-level = "z"
|
||||
lto = true
|
||||
|
||||
@@ -92,7 +92,7 @@ impl ClientArgs {
|
||||
///
|
||||
/// Targets are only required here. `--list-columns` returns before this is
|
||||
/// called, so listing columns still needs no target.
|
||||
pub fn prepare(&self) -> Result<ManagedRunSetup, String> {
|
||||
pub async fn prepare(&self) -> Result<ManagedRunSetup, String> {
|
||||
if self.targets_stdin && !self.is_continuous() {
|
||||
return Err("--targets-stdin requires --duration 0".to_owned());
|
||||
}
|
||||
@@ -105,6 +105,7 @@ impl ClientArgs {
|
||||
stdin_controlled: self.targets_stdin,
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn is_continuous(&self) -> bool {
|
||||
|
||||
@@ -9,6 +9,8 @@ use irtt_client::{
|
||||
ServerTiming, SignedDuration, WarningKind,
|
||||
};
|
||||
|
||||
use irtt_stats::{EventStatsUpdate, IpdvPairUpdate};
|
||||
|
||||
use super::args::{HeaderMode, OutputFormat};
|
||||
use crate::cmd::format::{format_duration, format_signed_duration, ABSENT};
|
||||
|
||||
@@ -94,7 +96,7 @@ impl OutputConfig {
|
||||
&self,
|
||||
event: &ClientEvent,
|
||||
target: Option<&str>,
|
||||
stats: Option<&EventRenderStats>,
|
||||
stats: Option<&EventStatsUpdate>,
|
||||
) -> Option<String> {
|
||||
let row = OutputRow::from_event(event);
|
||||
if self.default_table_rows && row.is_default_table_hidden() {
|
||||
@@ -133,10 +135,12 @@ impl OutputConfig {
|
||||
}
|
||||
writeln!(out).unwrap();
|
||||
writeln!(out, "Aliases:").unwrap();
|
||||
writeln!(out, " receive_delay, receive_delay_us").unwrap();
|
||||
writeln!(out, " send_delay, send_delay_us").unwrap();
|
||||
writeln!(out, " server_processing").unwrap();
|
||||
writeln!(out, " server_received_count, server_received_window").unwrap();
|
||||
for column in ALL_COLUMNS {
|
||||
let aliases = column.aliases();
|
||||
if !aliases.is_empty() {
|
||||
writeln!(out, " {} ({})", aliases.join(", "), column.name()).unwrap();
|
||||
}
|
||||
}
|
||||
writeln!(out).unwrap();
|
||||
writeln!(out, "Special column sets:").unwrap();
|
||||
writeln!(
|
||||
@@ -149,45 +153,9 @@ impl OutputConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||
pub struct EventRenderStats {
|
||||
pub contributed_sample: bool,
|
||||
pub ipdv_pairs: Vec<IpdvPair>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct IpdvPair {
|
||||
pub previous_seq: u32,
|
||||
pub current_seq: u32,
|
||||
pub rtt_ipdv: Duration,
|
||||
pub send_ipdv: Option<Duration>,
|
||||
pub receive_ipdv: Option<Duration>,
|
||||
}
|
||||
|
||||
impl From<irtt_stats::EventStatsUpdate> for EventRenderStats {
|
||||
fn from(value: irtt_stats::EventStatsUpdate) -> Self {
|
||||
Self {
|
||||
contributed_sample: value.contributed_sample,
|
||||
ipdv_pairs: value.ipdv_pairs.into_iter().map(Into::into).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<irtt_stats::IpdvPairUpdate> for IpdvPair {
|
||||
fn from(value: irtt_stats::IpdvPairUpdate) -> Self {
|
||||
Self {
|
||||
previous_seq: value.previous_seq,
|
||||
current_seq: value.current_seq,
|
||||
rtt_ipdv: value.rtt_ipdv,
|
||||
send_ipdv: value.send_ipdv,
|
||||
receive_ipdv: value.receive_ipdv,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct RenderContext<'a> {
|
||||
stats: Option<&'a EventRenderStats>,
|
||||
stats: Option<&'a EventStatsUpdate>,
|
||||
target: Option<&'a str>,
|
||||
verbose: bool,
|
||||
}
|
||||
@@ -368,6 +336,106 @@ impl OutputRow {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn seq(&self) -> Option<u32> {
|
||||
match self {
|
||||
Self::EchoSent { seq, .. } | Self::Loss { seq, .. } | Self::Duplicate { seq, .. } => {
|
||||
Some(*seq)
|
||||
}
|
||||
Self::EchoReply(reply) | Self::Late { reply, .. } => Some(reply.seq),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn remote(&self) -> Option<SocketAddr> {
|
||||
match self {
|
||||
Self::SessionStarted(row) | Self::NoTestCompleted(row) => Some(row.remote),
|
||||
Self::SessionClosed { remote, .. }
|
||||
| Self::EchoSent { remote, .. }
|
||||
| Self::Duplicate { remote, .. } => Some(*remote),
|
||||
Self::EchoReply(reply) | Self::Late { reply, .. } => Some(reply.remote),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn token(&self) -> Option<u64> {
|
||||
match self {
|
||||
Self::SessionStarted(row) | Self::NoTestCompleted(row) => row.token,
|
||||
Self::SessionClosed { token, .. } => Some(*token),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes(&self) -> Option<usize> {
|
||||
match self {
|
||||
Self::EchoSent { bytes, .. } | Self::Duplicate { bytes, .. } => Some(*bytes),
|
||||
Self::EchoReply(reply) | Self::Late { reply, .. } => Some(reply.bytes),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn message(&self, context: RenderContext<'_>) -> Option<String> {
|
||||
match self {
|
||||
Self::SessionStarted(row) => Some(format!(
|
||||
"token={:#x} duration_ns={} interval_ns={} length={}",
|
||||
row.token?, row.duration_ns, row.interval_ns, row.payload_length
|
||||
)),
|
||||
Self::NoTestCompleted(row) => Some(format!(
|
||||
"duration_ns={} interval_ns={} length={}",
|
||||
row.duration_ns, row.interval_ns, row.payload_length
|
||||
)),
|
||||
Self::SessionClosed { token, .. } => Some(format!("token={token:#x}")),
|
||||
Self::Late { highest_seen, .. } => Some(format!("highest_seen={highest_seen}")),
|
||||
Self::Warning { message, .. } => Some(message.clone()),
|
||||
Self::Loss { .. } => Some("timeout".to_owned()),
|
||||
Self::Duplicate { .. } if context.verbose => Some("duplicate reply".to_owned()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn event_wall(&self) -> Option<SystemTime> {
|
||||
match self {
|
||||
Self::SessionStarted(row) | Self::NoTestCompleted(row) => Some(row.event_wall),
|
||||
Self::SessionClosed { event_wall, .. } | Self::Warning { event_wall, .. } => {
|
||||
Some(*event_wall)
|
||||
}
|
||||
Self::EchoSent {
|
||||
client_send_wall, ..
|
||||
}
|
||||
| Self::Loss {
|
||||
client_send_wall, ..
|
||||
} => Some(*client_send_wall),
|
||||
Self::Duplicate {
|
||||
client_receive_wall,
|
||||
..
|
||||
} => Some(*client_receive_wall),
|
||||
Self::EchoReply(reply) | Self::Late { reply, .. } => Some(reply.client_receive_wall),
|
||||
}
|
||||
}
|
||||
|
||||
fn client_send_wall(&self) -> Option<SystemTime> {
|
||||
match self {
|
||||
Self::EchoSent {
|
||||
client_send_wall, ..
|
||||
}
|
||||
| Self::Loss {
|
||||
client_send_wall, ..
|
||||
} => Some(*client_send_wall),
|
||||
Self::EchoReply(reply) | Self::Late { reply, .. } => reply.client_send_wall,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn client_receive_wall(&self) -> Option<SystemTime> {
|
||||
match self {
|
||||
Self::Duplicate {
|
||||
client_receive_wall,
|
||||
..
|
||||
} => Some(*client_receive_wall),
|
||||
Self::EchoReply(reply) | Self::Late { reply, .. } => Some(reply.client_receive_wall),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -415,274 +483,91 @@ struct ReplyRow {
|
||||
packet_meta: PacketMeta,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Column {
|
||||
Target,
|
||||
Event,
|
||||
Seq,
|
||||
Remote,
|
||||
Token,
|
||||
Rtt,
|
||||
RttUs,
|
||||
RawRttUs,
|
||||
EffectiveRttUs,
|
||||
AdjustedRttUs,
|
||||
ReceiveDelay,
|
||||
ReceiveDelayUs,
|
||||
SendDelay,
|
||||
SendDelayUs,
|
||||
Ipdv,
|
||||
IpdvUs,
|
||||
ServerProcessing,
|
||||
ServerProcessingUs,
|
||||
Bytes,
|
||||
SendCallUs,
|
||||
TimerErrorUs,
|
||||
HighestSeen,
|
||||
ServerReceivedCount,
|
||||
ServerReceivedWindow,
|
||||
Dscp,
|
||||
Ecn,
|
||||
TrafficClass,
|
||||
KernelRxNs,
|
||||
WarningKind,
|
||||
Message,
|
||||
EventWallNs,
|
||||
ClientSendWallNs,
|
||||
ClientReceiveWallNs,
|
||||
DurationNs,
|
||||
IntervalNs,
|
||||
PayloadLength,
|
||||
ServerReceiveWallNs,
|
||||
ServerReceiveMonoNs,
|
||||
ServerSendWallNs,
|
||||
ServerSendMonoNs,
|
||||
ServerMidpointWallNs,
|
||||
ServerMidpointMonoNs,
|
||||
// Only schema metadata is generated here; cell_for keeps rendering behavior explicit.
|
||||
macro_rules! columns {
|
||||
($($variant:ident($name:literal, [$($alias:literal),*], $width:expr, $align:ident, $description:literal);)*) => {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Column { $($variant),* }
|
||||
|
||||
const ALL_COLUMNS: &[Column] = &[$(Column::$variant),*];
|
||||
|
||||
impl Column {
|
||||
fn name(self) -> &'static str {
|
||||
match self { $(Self::$variant => $name),* }
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> Option<Self> {
|
||||
match input {
|
||||
$($name $(| $alias)* => Some(Self::$variant),)*
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn aliases(self) -> &'static [&'static str] {
|
||||
match self { $(Self::$variant => &[$($alias),*]),* }
|
||||
}
|
||||
|
||||
fn description(self) -> &'static str {
|
||||
match self { $(Self::$variant => $description),* }
|
||||
}
|
||||
|
||||
fn table_width(self) -> usize {
|
||||
match self { $(Self::$variant => $width),* }
|
||||
}
|
||||
|
||||
fn align_right(self) -> bool {
|
||||
match self { $(Self::$variant => columns!(@align $align)),* }
|
||||
}
|
||||
}
|
||||
};
|
||||
(@align left) => { false };
|
||||
(@align right) => { true };
|
||||
}
|
||||
|
||||
const ALL_COLUMNS: &[Column] = &[
|
||||
Column::Target,
|
||||
Column::Event,
|
||||
Column::Seq,
|
||||
Column::Remote,
|
||||
Column::Token,
|
||||
Column::Rtt,
|
||||
Column::RttUs,
|
||||
Column::RawRttUs,
|
||||
Column::EffectiveRttUs,
|
||||
Column::AdjustedRttUs,
|
||||
Column::ReceiveDelay,
|
||||
Column::ReceiveDelayUs,
|
||||
Column::SendDelay,
|
||||
Column::SendDelayUs,
|
||||
Column::Ipdv,
|
||||
Column::IpdvUs,
|
||||
Column::ServerProcessing,
|
||||
Column::ServerProcessingUs,
|
||||
Column::Bytes,
|
||||
Column::SendCallUs,
|
||||
Column::TimerErrorUs,
|
||||
Column::HighestSeen,
|
||||
Column::ServerReceivedCount,
|
||||
Column::ServerReceivedWindow,
|
||||
Column::Dscp,
|
||||
Column::Ecn,
|
||||
Column::TrafficClass,
|
||||
Column::KernelRxNs,
|
||||
Column::WarningKind,
|
||||
Column::Message,
|
||||
Column::EventWallNs,
|
||||
Column::ClientSendWallNs,
|
||||
Column::ClientReceiveWallNs,
|
||||
Column::DurationNs,
|
||||
Column::IntervalNs,
|
||||
Column::PayloadLength,
|
||||
Column::ServerReceiveWallNs,
|
||||
Column::ServerReceiveMonoNs,
|
||||
Column::ServerSendWallNs,
|
||||
Column::ServerSendMonoNs,
|
||||
Column::ServerMidpointWallNs,
|
||||
Column::ServerMidpointMonoNs,
|
||||
];
|
||||
|
||||
impl Column {
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Target => "target",
|
||||
Self::Event => "event",
|
||||
Self::Seq => "seq",
|
||||
Self::Remote => "remote",
|
||||
Self::Token => "token",
|
||||
Self::Rtt => "rtt",
|
||||
Self::RttUs => "rtt_us",
|
||||
Self::RawRttUs => "raw_rtt_us",
|
||||
Self::EffectiveRttUs => "effective_rtt_us",
|
||||
Self::AdjustedRttUs => "adjusted_rtt_us",
|
||||
Self::ReceiveDelay => "rd",
|
||||
Self::ReceiveDelayUs => "rd_us",
|
||||
Self::SendDelay => "sd",
|
||||
Self::SendDelayUs => "sd_us",
|
||||
Self::Ipdv => "ipdv",
|
||||
Self::IpdvUs => "ipdv_us",
|
||||
Self::ServerProcessing => "proc",
|
||||
Self::ServerProcessingUs => "server_processing_us",
|
||||
Self::Bytes => "bytes",
|
||||
Self::SendCallUs => "send_call_us",
|
||||
Self::TimerErrorUs => "timer_error_us",
|
||||
Self::HighestSeen => "highest_seen",
|
||||
Self::ServerReceivedCount => "server_received",
|
||||
Self::ServerReceivedWindow => "server_window",
|
||||
Self::Dscp => "dscp",
|
||||
Self::Ecn => "ecn",
|
||||
Self::TrafficClass => "traffic_class",
|
||||
Self::KernelRxNs => "kernel_rx_ns",
|
||||
Self::WarningKind => "warning_kind",
|
||||
Self::Message => "message",
|
||||
Self::EventWallNs => "event_wall_ns",
|
||||
Self::ClientSendWallNs => "client_send_wall_ns",
|
||||
Self::ClientReceiveWallNs => "client_receive_wall_ns",
|
||||
Self::DurationNs => "duration_ns",
|
||||
Self::IntervalNs => "interval_ns",
|
||||
Self::PayloadLength => "payload_length",
|
||||
Self::ServerReceiveWallNs => "server_receive_wall_ns",
|
||||
Self::ServerReceiveMonoNs => "server_receive_mono_ns",
|
||||
Self::ServerSendWallNs => "server_send_wall_ns",
|
||||
Self::ServerSendMonoNs => "server_send_mono_ns",
|
||||
Self::ServerMidpointWallNs => "server_midpoint_wall_ns",
|
||||
Self::ServerMidpointMonoNs => "server_midpoint_mono_ns",
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> Option<Self> {
|
||||
Some(match input {
|
||||
"target" => Self::Target,
|
||||
"event" => Self::Event,
|
||||
"seq" => Self::Seq,
|
||||
"remote" => Self::Remote,
|
||||
"token" => Self::Token,
|
||||
"rtt" => Self::Rtt,
|
||||
"rtt_us" => Self::RttUs,
|
||||
"raw_rtt_us" => Self::RawRttUs,
|
||||
"effective_rtt_us" => Self::EffectiveRttUs,
|
||||
"adjusted_rtt_us" => Self::AdjustedRttUs,
|
||||
"rd" | "receive_delay" => Self::ReceiveDelay,
|
||||
"rd_us" | "receive_delay_us" => Self::ReceiveDelayUs,
|
||||
"sd" | "send_delay" => Self::SendDelay,
|
||||
"sd_us" | "send_delay_us" => Self::SendDelayUs,
|
||||
"ipdv" => Self::Ipdv,
|
||||
"ipdv_us" => Self::IpdvUs,
|
||||
"proc" | "server_processing" => Self::ServerProcessing,
|
||||
"server_processing_us" => Self::ServerProcessingUs,
|
||||
"bytes" => Self::Bytes,
|
||||
"send_call_us" => Self::SendCallUs,
|
||||
"timer_error_us" => Self::TimerErrorUs,
|
||||
"highest_seen" => Self::HighestSeen,
|
||||
"server_received" | "server_received_count" => Self::ServerReceivedCount,
|
||||
"server_window" | "server_received_window" => Self::ServerReceivedWindow,
|
||||
"dscp" => Self::Dscp,
|
||||
"ecn" => Self::Ecn,
|
||||
"traffic_class" => Self::TrafficClass,
|
||||
"kernel_rx_ns" => Self::KernelRxNs,
|
||||
"warning_kind" => Self::WarningKind,
|
||||
"message" => Self::Message,
|
||||
"event_wall_ns" => Self::EventWallNs,
|
||||
"client_send_wall_ns" => Self::ClientSendWallNs,
|
||||
"client_receive_wall_ns" => Self::ClientReceiveWallNs,
|
||||
"duration_ns" => Self::DurationNs,
|
||||
"interval_ns" => Self::IntervalNs,
|
||||
"payload_length" => Self::PayloadLength,
|
||||
"server_receive_wall_ns" => Self::ServerReceiveWallNs,
|
||||
"server_receive_mono_ns" => Self::ServerReceiveMonoNs,
|
||||
"server_send_wall_ns" => Self::ServerSendWallNs,
|
||||
"server_send_mono_ns" => Self::ServerSendMonoNs,
|
||||
"server_midpoint_wall_ns" => Self::ServerMidpointWallNs,
|
||||
"server_midpoint_mono_ns" => Self::ServerMidpointMonoNs,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn description(self) -> &'static str {
|
||||
match self {
|
||||
Self::Target => "logical CLI target label",
|
||||
Self::Event => "event kind",
|
||||
Self::Seq => "probe sequence number",
|
||||
Self::Remote => "remote socket address",
|
||||
Self::Token => "session token as hexadecimal",
|
||||
Self::Rtt => "human-readable effective RTT",
|
||||
Self::RttUs => "effective RTT in signed microseconds",
|
||||
Self::RawRttUs => "raw client send-to-receive RTT in microseconds",
|
||||
Self::EffectiveRttUs => "effective RTT in signed microseconds",
|
||||
Self::AdjustedRttUs => "adjusted RTT in signed microseconds",
|
||||
Self::ReceiveDelay => "human-readable server-to-client delay",
|
||||
Self::ReceiveDelayUs => "server-to-client delay in signed microseconds",
|
||||
Self::SendDelay => "human-readable client-to-server delay",
|
||||
Self::SendDelayUs => "client-to-server delay in signed microseconds",
|
||||
Self::Ipdv => "human-readable round-trip IPDV for adjacent samples",
|
||||
Self::IpdvUs => "round-trip IPDV in microseconds",
|
||||
Self::ServerProcessing => "human-readable server processing time",
|
||||
Self::ServerProcessingUs => "server processing time in microseconds",
|
||||
Self::Bytes => "packet bytes for packet events",
|
||||
Self::SendCallUs => "send system call duration in microseconds",
|
||||
Self::TimerErrorUs => "scheduled-vs-actual send timer error in microseconds",
|
||||
Self::HighestSeen => "highest sequence seen when a late reply arrived",
|
||||
Self::ServerReceivedCount => "server-reported received packet count",
|
||||
Self::ServerReceivedWindow => "server-reported received window as hexadecimal",
|
||||
Self::Dscp => "received packet DSCP codepoint",
|
||||
Self::Ecn => "received packet ECN bits",
|
||||
Self::TrafficClass => "received packet traffic class byte",
|
||||
Self::KernelRxNs => "kernel receive timestamp as Unix nanoseconds",
|
||||
Self::WarningKind => "warning classifier",
|
||||
Self::Message => "warning or lifecycle message",
|
||||
Self::EventWallNs => "event wall timestamp as Unix nanoseconds",
|
||||
Self::ClientSendWallNs => "client send wall timestamp as Unix nanoseconds",
|
||||
Self::ClientReceiveWallNs => "client receive wall timestamp as Unix nanoseconds",
|
||||
Self::DurationNs => "negotiated test duration in nanoseconds",
|
||||
Self::IntervalNs => "negotiated probe interval in nanoseconds",
|
||||
Self::PayloadLength => "negotiated payload length",
|
||||
Self::ServerReceiveWallNs => "server receive wall timestamp in nanoseconds",
|
||||
Self::ServerReceiveMonoNs => "server receive monotonic timestamp in nanoseconds",
|
||||
Self::ServerSendWallNs => "server send wall timestamp in nanoseconds",
|
||||
Self::ServerSendMonoNs => "server send monotonic timestamp in nanoseconds",
|
||||
Self::ServerMidpointWallNs => "server midpoint wall timestamp in nanoseconds",
|
||||
Self::ServerMidpointMonoNs => "server midpoint monotonic timestamp in nanoseconds",
|
||||
}
|
||||
}
|
||||
|
||||
fn table_width(self) -> usize {
|
||||
match self {
|
||||
Self::Target => 18,
|
||||
Self::Event => 17,
|
||||
Self::Seq => 6,
|
||||
Self::Remote => 21,
|
||||
Self::Token => 18,
|
||||
Self::Rtt
|
||||
| Self::ReceiveDelay
|
||||
| Self::SendDelay
|
||||
| Self::Ipdv
|
||||
| Self::ServerProcessing => 9,
|
||||
Self::Message => 24,
|
||||
Self::WarningKind => 28,
|
||||
Self::ServerReceivedWindow => 13,
|
||||
Self::ServerReceivedCount | Self::HighestSeen => 15,
|
||||
Self::Dscp | Self::Ecn => 4,
|
||||
Self::TrafficClass => 13,
|
||||
_ => self.name().len().max(10),
|
||||
}
|
||||
}
|
||||
|
||||
fn align_right(self) -> bool {
|
||||
!matches!(
|
||||
self,
|
||||
Self::Target
|
||||
| Self::Event
|
||||
| Self::Remote
|
||||
| Self::Token
|
||||
| Self::Message
|
||||
| Self::WarningKind
|
||||
| Self::ServerReceivedWindow
|
||||
)
|
||||
}
|
||||
// Variant(canonical name, aliases, table width, alignment, description).
|
||||
columns! {
|
||||
Target("target", [], 18, left, "logical CLI target label");
|
||||
Event("event", [], 17, left, "event kind");
|
||||
Seq("seq", [], 6, right, "probe sequence number");
|
||||
Remote("remote", [], 21, left, "remote socket address");
|
||||
Token("token", [], 18, left, "session token as hexadecimal");
|
||||
Rtt("rtt", [], 9, right, "human-readable effective RTT");
|
||||
RttUs("rtt_us", [], 10, right, "effective RTT in signed microseconds");
|
||||
RawRttUs("raw_rtt_us", [], 10, right, "raw client send-to-receive RTT in microseconds");
|
||||
EffectiveRttUs("effective_rtt_us", [], 16, right, "effective RTT in signed microseconds");
|
||||
AdjustedRttUs("adjusted_rtt_us", [], 15, right, "adjusted RTT in signed microseconds");
|
||||
ReceiveDelay("rd", ["receive_delay"], 9, right, "human-readable server-to-client delay");
|
||||
ReceiveDelayUs("rd_us", ["receive_delay_us"], 10, right, "server-to-client delay in signed microseconds");
|
||||
SendDelay("sd", ["send_delay"], 9, right, "human-readable client-to-server delay");
|
||||
SendDelayUs("sd_us", ["send_delay_us"], 10, right, "client-to-server delay in signed microseconds");
|
||||
Ipdv("ipdv", [], 9, right, "human-readable round-trip IPDV for adjacent samples");
|
||||
IpdvUs("ipdv_us", [], 10, right, "round-trip IPDV in microseconds");
|
||||
ServerProcessing("proc", ["server_processing"], 9, right, "human-readable server processing time");
|
||||
ServerProcessingUs("server_processing_us", [], 20, right, "server processing time in microseconds");
|
||||
Bytes("bytes", [], 10, right, "packet bytes for packet events");
|
||||
SendCallUs("send_call_us", [], 12, right, "send system call duration in microseconds");
|
||||
TimerErrorUs("timer_error_us", [], 14, right, "scheduled-vs-actual send timer error in microseconds");
|
||||
HighestSeen("highest_seen", [], 15, right, "highest sequence seen when a late reply arrived");
|
||||
ServerReceivedCount("server_received", ["server_received_count"], 15, right, "server-reported received packet count");
|
||||
ServerReceivedWindow("server_window", ["server_received_window"], 13, left, "server-reported received window as hexadecimal");
|
||||
Dscp("dscp", [], 4, right, "received packet DSCP codepoint");
|
||||
Ecn("ecn", [], 4, right, "received packet ECN bits");
|
||||
TrafficClass("traffic_class", [], 13, right, "received packet traffic class byte");
|
||||
KernelRxNs("kernel_rx_ns", [], 12, right, "kernel receive timestamp as Unix nanoseconds");
|
||||
WarningKind("warning_kind", [], 28, left, "warning classifier");
|
||||
Message("message", [], 24, left, "warning or lifecycle message");
|
||||
EventWallNs("event_wall_ns", [], 13, right, "event wall timestamp as Unix nanoseconds");
|
||||
ClientSendWallNs("client_send_wall_ns", [], 19, right, "client send wall timestamp as Unix nanoseconds");
|
||||
ClientReceiveWallNs("client_receive_wall_ns", [], 22, right, "client receive wall timestamp as Unix nanoseconds");
|
||||
DurationNs("duration_ns", [], 11, right, "negotiated test duration in nanoseconds");
|
||||
IntervalNs("interval_ns", [], 11, right, "negotiated probe interval in nanoseconds");
|
||||
PayloadLength("payload_length", [], 14, right, "negotiated payload length");
|
||||
ServerReceiveWallNs("server_receive_wall_ns", [], 22, right, "server receive wall timestamp in nanoseconds");
|
||||
ServerReceiveMonoNs("server_receive_mono_ns", [], 22, right, "server receive monotonic timestamp in nanoseconds");
|
||||
ServerSendWallNs("server_send_wall_ns", [], 19, right, "server send wall timestamp in nanoseconds");
|
||||
ServerSendMonoNs("server_send_mono_ns", [], 19, right, "server send monotonic timestamp in nanoseconds");
|
||||
ServerMidpointWallNs("server_midpoint_wall_ns", [], 23, right, "server midpoint wall timestamp in nanoseconds");
|
||||
ServerMidpointMonoNs("server_midpoint_mono_ns", [], 23, right, "server midpoint monotonic timestamp in nanoseconds");
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -890,9 +775,11 @@ fn cell_for(row: &OutputRow, column: Column, context: RenderContext<'_>) -> Opti
|
||||
match column {
|
||||
Column::Target => context.target.map(CellValue::text),
|
||||
Column::Event => Some(CellValue::text(row.event_name())),
|
||||
Column::Seq => row_seq(row).map(|seq| CellValue::Unsigned(u128::from(seq))),
|
||||
Column::Remote => row_remote(row).map(|remote| CellValue::text(remote.to_string())),
|
||||
Column::Token => row_token(row).map(|token| CellValue::Hex(u128::from(token))),
|
||||
Column::Seq => row.seq().map(|seq| CellValue::Unsigned(u128::from(seq))),
|
||||
Column::Remote => row
|
||||
.remote()
|
||||
.map(|remote| CellValue::text(remote.to_string())),
|
||||
Column::Token => row.token().map(|token| CellValue::Hex(u128::from(token))),
|
||||
Column::Rtt => row.reply().and_then(|reply| {
|
||||
reply
|
||||
.rtt
|
||||
@@ -930,10 +817,12 @@ fn cell_for(row: &OutputRow, column: Column, context: RenderContext<'_>) -> Opti
|
||||
.reply()
|
||||
.and_then(|reply| reply.one_way.and_then(|one_way| one_way.client_to_server))
|
||||
.map(|value| CellValue::Integer(signed_duration_us(value))),
|
||||
Column::Ipdv => row_seq(row)
|
||||
Column::Ipdv => row
|
||||
.seq()
|
||||
.and_then(|seq| ipdv_pair(context.stats, seq))
|
||||
.map(|pair| CellValue::text(format_duration(pair.rtt_ipdv))),
|
||||
Column::IpdvUs => row_seq(row)
|
||||
Column::IpdvUs => row
|
||||
.seq()
|
||||
.and_then(|seq| ipdv_pair(context.stats, seq))
|
||||
.map(|pair| CellValue::Unsigned(duration_us(pair.rtt_ipdv))),
|
||||
Column::ServerProcessing => row
|
||||
@@ -944,7 +833,7 @@ fn cell_for(row: &OutputRow, column: Column, context: RenderContext<'_>) -> Opti
|
||||
.reply()
|
||||
.and_then(|reply| reply.server_timing.and_then(|timing| timing.processing))
|
||||
.map(|value| CellValue::Unsigned(duration_us(value))),
|
||||
Column::Bytes => row_bytes(row).map(|bytes| CellValue::Unsigned(bytes as u128)),
|
||||
Column::Bytes => row.bytes().map(|bytes| CellValue::Unsigned(bytes as u128)),
|
||||
Column::SendCallUs => match row {
|
||||
OutputRow::EchoSent { send_call, .. } => {
|
||||
Some(CellValue::Unsigned(duration_us(*send_call)))
|
||||
@@ -992,14 +881,17 @@ fn cell_for(row: &OutputRow, column: Column, context: RenderContext<'_>) -> Opti
|
||||
OutputRow::Warning { kind, .. } => Some(CellValue::text(warning_kind(*kind))),
|
||||
_ => None,
|
||||
},
|
||||
Column::Message => row_message(row, context).map(CellValue::text),
|
||||
Column::EventWallNs => row_event_wall(row)
|
||||
Column::Message => row.message(context).map(CellValue::text),
|
||||
Column::EventWallNs => row
|
||||
.event_wall()
|
||||
.and_then(wall_time_ns)
|
||||
.map(CellValue::Unsigned),
|
||||
Column::ClientSendWallNs => row_client_send_wall(row)
|
||||
Column::ClientSendWallNs => row
|
||||
.client_send_wall()
|
||||
.and_then(wall_time_ns)
|
||||
.map(CellValue::Unsigned),
|
||||
Column::ClientReceiveWallNs => row_client_receive_wall(row)
|
||||
Column::ClientReceiveWallNs => row
|
||||
.client_receive_wall()
|
||||
.and_then(wall_time_ns)
|
||||
.map(CellValue::Unsigned),
|
||||
Column::DurationNs => match row {
|
||||
@@ -1029,110 +921,6 @@ fn cell_for(row: &OutputRow, column: Column, context: RenderContext<'_>) -> Opti
|
||||
}
|
||||
}
|
||||
|
||||
fn row_seq(row: &OutputRow) -> Option<u32> {
|
||||
match row {
|
||||
OutputRow::EchoSent { seq, .. }
|
||||
| OutputRow::Loss { seq, .. }
|
||||
| OutputRow::Duplicate { seq, .. } => Some(*seq),
|
||||
OutputRow::EchoReply(reply) | OutputRow::Late { reply, .. } => Some(reply.seq),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn row_remote(row: &OutputRow) -> Option<SocketAddr> {
|
||||
match row {
|
||||
OutputRow::SessionStarted(row) | OutputRow::NoTestCompleted(row) => Some(row.remote),
|
||||
OutputRow::SessionClosed { remote, .. }
|
||||
| OutputRow::EchoSent { remote, .. }
|
||||
| OutputRow::Duplicate { remote, .. } => Some(*remote),
|
||||
OutputRow::EchoReply(reply) | OutputRow::Late { reply, .. } => Some(reply.remote),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn row_token(row: &OutputRow) -> Option<u64> {
|
||||
match row {
|
||||
OutputRow::SessionStarted(row) | OutputRow::NoTestCompleted(row) => row.token,
|
||||
OutputRow::SessionClosed { token, .. } => Some(*token),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn row_bytes(row: &OutputRow) -> Option<usize> {
|
||||
match row {
|
||||
OutputRow::EchoSent { bytes, .. } | OutputRow::Duplicate { bytes, .. } => Some(*bytes),
|
||||
OutputRow::EchoReply(reply) | OutputRow::Late { reply, .. } => Some(reply.bytes),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn row_message(row: &OutputRow, context: RenderContext<'_>) -> Option<String> {
|
||||
match row {
|
||||
OutputRow::SessionStarted(row) => Some(format!(
|
||||
"token={:#x} duration_ns={} interval_ns={} length={}",
|
||||
row.token?, row.duration_ns, row.interval_ns, row.payload_length
|
||||
)),
|
||||
OutputRow::NoTestCompleted(row) => Some(format!(
|
||||
"duration_ns={} interval_ns={} length={}",
|
||||
row.duration_ns, row.interval_ns, row.payload_length
|
||||
)),
|
||||
OutputRow::SessionClosed { token, .. } => Some(format!("token={token:#x}")),
|
||||
OutputRow::Late { highest_seen, .. } => Some(format!("highest_seen={highest_seen}")),
|
||||
OutputRow::Warning { message, .. } => Some(message.clone()),
|
||||
OutputRow::Loss { .. } => Some("timeout".to_owned()),
|
||||
OutputRow::Duplicate { .. } if context.verbose => Some("duplicate reply".to_owned()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn row_event_wall(row: &OutputRow) -> Option<SystemTime> {
|
||||
match row {
|
||||
OutputRow::SessionStarted(row) | OutputRow::NoTestCompleted(row) => Some(row.event_wall),
|
||||
OutputRow::SessionClosed { event_wall, .. } | OutputRow::Warning { event_wall, .. } => {
|
||||
Some(*event_wall)
|
||||
}
|
||||
OutputRow::EchoSent {
|
||||
client_send_wall, ..
|
||||
}
|
||||
| OutputRow::Loss {
|
||||
client_send_wall, ..
|
||||
} => Some(*client_send_wall),
|
||||
OutputRow::Duplicate {
|
||||
client_receive_wall,
|
||||
..
|
||||
} => Some(*client_receive_wall),
|
||||
OutputRow::EchoReply(reply) | OutputRow::Late { reply, .. } => {
|
||||
Some(reply.client_receive_wall)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn row_client_send_wall(row: &OutputRow) -> Option<SystemTime> {
|
||||
match row {
|
||||
OutputRow::EchoSent {
|
||||
client_send_wall, ..
|
||||
}
|
||||
| OutputRow::Loss {
|
||||
client_send_wall, ..
|
||||
} => Some(*client_send_wall),
|
||||
OutputRow::EchoReply(reply) | OutputRow::Late { reply, .. } => reply.client_send_wall,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn row_client_receive_wall(row: &OutputRow) -> Option<SystemTime> {
|
||||
match row {
|
||||
OutputRow::Duplicate {
|
||||
client_receive_wall,
|
||||
..
|
||||
} => Some(*client_receive_wall),
|
||||
OutputRow::EchoReply(reply) | OutputRow::Late { reply, .. } => {
|
||||
Some(reply.client_receive_wall)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn server_timing_i64(
|
||||
row: &OutputRow,
|
||||
select: impl FnOnce(ServerTiming) -> Option<i64>,
|
||||
@@ -1143,7 +931,7 @@ fn server_timing_i64(
|
||||
.map(|value| CellValue::Integer(i128::from(value)))
|
||||
}
|
||||
|
||||
fn ipdv_pair(stats: Option<&EventRenderStats>, seq: u32) -> Option<&IpdvPair> {
|
||||
fn ipdv_pair(stats: Option<&EventStatsUpdate>, seq: u32) -> Option<&IpdvPairUpdate> {
|
||||
let stats = stats?;
|
||||
stats
|
||||
.ipdv_pairs
|
||||
|
||||
@@ -1,35 +1,31 @@
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet, HashSet, VecDeque},
|
||||
io::{self, BufRead, Write},
|
||||
sync::{atomic::AtomicBool, Arc, Mutex},
|
||||
sync::Arc,
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use irtt_client::{
|
||||
managed::{
|
||||
BlockingManagedClient, ManagedClientHandle, ManagedCommandApplyError, ManagedEndReason,
|
||||
ManagedEvent, ManagedEventSubscription, ManagedEventTryRecvError, ManagedStatus,
|
||||
ManagedTargetConfig, ManagedTargetEndReason, ManagedTargetOutcome, TargetInstance,
|
||||
ManagedClient, ManagedCommandApplyError, ManagedEndReason, ManagedEvent, ManagedStatus,
|
||||
ManagedTargetEndReason, ManagedTargetOutcome, TargetInstance,
|
||||
},
|
||||
ClientEvent,
|
||||
};
|
||||
|
||||
use super::{
|
||||
args::ClientArgs,
|
||||
output::{EventRenderStats, OutputConfig},
|
||||
};
|
||||
use super::{args::ClientArgs, output::OutputConfig};
|
||||
|
||||
use crate::shared::client::{
|
||||
expected_probe_count, is_shutdown_requested, parse_stdin_target_set,
|
||||
expected_probe_count, parse_stdin_target_set,
|
||||
session::{
|
||||
drain_managed_events, peer_close_run_error, request_managed_stop_for_peer_close,
|
||||
request_managed_stop_once, should_print_final_summary, ManagedDrainState,
|
||||
drain_final_events, peer_close_run_error, request_managed_stop_for_peer_close,
|
||||
request_managed_stop_once, should_print_final_summary,
|
||||
},
|
||||
ManagedRunSetup, STDIN_MAX_DESIRED_TARGETS, STDIN_OUTCOME_HISTORY_LIMIT,
|
||||
worker::ManagedWorker,
|
||||
ManagedRunSetup, TargetSpec, STDIN_MAX_DESIRED_TARGETS, STDIN_OUTCOME_HISTORY_LIMIT,
|
||||
};
|
||||
|
||||
use irtt_stats::{StatsCollector, StatsConfig};
|
||||
use irtt_stats::{EventStatsUpdate, StatsCollector, StatsConfig};
|
||||
|
||||
const MIB: u64 = 1024 * 1024;
|
||||
|
||||
@@ -40,20 +36,20 @@ const FINITE_STATS_MEMORY_WARNING_BYTES: u64 = 128 * MIB;
|
||||
const FINITE_STATS_MEMORY_STRONG_WARNING_BYTES: u64 = 512 * MIB;
|
||||
|
||||
const FINITE_STATS_MEMORY_VERY_STRONG_WARNING_BYTES: u64 = GIB;
|
||||
const MANAGED_EVENT_WAIT_SLICE: Duration = Duration::from_millis(20);
|
||||
const MAX_STDIN_RECORD_BYTES: usize = 64 * 1024;
|
||||
|
||||
pub fn run_stream(
|
||||
pub async fn run_stream(
|
||||
args: ClientArgs,
|
||||
shutdown_requested: &AtomicBool,
|
||||
mut shutdown: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if args.list_columns {
|
||||
print!("{}", OutputConfig::list_columns());
|
||||
return Ok(());
|
||||
}
|
||||
let setup = args
|
||||
.prepare()
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
|
||||
let setup = tokio::select! {
|
||||
result = args.prepare() => result.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?,
|
||||
_ = shutdown.wait_for(|requested| *requested) => return Ok(()),
|
||||
};
|
||||
let multi_target = setup.is_multi_target();
|
||||
let output_config = OutputConfig::new(
|
||||
args.format,
|
||||
@@ -63,7 +59,7 @@ pub fn run_stream(
|
||||
)
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
|
||||
if setup.stdin_controlled {
|
||||
return run_stdin_stream(setup, output_config, shutdown_requested);
|
||||
return run_stdin_stream(setup, output_config, shutdown).await;
|
||||
}
|
||||
let continuous = args.is_continuous();
|
||||
let target_count = setup.target_count();
|
||||
@@ -71,14 +67,13 @@ pub fn run_stream(
|
||||
if let Some(warning) = finite_stats_memory_warning(&args, target_count) {
|
||||
eprintln!("{warning}");
|
||||
}
|
||||
if is_shutdown_requested(shutdown_requested) {
|
||||
if *shutdown.borrow() {
|
||||
return Ok(());
|
||||
}
|
||||
let (owner, mut events) = BlockingManagedClient::start_with_subscription(
|
||||
setup.managed_config(),
|
||||
setup.managed_targets(),
|
||||
)?;
|
||||
let handle = owner.handle();
|
||||
let (task, handle) = ManagedClient::task(setup.managed_config(), setup.managed_targets())?;
|
||||
let mut events = handle.subscribe()?;
|
||||
let mut status = handle.subscribe_status();
|
||||
let owner = ManagedWorker::start(task, handle.clone())?;
|
||||
let mut stdout = io::LineWriter::new(io::stdout().lock());
|
||||
let mut stream_output = StreamOutput {
|
||||
config: output_config,
|
||||
@@ -102,9 +97,9 @@ pub fn run_stream(
|
||||
let mut dropped_events = 0_u64;
|
||||
let mut interrupted = false;
|
||||
let mut stop_requested = false;
|
||||
let mut subscription_closed = false;
|
||||
loop {
|
||||
if is_shutdown_requested(shutdown_requested) {
|
||||
let snapshot = status.borrow_and_update().clone();
|
||||
if *shutdown.borrow() {
|
||||
interrupted = true;
|
||||
if request_managed_stop_once(&mut stop_requested) {
|
||||
drop(handle.stop());
|
||||
@@ -113,38 +108,33 @@ pub fn run_stream(
|
||||
if request_managed_stop_for_peer_close(
|
||||
continuous,
|
||||
interrupted,
|
||||
handle.status().peer_closed_target_outcomes,
|
||||
snapshot.peer_closed_target_outcomes,
|
||||
&mut stop_requested,
|
||||
) {
|
||||
drop(handle.stop());
|
||||
}
|
||||
let drain_state = drain_events(
|
||||
&mut events,
|
||||
&mut stream_output,
|
||||
&mut stats,
|
||||
&mut terminal_targets,
|
||||
&mut dropped_events,
|
||||
)?;
|
||||
match drain_state {
|
||||
ManagedDrainState::Empty => thread::sleep(MANAGED_EVENT_WAIT_SLICE),
|
||||
ManagedDrainState::Closed => subscription_closed = true,
|
||||
ManagedDrainState::BudgetExhausted => {}
|
||||
}
|
||||
if handle.status().final_outcome.is_some() || subscription_closed {
|
||||
if snapshot.final_outcome.is_some() {
|
||||
break;
|
||||
}
|
||||
tokio::select! {
|
||||
_ = shutdown.changed(), if !interrupted => {}
|
||||
changed = status.changed() => { if changed.is_err() { break; } }
|
||||
event = events.recv() => match event {
|
||||
Ok(event) => process_event(event, &mut stream_output, &mut stats, &mut terminal_targets)?,
|
||||
Err(tokio::sync::broadcast::error::RecvError::Lagged(count)) => {
|
||||
dropped_events = dropped_events.saturating_add(count);
|
||||
}
|
||||
Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
if interrupted {
|
||||
eprintln!("interrupted, closing managed run...");
|
||||
}
|
||||
let outcome = owner.join()?;
|
||||
drain_final_events(
|
||||
&mut events,
|
||||
&mut stream_output,
|
||||
&mut stats,
|
||||
&mut terminal_targets,
|
||||
&mut dropped_events,
|
||||
)?;
|
||||
let outcome = owner.join().await?;
|
||||
drain_final_events(&mut events, &mut dropped_events, |event| {
|
||||
process_event(event, &mut stream_output, &mut stats, &mut terminal_targets)
|
||||
})?;
|
||||
if let Some(warning) = dropped_event_warning(dropped_events) {
|
||||
eprintln!("{warning}");
|
||||
}
|
||||
@@ -159,7 +149,7 @@ pub fn run_stream(
|
||||
report_target_failure(target);
|
||||
}
|
||||
}
|
||||
interrupted |= is_shutdown_requested(shutdown_requested);
|
||||
interrupted |= *shutdown.borrow();
|
||||
let terminal_error = match &outcome.end_reason {
|
||||
ManagedEndReason::DriverFailed(failure) => {
|
||||
Some(format!("managed driver failed: {failure}"))
|
||||
@@ -201,98 +191,9 @@ pub fn run_stream(
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct StdinTargetSet {
|
||||
revision: u64,
|
||||
targets: Vec<ManagedTargetConfig>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct StdinMailboxState {
|
||||
next_revision: u64,
|
||||
latest: Option<StdinTargetSet>,
|
||||
eof: bool,
|
||||
fatal: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct StdinMailbox {
|
||||
state: Mutex<StdinMailboxState>,
|
||||
}
|
||||
|
||||
impl StdinMailbox {
|
||||
fn publish(&self, targets: Vec<ManagedTargetConfig>) {
|
||||
let mut state = self
|
||||
.state
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
state.next_revision = state.next_revision.saturating_add(1);
|
||||
state.latest = Some(StdinTargetSet {
|
||||
revision: state.next_revision,
|
||||
targets,
|
||||
});
|
||||
}
|
||||
|
||||
fn finish(&self) {
|
||||
self.state
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.eof = true;
|
||||
}
|
||||
|
||||
fn fail(&self, error: String) {
|
||||
let mut state = self
|
||||
.state
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
state.fatal = Some(error);
|
||||
state.latest = None;
|
||||
}
|
||||
|
||||
fn latest(&self) -> Option<StdinTargetSet> {
|
||||
self.state
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.latest
|
||||
.clone()
|
||||
}
|
||||
|
||||
fn clear_if_current(&self, revision: u64) {
|
||||
let mut state = self
|
||||
.state
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if state
|
||||
.latest
|
||||
.as_ref()
|
||||
.is_some_and(|set| set.revision == revision)
|
||||
{
|
||||
state.latest = None;
|
||||
}
|
||||
}
|
||||
|
||||
fn is_current(&self, revision: u64) -> bool {
|
||||
self.state
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.latest
|
||||
.as_ref()
|
||||
.is_some_and(|set| set.revision == revision)
|
||||
}
|
||||
|
||||
fn fatal(&self) -> Option<String> {
|
||||
self.state
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.fatal
|
||||
.clone()
|
||||
}
|
||||
|
||||
fn is_finished(&self) -> bool {
|
||||
self.state
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.eof
|
||||
}
|
||||
enum StdinUpdate {
|
||||
Targets(Vec<TargetSpec>),
|
||||
Stop(StdinStop),
|
||||
}
|
||||
|
||||
fn read_stdin_record<R: BufRead>(
|
||||
@@ -371,7 +272,10 @@ fn read_stdin_record<R: BufRead>(
|
||||
}
|
||||
}
|
||||
|
||||
fn read_stdin_target_sets<R: BufRead>(reader: &mut R, mailbox: &StdinMailbox) {
|
||||
fn read_stdin_target_sets<R: BufRead>(
|
||||
reader: &mut R,
|
||||
updates: &tokio::sync::watch::Sender<StdinUpdate>,
|
||||
) {
|
||||
let mut line = 0_u64;
|
||||
let mut record = Vec::with_capacity(MAX_STDIN_RECORD_BYTES + 1);
|
||||
loop {
|
||||
@@ -383,101 +287,41 @@ fn read_stdin_target_sets<R: BufRead>(reader: &mut R, mailbox: &StdinMailbox) {
|
||||
}
|
||||
match parse_stdin_target_set(&record, STDIN_MAX_DESIRED_TARGETS) {
|
||||
Ok(targets) => {
|
||||
mailbox.publish(targets.into_iter().map(|target| target.managed).collect())
|
||||
updates.send_replace(StdinUpdate::Targets(targets));
|
||||
}
|
||||
Err(error) => {
|
||||
mailbox.fail(format!("invalid --targets-stdin line {line}: {error}"));
|
||||
updates.send_replace(StdinUpdate::Stop(StdinStop::Fatal(format!(
|
||||
"invalid --targets-stdin line {line}: {error}"
|
||||
))));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
mailbox.finish();
|
||||
updates.send_replace(StdinUpdate::Stop(StdinStop::Eof));
|
||||
return;
|
||||
}
|
||||
Err(_) => {
|
||||
mailbox.fail(format!("failed to read --targets-stdin line {}", line + 1));
|
||||
updates.send_replace(StdinUpdate::Stop(StdinStop::Fatal(format!(
|
||||
"failed to read --targets-stdin line {}",
|
||||
line + 1
|
||||
))));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_stdin_target_reader(mailbox: Arc<StdinMailbox>) -> io::Result<()> {
|
||||
fn spawn_stdin_target_reader(updates: tokio::sync::watch::Sender<StdinUpdate>) -> io::Result<()> {
|
||||
thread::Builder::new()
|
||||
.name("irtt-targets-stdin".to_owned())
|
||||
.spawn(move || {
|
||||
let stdin = io::stdin();
|
||||
read_stdin_target_sets(&mut stdin.lock(), &mailbox);
|
||||
read_stdin_target_sets(&mut stdin.lock(), &updates);
|
||||
})
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RetryAfterStatus {
|
||||
revision: u64,
|
||||
status: Arc<ManagedStatus>,
|
||||
}
|
||||
|
||||
fn retry_waits_for_status_change(
|
||||
retry_after: &mut Option<RetryAfterStatus>,
|
||||
target_set: &StdinTargetSet,
|
||||
status: &Arc<ManagedStatus>,
|
||||
) -> bool {
|
||||
let Some(retry) = retry_after else {
|
||||
return false;
|
||||
};
|
||||
if retry.revision != target_set.revision {
|
||||
*retry_after = None;
|
||||
return false;
|
||||
}
|
||||
Arc::ptr_eq(&retry.status, status)
|
||||
}
|
||||
|
||||
fn retry_after_capacity_rejection(
|
||||
mailbox: &StdinMailbox,
|
||||
target_set: &StdinTargetSet,
|
||||
status: Arc<ManagedStatus>,
|
||||
) -> Option<RetryAfterStatus> {
|
||||
mailbox
|
||||
.is_current(target_set.revision)
|
||||
.then_some(RetryAfterStatus {
|
||||
revision: target_set.revision,
|
||||
status,
|
||||
})
|
||||
}
|
||||
|
||||
fn apply_latest_stdin_target_set(
|
||||
handle: &ManagedClientHandle,
|
||||
mailbox: &StdinMailbox,
|
||||
retry_after: &mut Option<RetryAfterStatus>,
|
||||
) -> Result<(), String> {
|
||||
let Some(target_set) = mailbox.latest() else {
|
||||
*retry_after = None;
|
||||
return Ok(());
|
||||
};
|
||||
let status = handle.status();
|
||||
if retry_waits_for_status_change(retry_after, &target_set, &status) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let receipt = handle
|
||||
.update_targets(target_set.targets.clone())
|
||||
.map_err(|_| "failed to submit --targets-stdin update".to_owned())?;
|
||||
match receipt.blocking_wait() {
|
||||
Ok(_) => {
|
||||
mailbox.clear_if_current(target_set.revision);
|
||||
*retry_after = None;
|
||||
Ok(())
|
||||
}
|
||||
Err(ManagedCommandApplyError::LiveGenerationLimitExceeded { .. }) => {
|
||||
*retry_after = retry_after_capacity_rejection(mailbox, &target_set, status);
|
||||
Ok(())
|
||||
}
|
||||
Err(_) => Err("--targets-stdin update was rejected".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
struct BoundedTargetSet {
|
||||
limit: usize,
|
||||
order: VecDeque<TargetInstance>,
|
||||
@@ -525,12 +369,8 @@ fn process_stdin_event<W: Write>(
|
||||
let collector = stats
|
||||
.entry(target.clone())
|
||||
.or_insert_with(|| StatsCollector::new(stats_config(true)));
|
||||
print_events_with_stats(
|
||||
stream_output,
|
||||
std::slice::from_ref(&event),
|
||||
Some(target.id.as_str()),
|
||||
collector,
|
||||
)?;
|
||||
let update = collector.process(&event);
|
||||
stream_output.print_event(&event, Some(target.id.as_str()), &update)?;
|
||||
}
|
||||
ManagedEvent::TargetFinished { outcome }
|
||||
if terminal_targets.insert(outcome.target.clone()) =>
|
||||
@@ -542,37 +382,6 @@ fn process_stdin_event<W: Write>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn drain_stdin_events<W: Write>(
|
||||
events: &mut ManagedEventSubscription,
|
||||
stream_output: &mut StreamOutput<'_, W>,
|
||||
stats: &mut BTreeMap<TargetInstance, StatsCollector>,
|
||||
terminal_targets: &mut BoundedTargetSet,
|
||||
dropped_events: &mut u64,
|
||||
) -> io::Result<ManagedDrainState> {
|
||||
drain_managed_events(events, dropped_events, |event| {
|
||||
process_stdin_event(event, stream_output, stats, terminal_targets)
|
||||
})
|
||||
}
|
||||
|
||||
fn drain_final_stdin_events<W: Write>(
|
||||
events: &mut ManagedEventSubscription,
|
||||
stream_output: &mut StreamOutput<'_, W>,
|
||||
stats: &mut BTreeMap<TargetInstance, StatsCollector>,
|
||||
terminal_targets: &mut BoundedTargetSet,
|
||||
dropped_events: &mut u64,
|
||||
) -> io::Result<()> {
|
||||
loop {
|
||||
match events.try_recv() {
|
||||
Ok(event) => process_stdin_event(event, stream_output, stats, terminal_targets)?,
|
||||
Err(ManagedEventTryRecvError::Empty | ManagedEventTryRecvError::Closed) => break,
|
||||
Err(ManagedEventTryRecvError::Lagged(count)) => {
|
||||
*dropped_events = dropped_events.saturating_add(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reconcile_stdin_stats(
|
||||
stats: &mut BTreeMap<TargetInstance, StatsCollector>,
|
||||
status: &ManagedStatus,
|
||||
@@ -613,36 +422,20 @@ enum StdinStop {
|
||||
Fatal(String),
|
||||
}
|
||||
|
||||
fn stdin_stop_request(
|
||||
shutdown_requested: &AtomicBool,
|
||||
mailbox: &StdinMailbox,
|
||||
) -> Option<StdinStop> {
|
||||
if is_shutdown_requested(shutdown_requested) {
|
||||
Some(StdinStop::Interrupted)
|
||||
} else if let Some(error) = mailbox.fatal() {
|
||||
Some(StdinStop::Fatal(error))
|
||||
} else if mailbox.is_finished() {
|
||||
Some(StdinStop::Eof)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn run_stdin_stream(
|
||||
async fn run_stdin_stream(
|
||||
setup: ManagedRunSetup,
|
||||
output_config: OutputConfig,
|
||||
shutdown_requested: &AtomicBool,
|
||||
mut shutdown: tokio::sync::watch::Receiver<bool>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if is_shutdown_requested(shutdown_requested) {
|
||||
if *shutdown.borrow() {
|
||||
return Ok(());
|
||||
}
|
||||
let (owner, mut events) = BlockingManagedClient::start_with_subscription(
|
||||
setup.managed_config(),
|
||||
setup.managed_targets(),
|
||||
)?;
|
||||
let handle = owner.handle();
|
||||
let mailbox = Arc::new(StdinMailbox::default());
|
||||
spawn_stdin_target_reader(Arc::clone(&mailbox))?;
|
||||
let (task, handle) = ManagedClient::task(setup.managed_config(), setup.managed_targets())?;
|
||||
let mut events = handle.subscribe()?;
|
||||
let mut status = handle.subscribe_status();
|
||||
let owner = ManagedWorker::start(task, handle.clone())?;
|
||||
let (updates, mut stdin) = tokio::sync::watch::channel(StdinUpdate::Targets(Vec::new()));
|
||||
spawn_stdin_target_reader(updates)?;
|
||||
|
||||
let mut stdout = io::LineWriter::new(io::stdout().lock());
|
||||
let mut stream_output = StreamOutput {
|
||||
@@ -654,59 +447,138 @@ fn run_stdin_stream(
|
||||
};
|
||||
let mut stats = BTreeMap::new();
|
||||
let mut terminal_targets = BoundedTargetSet::new(STDIN_OUTCOME_HISTORY_LIMIT);
|
||||
let mut stdin_changed = false;
|
||||
let mut preparation = setup.preparation.clone();
|
||||
let mut desired_specs = None;
|
||||
let mut preparing = None;
|
||||
let mut desired = None;
|
||||
let mut pending = None;
|
||||
let mut submitted = Vec::new();
|
||||
let mut retry_after = None;
|
||||
let mut dropped_events = 0_u64;
|
||||
let mut stop_requested = false;
|
||||
let mut stop = None;
|
||||
let mut summary_targets = None;
|
||||
let mut subscription_closed = false;
|
||||
|
||||
loop {
|
||||
let status = handle.status();
|
||||
let snapshot = status.borrow_and_update().clone();
|
||||
if stop.is_none() {
|
||||
if let Some(requested_stop) = stdin_stop_request(shutdown_requested, &mailbox) {
|
||||
summary_targets = Some(snapshot_stdin_summary_targets(&mut stats, &status));
|
||||
stop = Some(requested_stop);
|
||||
if request_managed_stop_once(&mut stop_requested) {
|
||||
drop(handle.stop());
|
||||
if *shutdown.borrow() {
|
||||
stop = Some(StdinStop::Interrupted);
|
||||
} else if stdin_changed || stdin.has_changed().unwrap_or(true) {
|
||||
stdin_changed = false;
|
||||
match stdin.borrow_and_update().clone() {
|
||||
StdinUpdate::Targets(specs) => {
|
||||
desired_specs = Some(specs);
|
||||
desired = None;
|
||||
retry_after = None;
|
||||
}
|
||||
StdinUpdate::Stop(reason) => stop = Some(reason),
|
||||
}
|
||||
} else if let Err(error) =
|
||||
apply_latest_stdin_target_set(&handle, &mailbox, &mut retry_after)
|
||||
}
|
||||
if stop.is_none() && preparing.is_none() {
|
||||
if let Some(specs) = desired_specs.take() {
|
||||
// Only one preparation runs at a time. Newer records supersede
|
||||
// its result without piling up uncancellable system DNS jobs.
|
||||
preparing = Some(Box::pin(
|
||||
preparation
|
||||
.clone()
|
||||
.prepare(specs, Some(STDIN_MAX_DESIRED_TARGETS)),
|
||||
));
|
||||
}
|
||||
}
|
||||
if stop.is_none()
|
||||
&& pending.is_none()
|
||||
&& !retry_after
|
||||
.as_ref()
|
||||
.is_some_and(|previous| Arc::ptr_eq(previous, &snapshot))
|
||||
{
|
||||
summary_targets = Some(snapshot_stdin_summary_targets(&mut stats, &status));
|
||||
stop = Some(StdinStop::Fatal(error));
|
||||
if let Some(targets) = desired.take() {
|
||||
submitted = targets;
|
||||
match handle.update_targets(submitted.clone()) {
|
||||
Ok(receipt) => {
|
||||
pending = Some(receipt);
|
||||
retry_after = Some(snapshot.clone());
|
||||
}
|
||||
Err(_) => {
|
||||
stop = Some(StdinStop::Fatal(
|
||||
"failed to submit --targets-stdin update".to_owned(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if stop.is_some() {
|
||||
preparing = None;
|
||||
summary_targets = Some(snapshot_stdin_summary_targets(&mut stats, &snapshot));
|
||||
if request_managed_stop_once(&mut stop_requested) {
|
||||
drop(handle.stop());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let drain_state = drain_stdin_events(
|
||||
&mut events,
|
||||
&mut stream_output,
|
||||
&mut stats,
|
||||
&mut terminal_targets,
|
||||
&mut dropped_events,
|
||||
)?;
|
||||
reconcile_stdin_stats(&mut stats, &handle.status(), summary_targets.as_ref());
|
||||
match drain_state {
|
||||
ManagedDrainState::Empty => thread::sleep(MANAGED_EVENT_WAIT_SLICE),
|
||||
ManagedDrainState::Closed => subscription_closed = true,
|
||||
ManagedDrainState::BudgetExhausted => {}
|
||||
}
|
||||
if handle.status().final_outcome.is_some() || subscription_closed {
|
||||
reconcile_stdin_stats(&mut stats, &snapshot, summary_targets.as_ref());
|
||||
if snapshot.final_outcome.is_some() {
|
||||
break;
|
||||
}
|
||||
tokio::select! {
|
||||
_ = shutdown.changed(), if stop.is_none() => {}
|
||||
_ = stdin.changed(), if stop.is_none() => { stdin_changed = true; }
|
||||
changed = status.changed() => { if changed.is_err() { break; } }
|
||||
result = async { preparing.as_mut().expect("guarded target preparation").await }, if preparing.is_some() && stop.is_none() => {
|
||||
preparing = None;
|
||||
// Let the next loop consume any newer record, including EOF,
|
||||
// before considering this prepared revision for submission.
|
||||
if desired_specs.is_none() && !stdin.has_changed().unwrap_or(true) {
|
||||
match result {
|
||||
Ok((targets, next_preparation)) => {
|
||||
preparation = next_preparation;
|
||||
desired = Some(targets.into_iter().map(|target| target.managed).collect());
|
||||
retry_after = None;
|
||||
}
|
||||
Err(error) => {
|
||||
stop = Some(StdinStop::Fatal(format!("invalid --targets-stdin target set: {error}")));
|
||||
summary_targets = Some(snapshot_stdin_summary_targets(&mut stats, &status.borrow()));
|
||||
if request_managed_stop_once(&mut stop_requested) { drop(handle.stop()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
result = async { pending.as_mut().expect("guarded pending receipt").await }, if pending.is_some() && stop.is_none() => {
|
||||
pending = None;
|
||||
match result {
|
||||
Ok(_) => retry_after = None,
|
||||
Err(ManagedCommandApplyError::LiveGenerationLimitExceeded { .. }) => {
|
||||
// A newer desired set supersedes this rejected set. Otherwise,
|
||||
// retry only after the driver's status advances from submission.
|
||||
if desired.is_none() && desired_specs.is_none() && preparing.is_none() {
|
||||
desired = Some(std::mem::take(&mut submitted));
|
||||
} else {
|
||||
retry_after = None;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
stop = Some(StdinStop::Fatal("--targets-stdin update was rejected".to_owned()));
|
||||
summary_targets = Some(snapshot_stdin_summary_targets(&mut stats, &status.borrow()));
|
||||
if request_managed_stop_once(&mut stop_requested) { drop(handle.stop()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
event = events.recv() => match event {
|
||||
Ok(event) => {
|
||||
process_stdin_event(event, &mut stream_output, &mut stats, &mut terminal_targets)?;
|
||||
reconcile_stdin_stats(&mut stats, &status.borrow(), summary_targets.as_ref());
|
||||
}
|
||||
Err(tokio::sync::broadcast::error::RecvError::Lagged(count)) => {
|
||||
dropped_events = dropped_events.saturating_add(count);
|
||||
}
|
||||
Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let outcome = owner.join()?;
|
||||
drain_final_stdin_events(
|
||||
&mut events,
|
||||
&mut stream_output,
|
||||
&mut stats,
|
||||
&mut terminal_targets,
|
||||
&mut dropped_events,
|
||||
)?;
|
||||
let outcome = owner.join().await?;
|
||||
drain_final_events(&mut events, &mut dropped_events, |event| {
|
||||
process_stdin_event(event, &mut stream_output, &mut stats, &mut terminal_targets)
|
||||
})?;
|
||||
if let Some(warning) = dropped_event_warning(dropped_events) {
|
||||
eprintln!("{warning}");
|
||||
}
|
||||
@@ -766,12 +638,8 @@ fn process_event<W: Write>(
|
||||
let collector = stats
|
||||
.entry(target.id.as_str().to_owned())
|
||||
.or_insert_with(|| StatsCollector::new(stats_config(false)));
|
||||
print_events_with_stats(
|
||||
stream_output,
|
||||
std::slice::from_ref(&event),
|
||||
Some(target.id.as_str()),
|
||||
collector,
|
||||
)?;
|
||||
let update = collector.process(&event);
|
||||
stream_output.print_event(&event, Some(target.id.as_str()), &update)?;
|
||||
}
|
||||
ManagedEvent::TargetFinished { outcome } => {
|
||||
terminal_targets.insert(outcome.target.clone());
|
||||
@@ -782,37 +650,6 @@ fn process_event<W: Write>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn drain_events<W: Write>(
|
||||
events: &mut ManagedEventSubscription,
|
||||
stream_output: &mut StreamOutput<'_, W>,
|
||||
stats: &mut BTreeMap<String, StatsCollector>,
|
||||
terminal_targets: &mut HashSet<TargetInstance>,
|
||||
dropped_events: &mut u64,
|
||||
) -> io::Result<ManagedDrainState> {
|
||||
drain_managed_events(events, dropped_events, |event| {
|
||||
process_event(event, stream_output, stats, terminal_targets)
|
||||
})
|
||||
}
|
||||
|
||||
fn drain_final_events<W: Write>(
|
||||
events: &mut ManagedEventSubscription,
|
||||
stream_output: &mut StreamOutput<'_, W>,
|
||||
stats: &mut BTreeMap<String, StatsCollector>,
|
||||
terminal_targets: &mut HashSet<TargetInstance>,
|
||||
dropped_events: &mut u64,
|
||||
) -> io::Result<()> {
|
||||
loop {
|
||||
match events.try_recv() {
|
||||
Ok(event) => process_event(event, stream_output, stats, terminal_targets)?,
|
||||
Err(ManagedEventTryRecvError::Empty | ManagedEventTryRecvError::Closed) => break,
|
||||
Err(ManagedEventTryRecvError::Lagged(count)) => {
|
||||
*dropped_events = dropped_events.saturating_add(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn report_target_failure(target: &ManagedTargetOutcome) {
|
||||
for message in target_failure_messages(target) {
|
||||
eprintln!("{message}");
|
||||
@@ -847,17 +684,15 @@ struct StreamOutput<'a, W: Write> {
|
||||
out: &'a mut W,
|
||||
}
|
||||
impl<W: Write> StreamOutput<'_, W> {
|
||||
fn print_events(
|
||||
fn print_event(
|
||||
&mut self,
|
||||
events: &[ClientEvent],
|
||||
event: &ClientEvent,
|
||||
target: Option<&str>,
|
||||
stats_updates: &[EventRenderStats],
|
||||
update: &EventStatsUpdate,
|
||||
) -> io::Result<()> {
|
||||
self.print_header()?;
|
||||
for (event, stats_update) in events.iter().zip(stats_updates) {
|
||||
if let Some(line) = self.config.render_event(event, target, Some(stats_update)) {
|
||||
writeln!(self.out, "{line}")?;
|
||||
}
|
||||
if let Some(line) = self.config.render_event(event, target, Some(update)) {
|
||||
writeln!(self.out, "{line}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -890,19 +725,6 @@ impl<W: Write> StreamOutput<'_, W> {
|
||||
}
|
||||
}
|
||||
|
||||
fn print_events_with_stats<W: Write>(
|
||||
stream_output: &mut StreamOutput<'_, W>,
|
||||
events: &[ClientEvent],
|
||||
target: Option<&str>,
|
||||
stats: &mut StatsCollector,
|
||||
) -> io::Result<()> {
|
||||
let updates = events
|
||||
.iter()
|
||||
.map(|event| EventRenderStats::from(stats.process(event)))
|
||||
.collect::<Vec<_>>();
|
||||
stream_output.print_events(events, target, &updates)
|
||||
}
|
||||
|
||||
fn stats_config(continuous: bool) -> StatsConfig {
|
||||
if continuous {
|
||||
StatsConfig::continuous()
|
||||
|
||||
@@ -49,7 +49,7 @@ impl TuiArgs {
|
||||
///
|
||||
/// The TUI always requires a target: the parser rejects an empty target
|
||||
/// list, and this rejects a target set that cannot be labelled uniquely.
|
||||
pub fn prepare(&self) -> Result<ManagedRunSetup, String> {
|
||||
pub async fn prepare(&self) -> Result<ManagedRunSetup, String> {
|
||||
prepare_managed_run(
|
||||
&self.common,
|
||||
self.duration,
|
||||
@@ -59,6 +59,7 @@ impl TuiArgs {
|
||||
stdin_controlled: false,
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn is_continuous(&self) -> bool {
|
||||
|
||||
+216
-294
@@ -1,356 +1,278 @@
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
io,
|
||||
sync::atomic::{AtomicBool, Ordering},
|
||||
thread,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use std::{future::poll_fn, io, time::Duration};
|
||||
|
||||
use super::ui::{should_render, TuiConfig, TuiState, TuiStatus, TuiTerminal};
|
||||
use super::ui::{TuiConfig, TuiState, TuiStatus, TuiTerminal};
|
||||
use crate::{
|
||||
cmd::tui::args::TuiArgs,
|
||||
shared::client::{
|
||||
is_shutdown_requested,
|
||||
session::{
|
||||
drain_managed_events, peer_close_run_error, request_managed_stop_for_peer_close,
|
||||
request_managed_stop_once, ManagedDrainState,
|
||||
},
|
||||
session::{drain_final_events, request_managed_stop_once},
|
||||
worker::ManagedWorker,
|
||||
},
|
||||
};
|
||||
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
|
||||
use irtt_client::managed::{
|
||||
BlockingManagedClient, ManagedEndReason, ManagedEvent, ManagedEventSubscription,
|
||||
ManagedEventTryRecvError, TargetInstance,
|
||||
use crossterm::{
|
||||
event::{Event, EventStream, KeyCode, KeyEventKind, KeyModifiers},
|
||||
terminal,
|
||||
};
|
||||
use futures_core::Stream;
|
||||
use irtt_client::{
|
||||
managed::{
|
||||
ManagedClient, ManagedCommandApplyError, ManagedCommandError, ManagedCompletionPolicy,
|
||||
ManagedEndReason, ManagedEvent, ManagedTargetEndReason, ManagedTargetLifecycle,
|
||||
},
|
||||
ClientEvent,
|
||||
};
|
||||
use ratatui::layout::Rect;
|
||||
use tokio::{
|
||||
sync::{broadcast::error::RecvError, watch},
|
||||
time::{sleep, Instant},
|
||||
};
|
||||
|
||||
const RENDER_INTERVAL: Duration = Duration::from_millis(250);
|
||||
const TUI_WAIT_SLICE: Duration = Duration::from_millis(20);
|
||||
const INPUT_EVENT_WORK_BUDGET: usize = 128;
|
||||
const RETRY_DELAY: Duration = Duration::from_millis(1500);
|
||||
|
||||
pub fn run_tui(
|
||||
const RENDER_INTERVAL: Duration = Duration::from_millis(100);
|
||||
|
||||
pub async fn run_tui(
|
||||
args: TuiArgs,
|
||||
shutdown_requested: &AtomicBool,
|
||||
mut shutdown: watch::Receiver<bool>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let setup = args
|
||||
.prepare()
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
|
||||
let setup = tokio::select! {
|
||||
result = args.prepare() => result.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?,
|
||||
_ = shutdown.wait_for(|requested| *requested) => return Ok(()),
|
||||
};
|
||||
let continuous = args.is_continuous();
|
||||
if *shutdown.borrow() {
|
||||
return Ok(());
|
||||
}
|
||||
let mut terminal = TuiTerminal::enter()?;
|
||||
let mut state = TuiState::with_target_labels(
|
||||
TuiConfig::from_args(&args, &setup),
|
||||
setup.targets.iter().map(|target| target.label.clone()),
|
||||
);
|
||||
let mut next_render = Instant::now();
|
||||
if is_shutdown_requested(shutdown_requested) {
|
||||
return Ok(());
|
||||
}
|
||||
state.set_status(TuiStatus::Opening);
|
||||
render_if_due(&mut terminal, &state, &mut next_render, true)?;
|
||||
let (owner, mut events) = match BlockingManagedClient::start_with_subscription(
|
||||
setup.managed_config(),
|
||||
setup.managed_targets(),
|
||||
) {
|
||||
terminal.draw(&state)?;
|
||||
let desired_targets = setup.managed_targets();
|
||||
let mut config = setup.managed_config();
|
||||
if continuous {
|
||||
config.completion = ManagedCompletionPolicy::ExplicitStop;
|
||||
}
|
||||
let (task, handle) = match ManagedClient::task(config, desired_targets.clone()) {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
state.set_error(error.to_string());
|
||||
render_if_due(&mut terminal, &state, &mut next_render, true)?;
|
||||
terminal.draw(&state)?;
|
||||
return Err(Box::new(error));
|
||||
}
|
||||
};
|
||||
let handle = owner.handle();
|
||||
// Both subscriptions exist before the measurement task can be polled.
|
||||
let mut events = handle.subscribe()?;
|
||||
let mut status = handle.subscribe_status();
|
||||
let worker = match ManagedWorker::start(task, handle.clone()) {
|
||||
Ok(worker) => worker,
|
||||
Err(error) => {
|
||||
state.set_run_error(error.to_string());
|
||||
terminal.draw(&state)?;
|
||||
return Err(error.into());
|
||||
}
|
||||
};
|
||||
let completion = worker.join();
|
||||
tokio::pin!(completion);
|
||||
// Input and rendering stay on the frontend executor. EventStream's drop
|
||||
// wakes its reader; no terminal task can outlive the terminal guard.
|
||||
let mut input = EventStream::new();
|
||||
let render = sleep(RENDER_INTERVAL);
|
||||
tokio::pin!(render);
|
||||
let mut interrupted = false;
|
||||
let mut stop_requested = false;
|
||||
let mut terminal_targets = HashSet::new();
|
||||
let mut dropped_events = 0;
|
||||
let mut subscription_closed = false;
|
||||
loop {
|
||||
if is_shutdown_requested(shutdown_requested) {
|
||||
interrupted = true;
|
||||
if request_managed_stop_once(&mut stop_requested) {
|
||||
drop(handle.stop());
|
||||
let retry = sleep(RETRY_DELAY);
|
||||
tokio::pin!(retry);
|
||||
let mut retry_armed = false;
|
||||
let mut update_receipt = None;
|
||||
let mut dropped_events = 0_u64;
|
||||
let mut events_closed = false;
|
||||
let mut status_closed = false;
|
||||
let outcome = loop {
|
||||
let mut force_render = false;
|
||||
// Default select fairness lets input, status and rendering progress
|
||||
// even when measurement events are continuously ready.
|
||||
tokio::select! {
|
||||
outcome = &mut completion => break outcome,
|
||||
_ = shutdown.changed(), if !interrupted => {
|
||||
interrupted = true;
|
||||
state.set_status(TuiStatus::Interrupted);
|
||||
force_render = true;
|
||||
}
|
||||
}
|
||||
if handle_input(&mut state, shutdown_requested)? {
|
||||
render_if_due(&mut terminal, &state, &mut next_render, true)?;
|
||||
}
|
||||
if state.quit_requested {
|
||||
interrupted = true;
|
||||
if request_managed_stop_once(&mut stop_requested) {
|
||||
drop(handle.stop());
|
||||
changed = status.changed(), if !status_closed => {
|
||||
status_closed = changed.is_err();
|
||||
state.process_managed_status(&status.borrow_and_update());
|
||||
force_render = true;
|
||||
}
|
||||
event = events.recv(), if !events_closed => match event {
|
||||
Ok(event) => force_render = process_tui_event(
|
||||
event, &mut state,
|
||||
),
|
||||
Err(RecvError::Lagged(count)) => {
|
||||
dropped_events = dropped_events.saturating_add(count);
|
||||
state.mark_dropped_managed_events(dropped_events);
|
||||
force_render = true;
|
||||
}
|
||||
Err(RecvError::Closed) => events_closed = true,
|
||||
},
|
||||
event = poll_fn(|cx| std::pin::Pin::new(&mut input).poll_next(cx)) => {
|
||||
let event = event.ok_or_else(|| io::Error::new(
|
||||
io::ErrorKind::UnexpectedEof, "terminal input closed",
|
||||
))??;
|
||||
match event {
|
||||
Event::Resize(_, _) => force_render = true,
|
||||
Event::Key(key) if key.kind != KeyEventKind::Release => {
|
||||
match key.code {
|
||||
KeyCode::Char('q') | KeyCode::Char('c')
|
||||
if key.code == KeyCode::Char('q')
|
||||
|| key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
interrupted = true;
|
||||
state.set_status(TuiStatus::Interrupted);
|
||||
force_render = true;
|
||||
}
|
||||
_ => {
|
||||
let (width, height) = terminal::size()?;
|
||||
force_render = state.handle_key(
|
||||
key.code, Rect::new(0, 0, width, height),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ = &mut retry, if retry_armed && !interrupted && !*shutdown.borrow() => {
|
||||
state.process_managed_status(&status.borrow());
|
||||
retry_armed = false;
|
||||
update_receipt = match handle.update_targets(desired_targets.clone()) {
|
||||
Ok(receipt) => Some(receipt),
|
||||
Err(ManagedCommandError::Stopping | ManagedCommandError::DriverClosed)
|
||||
if stop_requested || *shutdown.borrow() => None,
|
||||
Err(error) => return Err(format!("reconnect update failed: {error}").into()),
|
||||
};
|
||||
}
|
||||
result = async { update_receipt.as_mut().unwrap().await },
|
||||
if update_receipt.is_some() => {
|
||||
update_receipt = None;
|
||||
match result {
|
||||
Ok(ack) => {
|
||||
state.process_managed_status(&ack.status);
|
||||
force_render = true;
|
||||
}
|
||||
Err(ManagedCommandApplyError::Stopping
|
||||
| ManagedCommandApplyError::AcknowledgementDisconnected)
|
||||
if stop_requested || *shutdown.borrow() => {}
|
||||
Err(error) => return Err(format!("reconnect update failed: {error}").into()),
|
||||
}
|
||||
}
|
||||
_ = &mut render, if !state.paused => force_render = true,
|
||||
}
|
||||
if request_managed_stop_for_peer_close(
|
||||
continuous,
|
||||
interrupted,
|
||||
handle.status().peer_closed_target_outcomes,
|
||||
&mut stop_requested,
|
||||
) {
|
||||
if interrupted && request_managed_stop_once(&mut stop_requested) {
|
||||
drop(handle.stop());
|
||||
}
|
||||
let previous_dropped = dropped_events;
|
||||
let drain_state = drain_tui_events(
|
||||
&mut events,
|
||||
&mut state,
|
||||
&mut terminal_targets,
|
||||
&mut dropped_events,
|
||||
);
|
||||
if dropped_events != previous_dropped {
|
||||
// Data loss changes the meaning of displayed statistics, even while
|
||||
// ordinary measurement refreshes are paused.
|
||||
render_if_due(&mut terminal, &state, &mut next_render, true)?;
|
||||
if continuous
|
||||
&& !interrupted
|
||||
&& !stop_requested
|
||||
&& !retry_armed
|
||||
&& update_receipt.is_none()
|
||||
&& status.borrow().targets.iter().any(|target| {
|
||||
target.desired
|
||||
&& target.outcome.as_ref().is_some_and(|outcome| {
|
||||
matches!(
|
||||
outcome.end_reason,
|
||||
ManagedTargetEndReason::Failed(_)
|
||||
| ManagedTargetEndReason::PeerClosed
|
||||
| ManagedTargetEndReason::TestComplete
|
||||
)
|
||||
})
|
||||
})
|
||||
{
|
||||
retry.as_mut().reset(Instant::now() + RETRY_DELAY);
|
||||
retry_armed = true;
|
||||
}
|
||||
match drain_state {
|
||||
ManagedDrainState::Empty => {
|
||||
thread::sleep(managed_tui_wait_duration(&next_render, state.paused));
|
||||
}
|
||||
ManagedDrainState::Closed => subscription_closed = true,
|
||||
ManagedDrainState::BudgetExhausted => {}
|
||||
if force_render {
|
||||
terminal.draw(&state)?;
|
||||
// Schedule from the completed draw, never catch up missed frames.
|
||||
render.as_mut().reset(Instant::now() + RENDER_INTERVAL);
|
||||
}
|
||||
if handle.status().final_outcome.is_some() || subscription_closed {
|
||||
break;
|
||||
}
|
||||
render_if_due(&mut terminal, &state, &mut next_render, false)?;
|
||||
}
|
||||
if interrupted {
|
||||
state.set_status(TuiStatus::Interrupted);
|
||||
render_if_due(&mut terminal, &state, &mut next_render, true)?;
|
||||
}
|
||||
};
|
||||
state.set_status(TuiStatus::Closing);
|
||||
render_if_due(&mut terminal, &state, &mut next_render, true)?;
|
||||
let outcome = owner.join()?;
|
||||
drain_final_tui_events(
|
||||
&mut events,
|
||||
&mut state,
|
||||
&mut terminal_targets,
|
||||
&mut dropped_events,
|
||||
);
|
||||
terminal.draw(&state)?;
|
||||
let outcome = match outcome {
|
||||
Ok(outcome) => outcome,
|
||||
Err(error) => {
|
||||
state.set_run_error(error.to_string());
|
||||
terminal.draw(&state)?;
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
drain_final_events(&mut events, &mut dropped_events, |event| {
|
||||
process_tui_event(event, &mut state);
|
||||
Ok::<(), std::convert::Infallible>(())
|
||||
})?;
|
||||
state.mark_dropped_managed_events(dropped_events);
|
||||
if outcome.discarded_target_outcomes != 0 {
|
||||
if !continuous && outcome.discarded_target_outcomes != 0 {
|
||||
state.set_run_error(format!(
|
||||
"{} final target outcomes were discarded",
|
||||
outcome.discarded_target_outcomes
|
||||
));
|
||||
}
|
||||
for target in outcome.recent_target_outcomes.iter() {
|
||||
if terminal_targets.insert(target.target.clone()) {
|
||||
state.process_target_outcome(target);
|
||||
}
|
||||
state.process_target_outcome(target);
|
||||
}
|
||||
interrupted |= is_shutdown_requested(shutdown_requested);
|
||||
interrupted |= *shutdown.borrow();
|
||||
let error = match &outcome.end_reason {
|
||||
ManagedEndReason::DriverFailed(failure) => {
|
||||
Some(format!("managed driver failed: {failure}"))
|
||||
}
|
||||
_ => peer_close_run_error(continuous, interrupted, outcome.peer_closed_target_outcomes)
|
||||
.or_else(|| {
|
||||
(!interrupted
|
||||
&& outcome.successful_target_outcomes == 0
|
||||
&& outcome.failed_target_outcomes > 0)
|
||||
.then(|| {
|
||||
format!(
|
||||
"no managed target completed successfully ({} failed)",
|
||||
outcome.failed_target_outcomes
|
||||
)
|
||||
})
|
||||
_ => (!interrupted
|
||||
&& outcome.successful_target_outcomes == 0
|
||||
&& outcome.failed_target_outcomes > 0)
|
||||
.then(|| {
|
||||
format!(
|
||||
"no managed target completed successfully ({} failed)",
|
||||
outcome.failed_target_outcomes
|
||||
)
|
||||
}),
|
||||
};
|
||||
if let Some(error) = error {
|
||||
state.set_run_error(error.clone());
|
||||
render_if_due(&mut terminal, &state, &mut next_render, true)?;
|
||||
terminal.draw(&state)?;
|
||||
return Err(error.into());
|
||||
}
|
||||
state.set_status(TuiStatus::Complete);
|
||||
render_if_due(&mut terminal, &state, &mut next_render, true)?;
|
||||
terminal.draw(&state)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_tui_event(
|
||||
event: ManagedEvent,
|
||||
state: &mut TuiState,
|
||||
terminal_targets: &mut HashSet<TargetInstance>,
|
||||
) {
|
||||
fn process_tui_event(event: ManagedEvent, state: &mut TuiState) -> bool {
|
||||
match event {
|
||||
ManagedEvent::Client { target, event } => state.process_target_event(&target, &event),
|
||||
ManagedEvent::Client { target, event } => {
|
||||
let force_render = matches!(
|
||||
event,
|
||||
ClientEvent::SessionStarted(_)
|
||||
| ClientEvent::NoTestCompleted(_)
|
||||
| ClientEvent::SessionClosed { .. }
|
||||
| ClientEvent::Warning { .. }
|
||||
);
|
||||
state.process_target_event(&target, &event);
|
||||
force_render
|
||||
}
|
||||
ManagedEvent::TargetFinished { outcome } => {
|
||||
terminal_targets.insert(outcome.target.clone());
|
||||
state.process_target_outcome(&outcome);
|
||||
true
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
fn drain_tui_events(
|
||||
events: &mut ManagedEventSubscription,
|
||||
state: &mut TuiState,
|
||||
terminal_targets: &mut HashSet<TargetInstance>,
|
||||
dropped_events: &mut u64,
|
||||
) -> ManagedDrainState {
|
||||
let previous_dropped = *dropped_events;
|
||||
let drained = drain_managed_events(events, dropped_events, |event| {
|
||||
process_tui_event(event, state, terminal_targets);
|
||||
Ok::<(), std::convert::Infallible>(())
|
||||
})
|
||||
.expect("processing TUI managed events is infallible");
|
||||
if *dropped_events != previous_dropped {
|
||||
state.mark_dropped_managed_events(*dropped_events);
|
||||
}
|
||||
drained
|
||||
}
|
||||
|
||||
fn drain_final_tui_events(
|
||||
events: &mut ManagedEventSubscription,
|
||||
state: &mut TuiState,
|
||||
terminal_targets: &mut HashSet<TargetInstance>,
|
||||
dropped_events: &mut u64,
|
||||
) {
|
||||
loop {
|
||||
match events.try_recv() {
|
||||
Ok(event) => process_tui_event(event, state, terminal_targets),
|
||||
Err(ManagedEventTryRecvError::Empty | ManagedEventTryRecvError::Closed) => break,
|
||||
Err(ManagedEventTryRecvError::Lagged(count)) => {
|
||||
*dropped_events = dropped_events.saturating_add(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn handle_input(state: &mut TuiState, shutdown_requested: &AtomicBool) -> io::Result<bool> {
|
||||
let mut force_render = false;
|
||||
for _ in 0..INPUT_EVENT_WORK_BUDGET {
|
||||
if !event::poll(Duration::ZERO)? {
|
||||
break;
|
||||
}
|
||||
let Event::Key(key) = event::read()? else {
|
||||
continue;
|
||||
};
|
||||
if key.kind == KeyEventKind::Release {
|
||||
continue;
|
||||
}
|
||||
match key.code {
|
||||
KeyCode::Char('q') | KeyCode::Char('c')
|
||||
if key.code == KeyCode::Char('q')
|
||||
|| key.modifiers.contains(KeyModifiers::CONTROL) =>
|
||||
{
|
||||
state.quit_requested = true;
|
||||
shutdown_requested.store(true, Ordering::Relaxed);
|
||||
force_render = true;
|
||||
break;
|
||||
}
|
||||
KeyCode::Char('r') => {
|
||||
state.clear_visible_history();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::Char('p') => {
|
||||
state.toggle_pause();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::Char('g') => {
|
||||
state.toggle_view();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::Char('m') => {
|
||||
state.cycle_graph_metric();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::Left => {
|
||||
state.pan_graph_left();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::Right => {
|
||||
state.pan_graph_right();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::PageUp => {
|
||||
state.pan_graph_page_left();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::PageDown => {
|
||||
state.pan_graph_page_right();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::Home => {
|
||||
state.jump_graph_oldest();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::End => {
|
||||
state.jump_graph_live();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::Char('+') | KeyCode::Char('=') => {
|
||||
state.zoom_graph_in();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::Char('-') => {
|
||||
state.zoom_graph_out();
|
||||
force_render = true;
|
||||
}
|
||||
KeyCode::Char('0') => {
|
||||
state.reset_graph_window();
|
||||
force_render = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(force_render)
|
||||
}
|
||||
fn render_if_due(
|
||||
terminal: &mut TuiTerminal,
|
||||
state: &TuiState,
|
||||
next_render: &mut Instant,
|
||||
force: bool,
|
||||
) -> io::Result<()> {
|
||||
let now = Instant::now();
|
||||
if should_render(now, *next_render, state.paused, force) {
|
||||
terminal.draw(state)?;
|
||||
*next_render = now + RENDER_INTERVAL;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn managed_tui_wait_duration(next_render: &Instant, paused: bool) -> Duration {
|
||||
let render_wait = if paused {
|
||||
TUI_WAIT_SLICE
|
||||
} else {
|
||||
next_render.saturating_duration_since(Instant::now())
|
||||
};
|
||||
render_wait.min(TUI_WAIT_SLICE)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ratatui::{backend::TestBackend, Terminal};
|
||||
|
||||
#[test]
|
||||
fn subscription_lag_keeps_incomplete_statistics_visible_while_paused() {
|
||||
for paused in [false, true] {
|
||||
// The production subscription is a broadcast receiver. Overflow the
|
||||
// real channel rather than synthesizing a lag result or UI state.
|
||||
let (sender, mut events) = tokio::sync::broadcast::channel(1);
|
||||
sender.send(ManagedEvent::Started).unwrap();
|
||||
sender.send(ManagedEvent::Started).unwrap();
|
||||
let mut state = TuiState::default();
|
||||
if paused {
|
||||
state.toggle_pause();
|
||||
}
|
||||
let mut dropped = 0;
|
||||
let mut terminal_targets = HashSet::new();
|
||||
drain_tui_events(&mut events, &mut state, &mut terminal_targets, &mut dropped);
|
||||
assert_eq!(dropped, 1);
|
||||
let mut terminal = Terminal::new(TestBackend::new(200, 40)).unwrap();
|
||||
for _ in 0..2 {
|
||||
// Empty drains must not clear the warning on later redraws.
|
||||
drain_tui_events(&mut events, &mut state, &mut terminal_targets, &mut dropped);
|
||||
terminal
|
||||
.draw(|frame| super::super::ui::draw_dashboard(frame, &state))
|
||||
.unwrap();
|
||||
let text = terminal
|
||||
.backend()
|
||||
.buffer()
|
||||
.content()
|
||||
.iter()
|
||||
.map(|cell| cell.symbol())
|
||||
.collect::<String>();
|
||||
assert!(text.contains("incomplete:dropped=1"));
|
||||
assert_eq!(text.contains("display paused"), paused);
|
||||
}
|
||||
}
|
||||
ManagedEvent::TargetStateChanged {
|
||||
target,
|
||||
lifecycle:
|
||||
ManagedTargetLifecycle::Pending
|
||||
| ManagedTargetLifecycle::Connecting
|
||||
| ManagedTargetLifecycle::Opening,
|
||||
} => {
|
||||
state.process_target_opening(&target);
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
+1197
-994
File diff suppressed because it is too large.
Load diff
@@ -1,9 +1,9 @@
|
||||
#[cfg(any(feature = "client", feature = "server", feature = "tui"))]
|
||||
#[cfg(feature = "server")]
|
||||
use std::sync::{atomic::AtomicBool, Arc};
|
||||
use std::{env, ffi::OsString, process::ExitCode};
|
||||
|
||||
use crate::applet::{dispatch_from_argv, AppletDispatch, RequestedApplet};
|
||||
#[cfg(any(feature = "client", feature = "server", feature = "tui"))]
|
||||
#[cfg(feature = "server")]
|
||||
use crate::signal::install_signal_handler;
|
||||
|
||||
/// Entry point for the `irtt-rs` multicall dispatcher binary.
|
||||
@@ -75,9 +75,9 @@ fn run_dispatcher_from_env() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Installs the shutdown signal handler shared by every applet and hands the
|
||||
/// Installs the shutdown signal handler for synchronous applets and hands the
|
||||
/// resulting flag to `f`.
|
||||
#[cfg(any(feature = "client", feature = "server", feature = "tui"))]
|
||||
#[cfg(feature = "server")]
|
||||
fn with_shutdown_flag(
|
||||
f: impl FnOnce(&AtomicBool) -> Result<(), Box<dyn std::error::Error>>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
@@ -91,10 +91,13 @@ fn with_shutdown_flag(
|
||||
fn run_client_applet(argv: Vec<OsString>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
use clap::Parser;
|
||||
|
||||
with_shutdown_flag(|shutdown_requested| {
|
||||
let args = crate::cmd::client::ClientArgs::parse_from(argv);
|
||||
crate::cmd::client::run_stream(args, shutdown_requested)
|
||||
})
|
||||
let shutdown = crate::signal::install_async_signal_handler()
|
||||
.map_err(|err| format!("failed to install signal handler: {err}"))?;
|
||||
let args = crate::cmd::client::ClientArgs::parse_from(argv);
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?
|
||||
.block_on(crate::cmd::client::run_stream(args, shutdown))
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "client"))]
|
||||
@@ -121,10 +124,13 @@ fn run_server_applet(_argv: Vec<OsString>) -> Result<(), Box<dyn std::error::Err
|
||||
fn run_tui_applet(argv: Vec<OsString>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
use clap::Parser;
|
||||
|
||||
with_shutdown_flag(|shutdown_requested| {
|
||||
let args = crate::cmd::tui::TuiArgs::parse_from(argv);
|
||||
crate::cmd::tui::run_tui(args, shutdown_requested)
|
||||
})
|
||||
let shutdown = crate::signal::install_async_signal_handler()
|
||||
.map_err(|err| format!("failed to install signal handler: {err}"))?;
|
||||
let args = crate::cmd::tui::TuiArgs::parse_from(argv);
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?
|
||||
.block_on(crate::cmd::tui::run_tui(args, shutdown))
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "tui"))]
|
||||
|
||||
@@ -6,6 +6,18 @@ use irtt_proto::{Clock, ReceivedStats, StampAt};
|
||||
|
||||
#[derive(Clone, clap::Args)]
|
||||
pub struct CommonClientArgs {
|
||||
/// Use IPv4 only.
|
||||
#[arg(short = '4', long, conflicts_with_all = ["ipv6", "dual_stack"])]
|
||||
pub ipv4: bool,
|
||||
|
||||
/// Use IPv6 only.
|
||||
#[arg(short = '6', long, conflicts_with_all = ["ipv4", "dual_stack"])]
|
||||
pub ipv6: bool,
|
||||
|
||||
/// Probe available IPv4 and IPv6 paths separately for each hostname.
|
||||
#[arg(long, conflicts_with_all = ["ipv4", "ipv6"], long_help = "Probe available IPv4 and IPv6 paths separately for each hostname. Labels gain /v4 or /v6; IP literals remain single targets. Families are discovered when a target is added; reconnects resolve the original hostname again within the chosen family.")]
|
||||
pub dual_stack: bool,
|
||||
|
||||
/// Probe interval.
|
||||
#[arg(long, default_value = "1s", value_parser = parse_duration)]
|
||||
pub interval: Duration,
|
||||
@@ -66,6 +78,9 @@ pub struct CommonClientArgs {
|
||||
impl fmt::Debug for CommonClientArgs {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("CommonClientArgs")
|
||||
.field("ipv4", &self.ipv4)
|
||||
.field("ipv6", &self.ipv6)
|
||||
.field("dual_stack", &self.dual_stack)
|
||||
.field("interval", &self.interval)
|
||||
.field("length", &self.length)
|
||||
.field("hmac", &self.hmac.as_ref().map(|_| "[REDACTED]"))
|
||||
|
||||
@@ -1,18 +1,29 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use irtt_client::{
|
||||
Authentication, ClientConfig, HmacKey, NegotiationPolicy, OpenPolicy, SessionRequest,
|
||||
SocketConfig,
|
||||
AddressFamily, Authentication, ClientConfig, HmacKey, NegotiationPolicy, OpenPolicy,
|
||||
SessionRequest, SocketConfig,
|
||||
};
|
||||
|
||||
use super::args::CommonClientArgs;
|
||||
|
||||
impl CommonClientArgs {
|
||||
pub fn address_family(&self) -> AddressFamily {
|
||||
if self.ipv4 {
|
||||
AddressFamily::Ipv4
|
||||
} else if self.ipv6 {
|
||||
AddressFamily::Ipv6
|
||||
} else {
|
||||
AddressFamily::Any
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the shared client configuration template for a managed run.
|
||||
///
|
||||
/// The config is endpoint-independent; managed targets own their addresses.
|
||||
pub fn to_client_config(&self, duration: Duration) -> ClientConfig {
|
||||
ClientConfig {
|
||||
address_family: self.address_family(),
|
||||
open: OpenPolicy {
|
||||
negotiation: if self.loose {
|
||||
NegotiationPolicy::Loose
|
||||
|
||||
@@ -3,6 +3,7 @@ pub mod config;
|
||||
pub mod prepare;
|
||||
pub mod session;
|
||||
pub mod targets;
|
||||
pub(crate) mod worker;
|
||||
|
||||
pub use args::{
|
||||
parse_dscp, parse_duration, parse_length, parse_server_fill, parse_test_duration, parse_ttl,
|
||||
@@ -13,8 +14,7 @@ pub use prepare::{
|
||||
prepare_managed_run, ManagedRunSetup, TargetSelection, MANAGED_EVENT_CAPACITY,
|
||||
STDIN_MAX_DESIRED_TARGETS, STDIN_MAX_LIVE_TARGET_GENERATIONS, STDIN_OUTCOME_HISTORY_LIMIT,
|
||||
};
|
||||
pub use session::is_shutdown_requested;
|
||||
pub use targets::{
|
||||
parse_stdin_target_set, parse_target, prepare_managed_targets, target_specs, GroupPacingArg,
|
||||
PreparedTarget, TargetArg, TargetSpec,
|
||||
parse_stdin_target_set, parse_target, target_specs, GroupPacingArg, PreparedTarget, TargetArg,
|
||||
TargetPreparation, TargetSpec,
|
||||
};
|
||||
@@ -16,7 +16,7 @@ use irtt_client::{
|
||||
|
||||
use super::{
|
||||
args::CommonClientArgs,
|
||||
targets::{prepare_managed_targets, target_specs, GroupPacingArg, TargetArg},
|
||||
targets::{target_specs, GroupPacingArg, TargetArg, TargetPreparation},
|
||||
PreparedTarget,
|
||||
};
|
||||
|
||||
@@ -52,6 +52,8 @@ pub struct ManagedRunSetup {
|
||||
pub pacing: ManagedPacing,
|
||||
/// Whether this stream may replace its desired set from stdin.
|
||||
pub stdin_controlled: bool,
|
||||
/// Preparation policy and family selections for later stdin declarations.
|
||||
pub preparation: TargetPreparation,
|
||||
}
|
||||
|
||||
impl ManagedRunSetup {
|
||||
@@ -98,7 +100,7 @@ impl ManagedRunSetup {
|
||||
///
|
||||
/// Targets are validated before any configuration is built, so a bad target set
|
||||
/// fails before the caller can act on a half-prepared run.
|
||||
pub fn prepare_managed_run(
|
||||
pub async fn prepare_managed_run(
|
||||
common: &CommonClientArgs,
|
||||
duration: Duration,
|
||||
selection: TargetSelection<'_>,
|
||||
@@ -113,11 +115,19 @@ pub fn prepare_managed_run(
|
||||
} else {
|
||||
target_specs(selection.targets)?
|
||||
};
|
||||
let targets = prepare_managed_targets(specs)?;
|
||||
let (targets, preparation) = TargetPreparation::new(common.address_family(), common.dual_stack)
|
||||
.prepare(
|
||||
specs,
|
||||
selection
|
||||
.stdin_controlled
|
||||
.then_some(STDIN_MAX_DESIRED_TARGETS),
|
||||
)
|
||||
.await?;
|
||||
Ok(ManagedRunSetup {
|
||||
targets,
|
||||
client: common.to_client_config(duration),
|
||||
pacing: selection.pacing.into(),
|
||||
stdin_controlled: selection.stdin_controlled,
|
||||
preparation,
|
||||
})
|
||||
}
|
||||
@@ -1,42 +1,20 @@
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
#[cfg(any(feature = "client", feature = "tui"))]
|
||||
use irtt_client::managed::{ManagedEvent, ManagedEventSubscription, ManagedEventTryRecvError};
|
||||
|
||||
#[cfg(any(feature = "client", feature = "tui"))]
|
||||
pub(crate) const MANAGED_EVENT_WORK_BUDGET: usize = 128;
|
||||
|
||||
#[cfg(any(feature = "client", feature = "tui"))]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum ManagedDrainState {
|
||||
Empty,
|
||||
Closed,
|
||||
BudgetExhausted,
|
||||
}
|
||||
|
||||
/// Drain a bounded amount of lossy presentation work before returning to the
|
||||
/// frontend control loop.
|
||||
#[cfg(any(feature = "client", feature = "tui"))]
|
||||
pub(crate) fn drain_managed_events<E>(
|
||||
pub(crate) fn drain_final_events<E>(
|
||||
events: &mut ManagedEventSubscription,
|
||||
dropped_events: &mut u64,
|
||||
mut process: impl FnMut(ManagedEvent) -> Result<(), E>,
|
||||
) -> Result<ManagedDrainState, E> {
|
||||
for _ in 0..MANAGED_EVENT_WORK_BUDGET {
|
||||
) -> Result<(), E> {
|
||||
loop {
|
||||
match events.try_recv() {
|
||||
Ok(event) => process(event)?,
|
||||
Err(ManagedEventTryRecvError::Empty | ManagedEventTryRecvError::Closed) => break,
|
||||
Err(ManagedEventTryRecvError::Lagged(count)) => {
|
||||
*dropped_events = dropped_events.saturating_add(count);
|
||||
}
|
||||
Err(ManagedEventTryRecvError::Empty) => return Ok(ManagedDrainState::Empty),
|
||||
Err(ManagedEventTryRecvError::Closed) => return Ok(ManagedDrainState::Closed),
|
||||
}
|
||||
}
|
||||
Ok(ManagedDrainState::BudgetExhausted)
|
||||
}
|
||||
|
||||
pub fn is_shutdown_requested(shutdown_requested: &AtomicBool) -> bool {
|
||||
shutdown_requested.load(Ordering::Relaxed)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn should_print_final_summary(continuous: bool, interrupted: bool) -> bool {
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
use std::{collections::HashSet, fmt};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
fmt, io,
|
||||
net::{IpAddr, SocketAddr},
|
||||
};
|
||||
|
||||
use clap::ValueEnum;
|
||||
use irtt_client::{
|
||||
managed::{ManagedPacing, ManagedTargetConfig, TargetAuth, TargetId},
|
||||
Authentication, HmacKey,
|
||||
AddressFamily, Authentication, HmacKey,
|
||||
};
|
||||
|
||||
/// One raw positional target captured by Clap.
|
||||
@@ -63,6 +67,7 @@ impl fmt::Debug for PreparedTarget {
|
||||
f.debug_struct("PreparedTarget")
|
||||
.field("label", &self.label)
|
||||
.field("server_addr", &self.managed.server_addr)
|
||||
.field("address_family", &self.managed.address_family)
|
||||
.field("auth", &self.managed.auth)
|
||||
.finish()
|
||||
}
|
||||
@@ -257,17 +262,146 @@ pub fn target_specs(targets: &[TargetArg]) -> Result<Vec<TargetSpec>, String> {
|
||||
target_specs_with_empty(targets, false)
|
||||
}
|
||||
|
||||
pub fn prepare_managed_targets(specs: Vec<TargetSpec>) -> Result<Vec<PreparedTarget>, String> {
|
||||
let mut targets = Vec::with_capacity(specs.len());
|
||||
for spec in specs {
|
||||
let mut managed = ManagedTargetConfig::new(TargetId::from(spec.label.clone()), spec.addr);
|
||||
managed.auth = spec.auth;
|
||||
targets.push(PreparedTarget {
|
||||
label: spec.label,
|
||||
managed,
|
||||
});
|
||||
/// Family selections for the current desired declarations, never resolved IPs.
|
||||
/// Unchanged stdin declarations retain their selections; removed declarations
|
||||
/// are forgotten, so adding them again performs fresh discovery.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TargetPreparation {
|
||||
family: AddressFamily,
|
||||
dual_stack: bool,
|
||||
discovered: HashMap<String, (String, [bool; 2])>,
|
||||
}
|
||||
|
||||
impl TargetPreparation {
|
||||
pub fn new(family: AddressFamily, dual_stack: bool) -> Self {
|
||||
Self {
|
||||
family,
|
||||
dual_stack,
|
||||
discovered: HashMap::new(),
|
||||
}
|
||||
}
|
||||
Ok(targets)
|
||||
|
||||
pub async fn prepare(
|
||||
mut self,
|
||||
specs: Vec<TargetSpec>,
|
||||
maximum_targets: Option<usize>,
|
||||
) -> Result<(Vec<PreparedTarget>, Self), String> {
|
||||
let mut targets = Vec::with_capacity(specs.len());
|
||||
let mut discovered = HashMap::new();
|
||||
let mut labels = HashSet::new();
|
||||
for (index, spec) in specs.into_iter().enumerate() {
|
||||
let families = if let Some(ip) = literal_ip(&spec.addr) {
|
||||
match (self.family, ip) {
|
||||
(AddressFamily::Ipv4, IpAddr::V6(_)) => {
|
||||
return Err(format!(
|
||||
"target {}: IPv6 literal conflicts with --ipv4",
|
||||
index + 1
|
||||
))
|
||||
}
|
||||
(AddressFamily::Ipv6, IpAddr::V4(_)) => {
|
||||
return Err(format!(
|
||||
"target {}: IPv4 literal conflicts with --ipv6",
|
||||
index + 1
|
||||
))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
} else if self.dual_stack {
|
||||
let families = match self.discovered.get(&spec.label) {
|
||||
Some((addr, families)) if *addr == spec.addr => *families,
|
||||
_ => {
|
||||
let endpoint = discovery_endpoint(&spec.addr)
|
||||
.map_err(|error| format!("target {}: {error}", index + 1))?;
|
||||
discovery_families(tokio::net::lookup_host(endpoint).await)
|
||||
.map_err(|error| format!("target {}: {error}", index + 1))?
|
||||
}
|
||||
};
|
||||
discovered.insert(spec.label.clone(), (spec.addr.clone(), families));
|
||||
Some(families)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
for target in expand_target(spec, families) {
|
||||
if !labels.insert(target.label.clone()) {
|
||||
return Err("duplicate target label after address-family expansion".to_owned());
|
||||
}
|
||||
targets.push(target);
|
||||
}
|
||||
if let Some(maximum) = maximum_targets.filter(|maximum| targets.len() > *maximum) {
|
||||
return Err(format!(
|
||||
"target set exceeds the {maximum}-target limit after address-family expansion"
|
||||
));
|
||||
}
|
||||
}
|
||||
self.discovered = discovered;
|
||||
Ok((targets, self))
|
||||
}
|
||||
}
|
||||
|
||||
fn literal_ip(endpoint: &str) -> Option<IpAddr> {
|
||||
endpoint
|
||||
.parse::<SocketAddr>()
|
||||
.map(|addr| addr.ip())
|
||||
.ok()
|
||||
.or_else(|| endpoint.parse().ok())
|
||||
.or_else(|| {
|
||||
if endpoint.starts_with('[') && endpoint.ends_with(']') {
|
||||
format!("{endpoint}:2112")
|
||||
.parse::<SocketAddr>()
|
||||
.ok()
|
||||
.map(|addr| addr.ip())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn discovery_endpoint(endpoint: &str) -> Result<(&str, u16), String> {
|
||||
match endpoint.rsplit_once(':') {
|
||||
Some((host, port)) => {
|
||||
let port = port.parse().map_err(|_| "invalid server port".to_owned())?;
|
||||
if host.is_empty() || host.contains(':') || host.starts_with('[') {
|
||||
return Err("invalid server endpoint".to_owned());
|
||||
}
|
||||
Ok((host, port))
|
||||
}
|
||||
None => Ok((endpoint, 2112)),
|
||||
}
|
||||
}
|
||||
|
||||
fn discovery_families(
|
||||
result: io::Result<impl Iterator<Item = SocketAddr>>,
|
||||
) -> Result<[bool; 2], String> {
|
||||
let mut families = [false; 2];
|
||||
for addr in result.map_err(|_| "DNS lookup failed".to_owned())? {
|
||||
families[usize::from(addr.is_ipv6())] = true;
|
||||
}
|
||||
if families == [false; 2] {
|
||||
return Err("DNS lookup returned no usable addresses".to_owned());
|
||||
}
|
||||
Ok(families)
|
||||
}
|
||||
|
||||
fn expand_target(spec: TargetSpec, families: Option<[bool; 2]>) -> Vec<PreparedTarget> {
|
||||
let selections = match families {
|
||||
None => vec![(spec.label, None)],
|
||||
Some(families) => [("v4", AddressFamily::Ipv4), ("v6", AddressFamily::Ipv6)]
|
||||
.into_iter()
|
||||
.zip(families)
|
||||
.filter(|(_, exists)| *exists)
|
||||
.map(|((suffix, family), _)| (format!("{}/{suffix}", spec.label), Some(family)))
|
||||
.collect(),
|
||||
};
|
||||
selections
|
||||
.into_iter()
|
||||
.map(|(label, family)| {
|
||||
let mut managed =
|
||||
ManagedTargetConfig::new(TargetId::from(label.clone()), spec.addr.clone());
|
||||
managed.address_family = family;
|
||||
managed.auth = spec.auth.clone();
|
||||
PreparedTarget { label, managed }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Parse one complete stdin target set after its line terminator was removed.
|
||||
@@ -277,7 +411,7 @@ pub fn prepare_managed_targets(specs: Vec<TargetSpec>) -> Result<Vec<PreparedTar
|
||||
pub fn parse_stdin_target_set(
|
||||
record: &str,
|
||||
maximum_targets: usize,
|
||||
) -> Result<Vec<PreparedTarget>, String> {
|
||||
) -> Result<Vec<TargetSpec>, String> {
|
||||
if record == "[]" {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
@@ -288,5 +422,5 @@ pub fn parse_stdin_target_set(
|
||||
));
|
||||
}
|
||||
let args = elements.into_iter().map(TargetArg::new).collect::<Vec<_>>();
|
||||
prepare_managed_targets(target_specs_with_empty(&args, true)?)
|
||||
target_specs_with_empty(&args, true)
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
use std::{io, thread, time::Duration};
|
||||
|
||||
use irtt_client::managed::{ManagedClientHandle, ManagedClientTask, ManagedOutcome};
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
/// Keeps measurement scheduling separate from frontend output and statistics.
|
||||
/// Drop also stops and joins on frontend errors or cancellation.
|
||||
pub(crate) struct ManagedWorker {
|
||||
handle: ManagedClientHandle,
|
||||
thread: Option<thread::JoinHandle<()>>,
|
||||
completion: oneshot::Receiver<io::Result<ManagedOutcome>>,
|
||||
}
|
||||
|
||||
impl ManagedWorker {
|
||||
pub(crate) fn start(task: ManagedClientTask, handle: ManagedClientHandle) -> io::Result<Self> {
|
||||
let (completed, completion) = oneshot::channel();
|
||||
let thread = thread::Builder::new()
|
||||
.name("irtt-managed".to_owned())
|
||||
.spawn(move || {
|
||||
// Construct and destroy the runtime on its owning thread, including
|
||||
// startup failures; never drop a runtime inside the frontend executor.
|
||||
let outcome = (|| {
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?;
|
||||
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
runtime.block_on(task)
|
||||
}));
|
||||
runtime.shutdown_timeout(Duration::from_millis(100));
|
||||
match outcome {
|
||||
Ok(outcome) => Ok(outcome),
|
||||
Err(panic) => std::panic::resume_unwind(panic),
|
||||
}
|
||||
})();
|
||||
let _ = completed.send(outcome);
|
||||
})?;
|
||||
Ok(Self {
|
||||
handle,
|
||||
thread: Some(thread),
|
||||
completion,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn join(mut self) -> Result<ManagedOutcome, Box<dyn std::error::Error>> {
|
||||
let outcome = (&mut self.completion).await;
|
||||
self.join_thread()?;
|
||||
Ok(outcome
|
||||
.map_err(|_| io::Error::other("managed worker completed without an outcome"))??)
|
||||
}
|
||||
|
||||
fn join_thread(&mut self) -> io::Result<()> {
|
||||
self.thread
|
||||
.take()
|
||||
.expect("managed worker is joined once")
|
||||
.join()
|
||||
.map_err(|_| io::Error::other("managed worker thread panicked"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ManagedWorker {
|
||||
fn drop(&mut self) {
|
||||
if self.thread.is_some() {
|
||||
drop(self.handle.stop());
|
||||
let _ = self.join_thread();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,22 @@
|
||||
#[cfg(feature = "server")]
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
};
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub fn install_signal_handler(shutdown_requested: Arc<AtomicBool>) -> Result<(), ctrlc::Error> {
|
||||
ctrlc::set_handler(move || {
|
||||
shutdown_requested.store(true, Ordering::Relaxed);
|
||||
})
|
||||
}
|
||||
|
||||
/// Preserve ctrlc's SIGINT/SIGTERM policy while waking the async frontends.
|
||||
#[cfg(any(feature = "client", feature = "tui"))]
|
||||
pub fn install_async_signal_handler() -> Result<tokio::sync::watch::Receiver<bool>, ctrlc::Error> {
|
||||
let (shutdown, receiver) = tokio::sync::watch::channel(false);
|
||||
ctrlc::set_handler(move || {
|
||||
shutdown.send_replace(true);
|
||||
})?;
|
||||
Ok(receiver)
|
||||
}
|
||||
@@ -1,487 +0,0 @@
|
||||
#[cfg(feature = "client")]
|
||||
#[path = "support/in_tree_server.rs"]
|
||||
mod in_tree_server;
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
use irtt_server::ServerConfig;
|
||||
use std::process::Command;
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
struct ClientProcess(std::process::Child);
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
impl Drop for ClientProcess {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.0.kill();
|
||||
let _ = self.0.wait();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
impl ClientProcess {
|
||||
fn wait(&mut self) -> std::process::ExitStatus {
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||
loop {
|
||||
if let Some(status) = self.0.try_wait().unwrap() {
|
||||
return status;
|
||||
}
|
||||
assert!(
|
||||
std::time::Instant::now() < deadline,
|
||||
"client did not terminate within five seconds"
|
||||
);
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dispatch/help/availability belong to the application and run in every build.
|
||||
#[test]
|
||||
fn dispatcher_reports_and_runs_the_enabled_applets() {
|
||||
let dispatcher = env!("CARGO_BIN_EXE_irtt-rs");
|
||||
let help = Command::new(dispatcher).arg("--help").output().unwrap();
|
||||
assert!(help.status.success());
|
||||
let help = String::from_utf8(help.stdout).unwrap();
|
||||
for (applet, enabled) in [
|
||||
("client", cfg!(feature = "client")),
|
||||
("server", cfg!(feature = "server")),
|
||||
("tui", cfg!(feature = "tui")),
|
||||
] {
|
||||
let row = help
|
||||
.lines()
|
||||
.find(|line| line.trim_start().starts_with(applet))
|
||||
.unwrap();
|
||||
assert_eq!(row.contains("not available"), !enabled);
|
||||
let output = Command::new(dispatcher)
|
||||
.args([applet, "--help"])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert_eq!(output.status.success(), enabled, "{applet}");
|
||||
if !enabled {
|
||||
assert!(String::from_utf8(output.stderr)
|
||||
.unwrap()
|
||||
.contains("not available"));
|
||||
}
|
||||
}
|
||||
let binaries = [
|
||||
dispatcher,
|
||||
#[cfg(feature = "client")]
|
||||
env!("CARGO_BIN_EXE_irtt-client"),
|
||||
#[cfg(feature = "server")]
|
||||
env!("CARGO_BIN_EXE_irtt-server"),
|
||||
#[cfg(feature = "tui")]
|
||||
env!("CARGO_BIN_EXE_irtt-tui"),
|
||||
];
|
||||
for binary in binaries {
|
||||
assert!(Command::new(binary)
|
||||
.arg("--help")
|
||||
.output()
|
||||
.unwrap()
|
||||
.status
|
||||
.success());
|
||||
assert!(!Command::new(binary)
|
||||
.arg("--invalid-option")
|
||||
.output()
|
||||
.unwrap()
|
||||
.status
|
||||
.success());
|
||||
}
|
||||
}
|
||||
|
||||
// The default presentation must reach a meaningful packet summary for a finite run.
|
||||
#[cfg(feature = "client")]
|
||||
#[test]
|
||||
fn default_client_output_emits_a_packet_summary() {
|
||||
use std::{io::Read, process::Stdio, thread};
|
||||
let server = in_tree_server::InTreeServer::start(ServerConfig::default());
|
||||
let mut client = ClientProcess(
|
||||
Command::new(env!("CARGO_BIN_EXE_irtt-client"))
|
||||
.env_clear()
|
||||
.args([
|
||||
"--duration",
|
||||
"100ms",
|
||||
"--interval",
|
||||
"20ms",
|
||||
&server.addr.to_string(),
|
||||
])
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
let mut stdout = client.0.stdout.take().unwrap();
|
||||
let stdout = thread::spawn(move || {
|
||||
let mut text = String::new();
|
||||
stdout.read_to_string(&mut text).unwrap();
|
||||
text
|
||||
});
|
||||
let mut stderr = client.0.stderr.take().unwrap();
|
||||
let stderr = thread::spawn(move || {
|
||||
let mut text = String::new();
|
||||
stderr.read_to_string(&mut text).unwrap();
|
||||
text
|
||||
});
|
||||
let status = client.wait();
|
||||
let stdout = stdout.join().unwrap();
|
||||
let stderr = stderr.join().unwrap();
|
||||
assert!(status.success(), "{stderr}");
|
||||
assert!(stdout.contains("irtt-rs summary"), "{stdout}");
|
||||
let packets = stdout
|
||||
.lines()
|
||||
.find(|line| line.starts_with("packets:"))
|
||||
.expect("default output must include the packet summary");
|
||||
for field in ["sent=", "received="] {
|
||||
let count = packets
|
||||
.split_whitespace()
|
||||
.find_map(|word| word.strip_prefix(field))
|
||||
.unwrap()
|
||||
.parse::<u64>()
|
||||
.unwrap();
|
||||
assert!(
|
||||
count > 0,
|
||||
"summary must report {field} a positive count: {packets}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
#[test]
|
||||
fn target_authentication_inherits_overrides_or_disables_the_global_key() {
|
||||
use clap::Parser;
|
||||
use irtt_app::cmd::client::args::ClientArgs;
|
||||
use irtt_client::{managed::TargetAuth, Authentication, HmacKey};
|
||||
let args = ClientArgs::try_parse_from([
|
||||
"irtt-client",
|
||||
"--hmac",
|
||||
"CONSPICUOUS_GLOBAL_HMAC_KEY",
|
||||
"inherit=127.0.0.1:2112",
|
||||
"override=127.0.0.1:2113@hmac=CONSPICUOUS_TARGET_HMAC_KEY",
|
||||
"disable=127.0.0.1:2114@hmac=",
|
||||
])
|
||||
.unwrap();
|
||||
let setup = args.prepare().unwrap();
|
||||
assert_eq!(
|
||||
setup.managed_config().client.auth,
|
||||
Authentication::Hmac(HmacKey::new(b"CONSPICUOUS_GLOBAL_HMAC_KEY".as_slice()))
|
||||
);
|
||||
let targets = setup.managed_targets();
|
||||
let authentication: Vec<_> = targets.iter().map(|target| target.auth.clone()).collect();
|
||||
assert_eq!(
|
||||
authentication,
|
||||
[
|
||||
TargetAuth::Inherit,
|
||||
TargetAuth::Override(Authentication::Hmac(HmacKey::new(
|
||||
b"CONSPICUOUS_TARGET_HMAC_KEY".as_slice()
|
||||
))),
|
||||
TargetAuth::Override(Authentication::Unauthenticated)
|
||||
]
|
||||
);
|
||||
for output in [format!("{args:?}"), format!("{setup:?}")] {
|
||||
assert!(!output.contains("CONSPICUOUS_GLOBAL_HMAC_KEY"));
|
||||
assert!(!output.contains("CONSPICUOUS_TARGET_HMAC_KEY"));
|
||||
}
|
||||
}
|
||||
|
||||
// A real authenticated server detects broken client argument-to-config mapping.
|
||||
#[cfg(feature = "client")]
|
||||
#[test]
|
||||
fn client_arguments_export_a_real_client_run() {
|
||||
use std::{io::Read, process::Stdio, thread};
|
||||
let dispatcher = env!("CARGO_BIN_EXE_irtt-rs");
|
||||
for option in ["--dscp", "--ttl"] {
|
||||
assert!(!Command::new(dispatcher)
|
||||
.args(["client", option, "256", "127.0.0.1:9"])
|
||||
.output()
|
||||
.unwrap()
|
||||
.status
|
||||
.success());
|
||||
}
|
||||
let server =
|
||||
in_tree_server::InTreeServer::start(ServerConfig::default().with_hmac_key(b"cli-test"));
|
||||
let mut client = ClientProcess(
|
||||
Command::new(dispatcher)
|
||||
.args([
|
||||
"client",
|
||||
"--duration",
|
||||
"100ms",
|
||||
"--interval",
|
||||
"20ms",
|
||||
"--hmac",
|
||||
"cli-test",
|
||||
"--length",
|
||||
"128",
|
||||
"--sfill",
|
||||
"pattern:ab",
|
||||
"--clock",
|
||||
"wall",
|
||||
"--tstamp",
|
||||
"both",
|
||||
"--stats",
|
||||
"both",
|
||||
"--ttl",
|
||||
"64",
|
||||
"--format",
|
||||
"jsonl",
|
||||
"--columns",
|
||||
"target,event,bytes",
|
||||
&format!("edge={}", server.addr),
|
||||
])
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
// Drain both pipes while waiting so output cannot block client termination.
|
||||
let mut stdout = client.0.stdout.take().unwrap();
|
||||
let stdout = thread::spawn(move || {
|
||||
let mut bytes = Vec::new();
|
||||
stdout.read_to_end(&mut bytes).unwrap();
|
||||
bytes
|
||||
});
|
||||
let mut stderr = client.0.stderr.take().unwrap();
|
||||
let stderr = thread::spawn(move || {
|
||||
let mut bytes = Vec::new();
|
||||
stderr.read_to_end(&mut bytes).unwrap();
|
||||
bytes
|
||||
});
|
||||
let status = client.wait();
|
||||
let stdout = stdout.join().unwrap();
|
||||
let stderr = stderr.join().unwrap();
|
||||
assert!(status.success(), "{}", String::from_utf8_lossy(&stderr));
|
||||
let stdout = String::from_utf8(stdout).unwrap();
|
||||
let rows: Vec<_> = stdout.lines().collect();
|
||||
assert!(!rows.is_empty());
|
||||
assert!(rows.iter().all(|row| row.starts_with('{')
|
||||
&& row.ends_with('}')
|
||||
&& row.contains("\"target\":\"edge\"")));
|
||||
assert!(rows
|
||||
.iter()
|
||||
.any(|row| row.contains("\"event\":\"echo_reply\"") && row.contains("\"bytes\":128")));
|
||||
assert!(rows
|
||||
.iter()
|
||||
.any(|row| row.contains("\"event\":\"session_closed\"")));
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "client", feature = "tui"))]
|
||||
#[test]
|
||||
fn malformed_target_diagnostics_do_not_expose_authentication_keys() {
|
||||
let secret = "malformed-target-secret-92817";
|
||||
for applet in [
|
||||
#[cfg(feature = "client")]
|
||||
"client",
|
||||
#[cfg(feature = "tui")]
|
||||
"tui",
|
||||
] {
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_irtt-rs"))
|
||||
.args([applet, &format!(r"edge=127.0.0.1:9@hmac={secret}\q")])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8(output.stderr).unwrap();
|
||||
assert!(
|
||||
!stderr.trim().is_empty(),
|
||||
"malformed target must report a diagnostic"
|
||||
);
|
||||
assert!(!stderr.contains(secret), "target authentication key leaked");
|
||||
assert!(!String::from_utf8_lossy(&output.stdout).contains(secret));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
#[test]
|
||||
fn server_arguments_resolve_binds_and_map_policy() {
|
||||
use clap::{CommandFactory, FromArgMatches, Parser};
|
||||
use irtt_app::cmd::server::ServerArgs;
|
||||
use irtt_server::TimestampAllowance;
|
||||
use std::time::Duration;
|
||||
// Isolate zero-argument defaults from the test runner's environment without
|
||||
// mutating process-global environment in this parallel test process.
|
||||
let defaults = ServerArgs::from_arg_matches(
|
||||
&ServerArgs::command()
|
||||
.mut_args(|arg| arg.env(None::<&str>))
|
||||
.try_get_matches_from(["irtt-server"])
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
defaults.resolve_binds(),
|
||||
vec![
|
||||
"[::]:2112".parse().unwrap(),
|
||||
"0.0.0.0:2112".parse().unwrap()
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
defaults.server_config(),
|
||||
irtt_server::ServerConfig::default()
|
||||
);
|
||||
let args = ServerArgs::try_parse_from([
|
||||
"irtt-server",
|
||||
"--bind",
|
||||
"127.0.0.1:2113",
|
||||
"--bind",
|
||||
"[::1]:2114",
|
||||
"--hmac",
|
||||
"server-cli-test",
|
||||
"--max-sessions",
|
||||
"3",
|
||||
"--max-packet-length",
|
||||
"96",
|
||||
"--min-interval",
|
||||
"25ms",
|
||||
"--burst",
|
||||
"2",
|
||||
"--idle-timeout",
|
||||
"8s",
|
||||
"--max-duration",
|
||||
"1m",
|
||||
"--timestamp-allowance",
|
||||
"single",
|
||||
"--no-dscp",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
args.resolve_binds(),
|
||||
vec![
|
||||
"127.0.0.1:2113".parse().unwrap(),
|
||||
"[::1]:2114".parse().unwrap()
|
||||
]
|
||||
);
|
||||
let config = args.server_config();
|
||||
assert_eq!(config.hmac_key(), Some(b"server-cli-test".as_slice()));
|
||||
assert_eq!(config.max_sessions(), 3);
|
||||
assert_eq!(config.max_packet_length(), 96);
|
||||
assert_eq!(config.min_send_interval(), Duration::from_millis(25));
|
||||
assert_eq!(config.burst_allowance(), 2);
|
||||
assert_eq!(config.idle_timeout(), Duration::from_secs(8));
|
||||
assert_eq!(config.max_test_duration(), Some(Duration::from_secs(60)));
|
||||
assert_eq!(config.timestamp_allowance(), TimestampAllowance::Single);
|
||||
assert!(!config.dscp_allowed());
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
#[test]
|
||||
fn server_command_line_binds_replace_the_environment_bind() {
|
||||
use std::net::UdpSocket;
|
||||
// Occupied ports give bounded, observable startup failures, without starting
|
||||
// a daemon or needing a process/signal harness merely to inspect arguments.
|
||||
let environment = UdpSocket::bind("127.0.0.1:0").unwrap();
|
||||
let explicit = UdpSocket::bind("127.0.0.1:0").unwrap();
|
||||
let environment_addr = environment.local_addr().unwrap().to_string();
|
||||
let explicit_addr = explicit.local_addr().unwrap().to_string();
|
||||
for use_explicit in [false, true] {
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_irtt-server"));
|
||||
command
|
||||
.env_clear()
|
||||
.env("IRTT_SERVER_BIND", &environment_addr)
|
||||
.env("IRTT_SERVER_MAX_SESSIONS", "invalid")
|
||||
.args(["--max-sessions", "3"]);
|
||||
if use_explicit {
|
||||
command.args(["--bind", "127.0.0.1:0", "--bind", &explicit_addr]);
|
||||
}
|
||||
let output = command.output().unwrap();
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8(output.stderr).unwrap();
|
||||
let expected = if use_explicit {
|
||||
&explicit_addr
|
||||
} else {
|
||||
&environment_addr
|
||||
};
|
||||
assert!(
|
||||
stderr.contains(expected),
|
||||
"startup must attempt the selected bind: {stderr}"
|
||||
);
|
||||
assert!(
|
||||
!stderr.contains("listening on"),
|
||||
"partial startup must not announce listeners"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Exercise stdin replacement and EOF through the actual application process.
|
||||
#[cfg(feature = "client")]
|
||||
#[test]
|
||||
fn targets_stdin_supersedes_the_active_set_and_stops_on_eof() {
|
||||
use std::{
|
||||
io::{BufRead, BufReader, Read, Write},
|
||||
process::Stdio,
|
||||
sync::mpsc,
|
||||
thread,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
let server = in_tree_server::InTreeServer::start(ServerConfig::default());
|
||||
let mut client = ClientProcess(
|
||||
Command::new(env!("CARGO_BIN_EXE_irtt-client"))
|
||||
.env_clear()
|
||||
.args([
|
||||
"--duration",
|
||||
"0",
|
||||
"--targets-stdin",
|
||||
"--interval",
|
||||
"20ms",
|
||||
"--format",
|
||||
"jsonl",
|
||||
"--columns",
|
||||
"target,event",
|
||||
])
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
let stdout = client.0.stdout.take().unwrap();
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let reader = thread::spawn(move || {
|
||||
for line in BufReader::new(stdout).lines() {
|
||||
if tx.send(line.unwrap()).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
let mut stdin = client.0.stdin.take().unwrap();
|
||||
let mut rows = Vec::new();
|
||||
for target in ["first", "second"] {
|
||||
writeln!(stdin, "{target}={}", server.addr).unwrap();
|
||||
stdin.flush().unwrap();
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
loop {
|
||||
let row = rx
|
||||
.recv_timeout(deadline.saturating_duration_since(Instant::now()))
|
||||
.expect("stdin target set did not produce a reply");
|
||||
let replied = row.contains(&format!("\"target\":\"{target}\""))
|
||||
&& row.contains("\"event\":\"echo_reply\"");
|
||||
rows.push(row);
|
||||
if replied {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Retirement can finish after the newer target's first reply.
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while !rows.iter().any(|row| {
|
||||
row.contains("\"target\":\"first\"") && row.contains("\"event\":\"session_closed\"")
|
||||
}) {
|
||||
rows.push(
|
||||
rx.recv_timeout(deadline.saturating_duration_since(Instant::now()))
|
||||
.expect("superseded stdin target did not retire"),
|
||||
);
|
||||
}
|
||||
drop(stdin);
|
||||
let status = client.wait();
|
||||
reader.join().unwrap();
|
||||
rows.extend(rx.try_iter());
|
||||
let mut stderr = String::new();
|
||||
client
|
||||
.0
|
||||
.stderr
|
||||
.take()
|
||||
.unwrap()
|
||||
.read_to_string(&mut stderr)
|
||||
.unwrap();
|
||||
assert!(status.success(), "{stderr}");
|
||||
assert!(rows
|
||||
.iter()
|
||||
.any(|row| row.contains("\"target\":\"second\"")
|
||||
&& row.contains("\"event\":\"session_closed\"")));
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
use std::{
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||
sync::mpsc,
|
||||
thread::{self, JoinHandle},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use irtt_server::{Server, ServerConfig};
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
const STARTUP_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
|
||||
pub struct InTreeServer {
|
||||
pub addr: SocketAddr,
|
||||
shutdown: Option<oneshot::Sender<()>>,
|
||||
done: Option<JoinHandle<Result<(), irtt_server::ServerRuntimeError>>>,
|
||||
}
|
||||
|
||||
impl InTreeServer {
|
||||
pub fn start(config: ServerConfig) -> Self {
|
||||
let (startup_tx, startup_rx) = mpsc::sync_channel(1);
|
||||
let (shutdown_tx, shutdown_rx) = oneshot::channel();
|
||||
let done = thread::spawn(move || {
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("failed to build in-tree test server runtime");
|
||||
runtime.block_on(async move {
|
||||
let bind_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0);
|
||||
let mut server = Server::bind(bind_addr, config).await?;
|
||||
startup_tx
|
||||
.send(server.local_addr()?)
|
||||
.expect("test stopped waiting for in-tree server startup");
|
||||
server
|
||||
.run(async {
|
||||
let _ = shutdown_rx.await;
|
||||
})
|
||||
.await
|
||||
})
|
||||
});
|
||||
|
||||
let addr = match startup_rx.recv_timeout(STARTUP_TIMEOUT) {
|
||||
Ok(addr) => addr,
|
||||
Err(error) => {
|
||||
let _ = shutdown_tx.send(());
|
||||
let result = done.join().expect("in-tree test server thread panicked");
|
||||
panic!("in-tree test server failed before startup ({error}): {result:?}");
|
||||
}
|
||||
};
|
||||
|
||||
Self {
|
||||
addr,
|
||||
shutdown: Some(shutdown_tx),
|
||||
done: Some(done),
|
||||
}
|
||||
}
|
||||
|
||||
fn stop_inner(&mut self) {
|
||||
if let Some(shutdown) = self.shutdown.take() {
|
||||
let _ = shutdown.send(());
|
||||
}
|
||||
if let Some(done) = self.done.take() {
|
||||
done.join()
|
||||
.expect("in-tree test server thread panicked")
|
||||
.expect("in-tree test server stopped with an error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for InTreeServer {
|
||||
fn drop(&mut self) {
|
||||
self.stop_inner();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "irtt-client"
|
||||
version = "0.5.3"
|
||||
version = "0.6.0"
|
||||
description = "IRTT-compatible Rust client library"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
@@ -59,9 +59,17 @@ Configuration is grouped by ownership:
|
||||
probe tracking policy.
|
||||
|
||||
Clone one config to connect to multiple endpoints. Managed targets retain their
|
||||
own ID, endpoint, and authentication inheritance/override; shared
|
||||
own ID, endpoint, address-family override, and authentication inheritance/override; shared
|
||||
`ManagedClientConfig.client` is reusable without endpoint mutation.
|
||||
|
||||
`ManagedTargetConfig.address_family` defaults to `None`, inheriting the shared
|
||||
family policy. `Some(AddressFamily::Ipv4)`, `Some(AddressFamily::Ipv6)`, or
|
||||
`Some(AddressFamily::Any)` overrides it for that target. Every new generation
|
||||
resolves the original endpoint again; callers can constrain a hostname without
|
||||
replacing it with a resolved IP. Changing the family specification replaces
|
||||
that target's generation, including switching between inheritance and an equal
|
||||
explicit override. Hostname expansion and discovery are frontend policies.
|
||||
|
||||
Blocking receive policy belongs to `Client`:
|
||||
`client.set_recv_timeout(Some(duration))?` sets it before or after opening,
|
||||
and `client.set_recv_timeout(None)?` restores the default indefinite wait.
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::{fmt, net::SocketAddr, sync::Arc, time::Duration};
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{Authentication, ClientConfig, ClientError, ClientEvent};
|
||||
use crate::{AddressFamily, Authentication, ClientConfig, ClientError, ClientEvent};
|
||||
|
||||
use super::TargetId;
|
||||
|
||||
@@ -48,11 +48,16 @@ impl TargetAuth {
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint and authentication specification for one managed target.
|
||||
/// Endpoint, address-family and authentication specification for one managed target.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct ManagedTargetConfig {
|
||||
pub id: TargetId,
|
||||
pub server_addr: String,
|
||||
/// Override the shared address-family policy. `None` inherits it.
|
||||
///
|
||||
/// The endpoint remains a hostname when supplied as one, so every new
|
||||
/// generation resolves it again using the effective family policy.
|
||||
pub address_family: Option<AddressFamily>,
|
||||
pub auth: TargetAuth,
|
||||
}
|
||||
|
||||
@@ -61,6 +66,7 @@ impl ManagedTargetConfig {
|
||||
Self {
|
||||
id: id.into(),
|
||||
server_addr: server_addr.into(),
|
||||
address_family: None,
|
||||
auth: TargetAuth::Inherit,
|
||||
}
|
||||
}
|
||||
@@ -86,7 +92,7 @@ pub enum ManagedCompletionPolicy {
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct ManagedClientConfig {
|
||||
/// Reusable session/socket configuration shared across target generations.
|
||||
/// Each target supplies its endpoint and resolves its authentication policy.
|
||||
/// Each target supplies its endpoint and resolves its family and authentication policies.
|
||||
pub client: ClientConfig,
|
||||
pub pacing: ManagedPacing,
|
||||
pub completion: ManagedCompletionPolicy,
|
||||
|
||||
@@ -397,6 +397,9 @@ fn build_task(
|
||||
.checked_add(1)
|
||||
.ok_or(ManagedConfigError::GenerationExhausted)?;
|
||||
let mut client_config = config.client.clone();
|
||||
client_config.address_family = target
|
||||
.address_family
|
||||
.unwrap_or(config.client.address_family);
|
||||
client_config.auth = target.auth.resolve(&config.client.auth);
|
||||
validate_target_config(&client_config).map_err(|source| {
|
||||
ManagedConfigError::InvalidTarget {
|
||||
|
||||
@@ -191,6 +191,9 @@ impl ManagedClientTask {
|
||||
return Err(ManagedCommandApplyError::DuplicateTargetId { id: target.id });
|
||||
}
|
||||
let mut client_config = self.config.client.clone();
|
||||
client_config.address_family = target
|
||||
.address_family
|
||||
.unwrap_or(self.config.client.address_family);
|
||||
client_config.auth = target.auth.resolve(&self.config.client.auth);
|
||||
validate_target_config(&client_config).map_err(|source| {
|
||||
ManagedCommandApplyError::InvalidTarget {
|
||||
|
||||
@@ -304,172 +304,3 @@ fn read_i64(packet: &[u8], pos: &mut usize) -> i64 {
|
||||
*pos += TIMESTAMP_SIZE;
|
||||
value
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
hmac,
|
||||
params::{Clock, ReceivedStats, StampAt},
|
||||
write_header, FLAG_HMAC, HMAC_SIZE, RECV_COUNT_SIZE, RECV_WINDOW_SIZE,
|
||||
};
|
||||
|
||||
fn default_params() -> Params {
|
||||
Params {
|
||||
received_stats: ReceivedStats::Both,
|
||||
stamp_at: StampAt::Both,
|
||||
clock: Clock::Both,
|
||||
..Params::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn params_with_payload_space(payload_space: usize) -> Params {
|
||||
let header_len = PacketLayout::echo(false, &Params::default()).header_len();
|
||||
Params {
|
||||
length: (header_len + payload_space) as i64,
|
||||
..Params::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero-fills every negotiated reply field, matching what a peer that
|
||||
/// reports no optional values would put on the wire.
|
||||
fn push_zeroed_layout_tail(layout: PacketLayout, out: &mut Vec<u8>) {
|
||||
if layout.recv_count {
|
||||
out.extend_from_slice(&[0; RECV_COUNT_SIZE]);
|
||||
}
|
||||
if layout.recv_window {
|
||||
out.extend_from_slice(&[0; RECV_WINDOW_SIZE]);
|
||||
}
|
||||
for _ in 0..layout.timestamp_count() {
|
||||
out.extend_from_slice(&[0; TIMESTAMP_SIZE]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_reply_decodes_default_fields() {
|
||||
let packet = [
|
||||
0x14, 0xa7, 0x5b, 0x02, 0x13, 0x52, 0x71, 0x87, 0xab, 0xb6, 0x96, 0x78, 0x02, 0x00,
|
||||
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0xb8, 0x1a, 0x33, 0x0c, 0x86, 0x6d, 0xaa, 0x18, 0xde, 0x26, 0x35, 0x95, 0x00, 0x00,
|
||||
0x00, 0x00, 0x80, 0x4d, 0x33, 0x0c, 0x86, 0x6d, 0xaa, 0x18, 0xb2, 0x57, 0x35, 0x95,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let reply = decode_echo_reply(&packet, &default_params(), None).unwrap();
|
||||
assert_eq!(reply.token, 0x7896_b6ab_8771_5213);
|
||||
assert_eq!(reply.sequence, 2);
|
||||
assert_eq!(reply.recv_count, Some(3));
|
||||
assert_eq!(reply.recv_window, Some(7));
|
||||
assert!(reply.timestamps.recv_wall.is_some());
|
||||
assert!(reply.timestamps.midpoint_wall.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_reply_decodes_exact_negotiated_length_with_payload() {
|
||||
let params = params_with_payload_space(4);
|
||||
let mut packet = Vec::new();
|
||||
write_header(&mut packet, FLAG_REPLY);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
packet.extend_from_slice(&[1, 2, 3, 4]);
|
||||
|
||||
let reply = decode_echo_reply(&packet, ¶ms, None).unwrap();
|
||||
assert_eq!(reply.payload, vec![1, 2, 3, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_reply_rejects_shorter_than_negotiated_length() {
|
||||
let params = params_with_payload_space(4);
|
||||
let mut packet = Vec::new();
|
||||
write_header(&mut packet, FLAG_REPLY);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
|
||||
assert_eq!(
|
||||
decode_echo_reply(&packet, ¶ms, None),
|
||||
Err(ProtoError::PacketLengthMismatch {
|
||||
expected: 20,
|
||||
actual: 16,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_reply_rejects_longer_than_negotiated_length() {
|
||||
let params = Params::default();
|
||||
let mut packet = Vec::new();
|
||||
write_header(&mut packet, FLAG_REPLY);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
packet.push(0);
|
||||
|
||||
assert_eq!(
|
||||
decode_echo_reply(&packet, ¶ms, None),
|
||||
Err(ProtoError::PacketLengthMismatch {
|
||||
expected: 16,
|
||||
actual: 17,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmac_echo_reply_decodes_default_fields_after_hmac() {
|
||||
let params = default_params();
|
||||
let layout = PacketLayout::echo(true, ¶ms);
|
||||
let mut packet = Vec::with_capacity(layout.header_len());
|
||||
write_header(&mut packet, FLAG_REPLY | FLAG_HMAC);
|
||||
packet.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
push_zeroed_layout_tail(layout, &mut packet);
|
||||
hmac::compute_hmac_in_place(b"testkey", &mut packet, hmac::hmac_offset()).unwrap();
|
||||
|
||||
assert_eq!(packet.len(), 76);
|
||||
let reply = decode_echo_reply(&packet, ¶ms, Some(b"testkey")).unwrap();
|
||||
assert_eq!(reply.token, 0x7896_b6ab_8771_5213);
|
||||
assert_eq!(reply.sequence, 2);
|
||||
assert_eq!(reply.recv_count, Some(0));
|
||||
assert_eq!(reply.recv_window, Some(0));
|
||||
assert_eq!(reply.payload.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmac_echo_reply_decodes_exact_negotiated_length_with_payload() {
|
||||
let mut params = Params {
|
||||
length: 48,
|
||||
..Params::default()
|
||||
};
|
||||
params.received_stats = ReceivedStats::Both;
|
||||
let layout = PacketLayout::echo(true, ¶ms);
|
||||
let mut packet = Vec::with_capacity(echo_packet_len(true, ¶ms).unwrap());
|
||||
write_header(&mut packet, FLAG_REPLY | FLAG_HMAC);
|
||||
packet.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
push_zeroed_layout_tail(layout, &mut packet);
|
||||
packet.extend_from_slice(&[1, 2, 3, 4]);
|
||||
hmac::compute_hmac_in_place(b"testkey", &mut packet, hmac::hmac_offset()).unwrap();
|
||||
|
||||
let reply = decode_echo_reply(&packet, ¶ms, Some(b"testkey")).unwrap();
|
||||
assert_eq!(reply.payload, vec![1, 2, 3, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmac_echo_reply_rejects_length_mismatch_before_hmac_verification() {
|
||||
let params = Params::default();
|
||||
let mut packet = Vec::new();
|
||||
write_header(&mut packet, FLAG_REPLY | FLAG_HMAC);
|
||||
packet.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
hmac::compute_hmac_in_place(b"testkey", &mut packet, hmac::hmac_offset()).unwrap();
|
||||
packet.push(0);
|
||||
|
||||
assert_eq!(
|
||||
decode_echo_reply(&packet, ¶ms, Some(b"testkey")),
|
||||
Err(ProtoError::PacketLengthMismatch {
|
||||
expected: 32,
|
||||
actual: 33,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -136,101 +136,3 @@ fn with_hmac_flag(flags: u8, authenticated: bool) -> u8 {
|
||||
flags & !FLAG_HMAC
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
flags::{FLAG_OPEN, FLAG_REPLY},
|
||||
MAGIC,
|
||||
};
|
||||
|
||||
const KEY: &[u8] = b"testkey";
|
||||
const OPEN_REQUEST_RULES: &[FlagRule] =
|
||||
&[FlagRule::Require(FLAG_OPEN), FlagRule::Reject(FLAG_REPLY)];
|
||||
|
||||
fn header(flags: u8) -> Vec<u8> {
|
||||
let mut packet = MAGIC.to_vec();
|
||||
packet.push(flags);
|
||||
packet
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rejects_bad_magic_and_reserved_flags() {
|
||||
assert_eq!(
|
||||
decode(&[0, MAGIC[1], MAGIC[2], FLAG_OPEN], None, &[]),
|
||||
Err(ProtoError::BadMagic)
|
||||
);
|
||||
assert_eq!(
|
||||
decode(&header(FLAG_OPEN | 0x10), None, &[]),
|
||||
Err(ProtoError::ReservedFlags(0x10))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_enforces_required_and_rejected_flags() {
|
||||
assert_eq!(
|
||||
decode(&header(0), None, OPEN_REQUEST_RULES),
|
||||
Err(ProtoError::MissingFlag(FLAG_OPEN))
|
||||
);
|
||||
assert_eq!(
|
||||
decode(&header(FLAG_OPEN | FLAG_REPLY), None, OPEN_REQUEST_RULES),
|
||||
Err(ProtoError::UnexpectedFlag(FLAG_REPLY))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rejects_hmac_presence_mismatch() {
|
||||
let mut authenticated = header(FLAG_OPEN | FLAG_HMAC);
|
||||
authenticated.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
assert_eq!(
|
||||
decode(&authenticated, None, OPEN_REQUEST_RULES),
|
||||
Err(ProtoError::HmacPresenceMismatch)
|
||||
);
|
||||
assert_eq!(
|
||||
decode(&header(FLAG_OPEN), Some(KEY), OPEN_REQUEST_RULES),
|
||||
Err(ProtoError::HmacPresenceMismatch)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structural_decode_reports_hmac_presence_without_a_key() {
|
||||
let mut authenticated = header(FLAG_OPEN | FLAG_HMAC);
|
||||
authenticated.extend_from_slice(&[0xff; HMAC_SIZE]);
|
||||
|
||||
let envelope = decode_structural(&authenticated).unwrap();
|
||||
assert!(envelope.hmac_present);
|
||||
assert_eq!(envelope.body_offset, HEADER_SIZE + HMAC_SIZE);
|
||||
|
||||
let envelope = decode_structural(&header(FLAG_OPEN)).unwrap();
|
||||
assert!(!envelope.hmac_present);
|
||||
assert_eq!(envelope.body_offset, HEADER_SIZE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_rejects_bad_hmac() {
|
||||
let mut packet = begin_checked(FLAG_OPEN, Some(KEY), OPEN_REQUEST_RULES, 22).unwrap();
|
||||
packet.extend_from_slice(&[1, 2]);
|
||||
let mut packet = finish(packet, Some(KEY)).unwrap();
|
||||
*packet.last_mut().unwrap() ^= 0x80;
|
||||
|
||||
decode(&packet, Some(KEY), OPEN_REQUEST_RULES).unwrap();
|
||||
assert_eq!(verify(&packet, Some(KEY)), Err(ProtoError::BadHmac));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoder_key_authoritatively_normalizes_hmac_flag() {
|
||||
let plain =
|
||||
begin_checked(FLAG_OPEN | FLAG_HMAC, None, OPEN_REQUEST_RULES, HEADER_SIZE).unwrap();
|
||||
assert_eq!(plain[3], FLAG_OPEN);
|
||||
|
||||
let authenticated = begin_checked(
|
||||
FLAG_OPEN,
|
||||
Some(KEY),
|
||||
OPEN_REQUEST_RULES,
|
||||
HEADER_SIZE + HMAC_SIZE,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(authenticated[3], FLAG_OPEN | FLAG_HMAC);
|
||||
}
|
||||
}
|
||||
@@ -96,76 +96,3 @@ pub fn verify_packet_hmac(key: &[u8], packet: &[u8]) -> Result<()> {
|
||||
pub(crate) fn hmac_offset() -> usize {
|
||||
HEADER_SIZE
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
flags::{FLAG_HMAC, FLAG_OPEN},
|
||||
MAGIC,
|
||||
};
|
||||
|
||||
fn reference_copy_zero_hmac(
|
||||
key: &[u8],
|
||||
packet: &[u8],
|
||||
hmac_offset: usize,
|
||||
) -> Result<[u8; HMAC_SIZE]> {
|
||||
if packet.len().saturating_sub(hmac_offset) < HMAC_SIZE {
|
||||
return Err(ProtoError::InvalidHmacOffset);
|
||||
}
|
||||
let mut copy = packet.to_vec();
|
||||
copy[hmac_offset..hmac_offset + HMAC_SIZE].fill(0);
|
||||
let mut mac = HmacMd5::new_from_slice(key).expect("HMAC accepts keys of any size");
|
||||
mac.update(©);
|
||||
let bytes = mac.finalize().into_bytes();
|
||||
let mut out = [0u8; HMAC_SIZE];
|
||||
out.copy_from_slice(&bytes);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn packet_with_hmac_field(hmac_bytes: [u8; HMAC_SIZE]) -> Vec<u8> {
|
||||
let mut packet = Vec::new();
|
||||
packet.extend_from_slice(&MAGIC);
|
||||
packet.push(FLAG_OPEN | FLAG_HMAC);
|
||||
packet.extend_from_slice(&hmac_bytes);
|
||||
packet.extend_from_slice(&[1, 2, 3, 4]);
|
||||
packet
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compute_and_verify_hmac() {
|
||||
let mut packet = packet_with_hmac_field([0; HMAC_SIZE]);
|
||||
|
||||
compute_hmac_in_place(b"testkey", &mut packet, hmac_offset()).unwrap();
|
||||
verify_hmac(b"testkey", &packet, hmac_offset()).unwrap();
|
||||
assert_eq!(
|
||||
verify_hmac(b"wrong", &packet, hmac_offset()),
|
||||
Err(ProtoError::BadHmac)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compute_hmac_matches_copy_and_zero_semantics() {
|
||||
let mut packet = packet_with_hmac_field([0xa5; HMAC_SIZE]);
|
||||
packet.extend_from_slice(&[5, 6, 7, 8, 9]);
|
||||
|
||||
let digest = compute_hmac(b"testkey", &packet, hmac_offset()).unwrap();
|
||||
let reference = reference_copy_zero_hmac(b"testkey", &packet, hmac_offset()).unwrap();
|
||||
|
||||
assert_eq!(digest, reference);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_hmac_rejects_modified_authenticated_byte() {
|
||||
let mut packet = packet_with_hmac_field([0; HMAC_SIZE]);
|
||||
compute_hmac_in_place(b"testkey", &mut packet, hmac_offset()).unwrap();
|
||||
|
||||
verify_hmac(b"testkey", &packet, hmac_offset()).unwrap();
|
||||
packet[hmac_offset() + HMAC_SIZE + 1] ^= 0x01;
|
||||
|
||||
assert_eq!(
|
||||
verify_hmac(b"testkey", &packet, hmac_offset()),
|
||||
Err(ProtoError::BadHmac)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -162,194 +162,3 @@ pub fn echo_packet_len(hmac: bool, params: &Params) -> Result<usize> {
|
||||
};
|
||||
Ok(header_len.max(requested))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::params::{Clock, ReceivedStats, StampAt};
|
||||
|
||||
fn params(stats: ReceivedStats, stamp_at: StampAt, clock: Clock) -> Params {
|
||||
Params {
|
||||
received_stats: stats,
|
||||
stamp_at,
|
||||
clock,
|
||||
..Params::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn expected_optional_len(stats: ReceivedStats, stamp_at: StampAt, clock: Clock) -> usize {
|
||||
let stats_len = match stats {
|
||||
ReceivedStats::None => 0,
|
||||
ReceivedStats::Count => RECV_COUNT_SIZE,
|
||||
ReceivedStats::Window => RECV_WINDOW_SIZE,
|
||||
ReceivedStats::Both => RECV_COUNT_SIZE + RECV_WINDOW_SIZE,
|
||||
};
|
||||
let clock_count = match clock {
|
||||
Clock::Unspecified => 0,
|
||||
Clock::Wall | Clock::Monotonic => 1,
|
||||
Clock::Both => 2,
|
||||
};
|
||||
let timestamp_groups = match stamp_at {
|
||||
StampAt::None => 0,
|
||||
StampAt::Send | StampAt::Receive | StampAt::Midpoint => 1,
|
||||
StampAt::Both => 2,
|
||||
};
|
||||
stats_len + timestamp_groups * clock_count * TIMESTAMP_SIZE
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verified_layout_lengths() {
|
||||
assert_eq!(PacketLayout::open_request(false).header_len(), 4);
|
||||
assert_eq!(PacketLayout::open_request(true).header_len(), 20);
|
||||
assert_eq!(PacketLayout::open_reply(false).header_len(), 12);
|
||||
assert_eq!(PacketLayout::open_reply(true).header_len(), 28);
|
||||
assert_eq!(PacketLayout::close_request(false).header_len(), 12);
|
||||
assert_eq!(PacketLayout::close_request(true).header_len(), 28);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::None, StampAt::None, Clock::Both)
|
||||
),
|
||||
16
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::Count, StampAt::Send, Clock::Wall)
|
||||
),
|
||||
28
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::Window, StampAt::Receive, Clock::Monotonic)
|
||||
),
|
||||
32
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::Both, StampAt::Midpoint, Clock::Both)
|
||||
),
|
||||
44
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::Both, StampAt::Both, Clock::Both)
|
||||
),
|
||||
60
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
true,
|
||||
¶ms(ReceivedStats::Both, StampAt::Both, Clock::Both)
|
||||
),
|
||||
76
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layout_matrix_matches_stats_timestamps_clock_and_hmac_rules() {
|
||||
for stats in [
|
||||
ReceivedStats::None,
|
||||
ReceivedStats::Count,
|
||||
ReceivedStats::Window,
|
||||
ReceivedStats::Both,
|
||||
] {
|
||||
for stamp_at in [
|
||||
StampAt::None,
|
||||
StampAt::Send,
|
||||
StampAt::Receive,
|
||||
StampAt::Both,
|
||||
StampAt::Midpoint,
|
||||
] {
|
||||
for clock in [Clock::Wall, Clock::Monotonic, Clock::Both] {
|
||||
for hmac in [false, true] {
|
||||
let params = params(stats, stamp_at, clock);
|
||||
let layout = PacketLayout::echo(hmac, ¶ms);
|
||||
let expected_len = HEADER_SIZE
|
||||
+ TOKEN_SIZE
|
||||
+ SEQ_SIZE
|
||||
+ if hmac { HMAC_SIZE } else { 0 }
|
||||
+ expected_optional_len(stats, stamp_at, clock);
|
||||
|
||||
assert_eq!(
|
||||
layout.header_len(),
|
||||
expected_len,
|
||||
"unexpected length for stats={stats:?} stamp_at={stamp_at:?} clock={clock:?} hmac={hmac}"
|
||||
);
|
||||
assert_eq!(layout.recv_count, stats.has_count());
|
||||
assert_eq!(layout.recv_window, stats.has_window());
|
||||
assert_eq!(
|
||||
layout.recv_wall,
|
||||
matches!(stamp_at, StampAt::Receive | StampAt::Both)
|
||||
&& clock.has_wall()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.recv_mono,
|
||||
matches!(stamp_at, StampAt::Receive | StampAt::Both)
|
||||
&& clock.has_mono()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.midpoint_wall,
|
||||
matches!(stamp_at, StampAt::Midpoint) && clock.has_wall()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.midpoint_mono,
|
||||
matches!(stamp_at, StampAt::Midpoint) && clock.has_mono()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.send_wall,
|
||||
matches!(stamp_at, StampAt::Send | StampAt::Both) && clock.has_wall()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.send_mono,
|
||||
matches!(stamp_at, StampAt::Send | StampAt::Both) && clock.has_mono()
|
||||
);
|
||||
|
||||
if hmac {
|
||||
assert_eq!(
|
||||
echo_header_len(true, ¶ms) - echo_header_len(false, ¶ms),
|
||||
HMAC_SIZE
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiated_length_is_floored_at_the_required_field_block() {
|
||||
// The 60-byte header of this layout is the floor. A negative negotiated
|
||||
// length is not an error: it asks for nothing beyond the mandatory
|
||||
// fields, exactly as zero does.
|
||||
let mut p = params(ReceivedStats::Both, StampAt::Both, Clock::Both);
|
||||
for (length, expected) in [(-4096, 60), (-1, 60), (0, 60), (20, 60), (92, 92)] {
|
||||
p.length = length;
|
||||
assert_eq!(
|
||||
echo_packet_len(false, &p),
|
||||
Ok(expected),
|
||||
"unexpected packet length for negotiated length {length}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A positive length wider than `usize` must stay an error rather than
|
||||
/// becoming a saturated buffer size. Only reachable where `usize` is
|
||||
/// narrower than `i64`; on a 64-bit target every positive `i64` converts.
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[test]
|
||||
fn a_length_wider_than_usize_is_rejected_rather_than_saturated() {
|
||||
let mut p = params(ReceivedStats::Both, StampAt::Both, Clock::Both);
|
||||
p.length = 5_000_000_000;
|
||||
|
||||
assert_eq!(
|
||||
echo_packet_len(false, &p),
|
||||
Err(ProtoError::PacketLengthUnrepresentable {
|
||||
length: 5_000_000_000
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -98,53 +98,3 @@ pub fn decode_open_reply(packet: &[u8], hmac_key: Option<&[u8]>) -> Result<OpenR
|
||||
params: Params::decode(&packet[pos..])?,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
hmac,
|
||||
params::{Clock, ReceivedStats, StampAt},
|
||||
FLAG_HMAC, HMAC_SIZE,
|
||||
};
|
||||
|
||||
fn default_params() -> Params {
|
||||
Params {
|
||||
protocol_version: 1,
|
||||
duration_ns: 3_000_000_000,
|
||||
interval_ns: 1_000_000_000,
|
||||
received_stats: ReceivedStats::Both,
|
||||
stamp_at: StampAt::Both,
|
||||
clock: Clock::Both,
|
||||
..Params::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_reply_decodes_token() {
|
||||
let mut packet = vec![0x14, 0xa7, 0x5b, 0x03];
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&default_params().encode());
|
||||
|
||||
let reply = decode_open_reply(&packet, None).unwrap();
|
||||
assert_eq!(reply.token, 0x7896_b6ab_8771_5213);
|
||||
assert_eq!(reply.params, default_params());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmac_open_reply_decodes_token_after_hmac_field() {
|
||||
let mut packet = vec![0x14, 0xa7, 0x5b, FLAG_OPEN | FLAG_REPLY | FLAG_HMAC];
|
||||
packet.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&default_params().encode());
|
||||
hmac::compute_hmac_in_place(b"testkey", &mut packet, hmac::hmac_offset()).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
packet.len(),
|
||||
4 + HMAC_SIZE + TOKEN_SIZE + default_params().encode().len()
|
||||
);
|
||||
let reply = decode_open_reply(&packet, Some(b"testkey")).unwrap();
|
||||
assert_eq!(reply.token, 0x7896_b6ab_8771_5213);
|
||||
assert_eq!(reply.params, default_params());
|
||||
}
|
||||
}
|
||||
@@ -368,773 +368,3 @@ fn read_int(input: &[u8], pos: &mut usize) -> Result<i64> {
|
||||
*pos += used;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn assert_round_trip(params: Params) {
|
||||
assert_eq!(Params::decode(¶ms.encode()), Ok(params));
|
||||
}
|
||||
|
||||
fn encode_server_fill_value(value: &[u8]) -> Vec<u8> {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(9, &mut encoded);
|
||||
varint::encode_uvarint(value.len() as u64, &mut encoded);
|
||||
encoded.extend_from_slice(value);
|
||||
encoded
|
||||
}
|
||||
|
||||
fn encode_int(tag: u64, value: i64) -> Vec<u8> {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(tag, &mut encoded);
|
||||
varint::encode_varint(value, &mut encoded);
|
||||
encoded
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn params_round_trip() {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
duration_ns: 3_000_000_000,
|
||||
interval_ns: 1_000_000_000,
|
||||
length: 1472,
|
||||
received_stats: ReceivedStats::Both,
|
||||
stamp_at: StampAt::Both,
|
||||
clock: Clock::Both,
|
||||
dscp: 184,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "pattern:abc".to_owned(),
|
||||
}),
|
||||
};
|
||||
assert_round_trip(params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn params_round_trip_negotiated_option_modes() {
|
||||
for received_stats in [
|
||||
ReceivedStats::None,
|
||||
ReceivedStats::Count,
|
||||
ReceivedStats::Window,
|
||||
ReceivedStats::Both,
|
||||
] {
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
received_stats,
|
||||
..Params::default()
|
||||
});
|
||||
}
|
||||
|
||||
for stamp_at in [
|
||||
StampAt::None,
|
||||
StampAt::Send,
|
||||
StampAt::Receive,
|
||||
StampAt::Both,
|
||||
StampAt::Midpoint,
|
||||
] {
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
stamp_at,
|
||||
clock: Clock::Both,
|
||||
..Params::default()
|
||||
});
|
||||
}
|
||||
|
||||
for clock in [Clock::Wall, Clock::Monotonic, Clock::Both] {
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
stamp_at: StampAt::Both,
|
||||
clock,
|
||||
..Params::default()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn params_round_trip_dscp_values_are_encoded_without_shifting() {
|
||||
// `Params::dscp` is a raw wire byte; encode/decode carry whatever
|
||||
// value is set without interpreting or shifting it, regardless of
|
||||
// whether it happens to look like a codepoint (46) or a raw TOS byte
|
||||
// (184).
|
||||
for dscp in [0, 46, 63, 64, 184, -1] {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
dscp,
|
||||
..Params::default()
|
||||
};
|
||||
assert_round_trip(params);
|
||||
}
|
||||
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
dscp: 46,
|
||||
..Params::default()
|
||||
};
|
||||
let encoded = params.encode();
|
||||
assert!(
|
||||
encoded.windows(2).any(|bytes| bytes == [8, 92]),
|
||||
"dscp value 46 must be encoded as param value 46"
|
||||
);
|
||||
assert!(
|
||||
!encoded.windows(3).any(|bytes| bytes == [8, 0xf0, 0x02]),
|
||||
"Params::encode must not shift dscp 46 to 184; any codepoint shift is a caller concern"
|
||||
);
|
||||
assert_eq!(Params::decode(&encoded).unwrap().dscp, 46);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_fill_absent_short_and_max_length_round_trip() {
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
server_fill: None,
|
||||
..Params::default()
|
||||
});
|
||||
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "rand".to_owned(),
|
||||
}),
|
||||
..Params::default()
|
||||
});
|
||||
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "0123456789abcdef0123456789abcdef".to_owned(),
|
||||
}),
|
||||
..Params::default()
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_fill_decode_accepts_max_length() {
|
||||
let value = b"0123456789abcdef0123456789abcdef";
|
||||
let params = Params::decode(&encode_server_fill_value(value)).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
params.server_fill,
|
||||
Some(ServerFill {
|
||||
value: "0123456789abcdef0123456789abcdef".to_owned(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_fill_decode_rejects_oversized_length() {
|
||||
let value = b"0123456789abcdef0123456789abcdefx";
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encode_server_fill_value(value)),
|
||||
Err(ProtoError::ParameterLengthTooLarge { tag: 9, length: 33 })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_fill_tag_and_length_are_encoded_before_utf8_bytes() {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "rand".to_owned(),
|
||||
}),
|
||||
..Params::default()
|
||||
};
|
||||
let encoded = params.encode();
|
||||
assert!(encoded
|
||||
.windows(6)
|
||||
.any(|bytes| bytes == [9, 4, b'r', b'a', b'n', b'd']));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_tags_are_ignored() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(99, &mut encoded);
|
||||
varint::encode_varint(123, &mut encoded);
|
||||
varint::encode_uvarint(1, &mut encoded);
|
||||
varint::encode_varint(1, &mut encoded);
|
||||
|
||||
let params = Params::decode(&encoded).unwrap();
|
||||
assert_eq!(params.protocol_version, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_received_stats_value_is_rejected() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(5, &mut encoded);
|
||||
varint::encode_varint(4, &mut encoded);
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encoded),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "ReceivedStats",
|
||||
value: 4,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_timestamp_value_is_rejected() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(6, &mut encoded);
|
||||
varint::encode_varint(5, &mut encoded);
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encoded),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "StampAt",
|
||||
value: 5,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_clock_zero_is_rejected() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(7, &mut encoded);
|
||||
varint::encode_varint(0, &mut encoded);
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encoded),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "Clock",
|
||||
value: 0,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
Params::decode_with_presence(&encoded),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "Clock",
|
||||
value: 0,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_clock_values_one_to_three_are_accepted() {
|
||||
for (value, expected) in [(1, Clock::Wall), (2, Clock::Monotonic), (3, Clock::Both)] {
|
||||
let decoded = Params::decode_with_presence(&encode_int(7, value)).unwrap();
|
||||
assert_eq!(decoded.params.clock, expected);
|
||||
assert!(decoded.presence.clock);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_payload_decodes_to_wire_defaults_with_nothing_present() {
|
||||
let decoded = Params::decode_with_presence(&[]).unwrap();
|
||||
|
||||
assert_eq!(decoded.params, Params::default());
|
||||
assert_eq!(decoded.params.protocol_version, 0);
|
||||
assert_eq!(decoded.params.duration_ns, 0);
|
||||
assert_eq!(decoded.params.interval_ns, 0);
|
||||
assert_eq!(decoded.params.length, 0);
|
||||
assert_eq!(decoded.params.dscp, 0);
|
||||
assert_eq!(decoded.params.received_stats, ReceivedStats::None);
|
||||
assert_eq!(decoded.params.stamp_at, StampAt::None);
|
||||
// The wire default clock is zero, not `Both`.
|
||||
assert_eq!(decoded.params.clock, Clock::Unspecified);
|
||||
assert!(!decoded.params.clock.has_wall());
|
||||
assert!(!decoded.params.clock.has_mono());
|
||||
assert_eq!(decoded.params.server_fill, None);
|
||||
|
||||
assert_eq!(decoded.presence, ParamPresence::default());
|
||||
assert_eq!(
|
||||
decoded.presence,
|
||||
ParamPresence {
|
||||
protocol_version: false,
|
||||
duration_ns: false,
|
||||
interval_ns: false,
|
||||
length: false,
|
||||
received_stats: false,
|
||||
stamp_at: false,
|
||||
clock: false,
|
||||
dscp: false,
|
||||
server_fill: false,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absent_and_explicitly_zero_parameters_share_a_value_but_not_a_presence() {
|
||||
// Duration and Interval are the pair a server must tell apart: absent is
|
||||
// accepted, explicitly zero is not.
|
||||
let absent = Params::decode_with_presence(&encode_int(1, 1)).unwrap();
|
||||
assert_eq!(absent.params.duration_ns, 0);
|
||||
assert!(!absent.presence.duration_ns);
|
||||
assert_eq!(absent.params.interval_ns, 0);
|
||||
assert!(!absent.presence.interval_ns);
|
||||
assert_eq!(absent.params.clock, Clock::Unspecified);
|
||||
assert!(!absent.presence.clock);
|
||||
|
||||
let explicit_duration = Params::decode_with_presence(&encode_int(2, 0)).unwrap();
|
||||
assert_eq!(explicit_duration.params.duration_ns, 0);
|
||||
assert!(explicit_duration.presence.duration_ns);
|
||||
|
||||
let explicit_interval = Params::decode_with_presence(&encode_int(3, 0)).unwrap();
|
||||
assert_eq!(explicit_interval.params.interval_ns, 0);
|
||||
assert!(explicit_interval.presence.interval_ns);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_known_tag_reports_its_own_presence() {
|
||||
let mut encoded = Vec::new();
|
||||
for (tag, value) in [
|
||||
(1, 1),
|
||||
(2, 5),
|
||||
(3, 6),
|
||||
(4, 7),
|
||||
(5, 3),
|
||||
(6, 4),
|
||||
(7, 3),
|
||||
(8, 184),
|
||||
] {
|
||||
encoded.extend_from_slice(&encode_int(tag, value));
|
||||
}
|
||||
encoded.extend_from_slice(&encode_server_fill_value(b"rand"));
|
||||
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
assert_eq!(
|
||||
decoded.presence,
|
||||
ParamPresence {
|
||||
protocol_version: true,
|
||||
duration_ns: true,
|
||||
interval_ns: true,
|
||||
length: true,
|
||||
received_stats: true,
|
||||
stamp_at: true,
|
||||
clock: true,
|
||||
dscp: true,
|
||||
server_fill: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unspecified_clock_is_encoded_by_omission() {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
clock: Clock::Unspecified,
|
||||
..Params::default()
|
||||
};
|
||||
let encoded = params.encode();
|
||||
|
||||
assert_eq!(encoded, encode_int(1, 1));
|
||||
assert!(
|
||||
!encoded.windows(2).any(|bytes| bytes[0] == 7),
|
||||
"an unspecified clock must omit the tag rather than encode an explicit zero"
|
||||
);
|
||||
assert_round_trip(params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_and_decode_with_presence_agree() {
|
||||
let mut payloads = vec![
|
||||
Vec::new(),
|
||||
encode_int(1, 1),
|
||||
encode_int(2, 0),
|
||||
encode_server_fill_value(b"rand"),
|
||||
];
|
||||
payloads.push(
|
||||
Params {
|
||||
protocol_version: 1,
|
||||
duration_ns: 3_000_000_000,
|
||||
interval_ns: 1_000_000_000,
|
||||
length: 1472,
|
||||
received_stats: ReceivedStats::Both,
|
||||
stamp_at: StampAt::Both,
|
||||
clock: Clock::Both,
|
||||
dscp: 184,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "rand".to_owned(),
|
||||
}),
|
||||
}
|
||||
.encode(),
|
||||
);
|
||||
// Malformed payloads must fail identically through both entry points.
|
||||
payloads.push(vec![1, 0x80]);
|
||||
payloads.push(encode_int(7, 0));
|
||||
|
||||
for payload in payloads {
|
||||
assert_eq!(
|
||||
Params::decode(&payload),
|
||||
Params::decode_with_presence(&payload).map(|decoded| decoded.params),
|
||||
"decode and decode_with_presence disagreed on {payload:02x?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_server_fill_is_rejected() {
|
||||
let encoded = [9, 4, b'a', b'b'];
|
||||
assert_eq!(Params::decode(&encoded), Err(ProtoError::MalformedParams));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_utf8_server_fill_is_rejected() {
|
||||
let encoded = [9, 1, 0xff];
|
||||
assert_eq!(Params::decode(&encoded), Err(ProtoError::InvalidUtf8));
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[test]
|
||||
fn server_fill_length_too_large_for_usize_is_rejected() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(9, &mut encoded);
|
||||
varint::encode_uvarint(u64::from(u32::MAX) + 1, &mut encoded);
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encoded),
|
||||
Err(ProtoError::ParameterLengthTooLarge {
|
||||
tag: 9,
|
||||
length: u64::from(u32::MAX) + 1,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_varint_parameter_is_rejected() {
|
||||
let encoded = [1, 0x80];
|
||||
assert_eq!(Params::decode(&encoded), Err(ProtoError::TruncatedVarint));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_unknown_parameter_value_is_rejected() {
|
||||
let encoded = [99];
|
||||
assert_eq!(Params::decode(&encoded), Err(ProtoError::TruncatedVarint));
|
||||
}
|
||||
|
||||
mod properties {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
|
||||
fn received_stats_strategy() -> impl Strategy<Value = ReceivedStats> {
|
||||
prop_oneof![
|
||||
Just(ReceivedStats::None),
|
||||
Just(ReceivedStats::Count),
|
||||
Just(ReceivedStats::Window),
|
||||
Just(ReceivedStats::Both),
|
||||
]
|
||||
}
|
||||
|
||||
fn stamp_at_strategy() -> impl Strategy<Value = StampAt> {
|
||||
prop_oneof![
|
||||
Just(StampAt::None),
|
||||
Just(StampAt::Send),
|
||||
Just(StampAt::Receive),
|
||||
Just(StampAt::Both),
|
||||
Just(StampAt::Midpoint),
|
||||
]
|
||||
}
|
||||
|
||||
/// A `Clock` valid for direct construction, including `Unspecified`
|
||||
/// (wire omission). Never produces an explicit zero on the wire; see
|
||||
/// `explicit_clock_strategy` for that.
|
||||
fn clock_strategy() -> impl Strategy<Value = Clock> {
|
||||
prop_oneof![
|
||||
Just(Clock::Unspecified),
|
||||
Just(Clock::Wall),
|
||||
Just(Clock::Monotonic),
|
||||
Just(Clock::Both),
|
||||
]
|
||||
}
|
||||
|
||||
/// A `Clock` value valid to encode *explicitly* on the wire (tag 7).
|
||||
/// Excludes `Unspecified`/zero, which is only ever reached by omission.
|
||||
fn explicit_clock_value_strategy() -> impl Strategy<Value = i64> {
|
||||
1_i64..=3
|
||||
}
|
||||
|
||||
fn server_fill_strategy() -> impl Strategy<Value = Option<ServerFill>> {
|
||||
prop_oneof![
|
||||
Just(None),
|
||||
proptest::collection::vec(proptest::char::any(), 0..=8).prop_map(|chars| Some(
|
||||
ServerFill {
|
||||
value: chars.into_iter().collect(),
|
||||
}
|
||||
)),
|
||||
]
|
||||
}
|
||||
|
||||
fn params_strategy() -> impl Strategy<Value = Params> {
|
||||
(
|
||||
any::<i64>(),
|
||||
any::<i64>(),
|
||||
any::<i64>(),
|
||||
any::<i64>(),
|
||||
any::<i64>(),
|
||||
received_stats_strategy(),
|
||||
stamp_at_strategy(),
|
||||
clock_strategy(),
|
||||
server_fill_strategy(),
|
||||
)
|
||||
.prop_map(
|
||||
|(
|
||||
protocol_version,
|
||||
duration_ns,
|
||||
interval_ns,
|
||||
length,
|
||||
dscp,
|
||||
received_stats,
|
||||
stamp_at,
|
||||
clock,
|
||||
server_fill,
|
||||
)| Params {
|
||||
protocol_version,
|
||||
duration_ns,
|
||||
interval_ns,
|
||||
length,
|
||||
received_stats,
|
||||
stamp_at,
|
||||
clock,
|
||||
dscp,
|
||||
server_fill,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn encode_tagged_int(tag: u64, value: i64, out: &mut Vec<u8>) {
|
||||
varint::encode_uvarint(tag, out);
|
||||
varint::encode_varint(value, out);
|
||||
}
|
||||
|
||||
fn encode_tagged_server_fill(value: &str, out: &mut Vec<u8>) {
|
||||
varint::encode_uvarint(9, out);
|
||||
varint::encode_uvarint(value.len() as u64, out);
|
||||
out.extend_from_slice(value.as_bytes());
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn valid_params_round_trip(params in params_strategy()) {
|
||||
let encoded = params.encode();
|
||||
let decoded = Params::decode(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded, params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoded_param_presence_matches_omission_rules(params in params_strategy()) {
|
||||
let encoded = params.encode();
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(&decoded.params, ¶ms);
|
||||
|
||||
prop_assert_eq!(decoded.presence.protocol_version, params.protocol_version != 0);
|
||||
prop_assert_eq!(decoded.presence.duration_ns, params.duration_ns != 0);
|
||||
prop_assert_eq!(decoded.presence.interval_ns, params.interval_ns != 0);
|
||||
prop_assert_eq!(decoded.presence.length, params.length != 0);
|
||||
prop_assert_eq!(decoded.presence.dscp, params.dscp != 0);
|
||||
prop_assert_eq!(
|
||||
decoded.presence.received_stats,
|
||||
params.received_stats != ReceivedStats::None
|
||||
);
|
||||
prop_assert_eq!(decoded.presence.stamp_at, params.stamp_at != StampAt::None);
|
||||
prop_assert_eq!(decoded.presence.clock, params.clock != Clock::Unspecified);
|
||||
prop_assert_eq!(decoded.presence.server_fill, params.server_fill.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_protocol_version_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(1, first, &mut encoded);
|
||||
encode_tagged_int(1, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.protocol_version, second);
|
||||
prop_assert!(decoded.presence.protocol_version);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_duration_ns_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(2, first, &mut encoded);
|
||||
encode_tagged_int(2, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.duration_ns, second);
|
||||
prop_assert!(decoded.presence.duration_ns);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_interval_ns_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(3, first, &mut encoded);
|
||||
encode_tagged_int(3, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.interval_ns, second);
|
||||
prop_assert!(decoded.presence.interval_ns);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_length_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(4, first, &mut encoded);
|
||||
encode_tagged_int(4, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.length, second);
|
||||
prop_assert!(decoded.presence.length);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_received_stats_is_last_wins(first in 0_i64..=3, second in 0_i64..=3) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(5, first, &mut encoded);
|
||||
encode_tagged_int(5, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.received_stats, ReceivedStats::try_from(second).unwrap());
|
||||
prop_assert!(decoded.presence.received_stats);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_stamp_at_is_last_wins(first in 0_i64..=4, second in 0_i64..=4) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(6, first, &mut encoded);
|
||||
encode_tagged_int(6, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.stamp_at, StampAt::try_from(second).unwrap());
|
||||
prop_assert!(decoded.presence.stamp_at);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_clock_is_last_wins(
|
||||
first in explicit_clock_value_strategy(),
|
||||
second in explicit_clock_value_strategy(),
|
||||
) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(7, first, &mut encoded);
|
||||
encode_tagged_int(7, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.clock, Clock::try_from(second).unwrap());
|
||||
prop_assert!(decoded.presence.clock);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_dscp_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(8, first, &mut encoded);
|
||||
encode_tagged_int(8, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.dscp, second);
|
||||
prop_assert!(decoded.presence.dscp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_server_fill_is_last_wins(
|
||||
first in proptest::collection::vec(proptest::char::any(), 0..=8)
|
||||
.prop_map(|chars| chars.into_iter().collect::<String>()),
|
||||
second in proptest::collection::vec(proptest::char::any(), 0..=8)
|
||||
.prop_map(|chars| chars.into_iter().collect::<String>()),
|
||||
) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_server_fill(&first, &mut encoded);
|
||||
encode_tagged_server_fill(&second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(
|
||||
decoded.params.server_fill,
|
||||
Some(ServerFill { value: second })
|
||||
);
|
||||
prop_assert!(decoded.presence.server_fill);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_scalar_tag_prefix_does_not_disturb_known_params(
|
||||
params in params_strategy(),
|
||||
unknown_tag in 10_u64..=u16::MAX as u64,
|
||||
unknown_value: u64,
|
||||
) {
|
||||
let mut prefixed = Vec::new();
|
||||
varint::encode_uvarint(unknown_tag, &mut prefixed);
|
||||
varint::encode_uvarint(unknown_value, &mut prefixed);
|
||||
prefixed.extend_from_slice(¶ms.encode());
|
||||
|
||||
let decoded = Params::decode(&prefixed).unwrap();
|
||||
prop_assert_eq!(decoded, params.clone());
|
||||
|
||||
let decoded_with_presence = Params::decode_with_presence(&prefixed).unwrap();
|
||||
let expected_presence = Params::decode_with_presence(¶ms.encode()).unwrap().presence;
|
||||
prop_assert_eq!(decoded_with_presence.presence, expected_presence);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_scalar_tag_suffix_does_not_disturb_known_params(
|
||||
params in params_strategy(),
|
||||
unknown_tag in 10_u64..=u16::MAX as u64,
|
||||
unknown_value: u64,
|
||||
) {
|
||||
let mut suffixed = params.encode();
|
||||
varint::encode_uvarint(unknown_tag, &mut suffixed);
|
||||
varint::encode_uvarint(unknown_value, &mut suffixed);
|
||||
|
||||
let decoded = Params::decode(&suffixed).unwrap();
|
||||
prop_assert_eq!(decoded, params.clone());
|
||||
|
||||
let decoded_with_presence = Params::decode_with_presence(&suffixed).unwrap();
|
||||
let expected_presence = Params::decode_with_presence(¶ms.encode()).unwrap().presence;
|
||||
prop_assert_eq!(decoded_with_presence.presence, expected_presence);
|
||||
}
|
||||
}
|
||||
|
||||
/// Omitted-vs-explicit-zero presence, for the fields where explicit
|
||||
/// zero is valid on the wire. `Clock` is deliberately excluded: an
|
||||
/// explicit zero there is rejected outright, not merely "present with
|
||||
/// the default value" (see `explicit_clock_zero_is_rejected`).
|
||||
#[test]
|
||||
fn omitted_vs_explicit_zero_presence_for_zero_valid_fields() {
|
||||
let scalar_tags = [1_u64, 2, 3, 4, 8];
|
||||
for tag in scalar_tags {
|
||||
let omitted = Params::decode_with_presence(&[]).unwrap();
|
||||
let mut explicit_zero = Vec::new();
|
||||
encode_tagged_int(tag, 0, &mut explicit_zero);
|
||||
let explicit = Params::decode_with_presence(&explicit_zero).unwrap();
|
||||
|
||||
assert_eq!(omitted.params, explicit.params);
|
||||
assert_eq!(explicit.params, Params::default());
|
||||
|
||||
let (omitted_presence, explicit_presence) = match tag {
|
||||
1 => (
|
||||
omitted.presence.protocol_version,
|
||||
explicit.presence.protocol_version,
|
||||
),
|
||||
2 => (omitted.presence.duration_ns, explicit.presence.duration_ns),
|
||||
3 => (omitted.presence.interval_ns, explicit.presence.interval_ns),
|
||||
4 => (omitted.presence.length, explicit.presence.length),
|
||||
8 => (omitted.presence.dscp, explicit.presence.dscp),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert!(!omitted_presence);
|
||||
assert!(explicit_presence);
|
||||
}
|
||||
|
||||
let enum_tags = [5_u64, 6];
|
||||
for tag in enum_tags {
|
||||
let omitted = Params::decode_with_presence(&[]).unwrap();
|
||||
let mut explicit_zero = Vec::new();
|
||||
encode_tagged_int(tag, 0, &mut explicit_zero);
|
||||
let explicit = Params::decode_with_presence(&explicit_zero).unwrap();
|
||||
|
||||
assert_eq!(omitted.params, explicit.params);
|
||||
|
||||
let (omitted_presence, explicit_presence) = match tag {
|
||||
5 => (
|
||||
omitted.presence.received_stats,
|
||||
explicit.presence.received_stats,
|
||||
),
|
||||
6 => (omitted.presence.stamp_at, explicit.presence.stamp_at),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert!(!omitted_presence);
|
||||
assert!(explicit_presence);
|
||||
}
|
||||
|
||||
// `Clock` deliberately breaks the pattern above: explicit zero is
|
||||
// rejected rather than accepted-but-present.
|
||||
let omitted = Params::decode_with_presence(&[]).unwrap();
|
||||
assert_eq!(omitted.params.clock, Clock::Unspecified);
|
||||
assert!(!omitted.presence.clock);
|
||||
let mut explicit_clock_zero = Vec::new();
|
||||
encode_tagged_int(7, 0, &mut explicit_clock_zero);
|
||||
assert_eq!(
|
||||
Params::decode_with_presence(&explicit_clock_zero),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "Clock",
|
||||
value: 0,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,112 +59,3 @@ pub fn decode_varint(input: &[u8]) -> Result<(i64, usize)> {
|
||||
let (value, used) = decode_uvarint(input)?;
|
||||
Ok((zigzag_decode(value), used))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn uvarint_round_trips() {
|
||||
for value in [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
127,
|
||||
128,
|
||||
255,
|
||||
16_384,
|
||||
u32::MAX as u64,
|
||||
i64::MAX as u64,
|
||||
u64::MAX,
|
||||
] {
|
||||
let mut encoded = Vec::new();
|
||||
encode_uvarint(value, &mut encoded);
|
||||
assert_eq!(decode_uvarint(&encoded), Ok((value, encoded.len())));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_varint_round_trips() {
|
||||
for value in [
|
||||
i64::MIN,
|
||||
-9_223_372_036,
|
||||
-2,
|
||||
-1,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
1_000_000_000,
|
||||
3_000_000_000,
|
||||
i64::MAX,
|
||||
] {
|
||||
let mut encoded = Vec::new();
|
||||
encode_varint(value, &mut encoded);
|
||||
assert_eq!(decode_varint(&encoded), Ok((value, encoded.len())));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verified_byte_examples() {
|
||||
let cases: &[(i64, &[u8])] = &[
|
||||
(1, &[0x02]),
|
||||
(3_000_000_000, &[0x80, 0xf8, 0x82, 0xad, 0x16]),
|
||||
(1_000_000_000, &[0x80, 0xa8, 0xd6, 0xb9, 0x07]),
|
||||
(1472, &[0x80, 0x17]),
|
||||
(3, &[0x06]),
|
||||
(184, &[0xf0, 0x02]),
|
||||
];
|
||||
for (value, expected) in cases {
|
||||
let mut encoded = Vec::new();
|
||||
encode_varint(*value, &mut encoded);
|
||||
assert_eq!(&encoded, expected);
|
||||
assert_eq!(decode_varint(expected), Ok((*value, expected.len())));
|
||||
}
|
||||
|
||||
let mut tag = Vec::new();
|
||||
encode_uvarint(9, &mut tag);
|
||||
assert_eq!(tag, [0x09]);
|
||||
tag.clear();
|
||||
encode_uvarint(24, &mut tag);
|
||||
assert_eq!(tag, [0x18]);
|
||||
}
|
||||
|
||||
mod properties {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn uvarint_round_trips(value: u64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_uvarint(value, &mut encoded);
|
||||
prop_assert!(!encoded.is_empty());
|
||||
prop_assert!(encoded.len() <= 10);
|
||||
let (decoded, used) = decode_uvarint(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded, value);
|
||||
prop_assert_eq!(used, encoded.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_varint_round_trips(value: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_varint(value, &mut encoded);
|
||||
prop_assert!(!encoded.is_empty());
|
||||
prop_assert!(encoded.len() <= 10);
|
||||
let (decoded, used) = decode_varint(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded, value);
|
||||
prop_assert_eq!(used, encoded.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zigzag_round_trips_from_signed(value: i64) {
|
||||
prop_assert_eq!(zigzag_decode(zigzag_encode(value)), value);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zigzag_round_trips_from_unsigned(value: u64) {
|
||||
prop_assert_eq!(zigzag_encode(zigzag_decode(value)), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
use irtt_proto::{compute_hmac, compute_hmac_in_place, verify_hmac, ProtoError};
|
||||
|
||||
fn captured_open() -> Vec<u8> {
|
||||
// Captured vector 6; provenance and independently checked digest are in
|
||||
// docs/protocol/test-vectors/README.md. This is an oracle for the primitive.
|
||||
"14 a7 5b 09 ff 90 16 a7 aa 53 78 16 9e c3 a2 d5 54 dc 30 36
|
||||
01 02 02 80 d0 ac f3 0e 03 80 a8 d6 b9 07 05 06 06 06 07 06"
|
||||
.split_whitespace()
|
||||
.map(|byte| u8::from_str_radix(byte, 16).unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn computing_hmac_ignores_existing_digest_and_only_replaces_its_field() {
|
||||
let expected = captured_open();
|
||||
let mut packet = expected.clone();
|
||||
packet[4..20].fill(0xa5);
|
||||
let before = packet.clone();
|
||||
assert_eq!(
|
||||
compute_hmac(b"testkey", &packet, 4).unwrap().as_slice(),
|
||||
&expected[4..20]
|
||||
);
|
||||
assert_eq!(packet, before);
|
||||
compute_hmac_in_place(b"testkey", &mut packet, 4).unwrap();
|
||||
assert_eq!(packet, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verification_rejects_wrong_key_and_modified_authenticated_bytes() {
|
||||
let packet = captured_open();
|
||||
verify_hmac(b"testkey", &packet, 4).unwrap();
|
||||
assert_eq!(verify_hmac(b"wrong", &packet, 4), Err(ProtoError::BadHmac));
|
||||
for offset in [0, 4, 21, packet.len() - 1] {
|
||||
let mut modified = packet.clone();
|
||||
modified[offset] ^= 1;
|
||||
assert_eq!(
|
||||
verify_hmac(b"testkey", &modified, 4),
|
||||
Err(ProtoError::BadHmac)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incomplete_or_out_of_bounds_digest_fields_are_rejected_without_mutation() {
|
||||
for offset in [5, 20, 21, usize::MAX] {
|
||||
let mut packet = vec![0xa5; 20];
|
||||
assert_eq!(
|
||||
compute_hmac(b"key", &packet, offset),
|
||||
Err(ProtoError::InvalidHmacOffset)
|
||||
);
|
||||
assert_eq!(
|
||||
verify_hmac(b"key", &packet, offset),
|
||||
Err(ProtoError::InvalidHmacOffset)
|
||||
);
|
||||
assert_eq!(
|
||||
compute_hmac_in_place(b"key", &mut packet, offset),
|
||||
Err(ProtoError::InvalidHmacOffset)
|
||||
);
|
||||
assert_eq!(packet, [0xa5; 20]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
use irtt_proto::params::{Clock, ReceivedStats, StampAt};
|
||||
use irtt_proto::{
|
||||
echo_header_len, echo_packet_len, PacketLayout, Params, HMAC_SIZE, RECV_COUNT_SIZE,
|
||||
RECV_WINDOW_SIZE, SEQ_SIZE, TIMESTAMP_SIZE, TOKEN_SIZE,
|
||||
};
|
||||
|
||||
const HEADER_SIZE: usize = 4;
|
||||
|
||||
fn params(stats: ReceivedStats, stamp_at: StampAt, clock: Clock) -> Params {
|
||||
Params {
|
||||
received_stats: stats,
|
||||
stamp_at,
|
||||
clock,
|
||||
..Params::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn expected_optional_len(stats: ReceivedStats, stamp_at: StampAt, clock: Clock) -> usize {
|
||||
let stats_len = match stats {
|
||||
ReceivedStats::None => 0,
|
||||
ReceivedStats::Count => RECV_COUNT_SIZE,
|
||||
ReceivedStats::Window => RECV_WINDOW_SIZE,
|
||||
ReceivedStats::Both => RECV_COUNT_SIZE + RECV_WINDOW_SIZE,
|
||||
};
|
||||
let clock_count = match clock {
|
||||
Clock::Unspecified => 0,
|
||||
Clock::Wall | Clock::Monotonic => 1,
|
||||
Clock::Both => 2,
|
||||
};
|
||||
let timestamp_groups = match stamp_at {
|
||||
StampAt::None => 0,
|
||||
StampAt::Send | StampAt::Receive | StampAt::Midpoint => 1,
|
||||
StampAt::Both => 2,
|
||||
};
|
||||
stats_len + timestamp_groups * clock_count * TIMESTAMP_SIZE
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_and_close_layout_lengths() {
|
||||
assert_eq!(PacketLayout::open_request(false).header_len(), 4);
|
||||
assert_eq!(PacketLayout::open_request(true).header_len(), 20);
|
||||
assert_eq!(PacketLayout::open_reply(false).header_len(), 12);
|
||||
assert_eq!(PacketLayout::open_reply(true).header_len(), 28);
|
||||
assert_eq!(PacketLayout::close_request(false).header_len(), 12);
|
||||
assert_eq!(PacketLayout::close_request(true).header_len(), 28);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layout_matrix_matches_stats_timestamps_clock_and_hmac_rules() {
|
||||
for stats in [
|
||||
ReceivedStats::None,
|
||||
ReceivedStats::Count,
|
||||
ReceivedStats::Window,
|
||||
ReceivedStats::Both,
|
||||
] {
|
||||
for stamp_at in [
|
||||
StampAt::None,
|
||||
StampAt::Send,
|
||||
StampAt::Receive,
|
||||
StampAt::Both,
|
||||
StampAt::Midpoint,
|
||||
] {
|
||||
for clock in [
|
||||
Clock::Unspecified,
|
||||
Clock::Wall,
|
||||
Clock::Monotonic,
|
||||
Clock::Both,
|
||||
] {
|
||||
for hmac in [false, true] {
|
||||
let params = params(stats, stamp_at, clock);
|
||||
let layout = PacketLayout::echo(hmac, ¶ms);
|
||||
let expected_len = HEADER_SIZE
|
||||
+ TOKEN_SIZE
|
||||
+ SEQ_SIZE
|
||||
+ if hmac { HMAC_SIZE } else { 0 }
|
||||
+ expected_optional_len(stats, stamp_at, clock);
|
||||
|
||||
assert_eq!(
|
||||
layout.header_len(),
|
||||
expected_len,
|
||||
"unexpected length for stats={stats:?} stamp_at={stamp_at:?} clock={clock:?} hmac={hmac}"
|
||||
);
|
||||
assert_eq!(
|
||||
layout.recv_count,
|
||||
matches!(stats, ReceivedStats::Count | ReceivedStats::Both)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.recv_window,
|
||||
matches!(stats, ReceivedStats::Window | ReceivedStats::Both)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.recv_wall,
|
||||
matches!(stamp_at, StampAt::Receive | StampAt::Both)
|
||||
&& matches!(clock, Clock::Wall | Clock::Both)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.recv_mono,
|
||||
matches!(stamp_at, StampAt::Receive | StampAt::Both)
|
||||
&& matches!(clock, Clock::Monotonic | Clock::Both)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.midpoint_wall,
|
||||
matches!(stamp_at, StampAt::Midpoint)
|
||||
&& matches!(clock, Clock::Wall | Clock::Both)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.midpoint_mono,
|
||||
matches!(stamp_at, StampAt::Midpoint)
|
||||
&& matches!(clock, Clock::Monotonic | Clock::Both)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.send_wall,
|
||||
matches!(stamp_at, StampAt::Send | StampAt::Both)
|
||||
&& matches!(clock, Clock::Wall | Clock::Both)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.send_mono,
|
||||
matches!(stamp_at, StampAt::Send | StampAt::Both)
|
||||
&& matches!(clock, Clock::Monotonic | Clock::Both)
|
||||
);
|
||||
|
||||
if hmac {
|
||||
assert_eq!(
|
||||
echo_header_len(true, ¶ms) - echo_header_len(false, ¶ms),
|
||||
HMAC_SIZE
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiated_length_is_floored_at_the_required_field_block() {
|
||||
// The 60-byte header of this layout is the floor. A negative negotiated
|
||||
// length is not an error: it asks for nothing beyond the mandatory
|
||||
// fields, exactly as zero does.
|
||||
let mut p = params(ReceivedStats::Both, StampAt::Both, Clock::Both);
|
||||
for (length, expected) in [(-4096, 60), (-1, 60), (0, 60), (20, 60), (92, 92)] {
|
||||
p.length = length;
|
||||
assert_eq!(
|
||||
echo_packet_len(false, &p),
|
||||
Ok(expected),
|
||||
"unexpected packet length for negotiated length {length}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A positive length wider than `usize` must stay an error rather than
|
||||
/// becoming a saturated buffer size. Only reachable where `usize` is
|
||||
/// narrower than `i64`; on a 64-bit target every positive `i64` converts.
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[test]
|
||||
fn a_length_wider_than_usize_is_rejected_rather_than_saturated() {
|
||||
let mut p = params(ReceivedStats::Both, StampAt::Both, Clock::Both);
|
||||
p.length = 5_000_000_000;
|
||||
|
||||
assert_eq!(
|
||||
echo_packet_len(false, &p),
|
||||
Err(irtt_proto::ProtoError::PacketLengthUnrepresentable {
|
||||
length: 5_000_000_000
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
use irtt_proto::{
|
||||
varint, Clock, ParamPresence, Params, ProtoError, ReceivedStats, ServerFill, StampAt,
|
||||
};
|
||||
use proptest::prelude::*;
|
||||
|
||||
fn int(tag: u64, value: i64) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
varint::encode_uvarint(tag, &mut out);
|
||||
varint::encode_varint(value, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
fn fill(value: &[u8]) -> Vec<u8> {
|
||||
let mut out = vec![9];
|
||||
varint::encode_uvarint(value.len() as u64, &mut out);
|
||||
out.extend_from_slice(value);
|
||||
out
|
||||
}
|
||||
|
||||
fn presence(p: ParamPresence) -> [bool; 9] {
|
||||
[
|
||||
p.protocol_version,
|
||||
p.duration_ns,
|
||||
p.interval_ns,
|
||||
p.length,
|
||||
p.received_stats,
|
||||
p.stamp_at,
|
||||
p.clock,
|
||||
p.dscp,
|
||||
p.server_fill,
|
||||
]
|
||||
}
|
||||
|
||||
fn assert_error(bytes: &[u8], expected: ProtoError) {
|
||||
assert_eq!(Params::decode(bytes).unwrap_err(), expected, "{bytes:02x?}");
|
||||
assert_eq!(
|
||||
Params::decode_with_presence(bytes).unwrap_err(),
|
||||
expected,
|
||||
"{bytes:02x?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn known_encodings_preserve_raw_dscp_and_utf8_byte_lengths() {
|
||||
for (params, bytes) in [
|
||||
(
|
||||
Params {
|
||||
dscp: 46,
|
||||
..Params::default()
|
||||
},
|
||||
vec![8, 92],
|
||||
),
|
||||
(
|
||||
Params {
|
||||
dscp: 184,
|
||||
..Params::default()
|
||||
},
|
||||
vec![8, 0xf0, 2],
|
||||
),
|
||||
(
|
||||
Params {
|
||||
server_fill: Some(ServerFill {
|
||||
value: "rand".into(),
|
||||
}),
|
||||
..Params::default()
|
||||
},
|
||||
vec![9, 4, b'r', b'a', b'n', b'd'],
|
||||
),
|
||||
(
|
||||
Params {
|
||||
server_fill: Some(ServerFill { value: "é".into() }),
|
||||
..Params::default()
|
||||
},
|
||||
vec![9, 2, 0xc3, 0xa9],
|
||||
),
|
||||
] {
|
||||
assert_eq!(params.encode(), bytes);
|
||||
assert_eq!(Params::decode(&bytes).unwrap(), params);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wire_defaults_and_explicit_zero_presence() {
|
||||
let defaults = Params {
|
||||
protocol_version: 0,
|
||||
duration_ns: 0,
|
||||
interval_ns: 0,
|
||||
length: 0,
|
||||
received_stats: ReceivedStats::None,
|
||||
stamp_at: StampAt::None,
|
||||
clock: Clock::Unspecified,
|
||||
dscp: 0,
|
||||
server_fill: None,
|
||||
};
|
||||
let absent = Params::decode_with_presence(&[]).unwrap();
|
||||
assert_eq!(absent.params, defaults);
|
||||
assert_eq!(presence(absent.presence), [false; 9]);
|
||||
assert_eq!(defaults.encode(), Vec::<u8>::new());
|
||||
assert!(!defaults.clock.has_wall());
|
||||
assert!(!defaults.clock.has_mono());
|
||||
|
||||
// Clock is the sole integer tag for which explicit zero is invalid.
|
||||
for tag in [1, 2, 3, 4, 5, 6, 8] {
|
||||
let bytes = int(tag, 0);
|
||||
let decoded = Params::decode_with_presence(&bytes).unwrap();
|
||||
assert_eq!(decoded.params, defaults);
|
||||
let mut expected = [false; 9];
|
||||
expected[tag as usize - 1] = true;
|
||||
assert_eq!(presence(decoded.presence), expected, "tag {tag}");
|
||||
assert_eq!(Params::decode(&bytes).unwrap(), decoded.params);
|
||||
}
|
||||
let empty_fill = Params::decode_with_presence(&[9, 0]).unwrap();
|
||||
assert_eq!(
|
||||
empty_fill.params.server_fill,
|
||||
Some(ServerFill {
|
||||
value: String::new()
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
presence(empty_fill.presence),
|
||||
[false, false, false, false, false, false, false, false, true]
|
||||
);
|
||||
assert_eq!(empty_fill.params.encode(), [9, 0]);
|
||||
assert_eq!(
|
||||
Params {
|
||||
protocol_version: 1,
|
||||
..defaults
|
||||
}
|
||||
.encode(),
|
||||
[1, 2]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn known_tags_map_to_their_fields_and_report_isolated_presence() {
|
||||
for (tag, value, expected) in [
|
||||
(
|
||||
1,
|
||||
1,
|
||||
Params {
|
||||
protocol_version: 1,
|
||||
..Params::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
2,
|
||||
5,
|
||||
Params {
|
||||
duration_ns: 5,
|
||||
..Params::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
3,
|
||||
6,
|
||||
Params {
|
||||
interval_ns: 6,
|
||||
..Params::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
4,
|
||||
7,
|
||||
Params {
|
||||
length: 7,
|
||||
..Params::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
8,
|
||||
184,
|
||||
Params {
|
||||
dscp: 184,
|
||||
..Params::default()
|
||||
},
|
||||
),
|
||||
] {
|
||||
let decoded = Params::decode_with_presence(&int(tag, value)).unwrap();
|
||||
assert_eq!(decoded.params, expected);
|
||||
let mut expected_presence = [false; 9];
|
||||
expected_presence[tag as usize - 1] = true;
|
||||
assert_eq!(presence(decoded.presence), expected_presence);
|
||||
}
|
||||
for (tag, modes) in [
|
||||
(
|
||||
5,
|
||||
vec![
|
||||
ReceivedStats::None as i64,
|
||||
ReceivedStats::Count as i64,
|
||||
ReceivedStats::Window as i64,
|
||||
ReceivedStats::Both as i64,
|
||||
],
|
||||
),
|
||||
(
|
||||
6,
|
||||
vec![
|
||||
StampAt::None as i64,
|
||||
StampAt::Send as i64,
|
||||
StampAt::Receive as i64,
|
||||
StampAt::Both as i64,
|
||||
StampAt::Midpoint as i64,
|
||||
],
|
||||
),
|
||||
(
|
||||
7,
|
||||
vec![
|
||||
Clock::Wall as i64,
|
||||
Clock::Monotonic as i64,
|
||||
Clock::Both as i64,
|
||||
],
|
||||
),
|
||||
] {
|
||||
for (index, mode) in modes.into_iter().enumerate() {
|
||||
let value = index as i64 + if tag == 7 { 1 } else { 0 };
|
||||
let decoded = Params::decode_with_presence(&int(tag, value)).unwrap();
|
||||
let actual = match tag {
|
||||
5 => decoded.params.received_stats as i64,
|
||||
6 => decoded.params.stamp_at as i64,
|
||||
7 => decoded.params.clock as i64,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert_eq!(actual, mode);
|
||||
let mut expected_presence = [false; 9];
|
||||
expected_presence[tag as usize - 1] = true;
|
||||
assert_eq!(presence(decoded.presence), expected_presence);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_values_and_invalid_enums_are_rejected_by_both_decoders() {
|
||||
for (tag, value, name) in [
|
||||
(5, 4, "ReceivedStats"),
|
||||
(6, 5, "StampAt"),
|
||||
(7, 0, "Clock"),
|
||||
(7, 4, "Clock"),
|
||||
(5, -1, "ReceivedStats"),
|
||||
(6, -1, "StampAt"),
|
||||
(7, -1, "Clock"),
|
||||
] {
|
||||
assert_error(&int(tag, value), ProtoError::InvalidEnum { name, value });
|
||||
}
|
||||
for bytes in [&[1, 0x80][..], &[99], &[0x80], &[9, 0x80]] {
|
||||
assert_error(bytes, ProtoError::TruncatedVarint);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_fill_accepts_valid_bytes_and_enforces_length_and_utf8_boundaries() {
|
||||
for value in [
|
||||
"",
|
||||
"rand",
|
||||
"0123456789abcdef0123456789abcdef",
|
||||
"éééééééééééééééé",
|
||||
] {
|
||||
let expected = Params {
|
||||
server_fill: Some(ServerFill {
|
||||
value: value.into(),
|
||||
}),
|
||||
..Params::default()
|
||||
};
|
||||
let bytes = fill(value.as_bytes());
|
||||
assert_eq!(expected.encode(), bytes);
|
||||
let decoded = Params::decode_with_presence(&bytes).unwrap();
|
||||
assert_eq!(decoded.params, expected);
|
||||
assert!(decoded.presence.server_fill);
|
||||
assert_eq!(Params::decode(&bytes).unwrap(), expected);
|
||||
}
|
||||
assert_error(
|
||||
&fill(b"0123456789abcdef0123456789abcdefx"),
|
||||
ProtoError::ParameterLengthTooLarge { tag: 9, length: 33 },
|
||||
);
|
||||
assert_error(&[9, 4, b'a', b'b'], ProtoError::MalformedParams);
|
||||
assert_error(&[9, 1, 0xff], ProtoError::InvalidUtf8);
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[test]
|
||||
fn server_fill_length_too_large_for_usize_is_rejected() {
|
||||
let mut bytes = vec![9];
|
||||
let length = u64::from(u32::MAX) + 1;
|
||||
varint::encode_uvarint(length, &mut bytes);
|
||||
assert_error(
|
||||
&bytes,
|
||||
ProtoError::ParameterLengthTooLarge { tag: 9, length },
|
||||
);
|
||||
}
|
||||
|
||||
fn string_strategy() -> impl Strategy<Value = String> {
|
||||
// Eight arbitrary Unicode scalars occupy at most the allowed 32 bytes.
|
||||
proptest::collection::vec(any::<char>(), 0..=8).prop_map(|chars| chars.into_iter().collect())
|
||||
}
|
||||
|
||||
fn params_strategy() -> impl Strategy<Value = Params> {
|
||||
(
|
||||
any::<[i64; 5]>(),
|
||||
prop::sample::select(vec![
|
||||
ReceivedStats::None,
|
||||
ReceivedStats::Count,
|
||||
ReceivedStats::Window,
|
||||
ReceivedStats::Both,
|
||||
]),
|
||||
prop::sample::select(vec![
|
||||
StampAt::None,
|
||||
StampAt::Send,
|
||||
StampAt::Receive,
|
||||
StampAt::Both,
|
||||
StampAt::Midpoint,
|
||||
]),
|
||||
prop::sample::select(vec![
|
||||
Clock::Unspecified,
|
||||
Clock::Wall,
|
||||
Clock::Monotonic,
|
||||
Clock::Both,
|
||||
]),
|
||||
prop::option::of(string_strategy()),
|
||||
)
|
||||
.prop_map(
|
||||
|(scalar, received_stats, stamp_at, clock, server_fill)| Params {
|
||||
protocol_version: scalar[0],
|
||||
duration_ns: scalar[1],
|
||||
interval_ns: scalar[2],
|
||||
length: scalar[3],
|
||||
dscp: scalar[4],
|
||||
received_stats,
|
||||
stamp_at,
|
||||
clock,
|
||||
server_fill: server_fill.map(|value| ServerFill { value }),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn all_tags(params: &Params) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
// Deliberately bypass encode's omission: duplicate zero values must be present.
|
||||
for (tag, value) in [
|
||||
(1, params.protocol_version),
|
||||
(2, params.duration_ns),
|
||||
(3, params.interval_ns),
|
||||
(4, params.length),
|
||||
(5, params.received_stats as i64),
|
||||
(6, params.stamp_at as i64),
|
||||
(7, params.clock as i64),
|
||||
(8, params.dscp),
|
||||
] {
|
||||
out.extend_from_slice(&int(tag, value));
|
||||
}
|
||||
out.extend_from_slice(&fill(params.server_fill.as_ref().unwrap().value.as_bytes()));
|
||||
out
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn valid_params_round_trip_with_omission_presence(params in params_strategy()) {
|
||||
let encoded = params.encode();
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(&decoded.params, ¶ms);
|
||||
prop_assert_eq!(Params::decode(&encoded).unwrap(), params.clone());
|
||||
prop_assert_eq!(presence(decoded.presence), [
|
||||
params.protocol_version != 0, params.duration_ns != 0,
|
||||
params.interval_ns != 0, params.length != 0,
|
||||
params.received_stats != ReceivedStats::None, params.stamp_at != StampAt::None,
|
||||
params.clock != Clock::Unspecified, params.dscp != 0, params.server_fill.is_some(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_known_tags_are_last_wins(
|
||||
mut first in params_strategy(), mut second in params_strategy(),
|
||||
first_clock in 1_i64..=3, second_clock in 1_i64..=3,
|
||||
first_fill in string_strategy(), second_fill in string_strategy(),
|
||||
) {
|
||||
first.clock = Clock::try_from(first_clock).unwrap();
|
||||
second.clock = Clock::try_from(second_clock).unwrap();
|
||||
first.server_fill = Some(ServerFill { value: first_fill });
|
||||
second.server_fill = Some(ServerFill { value: second_fill });
|
||||
let mut bytes = all_tags(&first);
|
||||
bytes.extend_from_slice(&all_tags(&second));
|
||||
let decoded = Params::decode_with_presence(&bytes).unwrap();
|
||||
prop_assert_eq!(&decoded.params, &second);
|
||||
prop_assert_eq!(Params::decode(&bytes).unwrap(), second);
|
||||
prop_assert_eq!(presence(decoded.presence), [true; 9]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_scalar_tags_do_not_disturb_known_params(
|
||||
params in params_strategy(), tag in 10_u64..=u16::MAX as u64, value: u64,
|
||||
) {
|
||||
let encoded = params.encode();
|
||||
let expected = Params::decode_with_presence(&encoded).unwrap();
|
||||
let mut unknown = Vec::new();
|
||||
varint::encode_uvarint(tag, &mut unknown);
|
||||
varint::encode_uvarint(value, &mut unknown);
|
||||
for bytes in [[unknown.as_slice(), &encoded].concat(), [&encoded, unknown.as_slice()].concat()] {
|
||||
prop_assert_eq!(Params::decode_with_presence(&bytes).unwrap(), expected.clone());
|
||||
prop_assert_eq!(Params::decode(&bytes).unwrap(), params.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
+223
-171
@@ -54,33 +54,41 @@ fn echo_reply(params: &Params, flags: u8) -> EchoReply {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authenticated_reply_encoders_set_hmac_from_key() {
|
||||
fn reply_encoders_normalize_hmac_flags_from_the_key() {
|
||||
let params = params();
|
||||
let open_reply = OpenReply {
|
||||
flags: FLAG_OPEN | FLAG_REPLY,
|
||||
token: TOKEN,
|
||||
params: params.clone(),
|
||||
};
|
||||
let packet = encode_open_reply(&open_reply, Some(KEY)).unwrap();
|
||||
assert_eq!(packet[3], FLAG_OPEN | FLAG_REPLY | FLAG_HMAC);
|
||||
assert_eq!(
|
||||
decode_open_reply(&packet, Some(KEY)),
|
||||
Ok(OpenReply {
|
||||
flags: FLAG_OPEN | FLAG_REPLY | FLAG_HMAC,
|
||||
..open_reply
|
||||
})
|
||||
);
|
||||
|
||||
let echo_reply = echo_reply(¶ms, FLAG_REPLY);
|
||||
let packet = encode_echo_reply(&echo_reply, ¶ms, Some(KEY)).unwrap();
|
||||
assert_eq!(packet[3], FLAG_REPLY | FLAG_HMAC);
|
||||
assert_eq!(
|
||||
decode_echo_reply(&packet, ¶ms, Some(KEY)),
|
||||
Ok(EchoReply {
|
||||
flags: FLAG_REPLY | FLAG_HMAC,
|
||||
..echo_reply
|
||||
})
|
||||
);
|
||||
for key in [None, Some(KEY)] {
|
||||
for supplied_hmac in [0, FLAG_HMAC] {
|
||||
let auth = if key.is_some() { FLAG_HMAC } else { 0 };
|
||||
let open = OpenReply {
|
||||
flags: FLAG_OPEN | FLAG_REPLY | supplied_hmac,
|
||||
token: TOKEN,
|
||||
params: params.clone(),
|
||||
};
|
||||
let packet = encode_open_reply(&open, key).unwrap();
|
||||
assert_eq!(packet[3], FLAG_OPEN | FLAG_REPLY | auth);
|
||||
let offset = if key.is_some() { 20 } else { 4 };
|
||||
assert_eq!(&packet[offset..offset + 8], &TOKEN.to_le_bytes());
|
||||
assert_eq!(
|
||||
decode_open_reply(&packet, key).unwrap(),
|
||||
OpenReply {
|
||||
flags: packet[3],
|
||||
..open
|
||||
}
|
||||
);
|
||||
let echo = echo_reply(¶ms, FLAG_REPLY | supplied_hmac);
|
||||
let packet = encode_echo_reply(&echo, ¶ms, key).unwrap();
|
||||
assert_eq!(packet[3], FLAG_REPLY | auth);
|
||||
assert_eq!(&packet[offset..offset + 8], &TOKEN.to_le_bytes());
|
||||
assert_eq!(&packet[offset + 8..offset + 12], &u32::MAX.to_le_bytes());
|
||||
assert_eq!(
|
||||
decode_echo_reply(&packet, ¶ms, key).unwrap(),
|
||||
EchoReply {
|
||||
flags: packet[3],
|
||||
..echo
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -95,6 +103,12 @@ fn rejected_open_reply_round_trips_and_zero_token_requires_close() {
|
||||
};
|
||||
let packet = encode_open_reply(&rejected, None).unwrap();
|
||||
assert_eq!(decode_open_reply(&packet, None), Ok(rejected.clone()));
|
||||
let mut zero_without_close = packet.clone();
|
||||
zero_without_close[3] &= !FLAG_CLOSE;
|
||||
assert_eq!(
|
||||
decode_open_reply(&zero_without_close, None),
|
||||
Err(ProtoError::ZeroToken)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
encode_open_reply(
|
||||
@@ -119,63 +133,51 @@ fn peer_close_echo_reply_round_trips_without_a_close_reply_codec() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_encoders_supply_their_packet_type_rules() {
|
||||
fn reply_codecs_enforce_packet_type_and_reserved_flag_rules() {
|
||||
let params = Params::default();
|
||||
assert_eq!(
|
||||
encode_open_reply(
|
||||
&OpenReply {
|
||||
flags: FLAG_REPLY,
|
||||
token: TOKEN,
|
||||
params: params.clone(),
|
||||
},
|
||||
None,
|
||||
for (flags, expected) in [
|
||||
(FLAG_REPLY, ProtoError::MissingFlag(FLAG_OPEN)),
|
||||
(FLAG_OPEN, ProtoError::MissingFlag(FLAG_REPLY)),
|
||||
(
|
||||
FLAG_OPEN | FLAG_REPLY | 0x10,
|
||||
ProtoError::ReservedFlags(0x10),
|
||||
),
|
||||
Err(ProtoError::MissingFlag(FLAG_OPEN))
|
||||
);
|
||||
assert_eq!(
|
||||
encode_echo_reply(
|
||||
&EchoReply {
|
||||
flags: 0,
|
||||
..echo_reply(¶ms, FLAG_REPLY)
|
||||
},
|
||||
¶ms,
|
||||
None,
|
||||
),
|
||||
Err(ProtoError::MissingFlag(FLAG_REPLY))
|
||||
);
|
||||
}
|
||||
|
||||
/// Every packet the reply encoders produce must be rejected by the inbound
|
||||
/// request decoder: a server never answers a datagram carrying `FLAG_REPLY`.
|
||||
#[test]
|
||||
fn encoded_replies_are_never_admitted_as_inbound_requests() {
|
||||
let params = Params::default();
|
||||
|
||||
for reply_flags in [FLAG_OPEN | FLAG_REPLY, FLAG_OPEN | FLAG_REPLY | FLAG_CLOSE] {
|
||||
let packet = encode_open_reply(
|
||||
] {
|
||||
let open = OpenReply {
|
||||
flags,
|
||||
token: TOKEN,
|
||||
params: params.clone(),
|
||||
};
|
||||
assert_eq!(encode_open_reply(&open, None).unwrap_err(), expected);
|
||||
let mut packet = encode_open_reply(
|
||||
&OpenReply {
|
||||
flags: reply_flags,
|
||||
token: if reply_flags & FLAG_CLOSE == 0 {
|
||||
TOKEN
|
||||
} else {
|
||||
0
|
||||
},
|
||||
params: params.clone(),
|
||||
flags: FLAG_OPEN | FLAG_REPLY,
|
||||
..open
|
||||
},
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
decode_request(&packet),
|
||||
Err(ProtoError::UnexpectedFlag(FLAG_REPLY))
|
||||
);
|
||||
packet[3] = flags;
|
||||
assert_eq!(decode_open_reply(&packet, None).unwrap_err(), expected);
|
||||
}
|
||||
|
||||
for reply_flags in [FLAG_REPLY, FLAG_REPLY | FLAG_CLOSE] {
|
||||
let packet = encode_echo_reply(&echo_reply(¶ms, reply_flags), ¶ms, None).unwrap();
|
||||
for (flags, expected) in [
|
||||
(0, ProtoError::MissingFlag(FLAG_REPLY)),
|
||||
(
|
||||
FLAG_OPEN | FLAG_REPLY,
|
||||
ProtoError::UnexpectedFlag(FLAG_OPEN),
|
||||
),
|
||||
(FLAG_REPLY | 0x10, ProtoError::ReservedFlags(0x10)),
|
||||
] {
|
||||
assert_eq!(
|
||||
decode_request(&packet),
|
||||
Err(ProtoError::UnexpectedFlag(FLAG_REPLY))
|
||||
encode_echo_reply(&echo_reply(¶ms, flags), ¶ms, None).unwrap_err(),
|
||||
expected
|
||||
);
|
||||
let mut packet =
|
||||
encode_echo_reply(&echo_reply(¶ms, FLAG_REPLY), ¶ms, None).unwrap();
|
||||
packet[3] = flags;
|
||||
assert_eq!(
|
||||
decode_echo_reply(&packet, ¶ms, None).unwrap_err(),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -207,7 +209,12 @@ fn echo_layout_combinations_round_trip() {
|
||||
StampAt::Both,
|
||||
StampAt::Midpoint,
|
||||
] {
|
||||
for clock in [Clock::Wall, Clock::Monotonic, Clock::Both] {
|
||||
for clock in [
|
||||
Clock::Unspecified,
|
||||
Clock::Wall,
|
||||
Clock::Monotonic,
|
||||
Clock::Both,
|
||||
] {
|
||||
for key in [None, Some(KEY)] {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
@@ -265,44 +272,48 @@ fn echo_reply_requires_exact_negotiated_optional_fields() {
|
||||
|
||||
#[test]
|
||||
fn echo_payload_is_copied_zero_filled_and_bounded_in_both_directions() {
|
||||
let params = Params {
|
||||
length: 20,
|
||||
..Params::default()
|
||||
};
|
||||
|
||||
let packet = encode_request(echo_request(¶ms, &[1, 2]), None).unwrap();
|
||||
assert_eq!(&packet[16..], &[1, 2, 0, 0]);
|
||||
assert_eq!(
|
||||
encode_request(echo_request(¶ms, &[0; 5]), None),
|
||||
Err(ProtoError::PayloadTooLarge {
|
||||
available: 4,
|
||||
provided: 5,
|
||||
})
|
||||
);
|
||||
|
||||
let reply = EchoReply {
|
||||
payload: vec![3, 4],
|
||||
..echo_reply(¶ms, FLAG_REPLY)
|
||||
};
|
||||
let packet = encode_echo_reply(&reply, ¶ms, None).unwrap();
|
||||
assert_eq!(&packet[16..], &[3, 4, 0, 0]);
|
||||
let decoded = decode_echo_reply(&packet, ¶ms, None).unwrap();
|
||||
assert_eq!(decoded.payload, vec![3, 4, 0, 0]);
|
||||
assert_eq!(encode_echo_reply(&decoded, ¶ms, None).unwrap(), packet);
|
||||
assert_eq!(
|
||||
encode_echo_reply(
|
||||
&EchoReply {
|
||||
payload: vec![0; 5],
|
||||
for stats in [ReceivedStats::None, ReceivedStats::Both] {
|
||||
for key in [None, Some(KEY)] {
|
||||
let mut params = Params {
|
||||
received_stats: stats,
|
||||
..Params::default()
|
||||
};
|
||||
let offset = PacketLayout::echo(key.is_some(), ¶ms).header_len();
|
||||
params.length = (offset + 4) as i64;
|
||||
let packet = encode_request(echo_request(¶ms, &[1, 2]), key).unwrap();
|
||||
assert_eq!(&packet[offset..], &[1, 2, 0, 0]);
|
||||
assert_eq!(
|
||||
encode_request(echo_request(¶ms, &[0; 5]), key),
|
||||
Err(ProtoError::PayloadTooLarge {
|
||||
available: 4,
|
||||
provided: 5
|
||||
})
|
||||
);
|
||||
let reply = EchoReply {
|
||||
payload: vec![3, 4],
|
||||
..echo_reply(¶ms, FLAG_REPLY)
|
||||
},
|
||||
¶ms,
|
||||
None,
|
||||
),
|
||||
Err(ProtoError::PayloadTooLarge {
|
||||
available: 4,
|
||||
provided: 5,
|
||||
})
|
||||
);
|
||||
};
|
||||
let packet = encode_echo_reply(&reply, ¶ms, key).unwrap();
|
||||
assert_eq!(&packet[offset..], &[3, 4, 0, 0]);
|
||||
let decoded = decode_echo_reply(&packet, ¶ms, key).unwrap();
|
||||
assert_eq!(decoded.payload, vec![3, 4, 0, 0]);
|
||||
assert_eq!(encode_echo_reply(&decoded, ¶ms, key).unwrap(), packet);
|
||||
assert_eq!(
|
||||
encode_echo_reply(
|
||||
&EchoReply {
|
||||
payload: vec![0; 5],
|
||||
..reply
|
||||
},
|
||||
¶ms,
|
||||
key
|
||||
),
|
||||
Err(ProtoError::PayloadTooLarge {
|
||||
available: 4,
|
||||
provided: 5
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -321,6 +332,14 @@ fn a_negative_negotiated_length_still_encodes_in_both_directions() {
|
||||
let request = encode_request(echo_request(¶ms, &[]), None)
|
||||
.expect("a negative negotiated length must still encode a request");
|
||||
assert_eq!(request.len(), 16);
|
||||
assert_eq!(
|
||||
decode_request(&request).unwrap().kind,
|
||||
DecodedRequestKind::Echo {
|
||||
token: TOKEN,
|
||||
sequence: 17,
|
||||
tail: &[]
|
||||
}
|
||||
);
|
||||
|
||||
let reply = encode_echo_reply(&echo_reply(¶ms, FLAG_REPLY), ¶ms, None)
|
||||
.expect("a negative negotiated length must still encode a reply");
|
||||
@@ -505,12 +524,14 @@ fn midpoint_dual_field_reply_authenticates_over_full_packet() {
|
||||
let reply = decode_echo_reply(&packet, ¶ms, Some(KEY)).unwrap();
|
||||
assert_eq!(reply.timestamps.midpoint_wall, Some(555));
|
||||
|
||||
let mut corrupted = packet;
|
||||
corrupted[HMAC_OFFSET] ^= 0xFF;
|
||||
assert_eq!(
|
||||
decode_echo_reply(&corrupted, ¶ms, Some(KEY)),
|
||||
Err(ProtoError::BadHmac)
|
||||
);
|
||||
for offset in [HMAC_OFFSET, PacketLayout::echo(true, ¶ms).header_len()] {
|
||||
let mut corrupted = packet.clone();
|
||||
corrupted[offset] ^= 0xFF;
|
||||
assert_eq!(
|
||||
decode_echo_reply(&corrupted, ¶ms, Some(KEY)),
|
||||
Err(ProtoError::BadHmac)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -539,61 +560,92 @@ fn only_the_exact_compat_length_is_accepted_as_an_extension() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn midpoint_both_clock_reply_has_no_additional_extension() {
|
||||
let params = midpoint_params(Clock::Both, 0);
|
||||
let reply = echo_reply(¶ms, FLAG_REPLY);
|
||||
let packet = encode_echo_reply(&reply, ¶ms, None).unwrap();
|
||||
|
||||
let mut too_long = packet.clone();
|
||||
too_long.extend_from_slice(&[0; 8]);
|
||||
assert_eq!(
|
||||
decode_echo_reply(&too_long, ¶ms, None),
|
||||
Err(ProtoError::PacketLengthMismatch {
|
||||
expected: packet.len(),
|
||||
actual: too_long.len(),
|
||||
})
|
||||
);
|
||||
fn other_layouts_have_no_longer_form_exception() {
|
||||
for (stamp_at, clock) in [
|
||||
(StampAt::Midpoint, Clock::Both),
|
||||
(StampAt::Receive, Clock::Wall),
|
||||
] {
|
||||
let params = Params {
|
||||
stamp_at,
|
||||
clock,
|
||||
..Params::default()
|
||||
};
|
||||
let packet = encode_echo_reply(&echo_reply(¶ms, FLAG_REPLY), ¶ms, None).unwrap();
|
||||
let mut too_long = packet.clone();
|
||||
too_long.extend_from_slice(&[0; 8]);
|
||||
assert_eq!(
|
||||
decode_echo_reply(&too_long, ¶ms, None),
|
||||
Err(ProtoError::PacketLengthMismatch {
|
||||
expected: packet.len(),
|
||||
actual: too_long.len()
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_midpoint_stamp_at_has_no_longer_form_exception() {
|
||||
let params = Params {
|
||||
received_stats: ReceivedStats::None,
|
||||
stamp_at: StampAt::Receive,
|
||||
clock: Clock::Wall,
|
||||
..Params::default()
|
||||
};
|
||||
let reply = echo_reply(¶ms, FLAG_REPLY);
|
||||
let packet = encode_echo_reply(&reply, ¶ms, None).unwrap();
|
||||
|
||||
let mut too_long = packet.clone();
|
||||
too_long.extend_from_slice(&[0; 8]);
|
||||
assert_eq!(
|
||||
decode_echo_reply(&too_long, ¶ms, None),
|
||||
Err(ProtoError::PacketLengthMismatch {
|
||||
expected: packet.len(),
|
||||
actual: too_long.len(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/// The midpoint compatibility length is a *reply* concept. An inbound ECHO
|
||||
/// request of that length is just a longer request, which a receiver accepts
|
||||
/// without applying any negotiated length rule.
|
||||
#[test]
|
||||
fn echo_request_decoding_has_no_compat_length_rule() {
|
||||
let params = midpoint_params(Clock::Wall, 0);
|
||||
let packet = encode_request(echo_request(¶ms, &[]), None).unwrap();
|
||||
|
||||
let mut too_long = packet.clone();
|
||||
too_long.extend_from_slice(&[0; 8]);
|
||||
let request = decode_request(&too_long).unwrap();
|
||||
assert_eq!(
|
||||
request.kind,
|
||||
DecodedRequestKind::Echo {
|
||||
token: TOKEN,
|
||||
sequence: 17,
|
||||
tail: &too_long[16..],
|
||||
fn reply_decoders_enforce_hmac_presence_and_authenticate_the_body() {
|
||||
let params = params();
|
||||
for key in [None, Some(KEY)] {
|
||||
let open = encode_open_reply(
|
||||
&OpenReply {
|
||||
flags: FLAG_OPEN | FLAG_REPLY,
|
||||
token: TOKEN,
|
||||
params: params.clone(),
|
||||
},
|
||||
key,
|
||||
)
|
||||
.unwrap();
|
||||
let echo = encode_echo_reply(&echo_reply(¶ms, FLAG_REPLY), ¶ms, key).unwrap();
|
||||
let mismatched_key = if key.is_some() { None } else { Some(KEY) };
|
||||
assert_eq!(
|
||||
decode_open_reply(&open, mismatched_key),
|
||||
Err(ProtoError::HmacPresenceMismatch)
|
||||
);
|
||||
assert_eq!(
|
||||
decode_echo_reply(&echo, ¶ms, mismatched_key),
|
||||
Err(ProtoError::HmacPresenceMismatch)
|
||||
);
|
||||
if let Some(key) = key {
|
||||
let mut bad_open = open;
|
||||
let mut bad_echo = echo;
|
||||
*bad_open.last_mut().unwrap() ^= 1;
|
||||
*bad_echo.last_mut().unwrap() ^= 1;
|
||||
assert_eq!(
|
||||
decode_open_reply(&bad_open, Some(key)),
|
||||
Err(ProtoError::BadHmac)
|
||||
);
|
||||
assert_eq!(
|
||||
decode_echo_reply(&bad_echo, ¶ms, Some(key)),
|
||||
Err(ProtoError::BadHmac)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_reply_length_errors_precede_hmac_verification() {
|
||||
for key in [None, Some(KEY)] {
|
||||
let mut params = Params::default();
|
||||
let header = PacketLayout::echo(key.is_some(), ¶ms).header_len();
|
||||
params.length = (header + 4) as i64;
|
||||
let packet = encode_echo_reply(&echo_reply(¶ms, FLAG_REPLY), ¶ms, key).unwrap();
|
||||
for actual in [header - 1, header, packet.len() - 1, packet.len() + 1] {
|
||||
let mut malformed = packet.clone();
|
||||
malformed.resize(actual, 0);
|
||||
// Truncating or extending the signed packet also invalidates its MAC.
|
||||
let expected = if actual < header {
|
||||
ProtoError::PacketTooShort {
|
||||
needed: header,
|
||||
actual,
|
||||
}
|
||||
} else {
|
||||
ProtoError::PacketLengthMismatch {
|
||||
expected: packet.len(),
|
||||
actual,
|
||||
}
|
||||
};
|
||||
assert_eq!(decode_echo_reply(&malformed, ¶ms, key), Err(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,8 +47,6 @@ fn token_and_sequence() -> Vec<u8> {
|
||||
body
|
||||
}
|
||||
|
||||
// ---------- A. Classification ----------
|
||||
|
||||
fn classify(kind: &DecodedRequestKind<'_>) -> &'static str {
|
||||
match kind {
|
||||
DecodedRequestKind::Open { no_test: false, .. } => "open",
|
||||
@@ -71,6 +69,34 @@ fn flag_combinations_classify_requests_with_orthogonal_hmac_presence() {
|
||||
(FLAG_OPEN | FLAG_CLOSE | FLAG_HMAC, true, "no-test open"),
|
||||
];
|
||||
|
||||
for no_test in [false, true] {
|
||||
for key in [None, Some(KEY)] {
|
||||
let params = Params::default();
|
||||
let packet = encode_request(
|
||||
RequestToEncode::Open {
|
||||
params: ¶ms,
|
||||
no_test,
|
||||
},
|
||||
key,
|
||||
)
|
||||
.unwrap();
|
||||
let expected_flags = FLAG_OPEN
|
||||
| if no_test { FLAG_CLOSE } else { 0 }
|
||||
| if key.is_some() { FLAG_HMAC } else { 0 };
|
||||
assert_eq!(packet[3], expected_flags);
|
||||
assert_eq!(
|
||||
decode_request(&packet).unwrap().kind,
|
||||
DecodedRequestKind::Open {
|
||||
no_test,
|
||||
params: &[]
|
||||
}
|
||||
);
|
||||
if let Some(key) = key {
|
||||
verify_packet_hmac(key, &packet).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (flags, hmac_present, expected) in cases {
|
||||
// Long enough for any kind, so only the flags decide classification.
|
||||
let packet = raw(flags, &token_and_sequence(), 64);
|
||||
@@ -82,6 +108,10 @@ fn flag_combinations_classify_requests_with_orthogonal_hmac_presence() {
|
||||
request.hmac_present, hmac_present,
|
||||
"flags 0x{flags:02x} HMAC presence"
|
||||
);
|
||||
if let DecodedRequestKind::Open { params, .. } = request.kind {
|
||||
let offset = if hmac_present { 20 } else { 4 };
|
||||
assert_eq!(params, &packet[offset..]);
|
||||
}
|
||||
assert_eq!(
|
||||
classify(&request.kind),
|
||||
expected,
|
||||
@@ -90,8 +120,6 @@ fn flag_combinations_classify_requests_with_orthogonal_hmac_presence() {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- B. Invalid header / flags ----------
|
||||
|
||||
#[test]
|
||||
fn structurally_invalid_headers_are_rejected() {
|
||||
assert_eq!(
|
||||
@@ -135,8 +163,6 @@ fn any_reply_flagged_datagram_is_rejected_as_a_request() {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- C. Structural minimum lengths ----------
|
||||
|
||||
#[test]
|
||||
fn structural_minimum_lengths_are_enforced_per_kind() {
|
||||
// (flags, minimum accepted length)
|
||||
@@ -172,8 +198,6 @@ fn structural_minimum_lengths_are_enforced_per_kind() {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- D. Field offsets ----------
|
||||
|
||||
#[test]
|
||||
fn token_and_sequence_are_read_from_the_hmac_dependent_offsets() {
|
||||
for hmac in [false, true] {
|
||||
@@ -211,69 +235,25 @@ fn token_and_sequence_are_read_from_the_hmac_dependent_offsets() {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- E. Open parameter slice ----------
|
||||
|
||||
#[test]
|
||||
fn open_params_are_borrowed_after_the_header_and_decoded_later() {
|
||||
let encoded = params().encode();
|
||||
|
||||
for hmac in [false, true] {
|
||||
let flags = FLAG_OPEN | if hmac { FLAG_HMAC } else { 0 };
|
||||
let expected_offset = if hmac { 4 + HMAC_SIZE } else { 4 };
|
||||
let packet = raw(flags, &encoded, expected_offset + encoded.len());
|
||||
|
||||
match decode_request(&packet).unwrap().kind {
|
||||
DecodedRequestKind::Open { params, .. } => {
|
||||
assert_eq!(params, &packet[expected_offset..]);
|
||||
assert_eq!(Params::decode(params).unwrap(), self::params());
|
||||
// Structural parsing accepts even malformed Params; semantic errors belong
|
||||
// to Params::decode and are covered in params.rs.
|
||||
for body in [params().encode(), Vec::new(), vec![1, 0x80]] {
|
||||
for hmac in [false, true] {
|
||||
let flags = FLAG_OPEN | if hmac { FLAG_HMAC } else { 0 };
|
||||
let offset = if hmac { 20 } else { 4 };
|
||||
let packet = raw(flags, &body, offset + body.len());
|
||||
match decode_request(&packet).unwrap().kind {
|
||||
DecodedRequestKind::Open { params, .. } => {
|
||||
assert_eq!(params, &packet[offset..]);
|
||||
}
|
||||
other => panic!("expected an open request, got {other:?}"),
|
||||
}
|
||||
other => panic!("expected an open request, got {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_and_malformed_open_params_decode_structurally() {
|
||||
let empty = header(FLAG_OPEN);
|
||||
match decode_request(&empty).unwrap().kind {
|
||||
DecodedRequestKind::Open { params, .. } => assert!(params.is_empty()),
|
||||
other => panic!("expected an open request, got {other:?}"),
|
||||
}
|
||||
|
||||
// A truncated varint: structurally fine, semantically not. A receiver must
|
||||
// be able to authenticate before spending effort on this.
|
||||
let malformed = raw(FLAG_OPEN, &[1, 0x80], 6);
|
||||
match decode_request(&malformed).unwrap().kind {
|
||||
DecodedRequestKind::Open { params, .. } => {
|
||||
assert_eq!(params, &[1, 0x80]);
|
||||
assert_eq!(Params::decode(params), Err(ProtoError::TruncatedVarint));
|
||||
}
|
||||
other => panic!("expected an open request, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- F. Open precedence ----------
|
||||
|
||||
#[test]
|
||||
fn open_precedence_beats_close_and_an_echo_shaped_body() {
|
||||
// An OPEN|CLOSE datagram whose body looks exactly like a token and
|
||||
// sequence number is still a no-test open, and those bytes are parameters.
|
||||
let packet = raw(FLAG_OPEN | FLAG_CLOSE, &token_and_sequence(), 16);
|
||||
|
||||
assert_eq!(
|
||||
decode_request(&packet).unwrap(),
|
||||
DecodedRequest {
|
||||
hmac_present: false,
|
||||
kind: DecodedRequestKind::Open {
|
||||
no_test: true,
|
||||
params: &packet[4..],
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// ---------- G. Trailing data ----------
|
||||
|
||||
#[test]
|
||||
fn trailing_bytes_are_tolerated_and_no_length_ceiling_applies() {
|
||||
let mut close = raw(FLAG_CLOSE, &TOKEN.to_le_bytes(), 12);
|
||||
@@ -303,8 +283,6 @@ fn trailing_bytes_are_tolerated_and_no_length_ceiling_applies() {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- H. Zero token ----------
|
||||
|
||||
#[test]
|
||||
fn a_zero_token_is_structurally_valid() {
|
||||
assert_eq!(
|
||||
@@ -323,8 +301,6 @@ fn a_zero_token_is_structurally_valid() {
|
||||
);
|
||||
}
|
||||
|
||||
// ---------- HMAC presence is not authentication ----------
|
||||
|
||||
#[test]
|
||||
fn hmac_presence_is_reported_independently_of_mac_validity() {
|
||||
// A structurally complete authenticated echo whose MAC is garbage.
|
||||
@@ -378,106 +354,3 @@ fn packet_verification_requires_a_present_and_complete_hmac_field() {
|
||||
Err(ProtoError::BadMagic)
|
||||
);
|
||||
}
|
||||
|
||||
// ---------- Encode -> decode cross-checks ----------
|
||||
|
||||
#[test]
|
||||
fn encoded_requests_decode_back_to_their_sender_side_identity() {
|
||||
for key in [None, Some(KEY)] {
|
||||
for no_test in [false, true] {
|
||||
let packet = encode_request(
|
||||
RequestToEncode::Open {
|
||||
params: ¶ms(),
|
||||
no_test,
|
||||
},
|
||||
key,
|
||||
)
|
||||
.unwrap();
|
||||
let request = decode_request(&packet).unwrap();
|
||||
assert_eq!(request.hmac_present, key.is_some());
|
||||
match request.kind {
|
||||
DecodedRequestKind::Open {
|
||||
no_test: decoded,
|
||||
params: encoded,
|
||||
} => {
|
||||
assert_eq!(decoded, no_test);
|
||||
assert_eq!(Params::decode(encoded).unwrap(), params());
|
||||
}
|
||||
other => panic!("expected an open request, got {other:?}"),
|
||||
}
|
||||
if let Some(key) = key {
|
||||
verify_packet_hmac(key, &packet).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
let packet = encode_request(RequestToEncode::Close { token: TOKEN }, key).unwrap();
|
||||
let request = decode_request(&packet).unwrap();
|
||||
assert_eq!(request.hmac_present, key.is_some());
|
||||
assert_eq!(request.kind, DecodedRequestKind::Close { token: TOKEN });
|
||||
|
||||
// The ECHO tail is deliberately not compared against the logical
|
||||
// payload: the sender placed it at a negotiated offset the receiver
|
||||
// cannot recover without `Params`.
|
||||
let echo_params = Params {
|
||||
length: 96,
|
||||
..params()
|
||||
};
|
||||
let packet = encode_request(
|
||||
RequestToEncode::Echo {
|
||||
token: TOKEN,
|
||||
sequence: SEQUENCE,
|
||||
params: &echo_params,
|
||||
payload: &[1, 2, 3, 4],
|
||||
},
|
||||
key,
|
||||
)
|
||||
.unwrap();
|
||||
let request = decode_request(&packet).unwrap();
|
||||
assert_eq!(request.hmac_present, key.is_some());
|
||||
match request.kind {
|
||||
DecodedRequestKind::Echo {
|
||||
token, sequence, ..
|
||||
} => {
|
||||
assert_eq!(token, TOKEN);
|
||||
assert_eq!(sequence, SEQUENCE);
|
||||
}
|
||||
other => panic!("expected an echo request, got {other:?}"),
|
||||
}
|
||||
if let Some(key) = key {
|
||||
verify_packet_hmac(key, &packet).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_encoding_floors_a_negative_negotiated_length_at_the_field_block() {
|
||||
// A negative negotiated length is accepted during open and echoed back
|
||||
// unchanged, so a real session can carry one. It must therefore stay
|
||||
// encodable: it asks for no space beyond the mandatory fields.
|
||||
let params = Params {
|
||||
length: -1,
|
||||
..Params::default()
|
||||
};
|
||||
let packet = encode_request(
|
||||
RequestToEncode::Echo {
|
||||
token: TOKEN,
|
||||
sequence: SEQUENCE,
|
||||
params: ¶ms,
|
||||
payload: &[],
|
||||
},
|
||||
None,
|
||||
)
|
||||
.expect("a negative negotiated length must still encode");
|
||||
|
||||
// header (4) + token (8) + sequence (4).
|
||||
assert_eq!(packet.len(), 16);
|
||||
let request = decode_request(&packet).unwrap();
|
||||
assert_eq!(
|
||||
request.kind,
|
||||
DecodedRequestKind::Echo {
|
||||
token: TOKEN,
|
||||
sequence: SEQUENCE,
|
||||
tail: &[],
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
use irtt_proto::{varint::*, ProtoError};
|
||||
use proptest::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn boundary_values_round_trip() {
|
||||
for value in [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
127,
|
||||
128,
|
||||
255,
|
||||
16_384,
|
||||
u32::MAX as u64,
|
||||
i64::MAX as u64,
|
||||
u64::MAX,
|
||||
] {
|
||||
let mut encoded = Vec::new();
|
||||
encode_uvarint(value, &mut encoded);
|
||||
assert_eq!(decode_uvarint(&encoded), Ok((value, encoded.len())));
|
||||
}
|
||||
for value in [
|
||||
i64::MIN,
|
||||
-9_223_372_036,
|
||||
-2,
|
||||
-1,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
1_000_000_000,
|
||||
3_000_000_000,
|
||||
i64::MAX,
|
||||
] {
|
||||
let mut encoded = Vec::new();
|
||||
encode_varint(value, &mut encoded);
|
||||
assert_eq!(decode_varint(&encoded), Ok((value, encoded.len())));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verified_byte_examples() {
|
||||
let cases: &[(i64, &[u8])] = &[
|
||||
(1, &[0x02]),
|
||||
(3_000_000_000, &[0x80, 0xf8, 0x82, 0xad, 0x16]),
|
||||
(1_000_000_000, &[0x80, 0xa8, 0xd6, 0xb9, 0x07]),
|
||||
(1472, &[0x80, 0x17]),
|
||||
(3, &[0x06]),
|
||||
(184, &[0xf0, 0x02]),
|
||||
];
|
||||
for (value, expected) in cases {
|
||||
let mut encoded = Vec::new();
|
||||
encode_varint(*value, &mut encoded);
|
||||
assert_eq!(&encoded, expected);
|
||||
assert_eq!(decode_varint(expected), Ok((*value, expected.len())));
|
||||
}
|
||||
for tag in [9, 24] {
|
||||
let mut encoded = Vec::new();
|
||||
encode_uvarint(tag, &mut encoded);
|
||||
assert_eq!(encoded, [tag as u8]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_and_overflowing_varints_are_rejected() {
|
||||
// Incomplete input differs from a tenth byte whose bits cannot fit u64,
|
||||
// or an eleventh byte after ten continuation bytes.
|
||||
for (bytes, expected) in [
|
||||
(vec![], ProtoError::TruncatedVarint),
|
||||
(vec![0x80], ProtoError::TruncatedVarint),
|
||||
(vec![0x80; 10], ProtoError::TruncatedVarint),
|
||||
(
|
||||
[vec![0x80; 9], vec![2]].concat(),
|
||||
ProtoError::VarintOverflow,
|
||||
),
|
||||
(
|
||||
[vec![0x80; 10], vec![0]].concat(),
|
||||
ProtoError::VarintOverflow,
|
||||
),
|
||||
] {
|
||||
assert_eq!(decode_uvarint(&bytes), Err(expected), "{bytes:02x?}");
|
||||
assert_eq!(
|
||||
decode_varint(&bytes),
|
||||
decode_uvarint(&bytes).map(|(v, n)| (zigzag_decode(v), n))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn uvarint_round_trips(value: u64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_uvarint(value, &mut encoded);
|
||||
prop_assert!(!encoded.is_empty());
|
||||
prop_assert!(encoded.len() <= 10);
|
||||
prop_assert_eq!(decode_uvarint(&encoded).unwrap(), (value, encoded.len()));
|
||||
prop_assert_eq!(zigzag_encode(zigzag_decode(value)), value);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_varint_round_trips(value: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_varint(value, &mut encoded);
|
||||
prop_assert!(!encoded.is_empty());
|
||||
prop_assert!(encoded.len() <= 10);
|
||||
prop_assert_eq!(decode_varint(&encoded).unwrap(), (value, encoded.len()));
|
||||
prop_assert_eq!(zigzag_decode(zigzag_encode(value)), value);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "irtt-stats"
|
||||
version = "0.5.1"
|
||||
version = "0.5.2"
|
||||
description = "Statistics aggregation for irtt-client events"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
+44
-1
@@ -58,6 +58,41 @@ once per interval instead. Final per-target summaries print in the order
|
||||
targets were supplied on the command line, not the order in which they
|
||||
finish or their labels sort alphabetically.
|
||||
|
||||
## ADDRESS FAMILY
|
||||
|
||||
By default, each target uses the first address returned by the system resolver,
|
||||
whether IPv4 or IPv6. Use `-4` / `--ipv4` to select IPv4 only, or
|
||||
`-6` / `--ipv6` to select IPv6 only. Hostnames are resolved when each session
|
||||
connects; these flags do not perform an extra discovery lookup. An explicit
|
||||
IP literal from the other family is rejected during preparation.
|
||||
|
||||
Use `--dual-stack` to compare IPv4 and IPv6 paths as separate measurements:
|
||||
|
||||
```sh
|
||||
irtt-client -6 host.example
|
||||
irtt-client --dual-stack cov=uk-cov1.irtt.lochnair.net
|
||||
```
|
||||
|
||||
For each hostname, the client discovers which address families the system
|
||||
resolver currently returns. IPv4 results create `cov/v4`, IPv6 results create
|
||||
`cov/v6`, and both create both targets, in that order. Without an alias,
|
||||
`host.example` becomes `host.example/v4` and/or `host.example/v6`. Multiple
|
||||
addresses within one family still produce only one target. Explicit IP literals
|
||||
keep their original label and remain single targets.
|
||||
|
||||
Each target has its own session, statistics, and failures. Its original hostname
|
||||
is retained: every new session resolves it again and selects the first address
|
||||
matching its family. Families are discovered only when a declaration is added,
|
||||
so a newly appearing family does not create another target automatically.
|
||||
Discovery failure or a successful lookup returning no addresses rejects the
|
||||
whole target set. A missing family is normal and simply creates no target for
|
||||
that family. Final labels must be unique, including generated `/v4` and `/v6`
|
||||
labels; choose another alias if one collides with an explicit label.
|
||||
|
||||
`-4`, `-6`, and `--dual-stack` are mutually exclusive. Expanded targets use
|
||||
ordinary multi-target pacing and each sends its own probes, so dual-stack can
|
||||
double the number of probes per interval compared with one target per hostname.
|
||||
|
||||
## DYNAMIC TARGET SETS FROM STANDARD INPUT
|
||||
|
||||
`--targets-stdin` is available only with continuous mode (`--duration 0`).
|
||||
@@ -75,6 +110,13 @@ are retired, unchanged target configurations retain their generation, and a
|
||||
changed address or HMAC setting creates a fresh generation. Under transient
|
||||
live-generation backpressure, only the latest unapplied desired set is kept.
|
||||
|
||||
The same address-family preparation applies to initial and stdin targets.
|
||||
With `--dual-stack`, unchanged labels and endpoints retain their discovered
|
||||
families across records, including HMAC-only changes. Removing a declaration
|
||||
and later adding it again performs fresh discovery. New declarations are
|
||||
prepared asynchronously while existing measurements continue. A preparation
|
||||
error stops the stream gracefully without applying a partial set.
|
||||
|
||||
Commas frame targets in stdin records; escape a literal comma within one
|
||||
target as `\,`. Target parsing inside each element is otherwise the same as
|
||||
for positional arguments, including the other escapes above, `@hmac=KEY`, and
|
||||
@@ -107,7 +149,8 @@ revision to become applicable.
|
||||
|
||||
The maximum stdin record payload is 64 KiB. Stdin-controlled mode bounds each
|
||||
desired set to 128 targets and retains at most 256 live target generations
|
||||
while replacements drain.
|
||||
while replacements drain. The 128-target limit applies after dual-stack
|
||||
expansion: a hostname producing both families consumes two target slots.
|
||||
|
||||
## FINITE VERSUS CONTINUOUS OPERATION
|
||||
|
||||
|
||||
+106
-16
@@ -21,6 +21,21 @@ Target syntax, multiple targets, and pacing are identical to
|
||||
`--sfill`, `--dscp`, `--ttl`, `--loose`) is shared with `irtt-client` as
|
||||
well; consult that manual for their meaning.
|
||||
|
||||
Address-family controls are shared too: `-4` / `--ipv4` uses IPv4 only,
|
||||
`-6` / `--ipv6` uses IPv6 only, and `--dual-stack` gives each available family
|
||||
of a hostname its own measurement row and graph series. These switches are
|
||||
mutually exclusive. Explicit IP literals remain single targets; a literal
|
||||
conflicting with `-4` or `-6` is rejected before the dashboard starts.
|
||||
|
||||
```sh
|
||||
irtt-tui -4 uk-cov1.irtt.lochnair.net
|
||||
irtt-tui --dual-stack cov=uk-cov1.irtt.lochnair.net
|
||||
```
|
||||
|
||||
The second command creates `cov/v4` and/or `cov/v6` according to the system
|
||||
resolver's initial results, letting you compare the two paths directly.
|
||||
See the client manual's address-family section for discovery errors and labels.
|
||||
|
||||
## CONTINUOUS DEFAULT
|
||||
|
||||
Unlike `irtt-client`, **the TUI defaults to continuous mode**
|
||||
@@ -31,32 +46,102 @@ a finite run:
|
||||
irtt-tui <server> --duration 30s
|
||||
```
|
||||
|
||||
Continuous runs reconnect after target-local failures, peer closure, or a
|
||||
session completing because the server restricted its duration. After a
|
||||
1.5-second delay, one coalesced retry reopens completed targets while healthy
|
||||
sessions continue undisturbed. Even if every target fails, the TUI stays alive
|
||||
until you quit. Explicit stops, removed or replaced targets, and no-test
|
||||
completion do not trigger retries. Quitting cancels pending retry work.
|
||||
|
||||
With `--dual-stack`, a failed family reconnects independently while its healthy
|
||||
sibling continues. Reconnect resolves the original hostname again within the
|
||||
target's family. It does not discover additional families or pin the address
|
||||
returned by initial discovery.
|
||||
|
||||
Each reconnect keeps the same logical target row. Statistics reset when a new
|
||||
session generation is accepted, including attempts that fail before opening.
|
||||
Retained graph history and the age of the last actual reply remain; the graph
|
||||
leaves a gap between generations instead of connecting their samples.
|
||||
|
||||
## DASHBOARD
|
||||
|
||||
One dashboard is used for every target count and terminal size. It shows:
|
||||
|
||||
- Run status, elapsed time, and finite duration or continuous mode.
|
||||
- The selected target's label and remote address, plus latest signed
|
||||
client-to-server and server-to-client delays (`-` means unavailable).
|
||||
- Target rows with latest effective RTT, cumulative loss percentage, jitter
|
||||
(standard deviation of round-trip IPDV), and age of the last primary reply.
|
||||
- A graph overlaying all targets, with matching target colors and a visible
|
||||
live/history indicator and window size.
|
||||
|
||||
An active session with no primary replies shows `waiting`. Reply age makes a
|
||||
stale RTT distinguishable from a fresh measurement. Loss follows the shared
|
||||
statistics collector: outstanding sends can temporarily contribute to the
|
||||
cumulative loss estimate. Jitter is `-` until an IPDV pair is available.
|
||||
|
||||
`Tab` / `Shift-Tab` selects a target, bringing its row into view when necessary.
|
||||
Up to six target rows are shown; smaller terminals show fewer. Selecting a target
|
||||
changes the identification, one-way summary and details; the graph continues to
|
||||
compare every target.
|
||||
|
||||
`d` opens a details sheet in the graph's space. It contains the selected target's
|
||||
session and negotiation, packet counters, progress, latest timing sample, full
|
||||
timing statistics, warnings, and recent events from all targets. Scroll it with
|
||||
the arrows or page keys. Long lines and timing columns can be read with horizontal
|
||||
scrolling, including full labels and remote addresses that do not fit in the
|
||||
header. `d` or `Esc` returns to the graph, preserving its metric and viewport.
|
||||
The former large/compact Dashboard screens have been replaced by this sheet;
|
||||
`g` remains an alias for `d`.
|
||||
|
||||
The minimum terminal size is 56 columns by 18 rows. Below that size the UI asks
|
||||
for a resize and continues acquiring measurements. Resize and important status
|
||||
redraws still work while the display is paused.
|
||||
|
||||
## CONTROLS
|
||||
|
||||
| Key | Action |
|
||||
| --- | --- |
|
||||
| `q`, `Ctrl-C` | Quit |
|
||||
| `p` | Toggle display pause (probing continues; only redraws stop) |
|
||||
| `g` | Toggle Graph / Dashboard view |
|
||||
| `m` | Cycle graph metric |
|
||||
| `r` | Clear visible graph history |
|
||||
| `←` / `→` | Pan graph |
|
||||
| `PageUp` / `PageDown` | Page-pan graph |
|
||||
| `Home` / `End` | Jump to oldest / jump to live |
|
||||
| `+` / `=` | Zoom graph in |
|
||||
| `-` | Zoom graph out |
|
||||
| `0` | Reset graph window and zoom |
|
||||
| `p` | Toggle display pause (probing and acquisition continue) |
|
||||
| `Tab` / `Shift-Tab` | Select next / previous target |
|
||||
| `d`, `g` | Toggle target details |
|
||||
| `Esc` | Close target details |
|
||||
| `r` | Clear graph history for all targets; keep latest samples and statistics |
|
||||
| `m` | Cycle graph metric (graph visible) |
|
||||
| `←` / `→` | Pan graph; scroll columns in details |
|
||||
| `↑` / `↓` | Scroll details |
|
||||
| `PageUp` / `PageDown` | Page-pan graph; page-scroll details |
|
||||
| `Home` / `End` | Oldest / live graph; top / bottom of details |
|
||||
| `+` / `=` | Zoom graph in (graph visible) |
|
||||
| `-` | Zoom graph out (graph visible) |
|
||||
| `0` | Reset graph window and zoom, keeping live/history position (graph visible) |
|
||||
|
||||
The six graph metrics are effective RTT, raw RTT, adjusted RTT,
|
||||
client-to-server delay, server-to-client delay, and server processing time.
|
||||
Signed effective/adjusted RTT and one-way values are preserved. Graph windows
|
||||
range from 5 seconds to 24 hours and default to one minute. The footer shows
|
||||
controls appropriate to the currently open graph or details sheet.
|
||||
|
||||
## RETAINED HISTORY
|
||||
|
||||
The TUI keeps its own bounded presentation state on top of the statistics
|
||||
retention described in `irtt-client(1)`:
|
||||
|
||||
- Up to 100,000 graph samples per target.
|
||||
- Up to 500,000 graph samples per target.
|
||||
- Up to 80 recent status/log messages.
|
||||
|
||||
`r` clears the currently visible graph history and returns the viewport to
|
||||
live; it does not change the 100,000-sample cap itself. These are
|
||||
At 500,000 replies per target, retention is approximately 13h53m20s at
|
||||
100ms intervals, 27h46m40s at 200ms, or 5d18h53m20s at 1s. Retained graph
|
||||
samples store both clocks, six graph-ready values, and a session-boundary
|
||||
flag; latest details keep the full typed timing values and sequence number.
|
||||
On 64-bit macOS, an 88-byte retained sample gives about 42 MiB per target
|
||||
at the cap, excluding statistics and temporary rendering buffers. Histories
|
||||
grow on demand and reserve exactly to the cap on their final growth step;
|
||||
they do not preallocate the full history for every target at startup.
|
||||
|
||||
`r` clears graph history for all targets and returns the viewport to
|
||||
live; it does not change the 500,000-sample cap itself. These are
|
||||
presentation bounds on top of the client's own statistics retention (see
|
||||
`irtt-client(1)` MEASUREMENTS AND MEMORY) — a long continuous TUI session's
|
||||
total memory is dominated by whichever of the two is larger for your target
|
||||
@@ -72,9 +157,14 @@ corrupt the display. Size a long finite TUI run from the figures in
|
||||
|
||||
## EXIT BEHAVIOR
|
||||
|
||||
Quitting with `q` or `Ctrl-C` is an interrupted, successful exit. Otherwise,
|
||||
exit status follows the same peer-close and driver-failure rules as
|
||||
`irtt-client(1)`.
|
||||
Quitting with `q` or `Ctrl-C` is an interrupted, successful exit. In continuous
|
||||
mode, peer closure and target failures trigger reconnect rather than ending the
|
||||
run. A managed driver failure or an unexpected reconnect-update failure still
|
||||
ends the TUI with an error.
|
||||
|
||||
Finite runs do not reconnect. They complete after their target sessions end;
|
||||
a peer closure is a normal session ending, while a run whose targets all fail
|
||||
exits with an error.
|
||||
|
||||
## EXAMPLES
|
||||
|
||||
|
||||
Reference in new issue
Block a user