msgpack_spec.lua (2432B)
1 local t = require('test.unit.testutil') 2 local cimport = t.cimport 3 local itp = t.gen_itp(it) 4 local lib = cimport('./src/nvim/msgpack_rpc/unpacker.h', './src/nvim/memory.h') 5 local ffi = t.ffi 6 local eq = t.eq 7 local to_cstr = t.to_cstr 8 9 --- @class Unpacker 10 --- @field read_ptr ffi.cdata* 11 --- @field read_size number 12 13 --- @alias Unpacker* table<number, Unpacker> 14 --- @return Unpacker* unpacker `unpacker[0]` to dereference 15 local function make_unpacker() 16 return ffi.gc(ffi.cast('Unpacker*', lib.xcalloc(1, ffi.sizeof('Unpacker'))), function(unpacker) 17 lib.unpacker_teardown(unpacker) 18 lib.xfree(unpacker) 19 end) 20 end 21 22 --- @param unpacker Unpacker* 23 --- @param data string 24 --- @param size number? *default: data:len()* 25 local function unpacker_goto(unpacker, data, size) 26 unpacker[0].read_ptr = to_cstr(data) 27 unpacker[0].read_size = size or data:len() 28 end 29 30 --- @param unpacker Unpacker* 31 --- @return boolean 32 local function unpacker_advance(unpacker) 33 return lib.unpacker_advance(unpacker) 34 end 35 36 describe('msgpack', function() 37 describe('unpacker', function() 38 itp( 39 'does not crash when paused between `cells` and `wrap` params of `grid_line` #25184', 40 function() 41 -- [kMessageTypeNotification, "redraw", [ 42 -- ["grid_line", 43 -- [2, 0, 0, [[" " , 0, 77]], false] 44 -- ] 45 -- ]] 46 local payload = 47 '\x93\x02\xa6\x72\x65\x64\x72\x61\x77\x91\x92\xa9\x67\x72\x69\x64\x5f\x6c\x69\x6e\x65\x95\x02\x00\x00\x91\x93\xa1\x20\x00\x4d\xc2' 48 49 local unpacker = make_unpacker() 50 lib.unpacker_init(unpacker) 51 52 unpacker_goto(unpacker, payload, payload:len() - 1) 53 local finished = unpacker_advance(unpacker) 54 eq(false, finished) 55 56 unpacker[0].read_size = unpacker[0].read_size + 1 57 finished = unpacker_advance(unpacker) 58 eq(true, finished) 59 end 60 ) 61 62 itp('does not crash when parsing grid_line event with 0 `cells` #25184', function() 63 local unpacker = make_unpacker() 64 lib.unpacker_init(unpacker) 65 66 unpacker_goto( 67 unpacker, 68 -- [kMessageTypeNotification, "redraw", [ 69 -- ["grid_line", 70 -- [2, 0, 0, [], false] 71 -- ] 72 -- ]] 73 '\x93\x02\xa6\x72\x65\x64\x72\x61\x77\x91\x92\xa9\x67\x72\x69\x64\x5f\x6c\x69\x6e\x65\x95\x02\x00\x00\x90\xc2' 74 ) 75 local finished = unpacker_advance(unpacker) 76 eq(true, finished) 77 end) 78 end) 79 end)