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