code wiki / (root) / nx_gap_junction_test.nx

nx_gap_junction_test.nx source

↩ module page · 54 lines · 1876 B

1// nx_gap_junction_test.nx -- smoke for nx_gap_junction. 2 3import "nx_syscalls.nx" 4import "nx_gap_junction.nx" 5 6func main() -> i64 { 7 let now: nx_size = 1000 8 9 if nx_gj_state_is_valid(NX_GJ_STATE_OPEN) != 1 { return 1 } 10 if nx_gj_state_is_valid(-1) != 0 { return 2 } 11 if nx_gj_v_is_valid(NX_GJ_V_OK) != 1 { return 3 } 12 if nx_gj_v_is_valid(4) != 0 { return 4 } 13 14 let j: *NxGapJunction = nx_gj_new(10, 20) 15 if j.peer_a != 10 { return 5 } 16 if j.peer_b != 20 { return 6 } 17 if j.state != NX_GJ_STATE_PENDING { return 7 } 18 19 // Cannot send before established 20 if nx_gj_send(j, 100, 0xCAFE) != NX_GJ_V_ERR_INVALID { return 8 } 21 22 // Establish with zero key rejected 23 if nx_gj_establish(j, 0, now) != NX_GJ_V_ERR_KEY_MISMATCH { return 9 } 24 25 // Establish properly 26 if nx_gj_establish(j, 0xCAFE, now) != NX_GJ_V_OK { return 10 } 27 if j.state != NX_GJ_STATE_OPEN { return 11 } 28 29 // Send + receive 30 if nx_gj_send(j, 1000, 0xCAFE) != NX_GJ_V_OK { return 12 } 31 if j.bytes_sent != 1000 { return 13 } 32 if nx_gj_receive(j, 2000, 0xCAFE) != NX_GJ_V_OK { return 14 } 33 if j.bytes_received != 2000 { return 15 } 34 35 // Wrong key rejected 36 if nx_gj_send(j, 100, 0xBAD) != NX_GJ_V_ERR_KEY_MISMATCH { return 16 } 37 38 // Cannot re-establish 39 if nx_gj_establish(j, 0xDEAD, now) != NX_GJ_V_ERR_INVALID { return 17 } 40 41 // Close 42 if nx_gj_close(j) != NX_GJ_V_OK { return 18 } 43 if j.state != NX_GJ_STATE_CLOSED { return 19 } 44 // Cannot send after close 45 if nx_gj_send(j, 100, 0xCAFE) != NX_GJ_V_ERR_INVALID { return 20 } 46 47 // Null 48 let null_j: *NxGapJunction = (0 as i64) as *NxGapJunction 49 if nx_gj_establish(null_j, 0xCAFE, now) != NX_GJ_V_ERR_NULL { return 21 } 50 if nx_gj_send(null_j, 100, 0xCAFE) != NX_GJ_V_ERR_NULL { return 22 } 51 if nx_gj_close(null_j) != NX_GJ_V_ERR_NULL { return 23 } 52 53 return 0 54}