You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.1 KiB

  1. #! /usr/bin/env escript
  2. % This file is part of Jiffy released under the MIT license.
  3. % See the LICENSE file for more information.
  4. main([]) ->
  5. code:add_pathz("ebin"),
  6. code:add_pathz("test"),
  7. etap:plan(87),
  8. util:test_good(good()),
  9. util:test_good(uescaped(), [uescape]),
  10. util:test_errors(errors()),
  11. test_utf8(utf8_cases()),
  12. etap:end_tests().
  13. good() ->
  14. [
  15. {<<"\"\"">>, <<"">>},
  16. {<<"\"0\"">>, <<"0">>},
  17. {<<"\"foo\"">>, <<"foo">>},
  18. {<<"\"\\\"foobar\\\"\"">>, <<"\"foobar\"">>},
  19. {<<"\"\\n\\n\\n\"">>, <<"\n\n\n">>},
  20. {<<"\"\\\" \\b\\f\\r\\n\\t\\\"\"">>, <<"\" \b\f\r\n\t\"">>},
  21. {<<"\"foo\\u0005bar\"">>, <<"foo", 5, "bar">>},
  22. {
  23. <<"\"\\uD834\\uDD1E\"">>,
  24. <<240, 157, 132, 158>>,
  25. <<34, 240, 157, 132, 158, 34>>
  26. }
  27. ].
  28. uescaped() ->
  29. [
  30. {
  31. <<"\"\\u8CA8\\u5481\\u3002\\u0091\\u0091\"">>,
  32. <<232,178,168,229,146,129,227,128,130,194,145,194,145>>
  33. },
  34. {
  35. <<"\"\\uD834\\uDD1E\"">>,
  36. <<240, 157, 132, 158>>
  37. },
  38. {
  39. <<"\"\\uD83D\\uDE0A\"">>,
  40. <<240, 159, 152, 138>>
  41. }
  42. ].
  43. errors() ->
  44. [
  45. <<"\"foo">>,
  46. <<"\"", 0, "\"">>,
  47. <<"\"\\g\"">>,
  48. <<"\"\\uFFFF\"">>,
  49. <<"\"\\uFFFE\"">>,
  50. <<"\"\\uD834foo\\uDD1E\"">>,
  51. % CouchDB-345
  52. <<34,78,69,73,77,69,78,32,70,216,82,82,32,70,65,69,78,33,34>>
  53. ].
  54. test_utf8([]) ->
  55. ok;
  56. test_utf8([Case | Rest]) ->
  57. etap:fun_is(
  58. fun({error, invalid_string}) -> true; (Else) -> Else end,
  59. (catch jiffy:encode(Case)),
  60. lists:flatten(io_lib:format("Invalid utf-8: ~p", [Case]))
  61. ),
  62. Case2 = <<34, Case/binary, 34>>,
  63. etap:fun_is(
  64. fun({error, {_, invalid_string}}) -> true; (Else) -> Else end,
  65. (catch jiffy:decode(Case2)),
  66. lists:flatten(io_lib:format("Invalid utf-8: ~p", [Case2]))
  67. ),
  68. test_utf8(Rest).
  69. utf8_cases() ->
  70. [
  71. % Stray continuation byte
  72. <<16#C2, 16#81, 16#80>>,
  73. <<"foo", 16#80, "bar">>,
  74. % Invalid Unicode code points
  75. <<239, 191, 190>>,
  76. <<237, 160, 129>>,
  77. % Not enough extension bytes
  78. <<16#C0>>,
  79. <<16#E0>>,
  80. <<16#E0, 16#80>>,
  81. <<16#F0>>,
  82. <<16#F0, 16#80>>,
  83. <<16#F0, 16#80, 16#80>>,
  84. <<16#F8>>,
  85. <<16#F8, 16#80>>,
  86. <<16#F8, 16#80, 16#80>>,
  87. <<16#F8, 16#80, 16#80, 16#80>>,
  88. <<16#FC>>,
  89. <<16#FC, 16#80>>,
  90. <<16#FC, 16#80, 16#80>>,
  91. <<16#FC, 16#80, 16#80, 16#80>>,
  92. <<16#FC, 16#80, 16#80, 16#80, 16#80>>,
  93. % No data in high bits.
  94. <<16#C0, 16#80>>,
  95. <<16#C1, 16#80>>,
  96. <<16#E0, 16#80, 16#80>>,
  97. <<16#E0, 16#90, 16#80>>,
  98. <<16#F0, 16#80, 16#80, 16#80>>,
  99. <<16#F0, 16#88, 16#80, 16#80>>,
  100. <<16#F8, 16#80, 16#80, 16#80, 16#80>>,
  101. <<16#F8, 16#84, 16#80, 16#80, 16#80>>,
  102. <<16#FC, 16#80, 16#80, 16#80, 16#80, 16#80>>,
  103. <<16#FC, 16#82, 16#80, 16#80, 16#80, 16#80>>
  104. ].