Files
moon-i2p/ntcp2_net/ntcp2_net_test.mbt
T
nandi dd5b132b5a
Rolling Release / Test (MoonBit, Extension, Scripts) (push) Successful in 21s
Rolling Release / Build & Publish Rolling Release (push) Successful in 2m20s
feat: persist I2P torrent list to EDN state file across restarts
- Save active and paused torrents to torrents.edn on add, remove, pause, resume, and metadata fetch, then restore them on router startup.
- Default path is $MOON_I2P_DATA_DIR, then $XDG_DATA_HOME, then ~/.local/share/moon-i2p-browser. Add --torrent-state <file> to override, or --no-torrent-state to disable.
- Write the state file atomically (temp file, fsync, rename). A corrupt or unsupported file logs a warning and does not block startup.
- Verify restored pieces incrementally in the background for large torrents; these show as state "checking" in /api/torrents.
- Expose state_file in /api/status and the startup banner.
- Add standard RFC 4648 Base64 encode/decode helpers, used for the info-b64 field.
- Add the moonedn dependency and pass --torrent-state from the Android service.
2026-11-29 02:46:23 -08:00

215 lines
6.5 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()
}
///|
test "write_file_atomic and file_exists replace longer files cleanly" {
let rand_hex = @encoding.hex_encode(@ntcp2_net.random_bytes(4))
let dir = "/tmp/moon_i2p_atomic_test_\{rand_hex}"
@ntcp2_net.mkdir_p(dir)
let path = "\{dir}/torrents.edn"
assert_true(!@ntcp2_net.file_exists(path))
let long_bytes = b"0123456789abcdef0123456789abcdef-long-payload"
@ntcp2_net.write_file_atomic(path, long_bytes)
assert_true(@ntcp2_net.file_exists(path))
assert_eq(@ntcp2_net.read_file_bytes(path), long_bytes)
let short_bytes = b"{:version 1}\n"
@ntcp2_net.write_file_atomic(path, short_bytes)
assert_eq(@ntcp2_net.read_file_bytes(path), short_bytes)
}