decode_spec.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. local helpers = require('test.unit.helpers')(after_each)
  2. local itp = helpers.gen_itp(it)
  3. local cimport = helpers.cimport
  4. local eq = helpers.eq
  5. local neq = helpers.neq
  6. local ffi = helpers.ffi
  7. local decode = cimport('./src/nvim/eval/decode.h', './src/nvim/eval/typval.h',
  8. './src/nvim/globals.h', './src/nvim/memory.h',
  9. './src/nvim/message.h')
  10. describe('json_decode_string()', function()
  11. local char = function(c)
  12. return ffi.gc(decode.xmemdup(c, 1), decode.xfree)
  13. end
  14. itp('does not overflow when running with `n…`, `t…`, `f…`', function()
  15. local rettv = ffi.new('typval_T', {v_type=decode.VAR_UNKNOWN})
  16. decode.emsg_silent = 1
  17. -- This will not crash, but if `len` argument will be ignored it will parse
  18. -- `null` as `null` and if not it will parse `null` as `n`.
  19. eq(0, decode.json_decode_string('null', 1, rettv))
  20. eq(decode.VAR_UNKNOWN, rettv.v_type)
  21. eq(0, decode.json_decode_string('true', 1, rettv))
  22. eq(decode.VAR_UNKNOWN, rettv.v_type)
  23. eq(0, decode.json_decode_string('false', 1, rettv))
  24. eq(decode.VAR_UNKNOWN, rettv.v_type)
  25. eq(0, decode.json_decode_string('null', 2, rettv))
  26. eq(decode.VAR_UNKNOWN, rettv.v_type)
  27. eq(0, decode.json_decode_string('true', 2, rettv))
  28. eq(decode.VAR_UNKNOWN, rettv.v_type)
  29. eq(0, decode.json_decode_string('false', 2, rettv))
  30. eq(decode.VAR_UNKNOWN, rettv.v_type)
  31. eq(0, decode.json_decode_string('null', 3, rettv))
  32. eq(decode.VAR_UNKNOWN, rettv.v_type)
  33. eq(0, decode.json_decode_string('true', 3, rettv))
  34. eq(decode.VAR_UNKNOWN, rettv.v_type)
  35. eq(0, decode.json_decode_string('false', 3, rettv))
  36. eq(decode.VAR_UNKNOWN, rettv.v_type)
  37. eq(0, decode.json_decode_string('false', 4, rettv))
  38. eq(decode.VAR_UNKNOWN, rettv.v_type)
  39. end)
  40. itp('does not overflow and crash when running with `n`, `t`, `f`', function()
  41. local rettv = ffi.new('typval_T', {v_type=decode.VAR_UNKNOWN})
  42. decode.emsg_silent = 1
  43. eq(0, decode.json_decode_string(char('n'), 1, rettv))
  44. eq(decode.VAR_UNKNOWN, rettv.v_type)
  45. eq(0, decode.json_decode_string(char('t'), 1, rettv))
  46. eq(decode.VAR_UNKNOWN, rettv.v_type)
  47. eq(0, decode.json_decode_string(char('f'), 1, rettv))
  48. eq(decode.VAR_UNKNOWN, rettv.v_type)
  49. end)
  50. itp('does not overflow when running with `"…`', function()
  51. local rettv = ffi.new('typval_T', {v_type=decode.VAR_UNKNOWN})
  52. decode.emsg_silent = 1
  53. eq(0, decode.json_decode_string('"t"', 2, rettv))
  54. eq(decode.VAR_UNKNOWN, rettv.v_type)
  55. eq(0, decode.json_decode_string('""', 1, rettv))
  56. eq(decode.VAR_UNKNOWN, rettv.v_type)
  57. end)
  58. local check_failure = function(s, len, msg)
  59. local rettv = ffi.new('typval_T', {v_type=decode.VAR_UNKNOWN})
  60. eq(0, decode.json_decode_string(s, len, rettv))
  61. eq(decode.VAR_UNKNOWN, rettv.v_type)
  62. neq(nil, decode.last_msg_hist)
  63. eq(msg, ffi.string(decode.last_msg_hist.msg))
  64. end
  65. itp('does not overflow in error messages', function()
  66. collectgarbage('restart')
  67. check_failure(']test', 1, 'E474: No container to close: ]')
  68. check_failure('[}test', 2, 'E474: Closing list with curly bracket: }')
  69. check_failure('{]test', 2,
  70. 'E474: Closing dictionary with square bracket: ]')
  71. check_failure('[1,]test', 4, 'E474: Trailing comma: ]')
  72. check_failure('{"1":}test', 6, 'E474: Expected value after colon: }')
  73. check_failure('{"1"}test', 5, 'E474: Expected value: }')
  74. check_failure(',test', 1, 'E474: Comma not inside container: ,')
  75. check_failure('[1,,1]test', 6, 'E474: Duplicate comma: ,1]')
  76. check_failure('{"1":,}test', 7, 'E474: Comma after colon: ,}')
  77. check_failure('{"1",}test', 6, 'E474: Using comma in place of colon: ,}')
  78. check_failure('{,}test', 3, 'E474: Leading comma: ,}')
  79. check_failure('[,]test', 3, 'E474: Leading comma: ,]')
  80. check_failure(':test', 1, 'E474: Colon not inside container: :')
  81. check_failure('[:]test', 3, 'E474: Using colon not in dictionary: :]')
  82. check_failure('{:}test', 3, 'E474: Unexpected colon: :}')
  83. check_failure('{"1"::1}test', 8, 'E474: Duplicate colon: :1}')
  84. check_failure('ntest', 1, 'E474: Expected null: n')
  85. check_failure('ttest', 1, 'E474: Expected true: t')
  86. check_failure('ftest', 1, 'E474: Expected false: f')
  87. check_failure('"\\test', 2, 'E474: Unfinished escape sequence: "\\')
  88. check_failure('"\\u"test', 4,
  89. 'E474: Unfinished unicode escape sequence: "\\u"')
  90. check_failure('"\\uXXXX"est', 8,
  91. 'E474: Expected four hex digits after \\u: \\uXXXX"')
  92. check_failure('"\\?"test', 4, 'E474: Unknown escape sequence: \\?"')
  93. check_failure(
  94. '"\t"test', 3,
  95. 'E474: ASCII control characters cannot be present inside string: \t"')
  96. check_failure('"\194"test', 3, 'E474: Only UTF-8 strings allowed: \194"')
  97. check_failure('"\252\144\128\128\128\128"test', 8, 'E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \252\144\128\128\128\128"')
  98. check_failure('"test', 1, 'E474: Expected string end: "')
  99. check_failure('-test', 1, 'E474: Missing number after minus sign: -')
  100. check_failure('-1.test', 3, 'E474: Missing number after decimal dot: -1.')
  101. check_failure('-1.0etest', 5, 'E474: Missing exponent: -1.0e')
  102. check_failure('?test', 1, 'E474: Unidentified byte: ?')
  103. check_failure('1?test', 2, 'E474: Trailing characters: ?')
  104. check_failure('[1test', 2, 'E474: Unexpected end of input: [1')
  105. end)
  106. itp('does not overflow with `-`', function()
  107. check_failure('-0', 1, 'E474: Missing number after minus sign: -')
  108. end)
  109. itp('does not overflow and crash when running with `"`', function()
  110. local rettv = ffi.new('typval_T', {v_type=decode.VAR_UNKNOWN})
  111. decode.emsg_silent = 1
  112. eq(0, decode.json_decode_string(char('"'), 1, rettv))
  113. eq(decode.VAR_UNKNOWN, rettv.v_type)
  114. end)
  115. end)