Files
nandi 9aa15f9ae8
check / check (macos-latest) (push) Failing after 26s
check / check (ubuntu-latest) (push) Failing after 27s
check / check (windows-latest) (push) Failing after 30s
feat: add non-blocking NTCP2 sends and Firefox settings sync
- NTCP2: queue outbound data frames and flush them without blocking (send_blocks_nb, send_i2np_nb, flush_nonblocking)
- NTCP2: bound write_all with a deadline and set TCP keepalive and user timeout on peer sockets
- Firefox extension 0.1.3: save settings as a patch, keep unsaved form fields from being overwritten on refresh, add timeouts to router API calls, and honor the interceptTorrents toggle in the content script
2026-11-28 18:39:32 -08:00

197 lines
5.9 KiB
MoonBit

///|
test "ntcp2_net CSPRNG, clock, and TCP loopback" {
let r1 = @ntcp2_net.random_bytes(32)
let r2 = @ntcp2_net.random_bytes(32)
assert_eq(r1.length(), 32)
assert_eq(r2.length(), 32)
assert_true(r1 != r2)
assert_true(@ntcp2_net.unix_time_sec() > 1700000000U)
// Loopback TCP listener & client
let listener = @ntcp2_net.TcpListener::bind("127.0.0.1", 0)
let port = listener.local_port()
assert_true(port > 0)
let client = @ntcp2_net.TcpSocket::connect("127.0.0.1", port)
let server = match listener.accept(timeout_ms=2000) {
Some(s) => s
None => fail("expected accepted TCP socket")
}
client.write_all(b"ping\n")
assert_eq(server.read_line(), Some("ping"))
server.write_all(b"pong\n")
assert_eq(client.read_line(), Some("pong"))
client.close()
server.close()
listener.close()
}
///|
test "extract_ntcp2_peer from signed RouterInfo" {
let ed_seed = @ntcp2_net.random_bytes(32)
let ed_pub = @crypto.ed25519_public_key(ed_seed)
let x_priv = @ntcp2_net.random_bytes(32)
let x_pub = @crypto.x25519_public_key(x_priv)
let iv = @ntcp2_net.random_bytes(16)
let ri_id = @common.RouterIdentity::new_x25519_ed25519(x_pub, ed_pub)
let addr_opts = @common.Mapping::from_entries([
("host", "127.0.0.1"),
("i", @encoding.base64_encode(iv)),
("port", "14567"),
("s", @encoding.base64_encode(x_pub)),
("v", "2"),
])
let ri : @common.RouterInfo = {
identity: ri_id,
published: @ntcp2_net.unix_time_ms(),
addresses: [@common.RouterAddress::new(b'\x03', "NTCP2", addr_opts)],
options: @common.Mapping::from_entries([("caps", "LR"), ("netId", "2")]),
signature: b"",
}
let signed = ri.sign_ed25519(ed_seed)
let peer = @ntcp2_net.extract_ntcp2_peer(signed)
assert_eq(peer.host, "127.0.0.1")
assert_eq(peer.port, 14567)
assert_eq(peer.static_key, x_pub)
assert_eq(peer.iv, iv)
assert_eq(peer.router_hash, ri_id.hash().as_bytes())
}
///|
test "TcpSocket::write_all times out instead of blocking on a full unread socket" {
let listener = @ntcp2_net.TcpListener::bind("127.0.0.1", 0)
let client = @ntcp2_net.TcpSocket::connect("127.0.0.1", listener.local_port())
let server = match listener.accept(timeout_ms=2000) {
Some(s) => s
None => fail("expected accepted TCP socket")
}
let chunk = Bytes::make(65536, b'\xab')
let mut got_timeout = false
for _i = 0; _i < 512; _i = _i + 1 {
let timed_out = try {
client.write_all(chunk, timeout_ms=100)
false
} catch {
Timeout => true
_ => false
}
if timed_out {
got_timeout = true
break
}
}
assert_true(got_timeout)
client.close()
server.close()
listener.close()
}
///|
test "PendingNtcp2Dial/Accept stay non-blocking and Ntcp2Conn::send_blocks_nb transfers frames" {
let alice_ed_seed = @ntcp2_net.random_bytes(32)
let alice_ed_pub = @crypto.ed25519_public_key(alice_ed_seed)
let alice_x_priv = @ntcp2_net.random_bytes(32)
let alice_static = @noise.KeyPair::from_private(alice_x_priv)
let alice_iv = @ntcp2_net.random_bytes(16)
let alice_id = @common.RouterIdentity::new_x25519_ed25519(
alice_static.public_key,
alice_ed_pub,
)
let bob_ed_seed = @ntcp2_net.random_bytes(32)
let bob_ed_pub = @crypto.ed25519_public_key(bob_ed_seed)
let bob_x_priv = @ntcp2_net.random_bytes(32)
let bob_static = @noise.KeyPair::from_private(bob_x_priv)
let bob_iv = @ntcp2_net.random_bytes(16)
let bob_id = @common.RouterIdentity::new_x25519_ed25519(
bob_static.public_key,
bob_ed_pub,
)
let listener = @ntcp2_net.TcpListener::bind("127.0.0.1", 0)
let listen_port = listener.local_port()
let alice_ri : @common.RouterInfo = {
identity: alice_id,
published: @ntcp2_net.unix_time_ms(),
addresses: [
@common.RouterAddress::new(
b'\x03',
"NTCP2",
@common.Mapping::from_entries([
("host", "127.0.0.1"),
("i", @encoding.base64_encode(alice_iv)),
("port", "1"),
("s", @encoding.base64_encode(alice_static.public_key)),
("v", "2"),
]),
),
],
options: @common.Mapping::from_entries([("caps", "LR"), ("netId", "2")]),
signature: b"",
}
let signed_alice_ri = alice_ri.sign_ed25519(alice_ed_seed)
let bob_peer : @ntcp2_net.Ntcp2PeerInfo = {
router_hash: bob_id.hash().as_bytes(),
static_key: bob_static.public_key,
iv: bob_iv,
host: "127.0.0.1",
port: listen_port,
}
let dial = @ntcp2_net.PendingNtcp2Dial::start(
bob_peer, alice_static, signed_alice_ri,
)
let acc_sock = match listener.accept(timeout_ms=2000) {
Some(s) => s
None => fail("expected incoming NTCP2 TCP connection")
}
let acc = @ntcp2_net.PendingNtcp2Accept::start(
acc_sock,
bob_static,
bob_id.hash().as_bytes(),
bob_iv,
)
let mut alice_conn_opt : @ntcp2_net.Ntcp2Conn? = None
let mut bob_conn_opt : @ntcp2_net.Ntcp2Conn? = None
for _step = 0; _step < 100; _step = _step + 1 {
if alice_conn_opt is None {
alice_conn_opt = dial.step()
}
if bob_conn_opt is None {
bob_conn_opt = acc.step()
}
if alice_conn_opt is Some(_) && bob_conn_opt is Some(_) {
break
}
}
let alice_conn = match alice_conn_opt {
Some(c) => c
None => fail("PendingNtcp2Dial did not finish")
}
let bob_conn = match bob_conn_opt {
Some(c) => c
None => fail("PendingNtcp2Accept did not finish")
}
alice_conn.send_blocks_nb([@ntcp2.Ntcp2Block::Padding(b"nb-ntcp2-ok")])
let _ = alice_conn.flush_nonblocking()
let mut got_blocks : Array[@ntcp2.Ntcp2Block]? = None
for _i = 0; _i < 100; _i = _i + 1 {
if bob_conn.poll_readable(10) {
got_blocks = bob_conn.try_recv_blocks()
if got_blocks is Some(_) {
break
}
}
}
match got_blocks {
Some(blks) => assert_eq(blks, [@ntcp2.Ntcp2Block::Padding(b"nb-ntcp2-ok")])
None => fail("expected NTCP2 non-blocking blocks on responder")
}
alice_conn.close()
bob_conn.close()
listener.close()
}