test.lua 17 KB

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