code wiki / (root) / nx_license_check_test.nx

nx_license_check_test.nx source

↩ module page · 87 lines · 3981 B

1// nx_license_check_test.nx -- smoke for the licensing-wall grader. 2// 3// Asserts: 4// 1. Sealed-enum validity predicate behaves 5// 2. Synthetic header `// license_tier: ORIGINAL` extracts as ORIGINAL 6// 3. Synthetic header `// license_tier: TIER_0_UNENCUMBERED` extracts 7// 4. Synthetic header `// license_tier: INDEPENDENT_REDERIVE` extracts 8// 5. Synthetic header `// license_tier: COPY_WITH_ATTRIB` extracts as 9// FORBIDDEN (banned tier from older drafts) 10// 6. Source with no header at all extracts as UNKNOWN 11// 7. Verdict mapping: WIN for 3 allowed, LOSE for FORBIDDEN + UNKNOWN 12// 8. nx_license_scan_file on this very file returns ORIGINAL 13// 14// expect_exit: 0 15// 16// license_tier: ORIGINAL 17 18import "nx_syscalls.nx" 19import "nx_runtime.nx" 20import "nx_types.nx" 21import "nx_license_check.nx" 22 23func _strlen(s: *u8) -> nx_int { 24 var n: nx_int = 0 25 while 1 == 1 { 26 let p: *u8 = (s as nx_int + n) as *u8 27 let c: nx_int = p[0] as nx_int 28 if (c & 255) == 0 { return n } 29 n = n + 1 30 } 31 return n 32} 33 34func main() -> nx_int { 35 // ---- Test 1: validity predicate --------------------------------- 36 if nx_license_tier_is_valid(NX_LIC_UNKNOWN) != 1 { return 1 } 37 if nx_license_tier_is_valid(NX_LIC_TIER_0_UNENCUMBERED) != 1 { return 2 } 38 if nx_license_tier_is_valid(NX_LIC_INDEPENDENT_REDERIVE) != 1 { return 3 } 39 if nx_license_tier_is_valid(NX_LIC_ORIGINAL) != 1 { return 4 } 40 if nx_license_tier_is_valid(NX_LIC_FORBIDDEN) != 1 { return 5 } 41 if nx_license_tier_is_valid(NX_LIC_N) != 0 { return 6 } 42 if nx_license_tier_is_valid(-1) != 0 { return 7 } 43 44 // ---- Test 2: ORIGINAL -------------------------------------------- 45 let s2: *u8 = "// license_tier: ORIGINAL\n" as *u8 46 let t2: nx_int = nx_license_extract(s2, _strlen(s2)) 47 if t2 != NX_LIC_ORIGINAL { return 10 } 48 49 // ---- Test 3: TIER_0_UNENCUMBERED --------------------------------- 50 let s3: *u8 = "// license_tier: TIER_0_UNENCUMBERED\n" as *u8 51 let t3: nx_int = nx_license_extract(s3, _strlen(s3)) 52 if t3 != NX_LIC_TIER_0_UNENCUMBERED { return 11 } 53 54 // ---- Test 4: INDEPENDENT_REDERIVE -------------------------------- 55 let s4: *u8 = "// license_tier: INDEPENDENT_REDERIVE\n" as *u8 56 let t4: nx_int = nx_license_extract(s4, _strlen(s4)) 57 if t4 != NX_LIC_INDEPENDENT_REDERIVE { return 12 } 58 59 // ---- Test 5: FORBIDDEN (older draft) ----------------------------- 60 let s5: *u8 = "// license_tier: COPY_WITH_ATTRIB\n" as *u8 61 let t5: nx_int = nx_license_extract(s5, _strlen(s5)) 62 if t5 != NX_LIC_FORBIDDEN { return 13 } 63 64 // ---- Test 6: missing header ------------------------------------- 65 let s6: *u8 = "func main() -> nx_int { return 0 }\n" as *u8 66 let t6: nx_int = nx_license_extract(s6, _strlen(s6)) 67 if t6 != NX_LIC_UNKNOWN { return 14 } 68 69 // ---- Test 7: verdict mapping ------------------------------------- 70 if nx_license_verdict_for_tier(NX_LIC_ORIGINAL) != NX_LIC_VERDICT_WIN { return 20 } 71 if nx_license_verdict_for_tier(NX_LIC_TIER_0_UNENCUMBERED) != NX_LIC_VERDICT_WIN { return 21 } 72 if nx_license_verdict_for_tier(NX_LIC_INDEPENDENT_REDERIVE) != NX_LIC_VERDICT_WIN { return 22 } 73 if nx_license_verdict_for_tier(NX_LIC_FORBIDDEN) != NX_LIC_VERDICT_LOSE { return 23 } 74 if nx_license_verdict_for_tier(NX_LIC_UNKNOWN) != NX_LIC_VERDICT_LOSE { return 24 } 75 76 // ---- Test 8: scan self file -------------------------------------- 77 let self_path: *u8 = "runtime/nx_license_check_test.nx" as *u8 78 let tself: nx_int = nx_license_scan_file(self_path) 79 if tself != NX_LIC_ORIGINAL { return 30 } 80 81 // ---- Test 9: scan the grader itself ----------------------------- 82 let gpath: *u8 = "runtime/nx_license_check.nx" as *u8 83 let tg: nx_int = nx_license_scan_file(gpath) 84 if tg != NX_LIC_ORIGINAL { return 31 } 85 86 return 0 87}