| 1 | module main |
| 2 | |
| 3 | import arena |
| 4 | import os |
| 5 | import scheme |
| 6 | |
| 7 | fn require(ok bool, message string) { |
| 8 | if !ok { panic(message) } |
| 9 | } |
| 10 | |
| 11 | fn quota(err IError, used u64, limit u64) { |
| 12 | require(scheme.is_control_transfer(err), 'quota request lost its type') |
| 13 | if err is scheme.ResourceLimitRequest { |
| 14 | require(err.kind == .open_files && err.used == used && err.limit == limit && err.requested == 1, 'wrong quota values') |
| 15 | } else { |
| 16 | panic(err) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn open_kind(mut vm scheme.VM, kind string, path string, options scheme.FilePortOptions) !scheme.Value { |
| 21 | return match kind { |
| 22 | 'input' { vm.open_input_file(path)! } |
| 23 | 'output' { vm.open_output_file(path)! } |
| 24 | 'binary-input' { vm.open_file_input_port(path, options)! } |
| 25 | 'binary-output' { vm.open_file_output_port(path, options)! } |
| 26 | 'duplex' { vm.open_file_input_output_port(path, options)! } |
| 27 | else { return error('unknown test operation') } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn files(directory string) { |
| 32 | path := os.join_path(directory, 'preserved') |
| 33 | os.write_file(path, 'keep') or { panic(err) } |
| 34 | mut zero := scheme.new_with_options(scheme.Options{ max_open_files: 0 }) or { panic(err) } |
| 35 | for kind in ['input', 'output', 'binary-input', 'binary-output', 'duplex'] { |
| 36 | if _ := open_kind(mut zero, kind, path, no_fail: true) { |
| 37 | panic('zero quota bypassed') |
| 38 | } else { |
| 39 | quota(err, 0, 0) |
| 40 | } |
| 41 | require(os.read_file(path) or { panic(err) } == 'keep', 'rejected open changed file') |
| 42 | } |
| 43 | zero.standard_input_port() or { panic(err) } |
| 44 | zero.standard_output_port() or { panic(err) } |
| 45 | mut vm := scheme.new_with_options(scheme.Options{ max_open_files: 1 }) or { panic(err) } |
| 46 | for kind in ['input', 'output', 'binary-input', 'binary-output', 'duplex'] { |
| 47 | p := open_kind(mut vm, kind, path, no_fail: true) or { panic(err) } |
| 48 | if _ := vm.open_input_file(path) { |
| 49 | panic('one-slot quota bypassed') |
| 50 | } else { |
| 51 | quota(err, 1, 1) |
| 52 | } |
| 53 | vm.close_port(p) or { panic(err) } |
| 54 | vm.close_port(p) or { panic(err) } |
| 55 | } |
| 56 | native := vm.eval('(native-transcoder)') or { panic(err) } |
| 57 | for kind in ['binary-input', 'binary-output', 'duplex'] { |
| 58 | p := open_kind(mut vm, kind, path, no_fail: true, transcoder: native) or { panic(err) } |
| 59 | if kind == 'duplex' { vm.close_input_port(p) or { panic(err) } } |
| 60 | if _ := vm.open_input_file(path) { |
| 61 | panic('transcoded file lost its charge') |
| 62 | } else { |
| 63 | quota(err, 1, 1) |
| 64 | } |
| 65 | vm.close_port(p) or { panic(err) } |
| 66 | } |
| 67 | if _ := vm.open_input_file(os.join_path(directory, 'missing')) { |
| 68 | panic('missing file opened') |
| 69 | } else { |
| 70 | require(err !is scheme.ResourceLimitRequest, 'failed open leaked a slot') |
| 71 | } |
| 72 | p := vm.open_input_file(path) or { panic(err) } |
| 73 | vm.close_port(p) or { panic(err) } |
| 74 | } |
| 75 | |
| 76 | fn sources(directory string) { |
| 77 | path := os.join_path(directory, 'entry.scm') |
| 78 | os.write_file(os.join_path(directory, 'defs.scm'), '(define answer 42)') or { panic(err) } |
| 79 | os.write_file(path, '(include "defs.scm") answer') or { panic(err) } |
| 80 | mut zero := scheme.new_with_options(scheme.Options{ max_open_files: 0 }) or { panic(err) } |
| 81 | if _ := zero.eval_file(path) { |
| 82 | panic('source quota bypassed') |
| 83 | } else { |
| 84 | quota(err, 0, 0) |
| 85 | } |
| 86 | mut vm := scheme.new_with_options(scheme.Options{ max_open_files: 1 }) or { panic(err) } |
| 87 | require(vm.eval_file(path) or { panic(err) }.as_integer() or { panic(err) } == 42, 'nested source read retained its slot') |
| 88 | p := vm.open_input_file(path) or { panic(err) } |
| 89 | if _ := vm.eval_file(path) { |
| 90 | panic('source reader exceeded quota') |
| 91 | } else { |
| 92 | quota(err, 1, 1) |
| 93 | } |
| 94 | vm.close_port(p) or { panic(err) } |
| 95 | require(vm.eval_file(path) or { panic(err) }.as_integer() or { panic(err) } == 42, 'source quota did not recover') |
| 96 | } |
| 97 | |
| 98 | fn nested(mut vm scheme.VM, _args []scheme.Value) !scheme.Value { |
| 99 | return vm.eval('(open-output-file path)') |
| 100 | } |
| 101 | |
| 102 | fn control(directory string) { |
| 103 | path := os.join_path(directory, 'controlled') |
| 104 | mut vm := scheme.new_with_options(scheme.Options{ max_open_files: 1 }) or { panic(err) } |
| 105 | vm.define('path', scheme.string_value(path)) or { panic(err) } |
| 106 | vm.register('nested', nested) or { panic(err) } |
| 107 | vm.eval('(define held #f) (define after #f)') or { panic(err) } |
| 108 | if _ := vm.eval('(dynamic-wind (lambda () 0) |
| 109 | (lambda () (with-output-to-file path (lambda () (set! held (current-output-port)) (display "saved") (nested)))) |
| 110 | (lambda () (set! after #t)))') { |
| 111 | panic('reentry quota bypassed') |
| 112 | } else { |
| 113 | quota(err, 1, 1) |
| 114 | } |
| 115 | require(scheme.write(vm.eval('(list (output-port-open? held) after)') or { panic(err) }) or { panic(err) } == '(#f #f)', 'managed cleanup failed') |
| 116 | require(os.read_file(path) or { panic(err) } == 'saved', 'cleanup did not flush') |
| 117 | p := vm.open_input_file(path) or { panic(err) } |
| 118 | vm.close_port(p) or { panic(err) } |
| 119 | } |
| 120 | |
| 121 | @[manualfree] |
| 122 | fn main() { |
| 123 | require(os.args.len == 2, 'pass fixture directory') |
| 124 | mut region := arena.new() |
| 125 | region.push() |
| 126 | files(os.args[1]) |
| 127 | sources(os.args[1]) |
| 128 | control(os.args[1]) |
| 129 | region.pop() |
| 130 | region.free() |
| 131 | println('file resource quotas passed') |
| 132 | } |
| 133 | |