saorsa-core 0.24.4

Saorsa - Core P2P networking library with DHT, QUIC transport, and post-quantum cryptography
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2024 Saorsa Labs Limited
//
// This software is dual-licensed under:
// - GNU Affero General Public License v3.0 or later (AGPL-3.0-or-later)
// - Commercial License
//
// For AGPL-3.0 license, see LICENSE-AGPL-3.0
// For commercial licensing, contact: david@saorsalabs.com
//
// Unless required by applicable law or agreed to in writing, software
// distributed under these licenses is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

//! Integration tests for the trust event flow through P2PNode → AdaptiveDHT → TrustEngine.
//!
//! These tests verify that trust signals reported via the public `P2PNode` API
//! flow through the full component stack and produce the expected score changes.

#![allow(clippy::unwrap_used, clippy::expect_used)]

use saorsa_core::{AdaptiveDhtConfig, NodeConfig, P2PNode, PeerId, TrustEvent};

/// Default neutral trust score for unknown peers.
const NEUTRAL_TRUST: f64 = 0.5;

/// Default trust threshold below which peers become eligible for swap-out.
const SWAP_THRESHOLD: f64 = 0.35;

/// Helper: create a local-only test node config (loopback, ephemeral port, IPv4 only).
fn test_node_config() -> NodeConfig {
    NodeConfig::builder()
        .local(true)
        .port(0)
        .ipv6(false)
        .build()
        .expect("test config should be valid")
}

// ---------------------------------------------------------------------------
// Basic trust scoring via P2PNode
// ---------------------------------------------------------------------------

/// Unknown peers start at neutral trust (0.5).
#[tokio::test]
async fn unknown_peer_starts_at_neutral() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let peer = PeerId::random();

    let score = node.peer_trust(&peer);
    assert!(
        (score - NEUTRAL_TRUST).abs() < f64::EPSILON,
        "Expected neutral trust {NEUTRAL_TRUST}, got {score}"
    );
}

/// Reporting successful events raises a peer's trust above neutral.
#[tokio::test]
async fn successes_raise_trust_above_neutral() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let peer = PeerId::random();

    for _ in 0..20 {
        node.report_trust_event(&peer, TrustEvent::ApplicationSuccess(1.0))
            .await;
    }

    let score = node.peer_trust(&peer);
    assert!(
        score > NEUTRAL_TRUST,
        "After 20 successes, trust {score} should exceed neutral {NEUTRAL_TRUST}"
    );
}

/// Reporting failure events lowers a peer's trust below neutral.
#[tokio::test]
async fn failures_lower_trust_below_neutral() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let peer = PeerId::random();

    for _ in 0..20 {
        node.report_trust_event(&peer, TrustEvent::ConnectionFailed)
            .await;
    }

    let score = node.peer_trust(&peer);
    assert!(
        score < NEUTRAL_TRUST,
        "After 20 failures, trust {score} should be below neutral {NEUTRAL_TRUST}"
    );
}

// ---------------------------------------------------------------------------
// Trust event variants
// ---------------------------------------------------------------------------

/// All TrustEvent variants with valid weights affect the score (no panics, no no-ops for valid inputs).
#[tokio::test]
async fn all_trust_event_variants_affect_score() {
    let node = P2PNode::new(test_node_config()).await.unwrap();

    let positive_events = [TrustEvent::ApplicationSuccess(1.0)];
    let negative_events = [TrustEvent::ConnectionFailed, TrustEvent::ConnectionTimeout];

    for event in positive_events {
        let peer = PeerId::random();
        node.report_trust_event(&peer, event).await;
        let score = node.peer_trust(&peer);
        assert!(
            score > NEUTRAL_TRUST,
            "Positive event {event:?} should raise score above neutral, got {score}"
        );
    }

    for event in negative_events {
        let peer = PeerId::random();
        node.report_trust_event(&peer, event).await;
        let score = node.peer_trust(&peer);
        assert!(
            score < NEUTRAL_TRUST,
            "Negative event {event:?} should lower score below neutral, got {score}"
        );
    }
}

// ---------------------------------------------------------------------------
// Trust scoring and swap threshold
// ---------------------------------------------------------------------------

/// Sustained failures push a peer below the swap threshold.
#[tokio::test]
async fn sustained_failures_drop_below_swap_threshold() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let bad_peer = PeerId::random();

    for _ in 0..50 {
        node.report_trust_event(&bad_peer, TrustEvent::ConnectionFailed)
            .await;
    }

    let score = node.peer_trust(&bad_peer);
    assert!(
        score < SWAP_THRESHOLD,
        "After 50 failures, trust {score} should be below swap threshold {SWAP_THRESHOLD}"
    );
}

/// A single failure from neutral does NOT cross the swap threshold.
#[tokio::test]
async fn single_failure_does_not_cross_swap_threshold() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let peer = PeerId::random();

    node.report_trust_event(&peer, TrustEvent::ConnectionFailed)
        .await;

    let score = node.peer_trust(&peer);
    assert!(
        score >= SWAP_THRESHOLD,
        "One failure from neutral should not cross threshold; score={score}, threshold={SWAP_THRESHOLD}"
    );
}

/// A well-trusted peer is resilient to a few failures.
#[tokio::test]
async fn trusted_peer_resilient_to_occasional_failures() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let peer = PeerId::random();

    // Build up trust
    for _ in 0..50 {
        node.report_trust_event(&peer, TrustEvent::ApplicationSuccess(1.0))
            .await;
    }
    let high_score = node.peer_trust(&peer);

    // A few failures
    for _ in 0..3 {
        node.report_trust_event(&peer, TrustEvent::ConnectionFailed)
            .await;
    }

    let score_after = node.peer_trust(&peer);
    assert!(
        score_after >= SWAP_THRESHOLD,
        "3 failures after 50 successes should not block; score={score_after}"
    );
    assert!(
        score_after < high_score,
        "Score should have decreased from {high_score} to {score_after}"
    );
}

// ---------------------------------------------------------------------------
// Trust engine access & peer removal
// ---------------------------------------------------------------------------

/// The trust engine Arc is shared: scores reported via P2PNode are visible
/// through the engine reference.
#[tokio::test]
async fn trust_engine_arc_shares_state_with_node() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let peer = PeerId::random();

    // Report via P2PNode
    node.report_trust_event(&peer, TrustEvent::ApplicationSuccess(1.0))
        .await;

    // Read via TrustEngine Arc
    let engine = node.trust_engine();
    let score = engine.score(&peer);
    assert!(
        score > NEUTRAL_TRUST,
        "Engine should reflect the event reported through P2PNode; got {score}"
    );
}

/// Removing a peer via the trust engine resets their score to neutral.
#[tokio::test]
async fn removing_peer_resets_to_neutral() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let peer = PeerId::random();

    // Tank the score
    for _ in 0..30 {
        node.report_trust_event(&peer, TrustEvent::ConnectionFailed)
            .await;
    }
    assert!(node.peer_trust(&peer) < NEUTRAL_TRUST);

    // Remove via engine
    node.trust_engine().remove_node(&peer);

    let score = node.peer_trust(&peer);
    assert!(
        (score - NEUTRAL_TRUST).abs() < f64::EPSILON,
        "Removed peer should return to neutral; got {score}"
    );
}

// ---------------------------------------------------------------------------
// Multiple peers tracked independently
// ---------------------------------------------------------------------------

/// Trust for different peers is tracked independently.
#[tokio::test]
async fn peers_tracked_independently() {
    let node = P2PNode::new(test_node_config()).await.unwrap();

    let good_peer = PeerId::random();
    let bad_peer = PeerId::random();
    let neutral_peer = PeerId::random();

    for _ in 0..20 {
        node.report_trust_event(&good_peer, TrustEvent::ApplicationSuccess(1.0))
            .await;
        node.report_trust_event(&bad_peer, TrustEvent::ConnectionFailed)
            .await;
    }

    let good_score = node.peer_trust(&good_peer);
    let bad_score = node.peer_trust(&bad_peer);
    let neutral_score = node.peer_trust(&neutral_peer);

    assert!(good_score > NEUTRAL_TRUST, "Good peer score: {good_score}");
    assert!(bad_score < NEUTRAL_TRUST, "Bad peer score: {bad_score}");
    assert!(
        (neutral_score - NEUTRAL_TRUST).abs() < f64::EPSILON,
        "Untouched peer should be neutral: {neutral_score}"
    );
}

// ---------------------------------------------------------------------------
// Trust scores bounded
// ---------------------------------------------------------------------------

/// Trust scores remain within [0.0, 1.0] regardless of extreme inputs.
#[tokio::test]
async fn trust_scores_bounded() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let peer = PeerId::random();

    // Extreme successes
    for _ in 0..500 {
        node.report_trust_event(&peer, TrustEvent::ApplicationSuccess(1.0))
            .await;
    }
    let high = node.peer_trust(&peer);
    assert!((0.0..=1.0).contains(&high), "Score out of bounds: {high}");

    // Extreme failures
    for _ in 0..1000 {
        node.report_trust_event(&peer, TrustEvent::ConnectionFailed)
            .await;
    }
    let low = node.peer_trust(&peer);
    assert!((0.0..=1.0).contains(&low), "Score out of bounds: {low}");
}

// ---------------------------------------------------------------------------
// AdaptiveDHT config validation flows through P2PNode
// ---------------------------------------------------------------------------

/// Custom swap threshold in AdaptiveDhtConfig is respected by the node.
#[tokio::test]
async fn custom_swap_threshold_respected() {
    let custom_threshold = 0.3;
    let config = NodeConfig::builder()
        .local(true)
        .port(0)
        .ipv6(false)
        .adaptive_dht_config(AdaptiveDhtConfig {
            swap_threshold: custom_threshold,
        })
        .build()
        .unwrap();

    let node = P2PNode::new(config).await.unwrap();
    let threshold = node.adaptive_dht().config().swap_threshold;

    assert!(
        (threshold - custom_threshold).abs() < f64::EPSILON,
        "Expected threshold {custom_threshold}, got {threshold}"
    );
}

/// Trust enforcement disabled (threshold 0.0) means no peers are ever swap-eligible.
#[tokio::test]
async fn trust_enforcement_disabled_no_swap_eligibility() {
    let config = NodeConfig::builder()
        .local(true)
        .port(0)
        .ipv6(false)
        .trust_enforcement(false)
        .build()
        .unwrap();

    let node = P2PNode::new(config).await.unwrap();
    let peer = PeerId::random();

    // Max failures
    for _ in 0..100 {
        node.report_trust_event(&peer, TrustEvent::ConnectionFailed)
            .await;
    }

    let score = node.peer_trust(&peer);
    let threshold = node.adaptive_dht().config().swap_threshold;

    // threshold is 0.0, so score (which is ≥0.0) is always >= threshold
    assert!(
        score >= threshold,
        "With enforcement disabled (threshold={threshold}), score {score} should be >= threshold"
    );
}

// ---------------------------------------------------------------------------
// EMA blending behavior
// ---------------------------------------------------------------------------

/// A success after a failure blends the score upward (EMA behavior).
#[tokio::test]
async fn ema_blends_observations() {
    let node = P2PNode::new(test_node_config()).await.unwrap();
    let peer = PeerId::random();

    // One failure
    node.report_trust_event(&peer, TrustEvent::ConnectionFailed)
        .await;
    let after_fail = node.peer_trust(&peer);

    // One success
    node.report_trust_event(&peer, TrustEvent::ApplicationSuccess(1.0))
        .await;
    let after_recovery = node.peer_trust(&peer);

    assert!(
        after_recovery > after_fail,
        "Success after failure should raise score: {after_fail} -> {after_recovery}"
    );
}

/// The swap threshold from AdaptiveDhtConfig matches the default constant.
#[tokio::test]
async fn default_config_matches_expected_threshold() {
    let config = AdaptiveDhtConfig::default();
    assert!(
        (config.swap_threshold - SWAP_THRESHOLD).abs() < f64::EPSILON,
        "Default threshold {} != expected {}",
        config.swap_threshold,
        SWAP_THRESHOLD
    );
}

/// Invalid swap threshold values are rejected during node creation.
#[tokio::test]
async fn invalid_swap_threshold_rejected() {
    for bad_threshold in [f64::NAN, f64::NEG_INFINITY, -0.1, 1.1, f64::INFINITY] {
        let config = NodeConfig::builder()
            .local(true)
            .port(0)
            .ipv6(false)
            .adaptive_dht_config(AdaptiveDhtConfig {
                swap_threshold: bad_threshold,
            })
            .build();

        // Validation may happen at build() or at P2PNode::new() — either is acceptable
        match config {
            Err(_) => {}
            Ok(config) => {
                let result = P2PNode::new(config).await;
                assert!(
                    result.is_err(),
                    "Swap threshold {bad_threshold} should be rejected"
                );
            }
        }
    }
}