parse_string.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "unity/examples/unity_config.h"
  23. #include "unity/src/unity.h"
  24. #include "common.h"
  25. static cJSON item[1];
  26. static void assert_is_string(cJSON *string_item)
  27. {
  28. TEST_ASSERT_NOT_NULL_MESSAGE(string_item, "Item is NULL.");
  29. assert_not_in_list(string_item);
  30. assert_has_no_child(string_item);
  31. assert_has_type(string_item, cJSON_String);
  32. assert_has_no_reference(string_item);
  33. assert_has_no_const_string(string_item);
  34. assert_has_valuestring(string_item);
  35. assert_has_no_string(string_item);
  36. }
  37. static void assert_parse_string(const char *string, const char *expected)
  38. {
  39. parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
  40. buffer.content = (const unsigned char*)string;
  41. buffer.length = strlen(string) + sizeof("");
  42. buffer.hooks = global_hooks;
  43. TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer), "Couldn't parse string.");
  44. assert_is_string(item);
  45. TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
  46. global_hooks.deallocate(item->valuestring);
  47. item->valuestring = NULL;
  48. }
  49. static void assert_not_parse_string(const char * const string)
  50. {
  51. parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
  52. buffer.content = (const unsigned char*)string;
  53. buffer.length = strlen(string) + sizeof("");
  54. buffer.hooks = global_hooks;
  55. TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer), "Malformed string should not be accepted.");
  56. assert_is_invalid(item);
  57. }
  58. static void parse_string_should_parse_strings(void)
  59. {
  60. assert_parse_string("\"\"", "");
  61. assert_parse_string(
  62. "\" !\\\"#$%&'()*+,-./\\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_'abcdefghijklmnopqrstuvwxyz{|}~\"",
  63. " !\"#$%&'()*+,-.//0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~");
  64. assert_parse_string(
  65. "\"\\\"\\\\\\/\\b\\f\\n\\r\\t\\u20AC\\u732b\"",
  66. "\"\\/\b\f\n\r\t€猫");
  67. reset(item);
  68. assert_parse_string("\"\b\f\n\r\t\"", "\b\f\n\r\t");
  69. reset(item);
  70. }
  71. static void parse_string_should_parse_utf16_surrogate_pairs(void)
  72. {
  73. assert_parse_string("\"\\uD83D\\udc31\"", "🐱");
  74. reset(item);
  75. }
  76. static void parse_string_should_not_parse_non_strings(void)
  77. {
  78. assert_not_parse_string("this\" is not a string\"");
  79. reset(item);
  80. assert_not_parse_string("");
  81. reset(item);
  82. }
  83. static void parse_string_should_not_parse_invalid_backslash(void)
  84. {
  85. assert_not_parse_string("Abcdef\\123");
  86. reset(item);
  87. assert_not_parse_string("Abcdef\\e23");
  88. reset(item);
  89. }
  90. static void parse_string_should_not_overflow_with_closing_backslash(void)
  91. {
  92. assert_not_parse_string("\"000000000000000000\\");
  93. reset(item);
  94. }
  95. static void parse_string_should_parse_bug_94(void)
  96. {
  97. const char string[] = "\"~!@\\\\#$%^&*()\\\\\\\\-\\\\+{}[]:\\\\;\\\\\\\"\\\\<\\\\>?/.,DC=ad,DC=com\"";
  98. assert_parse_string(string, "~!@\\#$%^&*()\\\\-\\+{}[]:\\;\\\"\\<\\>?/.,DC=ad,DC=com");
  99. reset(item);
  100. }
  101. int CJSON_CDECL main(void)
  102. {
  103. /* initialize cJSON item and error pointer */
  104. memset(item, 0, sizeof(cJSON));
  105. UNITY_BEGIN();
  106. RUN_TEST(parse_string_should_parse_strings);
  107. RUN_TEST(parse_string_should_parse_utf16_surrogate_pairs);
  108. RUN_TEST(parse_string_should_not_parse_non_strings);
  109. RUN_TEST(parse_string_should_not_parse_invalid_backslash);
  110. RUN_TEST(parse_string_should_parse_bug_94);
  111. RUN_TEST(parse_string_should_not_overflow_with_closing_backslash);
  112. return UNITY_END();
  113. }