msgpack_spec.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. --- @class Unpacker
  9. --- @field read_ptr ffi.cdata*
  10. --- @field read_size number
  11. --- @alias Unpacker* table<number, Unpacker>
  12. --- @return Unpacker* unpacker `unpacker[0]` to dereference
  13. local function make_unpacker()
  14. return ffi.gc(ffi.cast('Unpacker*', lib.xcalloc(1, ffi.sizeof('Unpacker'))), function(unpacker)
  15. lib.unpacker_teardown(unpacker, nil, nil)
  16. lib.xfree(unpacker)
  17. end)
  18. end
  19. --- @param unpacker Unpacker*
  20. --- @param data string
  21. --- @param size number? *default: data:len()*
  22. local function unpacker_goto(unpacker, data, size)
  23. unpacker[0].read_ptr = to_cstr(data)
  24. unpacker[0].read_size = size or data:len()
  25. end
  26. --- @param unpacker Unpacker*
  27. --- @return boolean
  28. local function unpacker_advance(unpacker)
  29. return lib.unpacker_advance(unpacker)
  30. end
  31. describe('msgpack', function()
  32. describe('unpacker', function()
  33. itp(
  34. 'does not crash when paused between `cells` and `wrap` params of `grid_line` #25184',
  35. function()
  36. -- [kMessageTypeNotification, "redraw", [
  37. -- ["grid_line",
  38. -- [2, 0, 0, [[" " , 0, 77]], false]
  39. -- ]
  40. -- ]]
  41. local payload =
  42. '\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'
  43. local unpacker = make_unpacker()
  44. lib.unpacker_init(unpacker)
  45. unpacker_goto(unpacker, payload, payload:len() - 1)
  46. local finished = unpacker_advance(unpacker)
  47. eq(false, finished)
  48. unpacker[0].read_size = unpacker[0].read_size + 1
  49. finished = unpacker_advance(unpacker)
  50. eq(true, finished)
  51. end
  52. )
  53. itp('does not crash when parsing grid_line event with 0 `cells` #25184', function()
  54. local unpacker = make_unpacker()
  55. lib.unpacker_init(unpacker)
  56. unpacker_goto(
  57. unpacker,
  58. -- [kMessageTypeNotification, "redraw", [
  59. -- ["grid_line",
  60. -- [2, 0, 0, [], false]
  61. -- ]
  62. -- ]]
  63. '\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'
  64. )
  65. local finished = unpacker_advance(unpacker)
  66. eq(true, finished)
  67. end)
  68. end)
  69. end)