test: cover ratchet NSR expiry and add I2pDht::has_node
- Add Proposal 144 test for high-concurrency NSR survival, same-tag multi-NSR, and zombie ES expiry - Add public I2pDht::has_node and assert node presence in the DHT round-trip test - Reformat DHT announce cooldown call and query match arms in torrent_engine
This commit is contained in:
1 parent
5d97ad8b28
commit
d95d4962f4
5 files changed
+188
-10
No files matched your search
@@ -232,7 +232,10 @@ test "I2P DHT Secure Node ID, 54B compact nodes, KRPC round-trips, and XOR routi
|
||||
assert_true(!@bittorrent.verify_i2p_dht_node_id(dht_a.local_id, dh_a, 6889U))
|
||||
|
||||
let id_b = @bittorrent.make_i2p_dht_node_id(dh_b, 6881U, b"rand14bytes_bb")
|
||||
assert_true(!dht_a.has_node(dh_b, 6881U))
|
||||
assert_true(dht_a.upsert_node(id_b, dh_b, 6881U, 1000UL))
|
||||
assert_true(dht_a.has_node(dh_b, 6881U))
|
||||
assert_true(!dht_a.has_node(dh_b, 6882U))
|
||||
assert_eq(dht_a.nodes.length(), 1)
|
||||
// Tampered Node ID must fail Secure Node ID check
|
||||
assert_true(!dht_a.upsert_node(Bytes::make(20, b'\x77'), dh_b, 6881U, 1001UL))
|
||||
|
||||
@@ -148,6 +148,7 @@ pub struct I2pDht {
|
||||
pub fn I2pDht::alloc_tx_id(Self) -> Bytes
|
||||
pub fn I2pDht::closest_nodes(Self, Bytes, limit? : Int32) -> Array[DhtNode]
|
||||
pub fn I2pDht::get_peers_local(Self, Bytes, UInt64) -> Array[@common.Hash]
|
||||
pub fn I2pDht::has_node(Self, @common.Hash, UInt32) -> Bool
|
||||
pub fn I2pDht::make_token(Self, @common.Hash) -> Bytes
|
||||
pub fn I2pDht::new(@common.Hash, Bytes, Bytes, query_port? : UInt32, reply_port? : UInt32) -> Self raise BtError
|
||||
pub fn I2pDht::prune_expired_txs(Self, UInt64) -> Unit
|
||||
|
||||
@@ -122,6 +122,7 @@ pub struct RatchetSessionManager {
|
||||
sessions : Array[RatchetPeerSession]
|
||||
outbound_pending : Array[(Bytes, PendingNewSession)]
|
||||
inbound_pending : Array[PendingNewSession]
|
||||
// private fields
|
||||
}
|
||||
pub fn RatchetSessionManager::create_next_key_block(Self, Bytes, () -> Bytes) -> RatchetBlock? raise RatchetError
|
||||
pub fn RatchetSessionManager::decrypt_incoming(Self, Bytes, now_sec? : UInt32) -> (Bytes?, Array[RatchetBlock]) raise RatchetError
|
||||
|
||||
@@ -491,3 +491,177 @@ test "Proposal 144 multi-NSR replies, mark_send_stale preservation, and unconfir
|
||||
assert_eq(f_exp, Some(alice_s.public_key))
|
||||
assert_eq(dec_exp.length(), 2)
|
||||
}
|
||||
|
||||
///|
|
||||
test "Proposal 144 high-concurrency NSR survival, same-tag multi-NSR, and zombie ES expiry" {
|
||||
let alice_s = @noise.KeyPair::from_private(@crypto.sha256(b"alice-scale-s"))
|
||||
let peer0_s = @noise.KeyPair::from_private(@crypto.sha256(b"peer0-scale-s"))
|
||||
let rand_a = det_rand_gen(b'\x41')
|
||||
let rand_b = det_rand_gen(b'\x81')
|
||||
let alice_mgr = @ratchet.RatchetSessionManager::new(alice_s)
|
||||
|
||||
// 1. High-concurrency outbound_pending survival: 40 peers (240 tags > old 128 cap)
|
||||
let c0 = @ratchet.garlic_clove_block(
|
||||
Local,
|
||||
101U,
|
||||
1700000900U,
|
||||
Data(data=b"p0-ns"),
|
||||
)
|
||||
let w0 = alice_mgr.encrypt_to_peer(
|
||||
peer0_s.public_key,
|
||||
[c0],
|
||||
1700000100U,
|
||||
rand_a,
|
||||
)
|
||||
let (_, p0_opt) = @ratchet.decrypt_new_session(peer0_s, w0)
|
||||
let p0 = match p0_opt {
|
||||
Some(p) => p
|
||||
None => fail("expected bound PendingNewSession for peer0")
|
||||
}
|
||||
for i = 1; i <= 40; i = i + 1 {
|
||||
let ps = @noise.KeyPair::from_private(
|
||||
@crypto.sha256(@encoding.string_to_utf8_bytes("flood-peer-\{i}")),
|
||||
)
|
||||
let ci = @ratchet.garlic_clove_block(
|
||||
Local,
|
||||
(200 + i).reinterpret_as_uint(),
|
||||
1700000900U,
|
||||
Data(data=b"flood"),
|
||||
)
|
||||
let _ = alice_mgr.encrypt_to_peer(ps.public_key, [ci], 1700000101U, rand_a)
|
||||
}
|
||||
let c0_nsr = @ratchet.garlic_clove_block(
|
||||
Local,
|
||||
102U,
|
||||
1700000900U,
|
||||
Data(data=b"p0-nsr"),
|
||||
)
|
||||
let (w0_nsr, _) = @ratchet.encrypt_new_session_reply(
|
||||
p0,
|
||||
@ratchet.generate_elligator_keypair(rand_b),
|
||||
[c0_nsr],
|
||||
)
|
||||
let (from0, dec0) = alice_mgr.decrypt_incoming(w0_nsr, now_sec=1700000105U)
|
||||
assert_eq(from0, Some(peer0_s.public_key))
|
||||
assert_eq(dec0, [c0_nsr])
|
||||
|
||||
// 2. Same-tag multi-NSR tolerance: two distinct NSRs using the exact same 8B nsr_tag
|
||||
let bob_s = @noise.KeyPair::from_private(@crypto.sha256(b"bob-sametag-s"))
|
||||
let a_same = @ratchet.RatchetSessionManager::new(alice_s)
|
||||
let c_ns = @ratchet.garlic_clove_block(
|
||||
Local,
|
||||
300U,
|
||||
1700000900U,
|
||||
Data(data=b"same-tag-ns"),
|
||||
)
|
||||
let ns_wire = a_same.encrypt_to_peer(
|
||||
bob_s.public_key,
|
||||
[c_ns],
|
||||
1700000100U,
|
||||
rand_a,
|
||||
)
|
||||
let (_, p_copy1_opt) = @ratchet.decrypt_new_session(bob_s, ns_wire)
|
||||
let (_, p_copy2_opt) = @ratchet.decrypt_new_session(bob_s, ns_wire)
|
||||
let p_copy1 = match p_copy1_opt {
|
||||
Some(p) => p
|
||||
None => fail("expected p_copy1")
|
||||
}
|
||||
let p_copy2 = match p_copy2_opt {
|
||||
Some(p) => p
|
||||
None => fail("expected p_copy2")
|
||||
}
|
||||
let c_nsr_a = @ratchet.garlic_clove_block(
|
||||
Local,
|
||||
301U,
|
||||
1700000900U,
|
||||
Data(data=b"same-tag-nsr-a"),
|
||||
)
|
||||
let c_nsr_b = @ratchet.garlic_clove_block(
|
||||
Local,
|
||||
302U,
|
||||
1700000900U,
|
||||
Data(data=b"same-tag-nsr-b"),
|
||||
)
|
||||
let (nsr_a, bob_sess_a) = @ratchet.encrypt_new_session_reply(
|
||||
p_copy1,
|
||||
@ratchet.generate_elligator_keypair(rand_b),
|
||||
[c_nsr_a],
|
||||
)
|
||||
let (nsr_b, _bob_sess_b) = @ratchet.encrypt_new_session_reply(
|
||||
p_copy2,
|
||||
@ratchet.generate_elligator_keypair(rand_b),
|
||||
[c_nsr_b],
|
||||
)
|
||||
assert_eq(nsr_a[:8].to_owned(), nsr_b[:8].to_owned())
|
||||
let (fa, da) = a_same.decrypt_incoming(nsr_a, now_sec=1700000101U)
|
||||
let (fb, db) = a_same.decrypt_incoming(nsr_b, now_sec=1700000102U)
|
||||
assert_eq(fa, Some(bob_s.public_key))
|
||||
assert_eq(fb, Some(bob_s.public_key))
|
||||
assert_eq(da, [c_nsr_a])
|
||||
assert_eq(db, [c_nsr_b])
|
||||
let c_es = @ratchet.garlic_clove_block(
|
||||
Local,
|
||||
303U,
|
||||
1700000900U,
|
||||
Data(data=b"es-after-sametag"),
|
||||
)
|
||||
let es_wire = a_same.encrypt_to_peer(
|
||||
bob_s.public_key,
|
||||
[c_es],
|
||||
1700000103U,
|
||||
rand_a,
|
||||
)
|
||||
assert_true(es_wire.length() < 96)
|
||||
let _ = c_es
|
||||
let _ = bob_sess_a
|
||||
|
||||
// 3a. Zombie ES expiry: unconfirmed NSR -> unanswered ES sends expire >45s after last_recv_sec
|
||||
let a_z1 = @ratchet.RatchetSessionManager::new(alice_s)
|
||||
let b_z1 = @ratchet.RatchetSessionManager::new(bob_s)
|
||||
let wz1 = a_z1.encrypt_to_peer(bob_s.public_key, [c_ns], 100U, rand_a)
|
||||
let _ = b_z1.decrypt_incoming(wz1, now_sec=100U)
|
||||
let wz1_nsr = b_z1.encrypt_to_peer(
|
||||
alice_s.public_key,
|
||||
[c_nsr_a],
|
||||
100U,
|
||||
rand_b,
|
||||
)
|
||||
let _ = a_z1.decrypt_incoming(wz1_nsr, now_sec=100U)
|
||||
let _ = a_z1.encrypt_to_peer(bob_s.public_key, [c_es], 110U, rand_a)
|
||||
let _ = a_z1.encrypt_to_peer(bob_s.public_key, [c_es], 130U, rand_a)
|
||||
assert_true(a_z1.has_active_session(bob_s.public_key, now_sec=140U))
|
||||
assert_true(!a_z1.has_active_session(bob_s.public_key, now_sec=146U))
|
||||
let wz1_re = a_z1.encrypt_to_peer(bob_s.public_key, [c_es], 146U, rand_a)
|
||||
assert_true(wz1_re.length() >= 96)
|
||||
let b_z1_fresh = @ratchet.RatchetSessionManager::new(bob_s)
|
||||
let (fz1, dz1) = b_z1_fresh.decrypt_incoming(wz1_re, now_sec=146U)
|
||||
assert_eq(fz1, Some(alice_s.public_key))
|
||||
assert_eq(dz1.length(), 2)
|
||||
|
||||
// 3b. Zombie ES expiry: confirmed ES -> unanswered ES sends expire >180s after last_recv_sec
|
||||
let a_z2 = @ratchet.RatchetSessionManager::new(alice_s)
|
||||
let b_z2 = @ratchet.RatchetSessionManager::new(bob_s)
|
||||
let wz2 = a_z2.encrypt_to_peer(bob_s.public_key, [c_ns], 100U, rand_a)
|
||||
let _ = b_z2.decrypt_incoming(wz2, now_sec=100U)
|
||||
let wz2_nsr = b_z2.encrypt_to_peer(
|
||||
alice_s.public_key,
|
||||
[c_nsr_a],
|
||||
101U,
|
||||
rand_b,
|
||||
)
|
||||
let _ = a_z2.decrypt_incoming(wz2_nsr, now_sec=101U)
|
||||
let wz2_es_ab = a_z2.encrypt_to_peer(bob_s.public_key, [c_es], 102U, rand_a)
|
||||
let _ = b_z2.decrypt_incoming(wz2_es_ab, now_sec=103U)
|
||||
let wz2_es_ba = b_z2.encrypt_to_peer(alice_s.public_key, [c_es], 104U, rand_b)
|
||||
let _ = a_z2.decrypt_incoming(wz2_es_ba, now_sec=105U)
|
||||
let _ = a_z2.encrypt_to_peer(bob_s.public_key, [c_es], 200U, rand_a)
|
||||
let _ = a_z2.encrypt_to_peer(bob_s.public_key, [c_es], 260U, rand_a)
|
||||
assert_true(a_z2.has_active_session(bob_s.public_key, now_sec=280U))
|
||||
assert_true(!a_z2.has_active_session(bob_s.public_key, now_sec=286U))
|
||||
let wz2_re = a_z2.encrypt_to_peer(bob_s.public_key, [c_es], 286U, rand_a)
|
||||
assert_true(wz2_re.length() >= 96)
|
||||
let b_z2_fresh = @ratchet.RatchetSessionManager::new(bob_s)
|
||||
let (fz2, dz2) = b_z2_fresh.decrypt_incoming(wz2_re, now_sec=286U)
|
||||
assert_eq(fz2, Some(alice_s.public_key))
|
||||
assert_eq(dz2.length(), 2)
|
||||
}
|
||||
@@ -1150,13 +1150,7 @@ fn bt_dht_can_announce(
|
||||
now_ms : UInt64,
|
||||
cooldown_ms : UInt64,
|
||||
) -> Bool {
|
||||
bt_dht_cooldown_allow(
|
||||
eng.dht_announce_cd,
|
||||
node_hash,
|
||||
ih,
|
||||
now_ms,
|
||||
cooldown_ms,
|
||||
)
|
||||
bt_dht_cooldown_allow(eng.dht_announce_cd, node_hash, ih, now_ms, cooldown_ms)
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -1409,7 +1403,11 @@ fn Router::poll_dht_datagrams(
|
||||
now_ms,
|
||||
)
|
||||
let query_cd_ms = if self.public_mode { 60000UL } else { 1500UL }
|
||||
let announce_cd_ms = if self.public_mode { 300000UL } else { 5000UL }
|
||||
let announce_cd_ms = if self.public_mode {
|
||||
300000UL
|
||||
} else {
|
||||
5000UL
|
||||
}
|
||||
if nodes.length() > 0 {
|
||||
let decoded_nodes = @bittorrent.decode_compact_nodes(
|
||||
nodes,
|
||||
@@ -1430,7 +1428,7 @@ fn Router::poll_dht_datagrams(
|
||||
match tx.info_hash {
|
||||
Some(ih) =>
|
||||
match bt_find_torrent_by_ih(eng, ih) {
|
||||
Some(t)
|
||||
Some(t) =>
|
||||
if !t.paused &&
|
||||
t.known_peers.length() < 8 &&
|
||||
eng.dht.pending_txs.length() < 24 &&
|
||||
@@ -1440,7 +1438,7 @@ fn Router::poll_dht_datagrams(
|
||||
ih,
|
||||
now_ms,
|
||||
query_cd_ms,
|
||||
) =>
|
||||
) {
|
||||
if self.send_dht_query(
|
||||
eng,
|
||||
sess,
|
||||
@@ -1452,6 +1450,7 @@ fn Router::poll_dht_datagrams(
|
||||
) {
|
||||
fanout_sent += 1
|
||||
}
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
None => ()
|
||||
|
||||
Reference in new issue
Block a user