| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import encoding.hex |
| 5 | import os |
| 6 | import rand |
| 7 | |
| 8 | struct CombinedTextOracle { |
| 9 | name string |
| 10 | initial string |
| 11 | final string |
| 12 | operations string |
| 13 | put string |
| 14 | next string |
| 15 | codec CodecKind |
| 16 | eol EolStyle |
| 17 | mode ErrorHandlingMode |
| 18 | } |
| 19 | |
| 20 | fn combined_text_directory() string { |
| 21 | path := os.join_path(os.vtmp_dir(), 'vile-combined-text-' + rand.ulid()) |
| 22 | os.mkdir_all(path) or { panic(err.msg()) } |
| 23 | return path |
| 24 | } |
| 25 | |
| 26 | fn combined_text_put(mut vm VM, p Value, text string) ! { |
| 27 | port_put_string(mut vm, [p, string_value(text)])! |
| 28 | } |
| 29 | |
| 30 | fn test_combined_text_native_byte_boundary_oracles_in_all_buffer_modes() { |
| 31 | root := combined_text_directory() |
| 32 | defer { os.rmdir_all(root) or {} } |
| 33 | mut vm := new() |
| 34 | rows := [ |
| 35 | CombinedTextOracle{ name: 'utf8_equal_width_peek', initial: 'cebb4243', final: 'cea94243', operations: 'p', put: 'Ω', next: 'B', codec: .utf8, eol: .none, mode: .raise }, |
| 36 | CombinedTextOracle{ name: 'utf8_read_then_write', initial: 'cebb4243', final: 'cebb5843', operations: 'r', put: 'X', next: 'C', codec: .utf8, eol: .none, mode: .raise }, |
| 37 | CombinedTextOracle{ name: 'byte_overwrite_not_character_splice', initial: 'cebb4243', final: '58bb4243', operations: 'p', put: 'X', next: '', codec: .utf8, eol: .none, mode: .raise }, |
| 38 | CombinedTextOracle{ name: 'utf16_be_bom_peek', initial: 'feff004100420043', final: 'feff005a00420043', operations: 'p', put: 'Z', next: 'B', codec: .utf16, eol: .none, mode: .raise }, |
| 39 | CombinedTextOracle{ name: 'utf16_le_bom_read_write', initial: 'fffe410042004300', final: 'fffe41005a004300', operations: 'r', put: 'Z', next: 'C', codec: .utf16, eol: .none, mode: .raise }, |
| 40 | CombinedTextOracle{ name: 'utf16_le_bom_write_first', initial: 'fffe410042004300', final: 'fffe5a0042004300', operations: '', put: 'Z', next: 'B', codec: .utf16, eol: .none, mode: .raise }, |
| 41 | CombinedTextOracle{ name: 'utf16_surrogate_scalar', initial: 'feff0041d83dde0000420043', final: 'feff0041d83dde0100420043', operations: 'rp', put: '😁', next: 'B', codec: .utf16, eol: .none, mode: .raise }, |
| 42 | CombinedTextOracle{ name: 'utf16_literal_bom_after_switch', initial: 'feff00410042feff0043', final: 'feff00410058feff0043', operations: 'r', put: 'X', next: 'C', codec: .utf16, eol: .none, mode: .raise }, |
| 43 | CombinedTextOracle{ name: 'utf8_bom_existing_control', initial: 'efbbbf4142', final: 'efbbbf5842', operations: 'r', put: 'X', next: 'B', codec: .utf8, eol: .none, mode: .raise }, |
| 44 | CombinedTextOracle{ name: 'crlf_read_boundary', initial: '410d0a4243', final: '410d0a5843', operations: 'rr', put: 'X', next: 'C', codec: .utf8, eol: .lf, mode: .raise }, |
| 45 | CombinedTextOracle{ name: 'crlf_peek_boundary', initial: '410d0a4243', final: '410d0a4243', operations: 'rp', put: '\n', next: 'B', codec: .utf8, eol: .crlf, mode: .raise }, |
| 46 | CombinedTextOracle{ name: 'crnel_utf8_boundary', initial: '410dc2854243', final: '410dc2855843', operations: 'rr', put: 'X', next: 'C', codec: .utf8, eol: .lf, mode: .raise }, |
| 47 | CombinedTextOracle{ name: 'cr_nonLF_queued_scalar', initial: '410dcebb42', final: '410dcea942', operations: 'rr', put: 'Ω', next: 'B', codec: .utf8, eol: .lf, mode: .raise }, |
| 48 | CombinedTextOracle{ name: 'cr_nonLF_peek', initial: '410dcebb42', final: '4158cebb42', operations: 'rp', put: 'X', next: 'λ', codec: .utf8, eol: .lf, mode: .raise }, |
| 49 | CombinedTextOracle{ name: 'CR_at_EOF', initial: '410d', final: '4158', operations: 'rp', put: 'X', next: '', codec: .utf8, eol: .lf, mode: .raise }, |
| 50 | CombinedTextOracle{ name: 'encoded_output_expansion', initial: '41424344', final: '410d0a44', operations: 'r', put: '\n', next: 'D', codec: .utf8, eol: .crlf, mode: .raise }, |
| 51 | CombinedTextOracle{ name: 'ignore_gap_before_peeked_scalar', initial: '41ff4243', final: '41584243', operations: 'rp', put: 'X', next: 'B', codec: .utf8, eol: .none, mode: .ignore }, |
| 52 | CombinedTextOracle{ name: 'ignore_after_emitted_scalar', initial: '41ff4243', final: '41ff4258', operations: 'rr', put: 'X', next: '', codec: .utf8, eol: .none, mode: .ignore }, |
| 53 | CombinedTextOracle{ name: 'CR_ignore_gap_nonLF', initial: '410dff4243', final: '410d584243', operations: 'rr', put: 'X', next: 'B', codec: .utf8, eol: .lf, mode: .ignore }, |
| 54 | CombinedTextOracle{ name: 'CR_ignore_gap_actual_LF', initial: '410dff0a4243', final: '410dff0a5843', operations: 'rr', put: 'X', next: 'C', codec: .utf8, eol: .lf, mode: .ignore }, |
| 55 | CombinedTextOracle{ name: 'replacement_raw_span', initial: '41e2824243', final: '41e2825843', operations: 'rr', put: 'X', next: 'C', codec: .utf8, eol: .none, mode: .replace }, |
| 56 | CombinedTextOracle{ name: 'raise_recovery_raw_boundary', initial: '41e2824243', final: '41e2825843', operations: 're', put: 'X', next: 'C', codec: .utf8, eol: .none, mode: .raise }, |
| 57 | CombinedTextOracle{ name: 'utf16_buffered_valid_unit_after_malformed', initial: 'feffd80000410042', final: 'feffd800005a0042', operations: 'r', put: 'Z', next: 'B', codec: .utf16, eol: .none, mode: .replace }, |
| 58 | ] |
| 59 | for mode in [PortBufferMode.none, .block, .line] { |
| 60 | for row in rows { |
| 61 | path := os.join_path(root, row.name) |
| 62 | initial := hex.decode(row.initial) or { panic(err.msg()) } |
| 63 | os.write_file_array(path, initial) or { panic(err.msg()) } |
| 64 | p := vm.open_file_input_output_port(path, |
| 65 | no_create: true |
| 66 | no_truncate: true |
| 67 | buffer_mode: mode |
| 68 | transcoder: transcoder_value(row.codec, row.eol, row.mode) |
| 69 | ) or { panic(err.msg()) } |
| 70 | for operation in row.operations { |
| 71 | match operation { |
| 72 | `p` { |
| 73 | assert (vm.peek_char(p) or { panic('${row.name}: ${err.msg()}') }).tag == .character |
| 74 | } |
| 75 | `r` { |
| 76 | assert (vm.read_char(p) or { panic('${row.name}: ${err.msg()}') }).tag == .character |
| 77 | } |
| 78 | `e` { |
| 79 | if _ := vm.read_char(p) { |
| 80 | assert false, row.name |
| 81 | } |
| 82 | } |
| 83 | else { |
| 84 | assert false |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | combined_text_put(mut vm, p, row.put) or { panic('${row.name}: ${err.msg()}') } |
| 89 | for ch in row.next.runes() { |
| 90 | assert (vm.read_char(p) or { panic('${row.name}: ${err.msg()}') }).whole() == ch, row.name |
| 91 | } |
| 92 | if row.name == 'CR_at_EOF' || row.name == 'ignore_after_emitted_scalar' { |
| 93 | assert (vm.read_char(p) or { panic(err.msg()) }).tag == .eof |
| 94 | } |
| 95 | assert !(vm.port_has_position(p) or { panic(err.msg()) }) |
| 96 | vm.close_port(p) or { panic(err.msg()) } |
| 97 | expected := hex.decode(row.final) or { panic(err.msg()) } |
| 98 | assert (os.read_bytes(path) or { panic(err.msg()) }) == expected, '${row.name}: ${mode}' |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn test_combined_text_header_begins_at_attachment_origin_and_preserves_closed_halves() { |
| 104 | root := combined_text_directory() |
| 105 | defer { os.rmdir_all(root) or {} } |
| 106 | mut vm := new() |
| 107 | for input_closed in [false, true] { |
| 108 | path := os.join_path(root, '${input_closed}.bin') |
| 109 | os.write_file_array(path, [u8(88), 255, 254, 65, 0, 66, 0]) or { panic(err.msg()) } |
| 110 | binary := vm.open_file_input_output_port(path, no_create: true, no_truncate: true) or { panic(err.msg()) } |
| 111 | assert (vm.read_u8(binary) or { panic(err.msg()) }).whole() == 88 |
| 112 | if input_closed { vm.close_input_port(binary) or { panic(err.msg()) } } |
| 113 | p := vm.transcoded_port(binary, transcoder_value(.utf16, .none, .raise)) or { panic(err.msg()) } |
| 114 | assert binary.input_pointer().closed && binary.output_pointer().closed |
| 115 | assert p.input_pointer().closed == input_closed |
| 116 | combined_text_put(mut vm, p, 'Z') or { panic(err.msg()) } |
| 117 | vm.close_port(p) or { panic(err.msg()) } |
| 118 | assert (os.read_bytes(path) or { panic(err.msg()) }) == [u8(88), 255, 254, 90, 0, 66, 0] |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | fn test_combined_text_native_half_close_both_orders_and_gc_root_retargeting() { |
| 123 | root := combined_text_directory() |
| 124 | defer { os.rmdir_all(root) or {} } |
| 125 | for input_first in [false, true] { |
| 126 | mut vm := new() |
| 127 | path := os.join_path(root, '${input_first}.bin') |
| 128 | os.write_file(path, 'λABC') or { panic(err.msg()) } |
| 129 | p := vm.open_file_input_output_port(path, |
| 130 | no_create: true |
| 131 | no_truncate: true |
| 132 | transcoder: transcoder_value(.utf8, .none, .raise) |
| 133 | ) or { panic(err.msg()) } |
| 134 | group := combined_transcoded_resource(p) |
| 135 | assert voidptr(group.file.input) == voidptr(group) |
| 136 | assert voidptr(group.file.output) == voidptr(&group.output.raw) |
| 137 | assert (vm.peek_char(p) or { panic(err.msg()) }).whole() == `λ` |
| 138 | vm.define('p', p) or { panic(err.msg()) } |
| 139 | gc_collect() |
| 140 | if input_first { |
| 141 | vm.close_input_port(p) or { panic(err.msg()) } |
| 142 | combined_text_put(mut vm, p, 'Ω') or { panic(err.msg()) } |
| 143 | } else { |
| 144 | combined_text_put(mut vm, p, 'Ω') or { panic(err.msg()) } |
| 145 | vm.close_output_port(p) or { panic(err.msg()) } |
| 146 | assert (vm.read_char(p) or { panic(err.msg()) }).whole() == `A` |
| 147 | } |
| 148 | vm.close_port(p) or { panic(err.msg()) } |
| 149 | assert !group.file.stream.file.is_opened |
| 150 | assert (os.read_file(path) or { panic(err.msg()) }) == 'ΩABC' |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | fn test_combined_text_steady_shared_ascii_reuses_buffers_and_boundary_storage() { |
| 155 | root := combined_text_directory() |
| 156 | defer { os.rmdir_all(root) or {} } |
| 157 | path := os.join_path(root, 'steady') |
| 158 | os.write_file(path, 'AB'.repeat(10000)) or { panic(err.msg()) } |
| 159 | mut region := arena.new(chunk_size: 1048576) |
| 160 | region.push() |
| 161 | defer { |
| 162 | region.pop() |
| 163 | region.free() |
| 164 | } |
| 165 | mut vm := new() |
| 166 | p := vm.open_file_input_output_port(path, |
| 167 | no_create: true |
| 168 | no_truncate: true |
| 169 | transcoder: transcoder_value(.utf8, .none, .raise) |
| 170 | ) or { panic(err.msg()) } |
| 171 | args := [p, string_value('X')] |
| 172 | for _ in 0 .. 16 { |
| 173 | assert (vm.read_char(p) or { panic(err.msg()) }).tag == .character |
| 174 | port_put_string(mut vm, args) or { panic(err.msg()) } |
| 175 | } |
| 176 | before := region.used() |
| 177 | for _ in 0 .. 1000 { |
| 178 | assert (vm.read_char(p) or { panic(err.msg()) }).tag == .character |
| 179 | port_put_string(mut vm, args) or { panic(err.msg()) } |
| 180 | } |
| 181 | assert region.used() == before, 'steady mixed I/O allocated ${region.used() - before} bytes' |
| 182 | vm.close_port(p) or { panic(err.msg()) } |
| 183 | } |
| 184 | |
| 185 | @[heap] |
| 186 | struct CombinedTextHost { |
| 187 | input string |
| 188 | mut: |
| 189 | offset int |
| 190 | reads int |
| 191 | bytes []u8 |
| 192 | writes int |
| 193 | fail_on_write int |
| 194 | flushes int |
| 195 | } |
| 196 | |
| 197 | fn (mut host CombinedTextHost) read(mut destination []u8) !int { |
| 198 | host.reads++ |
| 199 | count := int_min(destination.len, host.input.len - host.offset) |
| 200 | if count > 0 { unsafe { vmemcpy(destination.data, host.input.str + host.offset, count) } } |
| 201 | host.offset += count |
| 202 | return count |
| 203 | } |
| 204 | |
| 205 | fn (mut host CombinedTextHost) write(bytes []u8) !int { |
| 206 | host.writes++ |
| 207 | if host.writes == host.fail_on_write { return failure('injected duplex write failure') } |
| 208 | count := int_min(2, bytes.len) |
| 209 | host.bytes << bytes[..count] |
| 210 | return count |
| 211 | } |
| 212 | |
| 213 | fn (mut host CombinedTextHost) flush() ! { host.flushes++ } |
| 214 | |
| 215 | fn test_combined_text_host_independent_rx_survives_failed_output_halfclose_and_final_flush_retries() { |
| 216 | mut vm := new() |
| 217 | mut host := &CombinedTextHost{ input: 'λRX', fail_on_write: 2 } |
| 218 | binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err.msg()) } |
| 219 | p := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err.msg()) } |
| 220 | assert host.reads == 0 && host.writes == 0 && host.flushes == 0 |
| 221 | if _ := combined_text_put(mut vm, p, 'abcde') { |
| 222 | assert false |
| 223 | } |
| 224 | assert host.bytes.bytestr() == 'ab' |
| 225 | host.fail_on_write = 3 |
| 226 | if _ := vm.close_output_port(p) { |
| 227 | assert false |
| 228 | } |
| 229 | assert p.output_pointer().closed && !p.input_pointer().closed |
| 230 | assert host.writes == 3 |
| 231 | assert (vm.read_char(p) or { panic(err.msg()) }).whole() == `λ` |
| 232 | assert host.writes == 3 |
| 233 | vm.close_input_port(p) or { panic(err.msg()) } |
| 234 | assert host.bytes.bytestr() == 'abcde' && host.flushes == 1 |
| 235 | assert host.reads == 2 |
| 236 | } |
| 237 | |
| 238 | fn test_combined_text_host_ownership_validation_preserves_raw_alias_and_cached_rx() { |
| 239 | mut vm := new() |
| 240 | mut foreign := new() |
| 241 | mut host := &CombinedTextHost{ input: 'λRX' } |
| 242 | binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err.msg()) } |
| 243 | for options in [integer(1), boolean(false), codec_value(.utf8)] { |
| 244 | if _ := vm.transcoded_port(binary, options) { |
| 245 | assert false |
| 246 | } |
| 247 | } |
| 248 | if _ := foreign.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) { |
| 249 | assert false |
| 250 | } |
| 251 | binary.output_pointer().busy = true |
| 252 | if _ := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) { |
| 253 | assert false |
| 254 | } |
| 255 | binary.output_pointer().busy = false |
| 256 | assert !binary.input_pointer().closed && !binary.output_pointer().closed |
| 257 | assert (vm.peek_u8(binary) or { panic(err.msg()) }).whole() == 0xce |
| 258 | reads := host.reads |
| 259 | p := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err.msg()) } |
| 260 | assert host.reads == reads && host.writes == 0 |
| 261 | assert (vm.peek_char(p) or { panic(err.msg()) }).whole() == `λ` |
| 262 | combined_text_put(mut vm, p, 'X') or { panic(err.msg()) } |
| 263 | assert (vm.read_char(p) or { panic(err.msg()) }).whole() == `λ` |
| 264 | assert (vm.read_char(p) or { panic(err.msg()) }).whole() == `R` |
| 265 | assert host.bytes.bytestr() == 'X' |
| 266 | vm.close_port(p) or { panic(err.msg()) } |
| 267 | } |
| 268 | |