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
+202
-24
No files matched your search
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user