test.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #!/usr/bin/env lua
  2. -- Lua CJSON tests
  3. --
  4. -- Mark Pulford <mark@kyne.com.au>
  5. --
  6. -- Note: The output of this script is easier to read with "less -S"
  7. local json = require "cjson"
  8. local json_safe = require "cjson.safe"
  9. local util = require "cjson.util"
  10. local function gen_raw_octets()
  11. local chars = {}
  12. for i = 0, 255 do chars[i + 1] = string.char(i) end
  13. return table.concat(chars)
  14. end
  15. -- Generate every UTF-16 codepoint, including supplementary codes
  16. local function gen_utf16_escaped()
  17. -- Create raw table escapes
  18. local utf16_escaped = {}
  19. local count = 0
  20. local function append_escape(code)
  21. local esc = ('\\u%04X'):format(code)
  22. table.insert(utf16_escaped, esc)
  23. end
  24. table.insert(utf16_escaped, '"')
  25. for i = 0, 0xD7FF do
  26. append_escape(i)
  27. end
  28. -- Skip 0xD800 - 0xDFFF since they are used to encode supplementary
  29. -- codepoints
  30. for i = 0xE000, 0xFFFF do
  31. append_escape(i)
  32. end
  33. -- Append surrogate pair for each supplementary codepoint
  34. for high = 0xD800, 0xDBFF do
  35. for low = 0xDC00, 0xDFFF do
  36. append_escape(high)
  37. append_escape(low)
  38. end
  39. end
  40. table.insert(utf16_escaped, '"')
  41. return table.concat(utf16_escaped)
  42. end
  43. function load_testdata()
  44. local data = {}
  45. -- Data for 8bit raw <-> escaped octets tests
  46. data.octets_raw = gen_raw_octets()
  47. data.octets_escaped = util.file_load("octets-escaped.dat")
  48. -- Data for \uXXXX -> UTF-8 test
  49. data.utf16_escaped = gen_utf16_escaped()
  50. -- Load matching data for utf16_escaped
  51. local utf8_loaded
  52. utf8_loaded, data.utf8_raw = pcall(util.file_load, "utf8.dat")
  53. if not utf8_loaded then
  54. data.utf8_raw = "Failed to load utf8.dat - please run genutf8.pl"
  55. end
  56. data.table_cycle = {}
  57. data.table_cycle[1] = data.table_cycle
  58. local big = {}
  59. for i = 1, 1100 do
  60. big = { { 10, false, true, json.null }, "string", a = big }
  61. end
  62. data.deeply_nested_data = big
  63. return data
  64. end
  65. function test_decode_cycle(filename)
  66. local obj1 = json.decode(util.file_load(filename))
  67. local obj2 = json.decode(json.encode(obj1))
  68. return util.compare_values(obj1, obj2)
  69. end
  70. -- Set up data used in tests
  71. local Inf = math.huge;
  72. local NaN = math.huge * 0;
  73. local testdata = load_testdata()
  74. local cjson_tests = {
  75. -- Test API variables
  76. { "Check module name, version",
  77. function () return json._NAME, json._VERSION end, { },
  78. true, { "cjson", "2.1devel" } },
  79. -- Test decoding simple types
  80. { "Decode string",
  81. json.decode, { '"test string"' }, true, { "test string" } },
  82. { "Decode numbers",
  83. json.decode, { '[ 0.0, -5e3, -1, 0.3e-3, 1023.2, 0e10 ]' },
  84. true, { { 0.0, -5000, -1, 0.0003, 1023.2, 0 } } },
  85. { "Decode null",
  86. json.decode, { 'null' }, true, { json.null } },
  87. { "Decode true",
  88. json.decode, { 'true' }, true, { true } },
  89. { "Decode false",
  90. json.decode, { 'false' }, true, { false } },
  91. { "Decode object with numeric keys",
  92. json.decode, { '{ "1": "one", "3": "three" }' },
  93. true, { { ["1"] = "one", ["3"] = "three" } } },
  94. { "Decode object with string keys",
  95. json.decode, { '{ "a": "a", "b": "b" }' },
  96. true, { { a = "a", b = "b" } } },
  97. { "Decode array",
  98. json.decode, { '[ "one", null, "three" ]' },
  99. true, { { "one", json.null, "three" } } },
  100. -- Test decoding errors
  101. { "Decode UTF-16BE [throw error]",
  102. json.decode, { '\0"\0"' },
  103. false, { "JSON parser does not support UTF-16 or UTF-32" } },
  104. { "Decode UTF-16LE [throw error]",
  105. json.decode, { '"\0"\0' },
  106. false, { "JSON parser does not support UTF-16 or UTF-32" } },
  107. { "Decode UTF-32BE [throw error]",
  108. json.decode, { '\0\0\0"' },
  109. false, { "JSON parser does not support UTF-16 or UTF-32" } },
  110. { "Decode UTF-32LE [throw error]",
  111. json.decode, { '"\0\0\0' },
  112. false, { "JSON parser does not support UTF-16 or UTF-32" } },
  113. { "Decode partial JSON [throw error]",
  114. json.decode, { '{ "unexpected eof": ' },
  115. false, { "Expected value but found T_END at character 21" } },
  116. { "Decode with extra comma [throw error]",
  117. json.decode, { '{ "extra data": true }, false' },
  118. false, { "Expected the end but found T_COMMA at character 23" } },
  119. { "Decode invalid escape code [throw error]",
  120. json.decode, { [[ { "bad escape \q code" } ]] },
  121. false, { "Expected object key string but found invalid escape code at character 16" } },
  122. { "Decode invalid unicode escape [throw error]",
  123. json.decode, { [[ { "bad unicode \u0f6 escape" } ]] },
  124. false, { "Expected object key string but found invalid unicode escape code at character 17" } },
  125. { "Decode invalid keyword [throw error]",
  126. json.decode, { ' [ "bad barewood", test ] ' },
  127. false, { "Expected value but found invalid token at character 20" } },
  128. { "Decode invalid number #1 [throw error]",
  129. json.decode, { '[ -+12 ]' },
  130. false, { "Expected value but found invalid number at character 3" } },
  131. { "Decode invalid number #2 [throw error]",
  132. json.decode, { '-v' },
  133. false, { "Expected value but found invalid number at character 1" } },
  134. { "Decode invalid number exponent [throw error]",
  135. json.decode, { '[ 0.4eg10 ]' },
  136. false, { "Expected comma or array end but found invalid token at character 6" } },
  137. -- Test decoding nested arrays / objects
  138. { "Set decode_max_depth(5)",
  139. json.decode_max_depth, { 5 }, true, { 5 } },
  140. { "Decode array at nested limit",
  141. json.decode, { '[[[[[ "nested" ]]]]]' },
  142. true, { {{{{{ "nested" }}}}} } },
  143. { "Decode array over nested limit [throw error]",
  144. json.decode, { '[[[[[[ "nested" ]]]]]]' },
  145. false, { "Found too many nested data structures (6) at character 6" } },
  146. { "Decode object at nested limit",
  147. json.decode, { '{"a":{"b":{"c":{"d":{"e":"nested"}}}}}' },
  148. true, { {a={b={c={d={e="nested"}}}}} } },
  149. { "Decode object over nested limit [throw error]",
  150. json.decode, { '{"a":{"b":{"c":{"d":{"e":{"f":"nested"}}}}}}' },
  151. false, { "Found too many nested data structures (6) at character 26" } },
  152. { "Set decode_max_depth(1000)",
  153. json.decode_max_depth, { 1000 }, true, { 1000 } },
  154. { "Decode deeply nested array [throw error]",
  155. json.decode, { string.rep("[", 1100) .. '1100' .. string.rep("]", 1100)},
  156. false, { "Found too many nested data structures (1001) at character 1001" } },
  157. -- Test encoding nested tables
  158. { "Set encode_max_depth(5)",
  159. json.encode_max_depth, { 5 }, true, { 5 } },
  160. { "Encode nested table as array at nested limit",
  161. json.encode, { {{{{{"nested"}}}}} }, true, { '[[[[["nested"]]]]]' } },
  162. { "Encode nested table as array after nested limit [throw error]",
  163. json.encode, { { {{{{{"nested"}}}}} } },
  164. false, { "Cannot serialise, excessive nesting (6)" } },
  165. { "Encode nested table as object at nested limit",
  166. json.encode, { {a={b={c={d={e="nested"}}}}} },
  167. true, { '{"a":{"b":{"c":{"d":{"e":"nested"}}}}}' } },
  168. { "Encode nested table as object over nested limit [throw error]",
  169. json.encode, { {a={b={c={d={e={f="nested"}}}}}} },
  170. false, { "Cannot serialise, excessive nesting (6)" } },
  171. { "Encode table with cycle [throw error]",
  172. json.encode, { testdata.table_cycle },
  173. false, { "Cannot serialise, excessive nesting (6)" } },
  174. { "Set encode_max_depth(1000)",
  175. json.encode_max_depth, { 1000 }, true, { 1000 } },
  176. { "Encode deeply nested data [throw error]",
  177. json.encode, { testdata.deeply_nested_data },
  178. false, { "Cannot serialise, excessive nesting (1001)" } },
  179. -- Test encoding simple types
  180. { "Encode null",
  181. json.encode, { json.null }, true, { 'null' } },
  182. { "Encode true",
  183. json.encode, { true }, true, { 'true' } },
  184. { "Encode false",
  185. json.encode, { false }, true, { 'false' } },
  186. { "Encode empty object",
  187. json.encode, { { } }, true, { '{}' } },
  188. { "Encode integer",
  189. json.encode, { 10 }, true, { '10' } },
  190. { "Encode string",
  191. json.encode, { "hello" }, true, { '"hello"' } },
  192. { "Encode Lua function [throw error]",
  193. json.encode, { function () end },
  194. false, { "Cannot serialise function: type not supported" } },
  195. -- Test decoding invalid numbers
  196. { "Set decode_invalid_numbers(true)",
  197. json.decode_invalid_numbers, { true }, true, { true } },
  198. { "Decode hexadecimal",
  199. json.decode, { '0x6.ffp1' }, true, { 13.9921875 } },
  200. { "Decode numbers with leading zero",
  201. json.decode, { '[ 0123, 00.33 ]' }, true, { { 123, 0.33 } } },
  202. { "Decode +-Inf",
  203. json.decode, { '[ +Inf, Inf, -Inf ]' }, true, { { Inf, Inf, -Inf } } },
  204. { "Decode +-Infinity",
  205. json.decode, { '[ +Infinity, Infinity, -Infinity ]' },
  206. true, { { Inf, Inf, -Inf } } },
  207. { "Decode +-NaN",
  208. json.decode, { '[ +NaN, NaN, -NaN ]' }, true, { { NaN, NaN, NaN } } },
  209. { "Decode Infrared (not infinity) [throw error]",
  210. json.decode, { 'Infrared' },
  211. false, { "Expected the end but found invalid token at character 4" } },
  212. { "Decode Noodle (not NaN) [throw error]",
  213. json.decode, { 'Noodle' },
  214. false, { "Expected value but found invalid token at character 1" } },
  215. { "Set decode_invalid_numbers(false)",
  216. json.decode_invalid_numbers, { false }, true, { false } },
  217. { "Decode hexadecimal [throw error]",
  218. json.decode, { '0x6' },
  219. false, { "Expected value but found invalid number at character 1" } },
  220. { "Decode numbers with leading zero [throw error]",
  221. json.decode, { '[ 0123, 00.33 ]' },
  222. false, { "Expected value but found invalid number at character 3" } },
  223. { "Decode +-Inf [throw error]",
  224. json.decode, { '[ +Inf, Inf, -Inf ]' },
  225. false, { "Expected value but found invalid token at character 3" } },
  226. { "Decode +-Infinity [throw error]",
  227. json.decode, { '[ +Infinity, Infinity, -Infinity ]' },
  228. false, { "Expected value but found invalid token at character 3" } },
  229. { "Decode +-NaN [throw error]",
  230. json.decode, { '[ +NaN, NaN, -NaN ]' },
  231. false, { "Expected value but found invalid token at character 3" } },
  232. { 'Set decode_invalid_numbers("on")',
  233. json.decode_invalid_numbers, { "on" }, true, { true } },
  234. -- Test encoding invalid numbers
  235. { "Set encode_invalid_numbers(false)",
  236. json.encode_invalid_numbers, { false }, true, { false } },
  237. { "Encode NaN [throw error]",
  238. json.encode, { NaN },
  239. false, { "Cannot serialise number: must not be NaN or Infinity" } },
  240. { "Encode Infinity [throw error]",
  241. json.encode, { Inf },
  242. false, { "Cannot serialise number: must not be NaN or Infinity" } },
  243. { "Set encode_invalid_numbers(\"null\")",
  244. json.encode_invalid_numbers, { "null" }, true, { "null" } },
  245. { "Encode NaN as null",
  246. json.encode, { NaN }, true, { "null" } },
  247. { "Encode Infinity as null",
  248. json.encode, { Inf }, true, { "null" } },
  249. { "Set encode_invalid_numbers(true)",
  250. json.encode_invalid_numbers, { true }, true, { true } },
  251. { "Encode NaN",
  252. json.encode, { NaN }, true, { "NaN" } },
  253. { "Encode +Infinity",
  254. json.encode, { Inf }, true, { "Infinity" } },
  255. { "Encode -Infinity",
  256. json.encode, { -Inf }, true, { "-Infinity" } },
  257. { 'Set encode_invalid_numbers("off")',
  258. json.encode_invalid_numbers, { "off" }, true, { false } },
  259. -- Test encoding tables
  260. { "Set encode_sparse_array(true, 2, 3)",
  261. json.encode_sparse_array, { true, 2, 3 }, true, { true, 2, 3 } },
  262. { "Encode sparse table as array #1",
  263. json.encode, { { [3] = "sparse test" } },
  264. true, { '[null,null,"sparse test"]' } },
  265. { "Encode sparse table as array #2",
  266. json.encode, { { [1] = "one", [4] = "sparse test" } },
  267. true, { '["one",null,null,"sparse test"]' } },
  268. { "Encode sparse array as object",
  269. json.encode, { { [1] = "one", [5] = "sparse test" } },
  270. true, { '{"1":"one","5":"sparse test"}' } },
  271. { "Encode table with numeric string key as object",
  272. json.encode, { { ["2"] = "numeric string key test" } },
  273. true, { '{"2":"numeric string key test"}' } },
  274. { "Set encode_sparse_array(false)",
  275. json.encode_sparse_array, { false }, true, { false, 2, 3 } },
  276. { "Encode table with incompatible key [throw error]",
  277. json.encode, { { [false] = "wrong" } },
  278. false, { "Cannot serialise boolean: table key must be a number or string" } },
  279. -- Test escaping
  280. { "Encode all octets (8-bit clean)",
  281. json.encode, { testdata.octets_raw }, true, { testdata.octets_escaped } },
  282. { "Decode all escaped octets",
  283. json.decode, { testdata.octets_escaped }, true, { testdata.octets_raw } },
  284. { "Decode single UTF-16 escape",
  285. json.decode, { [["\uF800"]] }, true, { "\239\160\128" } },
  286. { "Decode all UTF-16 escapes (including surrogate combinations)",
  287. json.decode, { testdata.utf16_escaped }, true, { testdata.utf8_raw } },
  288. { "Decode swapped surrogate pair [throw error]",
  289. json.decode, { [["\uDC00\uD800"]] },
  290. false, { "Expected value but found invalid unicode escape code at character 2" } },
  291. { "Decode duplicate high surrogate [throw error]",
  292. json.decode, { [["\uDB00\uDB00"]] },
  293. false, { "Expected value but found invalid unicode escape code at character 2" } },
  294. { "Decode duplicate low surrogate [throw error]",
  295. json.decode, { [["\uDB00\uDB00"]] },
  296. false, { "Expected value but found invalid unicode escape code at character 2" } },
  297. { "Decode missing low surrogate [throw error]",
  298. json.decode, { [["\uDB00"]] },
  299. false, { "Expected value but found invalid unicode escape code at character 2" } },
  300. { "Decode invalid low surrogate [throw error]",
  301. json.decode, { [["\uDB00\uD"]] },
  302. false, { "Expected value but found invalid unicode escape code at character 2" } },
  303. -- Test locale support
  304. --
  305. -- The standard Lua interpreter is ANSI C online doesn't support locales
  306. -- by default. Force a known problematic locale to test strtod()/sprintf().
  307. { "Set locale to cs_CZ (comma separator)", function ()
  308. os.setlocale("cs_CZ")
  309. json.new()
  310. end },
  311. { "Encode number under comma locale",
  312. json.encode, { 1.5 }, true, { '1.5' } },
  313. { "Decode number in array under comma locale",
  314. json.decode, { '[ 10, "test" ]' }, true, { { 10, "test" } } },
  315. { "Revert locale to POSIX", function ()
  316. os.setlocale("C")
  317. json.new()
  318. end },
  319. -- Test encode_keep_buffer() and enable_number_precision()
  320. { "Set encode_keep_buffer(false)",
  321. json.encode_keep_buffer, { false }, true, { false } },
  322. { "Set encode_number_precision(3)",
  323. json.encode_number_precision, { 3 }, true, { 3 } },
  324. { "Encode number with precision 3",
  325. json.encode, { 1/3 }, true, { "0.333" } },
  326. { "Set encode_number_precision(14)",
  327. json.encode_number_precision, { 14 }, true, { 14 } },
  328. { "Set encode_keep_buffer(true)",
  329. json.encode_keep_buffer, { true }, true, { true } },
  330. -- Test config API errors
  331. -- Function is listed as '?' due to pcall
  332. { "Set encode_number_precision(0) [throw error]",
  333. json.encode_number_precision, { 0 },
  334. false, { "bad argument #1 to '?' (expected integer between 1 and 14)" } },
  335. { "Set encode_number_precision(\"five\") [throw error]",
  336. json.encode_number_precision, { "five" },
  337. false, { "bad argument #1 to '?' (number expected, got string)" } },
  338. { "Set encode_keep_buffer(nil, true) [throw error]",
  339. json.encode_keep_buffer, { nil, true },
  340. false, { "bad argument #2 to '?' (found too many arguments)" } },
  341. { "Set encode_max_depth(\"wrong\") [throw error]",
  342. json.encode_max_depth, { "wrong" },
  343. false, { "bad argument #1 to '?' (number expected, got string)" } },
  344. { "Set decode_max_depth(0) [throw error]",
  345. json.decode_max_depth, { "0" },
  346. false, { "bad argument #1 to '?' (expected integer between 1 and 2147483647)" } },
  347. { "Set encode_invalid_numbers(-2) [throw error]",
  348. json.encode_invalid_numbers, { -2 },
  349. false, { "bad argument #1 to '?' (invalid option '-2')" } },
  350. { "Set decode_invalid_numbers(true, false) [throw error]",
  351. json.decode_invalid_numbers, { true, false },
  352. false, { "bad argument #2 to '?' (found too many arguments)" } },
  353. { "Set encode_sparse_array(\"not quite on\") [throw error]",
  354. json.encode_sparse_array, { "not quite on" },
  355. false, { "bad argument #1 to '?' (invalid option 'not quite on')" } },
  356. { "Reset Lua CJSON configuration", function () json = json.new() end },
  357. -- Wrap in a function to ensure the table returned by json.new() is used
  358. { "Check encode_sparse_array()",
  359. function (...) return json.encode_sparse_array(...) end, { },
  360. true, { false, 2, 10 } },
  361. { "Encode (safe) simple value",
  362. json_safe.encode, { true },
  363. true, { "true" } },
  364. { "Encode (safe) argument validation [throw error]",
  365. json_safe.encode, { "arg1", "arg2" },
  366. false, { "bad argument #1 to '?' (expected 1 argument)" } },
  367. { "Decode (safe) error generation",
  368. json_safe.decode, { "Oops" },
  369. true, { nil, "Expected value but found invalid token at character 1" } },
  370. { "Decode (safe) error generation after new()",
  371. function(...) return json_safe.new().decode(...) end, { "Oops" },
  372. true, { nil, "Expected value but found invalid token at character 1" } },
  373. }
  374. print(("==> Testing Lua CJSON version %s\n"):format(json._VERSION))
  375. util.run_test_group(cjson_tests)
  376. for _, filename in ipairs(arg) do
  377. util.run_test("Decode cycle " .. filename, test_decode_cycle, { filename },
  378. true, { true })
  379. end
  380. local pass, total = util.run_test_summary()
  381. if pass == total then
  382. print("==> Summary: all tests succeeded")
  383. else
  384. print(("==> Summary: %d/%d tests failed"):format(total - pass, total))
  385. os.exit(1)
  386. end
  387. -- vi:ai et sw=4 ts=4: