nx_covenant_test.nx source
↩ module page · 124 lines · 5592 B
1// nx_covenant_test.nx -- smoke for nx_covenant.
2
3import "nx_syscalls.nx"
4import "nx_covenant.nx"
5
6func main() -> i64 {
7 let now: nx_size = 1000000
8
9 // 1: enum validity
10 if nx_sh_class_is_valid(NX_SH_LANDOWNER) != 1 { return 1 }
11 if nx_sh_class_is_valid(NX_SH_TRUSTED_ADVISOR) != 1 { return 2 }
12 if nx_sh_class_is_valid(-1) != 0 { return 3 }
13 if nx_sh_class_is_valid(10) != 0 { return 4 }
14 if NX_SH_N_CLASSES != 10 { return 5 }
15
16 if nx_cn_state_is_valid(NX_CN_PENDING) != 1 { return 6 }
17 if nx_cn_state_is_valid(NX_CN_NOT_APPLICABLE) != 1 { return 7 }
18 if nx_cn_state_is_valid(-1) != 0 { return 8 }
19 if nx_cn_state_is_valid(7) != 0 { return 9 }
20
21 if nx_cv_v_is_valid(NX_CV_V_OK_ALL_GRANTED) != 1 { return 10 }
22 if nx_cv_v_is_valid(NX_CV_V_FPIC_REQUIRED_MISSING) != 1 { return 11 }
23
24 // 2: state predicates
25 if nx_cn_state_is_blocking(NX_CN_DECLINED) != 1 { return 12 }
26 if nx_cn_state_is_blocking(NX_CN_REVOKED) != 1 { return 13 }
27 if nx_cn_state_is_blocking(NX_CN_EXPIRED) != 1 { return 14 }
28 if nx_cn_state_is_blocking(NX_CN_PENDING) != 0 { return 15 }
29 if nx_cn_state_is_blocking(NX_CN_GRANTED) != 0 { return 16 }
30
31 if nx_cn_state_is_satisfied(NX_CN_GRANTED) != 1 { return 17 }
32 if nx_cn_state_is_satisfied(NX_CN_GRANTED_CONDITIONAL) != 1 { return 18 }
33 if nx_cn_state_is_satisfied(NX_CN_NOT_APPLICABLE) != 1 { return 19 }
34 if nx_cn_state_is_satisfied(NX_CN_PENDING) != 0 { return 20 }
35
36 if nx_sh_class_requires_fpic(NX_SH_INDIGENOUS_NATION) != 1 { return 21 }
37 if nx_sh_class_requires_fpic(NX_SH_LANDOWNER) != 0 { return 22 }
38
39 // 3: fresh covenant
40 let c: *NxCovenant = nx_cv_new(42, 0xCAFE, 8, 0, now)
41 if c.covenant_id != 42 { return 23 }
42 if c.capacity != 8 { return 24 }
43 if c.n_entries != 0 { return 25 }
44 if c.requires_fpic != 0 { return 26 }
45
46 // 4: invalid capacity rejected
47 if nx_cv_new(1, 0, 0, 0, now) != (0 as *NxCovenant) { return 27 }
48
49 // 5: add stakeholders + verify count
50 nx_cv_add_stakeholder(c, 100, NX_SH_LANDOWNER, now)
51 nx_cv_add_stakeholder(c, 200, NX_SH_NEIGHBOR, now)
52 nx_cv_add_stakeholder(c, 300, NX_SH_OPERATOR, now)
53 if c.n_entries != 3 { return 28 }
54
55 // 6: empty + all pending -> PENDING_STAKEHOLDERS
56 if nx_cv_classify(c, now) != NX_CV_V_PENDING_STAKEHOLDERS { return 29 }
57
58 // 7: grant each -> classify changes
59 nx_cv_set_consent(c, 100, NX_CN_GRANTED, 0, 3600000, now)
60 nx_cv_set_consent(c, 200, NX_CN_GRANTED, 0, 3600000, now)
61 nx_cv_set_consent(c, 300, NX_CN_GRANTED, 0, 3600000, now)
62 if nx_cv_classify(c, now) != NX_CV_V_OK_ALL_GRANTED { return 30 }
63
64 // 8: revoke one -> REVOKED
65 nx_cv_set_consent(c, 200, NX_CN_REVOKED, 0, 0, now + 100)
66 if nx_cv_classify(c, now + 200) != NX_CV_V_REVOKED { return 31 }
67
68 // 9: decline blocks
69 let c2: *NxCovenant = nx_cv_new(1, 0, 4, 0, now)
70 nx_cv_add_stakeholder(c2, 1, NX_SH_LANDOWNER, now)
71 nx_cv_add_stakeholder(c2, 2, NX_SH_NEIGHBOR, now)
72 nx_cv_set_consent(c2, 1, NX_CN_GRANTED, 0, 3600000, now)
73 nx_cv_set_consent(c2, 2, NX_CN_DECLINED, 0, 0, now)
74 if nx_cv_classify(c2, now) != NX_CV_V_BLOCKED_DECLINED { return 32 }
75
76 // 10: FPIC-required without indigenous stakeholder -> FPIC_REQUIRED_MISSING
77 let c3: *NxCovenant = nx_cv_new(1, 0, 4, 1, now) // requires_fpic=1
78 nx_cv_add_stakeholder(c3, 1, NX_SH_LANDOWNER, now)
79 nx_cv_set_consent(c3, 1, NX_CN_GRANTED, 0, 3600000, now)
80 if nx_cv_classify(c3, now) != NX_CV_V_FPIC_REQUIRED_MISSING { return 33 }
81
82 // 11: FPIC-required with indigenous pending -> still FPIC_REQUIRED_MISSING
83 nx_cv_add_stakeholder(c3, 99, NX_SH_INDIGENOUS_NATION, now)
84 if nx_cv_classify(c3, now) != NX_CV_V_FPIC_REQUIRED_MISSING { return 34 }
85
86 // 12: FPIC-required with indigenous granted -> OK_ALL_GRANTED
87 nx_cv_set_consent(c3, 99, NX_CN_GRANTED, 0, 3600000, now)
88 if nx_cv_classify(c3, now) != NX_CV_V_OK_ALL_GRANTED { return 35 }
89
90 // 13: count by class
91 nx_cv_add_stakeholder(c, 400, NX_SH_NEIGHBOR, now)
92 if nx_cv_count_by_class(c, NX_SH_NEIGHBOR) != 2 { return 36 }
93 if nx_cv_count_by_class(c, NX_SH_LANDOWNER) != 1 { return 37 }
94 if nx_cv_count_by_class(c, NX_SH_FUTURE_INHERITOR) != 0 { return 38 }
95
96 // 14: invalid stakeholder class rejected
97 let bad_add: nx_int = nx_cv_add_stakeholder(c, 999, 99, now)
98 if bad_add != NX_CV_V_INVALID { return 39 }
99
100 // 15: capacity exceeded
101 let c_full: *NxCovenant = nx_cv_new(1, 0, 1, 0, now)
102 nx_cv_add_stakeholder(c_full, 1, NX_SH_LANDOWNER, now)
103 if nx_cv_add_stakeholder(c_full, 2, NX_SH_NEIGHBOR, now) != NX_CV_V_INVALID { return 40 }
104
105 // 16: set_consent on unknown stakeholder -> INVALID
106 if nx_cv_set_consent(c, 9999, NX_CN_GRANTED, 0, 1000, now) != NX_CV_V_INVALID { return 41 }
107
108 // 17: expiry path
109 let c_exp: *NxCovenant = nx_cv_new(1, 0, 2, 0, now)
110 nx_cv_add_stakeholder(c_exp, 1, NX_SH_LANDOWNER, now)
111 nx_cv_set_consent(c_exp, 1, NX_CN_GRANTED, 0, 1000, now) // 1s lifetime
112 if nx_cv_classify(c_exp, now) != NX_CV_V_OK_ALL_GRANTED { return 42 }
113 // 2 sec later -- expired
114 if nx_cv_classify(c_exp, now + 2000000) != NX_CV_V_EXPIRED { return 43 }
115
116 // 18: null handling
117 let null_c: *NxCovenant = (0 as i64) as *NxCovenant
118 if nx_cv_add_stakeholder(null_c, 1, NX_SH_LANDOWNER, now) != NX_CV_V_NULL { return 44 }
119 if nx_cv_classify(null_c, now) != NX_CV_V_NULL { return 45 }
120 if nx_cv_set_consent(null_c, 1, NX_CN_GRANTED, 0, 0, now) != NX_CV_V_NULL { return 46 }
121 if nx_cv_count_by_class(null_c, 0) != 0 { return 47 }
122
123 return 0
124}