parse_string.c 4.3 KB
Newer Older
M
Max Bruckner 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "unity/examples/unity_config.h"
#include "unity/src/unity.h"
29
#include "common.h"
M
Max Bruckner 已提交
30 31 32 33 34 35 36 37 38

static cJSON item[1];

static const unsigned char *error_pointer = NULL;

static void assert_is_string(cJSON *string_item)
{
    TEST_ASSERT_NOT_NULL_MESSAGE(string_item, "Item is NULL.");

M
Max Bruckner 已提交
39 40 41 42 43 44 45
    assert_not_in_list(string_item);
    assert_has_no_child(string_item);
    assert_has_type(string_item, cJSON_String);
    assert_has_no_reference(string_item);
    assert_has_no_const_string(string_item);
    assert_has_valuestring(string_item);
    assert_has_no_string(string_item);
M
Max Bruckner 已提交
46 47 48 49 50 51 52 53 54 55 56
}

static void assert_parse_string(const char *string, const char *expected)
{
    TEST_ASSERT_NOT_NULL_MESSAGE(parse_string(item, (const unsigned char*)string, &error_pointer), "Couldn't parse string.");
    assert_is_string(item);
    TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
    cJSON_free(item->valuestring);
    item->valuestring = NULL;
}

57 58 59 60 61
#define assert_not_parse_string(string) \
    TEST_ASSERT_NULL_MESSAGE(parse_string(item, (const unsigned char*)string, &error_pointer), "Malformed string should not be accepted");\
    assert_is_invalid(item)


M
Max Bruckner 已提交
62

M
Max Bruckner 已提交
63 64 65 66 67 68 69 70 71
static void parse_string_should_parse_strings(void)
{
    assert_parse_string("\"\"", "");
    assert_parse_string(
        "\" !\\\"#$%&'()*+,-./\\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_'abcdefghijklmnopqrstuvwxyz{|}~\"",
        " !\"#$%&'()*+,-.//0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~");
    assert_parse_string(
        "\"\\\"\\\\\\/\\b\\f\\n\\r\\t\\u20AC\\u732b\"",
        "\"\\/\b\f\n\r\t€猫");
72
    reset(item);
M
Max Bruckner 已提交
73
    assert_parse_string("\"\b\f\n\r\t\"", "\b\f\n\r\t");
74
    reset(item);
M
Max Bruckner 已提交
75 76 77 78 79
}

static void parse_string_should_parse_utf16_surrogate_pairs(void)
{
    assert_parse_string("\"\\uD83D\\udc31\"", "🐱");
80
    reset(item);
M
Max Bruckner 已提交
81 82 83 84
}

static void parse_string_should_not_parse_non_strings(void)
{
M
Max Bruckner 已提交
85
    assert_not_parse_string("this\" is not a string\"");
86
    reset(item);
M
Max Bruckner 已提交
87
    assert_not_parse_string("");
88
    reset(item);
M
Max Bruckner 已提交
89 90 91 92
}

static void parse_string_should_not_parse_invalid_backslash(void)
{
M
Max Bruckner 已提交
93
    assert_not_parse_string("Abcdef\\123");
94
    reset(item);
M
Max Bruckner 已提交
95
    assert_not_parse_string("Abcdef\\e23");
96
    reset(item);
M
Max Bruckner 已提交
97 98 99 100
}

static void parse_string_should_not_overflow_with_closing_backslash(void)
{
M
Max Bruckner 已提交
101
    assert_not_parse_string("\"000000000000000000\\");
102
    reset(item);
M
Max Bruckner 已提交
103 104 105 106 107 108
}

static void parse_string_should_parse_bug_94(void)
{
    const char string[] = "\"~!@\\\\#$%^&*()\\\\\\\\-\\\\+{}[]:\\\\;\\\\\\\"\\\\<\\\\>?/.,DC=ad,DC=com\"";
    assert_parse_string(string, "~!@\\#$%^&*()\\\\-\\+{}[]:\\;\\\"\\<\\>?/.,DC=ad,DC=com");
109
    reset(item);
M
Max Bruckner 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
}

int main(void)
{
    /* initialize cJSON item and error pointer */
    memset(item, 0, sizeof(cJSON));

    UNITY_BEGIN();
    RUN_TEST(parse_string_should_parse_strings);
    RUN_TEST(parse_string_should_parse_utf16_surrogate_pairs);
    RUN_TEST(parse_string_should_not_parse_non_strings);
    RUN_TEST(parse_string_should_not_parse_invalid_backslash);
    RUN_TEST(parse_string_should_parse_bug_94);
    RUN_TEST(parse_string_should_not_overflow_with_closing_backslash);
    return UNITY_END();
}