code wiki / (root) / nx_methyl_test.nx

nx_methyl_test.nx source

↩ module page · 57 lines · 2467 B

1// nx_methyl_test.nx -- smoke for nx_methyl. 2 3import "nx_syscalls.nx" 4import "nx_methyl.nx" 5 6func main() -> i64 { 7 let sig: *u8 = (sys_mmap(96)) as *u8 8 sig[0] = 42 as u8 9 10 // 1: legit substrate-self mark 11 // originator=100, chromatin=0xcafef00d, ts=1000, sig, sig_len=96 12 let m1: *NxMethylMark = nx_methyl_new(100, 13 0xcafef00d, 1000, sig, 96) 14 if m1.originator_id != 100 { return 1 } 15 if m1.is_decoy_invalid != 0 { return 2 } 16 if nx_methyl_is_self(m1, 2000, 5000, 100) != 1 { return 3 } 17 if nx_methyl_verdict(m1, 2000, 5000, 100) != NX_METHYL_OK { return 4 } 18 19 // 2: decoy-invalid mark 20 // fake_orig=100, fake_chromatin=0xdeadbeef, ts=1000 21 let m2: *NxMethylMark = nx_methyl_new_decoy(100, 22 0xdeadbeef, 1000) 23 if m2.is_decoy_invalid != 1 { return 5 } 24 if nx_methyl_is_self(m2, 2000, 5000, 100) != 0 { return 6 } 25 if nx_methyl_verdict(m2, 2000, 5000, 100) != NX_METHYL_ERR_DECOY { return 7 } 26 if nx_methyl_is_decoy(m2) != 1 { return 8 } 27 28 // 3: wrong originator rejected 29 if nx_methyl_is_self(m1, 2000, 5000, 999) != 0 { return 9 } 30 if nx_methyl_verdict(m1, 2000, 5000, 999) != NX_METHYL_ERR_BAD_ORIG { return 10 } 31 32 // 4: stale mark rejected (ts is too old) 33 // ts=100, now=10000, max_age=5000 -> stale 34 let m_old: *NxMethylMark = nx_methyl_new(100, 0xabc, 100, sig, 96) 35 if nx_methyl_is_self(m_old, 10000, 5000, 100) != 0 { return 11 } 36 if nx_methyl_verdict(m_old, 10000, 5000, 100) != NX_METHYL_ERR_STALE { return 12 } 37 38 // 5: future-dated mark rejected 39 // ts=9000, now=1000 -> future-dated, refused 40 let m_future: *NxMethylMark = nx_methyl_new(100, 0xabc, 9000, sig, 96) 41 if nx_methyl_is_self(m_future, 1000, 5000, 100) != 0 { return 13 } 42 if nx_methyl_verdict(m_future, 1000, 5000, 100) != NX_METHYL_ERR_STALE { return 14 } 43 44 // 6: null sig rejected 45 let null_sig: *u8 = (0 as i64) as *u8 46 let m_nosig: *NxMethylMark = nx_methyl_new(100, 0xabc, 1000, null_sig, 0) 47 if nx_methyl_is_self(m_nosig, 2000, 5000, 100) != 0 { return 15 } 48 if nx_methyl_verdict(m_nosig, 2000, 5000, 100) != NX_METHYL_ERR_NO_MARK { return 16 } 49 50 // 7: NULL mark itself 51 let null_mark: *NxMethylMark = (0 as i64) as *NxMethylMark 52 if nx_methyl_is_self(null_mark, 2000, 5000, 100) != 0 { return 17 } 53 if nx_methyl_verdict(null_mark, 2000, 5000, 100) != NX_METHYL_ERR_NO_MARK { return 18 } 54 if nx_methyl_is_decoy(null_mark) != 0 { return 19 } 55 56 return 0 57}