code wiki / (root) / nx_rights_ledger_gate.nx

nx_rights_ledger_gate.nx source

↩ module page · 67 lines · 3190 B

1// nx_rights_ledger_gate.nx -- liar-kill gate for nx_rights_ledger. 2// 3// Proves the default-DENY posture BY CONSTRUCTION. The neg-controls are the 4// point: a likeness use is refused unless an exact, current grant exists. 5// never-granted -> DENY wrong-talent -> DENY wrong-use -> DENY 6// expired -> DENY revoked -> DENY only a live grant -> ALLOW 7// 8// Returns 0 iff every check holds; a nonzero code pinpoints the first failure. 9 10import "nx_syscalls.nx" 11import "nx_rights_ledger.nx" 12 13func main() -> i64 { 14 // ---- enum sanity ---- 15 if NX_RL_N_STATES != 4 { return 1 } 16 if NX_USE_N_SCOPES != 4 { return 2 } 17 if nx_use_is_valid(NX_USE_MERCH) != 1 { return 3 } 18 if nx_use_is_valid(99) != 0 { return 4 } 19 20 let l: *NxRightsLedger = nx_rights_ledger_new(16) 21 if nx_rights_count(l) != 0 { return 5 } 22 23 // ---- NEG-CONTROL: empty ledger -> default DENY for anything ---- 24 if nx_rights_is_licensed(l, 7, NX_USE_MERCH, 1000) != 0 { return 6 } 25 26 // bad use scope refused at grant time 27 if nx_rights_grant(l, 1, 7, 99, 1000, 10000) != NX_RL_ERR_BAD_USE { return 7 } 28 29 // talent 7 grants MERCH at t=1000 for a 10000us term (expires 11000) 30 if nx_rights_grant(l, 1, 7, NX_USE_MERCH, 1000, 10000) != NX_RL_OK { return 8 } 31 if nx_rights_count(l) != 1 { return 9 } 32 if nx_rights_count_by_state(l, NX_RL_GRANTED) != 1 { return 10 } 33 34 // ---- POSITIVE: within term, exact talent + exact use -> ALLOW ---- 35 if nx_rights_is_licensed(l, 7, NX_USE_MERCH, 5000) != 1 { return 11 } 36 37 // ---- NEG-CONTROL: right talent, WRONG use -> DENY ---- 38 // a MERCH license does NOT authorize a web-gallery display 39 if nx_rights_is_licensed(l, 7, NX_USE_WEB_GALLERY, 5000) != 0 { return 12 } 40 41 // ---- NEG-CONTROL: WRONG talent, right use -> DENY ---- 42 if nx_rights_is_licensed(l, 8, NX_USE_MERCH, 5000) != 0 { return 13 } 43 44 // ---- NEG-CONTROL: past the term -> EXPIRED -> DENY ---- 45 if nx_rights_is_licensed(l, 7, NX_USE_MERCH, 99999) != 0 { return 14 } 46 if nx_rights_state(l, 7, NX_USE_MERCH, 99999) != NX_RL_EXPIRED { return 15 } 47 48 // talent 7 grants WEB_GALLERY at t=2000 term 10000 (expires 12000) 49 if nx_rights_grant(l, 2, 7, NX_USE_WEB_GALLERY, 2000, 10000) != NX_RL_OK { return 16 } 50 if nx_rights_is_licensed(l, 7, NX_USE_WEB_GALLERY, 3000) != 1 { return 17 } 51 52 // ---- NEG-CONTROL: revoke -> DENY immediately, even within the term ---- 53 if nx_rights_revoke(l, 2, 4000) != NX_RL_OK { return 18 } 54 if nx_rights_is_licensed(l, 7, NX_USE_WEB_GALLERY, 5000) != 0 { return 19 } 55 if nx_rights_state(l, 7, NX_USE_WEB_GALLERY, 5000) != NX_RL_REVOKED { return 20 } 56 57 // cannot revoke a non-granted (already revoked) row; audit stays intact 58 if nx_rights_revoke(l, 2, 6000) != NX_RL_ERR_NOT_GRANTED { return 21 } 59 // cannot revoke an unknown grant 60 if nx_rights_revoke(l, 999, 6000) != NX_RL_ERR_NOT_FOUND { return 22 } 61 62 // ---- re-grant after revoke: a NEW valid grant authorizes again ---- 63 if nx_rights_grant(l, 3, 7, NX_USE_WEB_GALLERY, 7000, 10000) != NX_RL_OK { return 23 } 64 if nx_rights_is_licensed(l, 7, NX_USE_WEB_GALLERY, 8000) != 1 { return 24 } 65 66 return 0 67}