robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.unit.testutil' ) 2local describe =t . describe 3local cimport =t . cimport 4local itp =t . gen_itp ( t . it ) 5local lib = 6cimport ( './src/nvim/msgpack_rpc/unpacker.h' , './src/nvim/memory.h' , './src/mpack/conv.h' ) 7local ffi =t . ffi 8local eq =t . eq 9local to_cstr =t . to_cstr 10 11--- @class Unpacker 12--- @field read_ptr ffi.cdata* 13--- @field read_size number 14 15--- @alias Unpacker* table<number, Unpacker> 16--- @return Unpacker* unpacker `unpacker[0]` to dereference 17local function make_unpacker () 18return ffi . gc ( ffi . cast ( 'Unpacker*' , lib . xcalloc ( 1 , ffi . sizeof ( 'Unpacker' ))), function ( unpacker ) 19lib . unpacker_teardown ( unpacker ) 20lib . xfree ( unpacker ) 21end ) 22end 23 24--- @param unpacker Unpacker* 25--- @param data string 26--- @param size number? *default: data:len()* 27local function unpacker_goto ( unpacker , data , size ) 28unpacker [ 0 ]. read_ptr =to_cstr ( data ) 29unpacker [ 0 ]. read_size =size or data : len () 30end 31 32--- @param unpacker Unpacker* 33--- @return boolean 34local function unpacker_advance ( unpacker ) 35return lib . unpacker_advance ( unpacker ) 36end 37 38describe ( 'msgpack' , function () 39describe ( 'unpacker' , function () 40itp ( 41'does not crash when paused between `cells` and `wrap` params of `grid_line` #25184' , 42function () 43-- [kMessageTypeNotification, "redraw", [ 44-- ["grid_line", 45-- [2, 0, 0, [[" " , 0, 77]], false] 46-- ] 47-- ]] 48local payload = 49'\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' 50 51local unpacker =make_unpacker () 52lib . unpacker_init ( unpacker ) 53 54unpacker_goto ( unpacker , payload , payload : len () - 1 ) 55local finished =unpacker_advance ( unpacker ) 56eq ( false , finished ) 57 58unpacker [ 0 ]. read_size =unpacker [ 0 ]. read_size + 1 59finished =unpacker_advance ( unpacker ) 60eq ( true , finished ) 61end 62) 63 64itp ( 'does not crash when parsing grid_line event with 0 `cells` #25184' , function () 65local unpacker =make_unpacker () 66lib . unpacker_init ( unpacker ) 67 68unpacker_goto ( 69unpacker , 70-- [kMessageTypeNotification, "redraw", [ 71-- ["grid_line", 72-- [2, 0, 0, [], false] 73-- ] 74-- ]] 75'\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' 76) 77local finished =unpacker_advance ( unpacker ) 78eq ( true , finished ) 79end ) 80end ) 81end ) 82 83describe ( 'libmpack' , function () 84describe ( 'mpack_pack_number' , function () 85itp ( 'token length at negative integer boundaries #37202' , function () 86-- tok.length is the byte count for the integer value (not including the msgpack type prefix byte). 87 88-- int8 min: -128 → 1 byte 89local tok =lib . mpack_pack_number ( - 128 ) 90eq ( lib . MPACK_TOKEN_SINT , tok . type ) 91eq ( 1 , tok . length ) 92 93-- one past int8: -129 → 2 bytes (int16) 94tok =lib . mpack_pack_number ( - 129 ) 95eq ( lib . MPACK_TOKEN_SINT , tok . type ) 96eq ( 2 , tok . length ) 97 98-- int16 min: -32768 → 2 bytes 99tok =lib . mpack_pack_number ( - 32768 ) 100eq ( lib . MPACK_TOKEN_SINT , tok . type ) 101eq ( 2 , tok . length ) 102 103-- one past int16: -32769 → 4 bytes (int32) 104tok =lib . mpack_pack_number ( - 32769 ) 105eq ( lib . MPACK_TOKEN_SINT , tok . type ) 106eq ( 4 , tok . length ) 107end ) 108end ) 109end )