1/*
2  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
3
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  THE SOFTWARE.
21*/
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "unity/examples/unity_config.h"
28#include "unity/src/unity.h"
29#include "common.h"
30#include "../cJSON_Utils.h"
31
32static cJSON *parse_test_file(const char * const filename)
33{
34    char *file = NULL;
35    cJSON *json = NULL;
36
37    file = read_file(filename);
38    TEST_ASSERT_NOT_NULL_MESSAGE(file, "Failed to read file.");
39
40    json = cJSON_Parse(file);
41    TEST_ASSERT_NOT_NULL_MESSAGE(json, "Failed to parse test json.");
42    TEST_ASSERT_TRUE_MESSAGE(cJSON_IsArray(json), "Json is not an array.");
43
44    free(file);
45
46    return json;
47}
48
49static cJSON_bool test_apply_patch(const cJSON * const test)
50{
51    cJSON *doc = NULL;
52    cJSON *patch = NULL;
53    cJSON *expected = NULL;
54    cJSON *error_element = NULL;
55    cJSON *comment = NULL;
56    cJSON *disabled = NULL;
57
58    cJSON *object = NULL;
59    cJSON_bool successful = false;
60
61    /* extract all the data out of the test */
62    comment = cJSON_GetObjectItemCaseSensitive(test, "comment");
63    if (cJSON_IsString(comment))
64    {
65        printf("Testing \"%s\"\n", comment->valuestring);
66    }
67    else
68    {
69        printf("Testing unknown\n");
70    }
71
72    disabled = cJSON_GetObjectItemCaseSensitive(test, "disabled");
73    if (cJSON_IsTrue(disabled))
74    {
75        printf("SKIPPED\n");
76        return true;
77    }
78
79    doc = cJSON_GetObjectItemCaseSensitive(test, "doc");
80    TEST_ASSERT_NOT_NULL_MESSAGE(doc, "No \"doc\" in the test.");
81    patch = cJSON_GetObjectItemCaseSensitive(test, "patch");
82    TEST_ASSERT_NOT_NULL_MESSAGE(patch, "No \"patch\"in the test.");
83    /* Make a working copy of 'doc' */
84    object = cJSON_Duplicate(doc, true);
85    TEST_ASSERT_NOT_NULL(object);
86
87    expected = cJSON_GetObjectItemCaseSensitive(test, "expected");
88    error_element = cJSON_GetObjectItemCaseSensitive(test, "error");
89    if (error_element != NULL)
90    {
91        /* excepting an error */
92        TEST_ASSERT_TRUE_MESSAGE(0 != cJSONUtils_ApplyPatchesCaseSensitive(object, patch), "Test didn't fail as it's supposed to.");
93
94        successful = true;
95    }
96    else
97    {
98        /* apply the patch */
99        TEST_ASSERT_EQUAL_INT_MESSAGE(0, cJSONUtils_ApplyPatchesCaseSensitive(object, patch), "Failed to apply patches.");
100        successful = true;
101
102        if (expected != NULL)
103        {
104            successful = cJSON_Compare(object, expected, true);
105        }
106    }
107
108    cJSON_Delete(object);
109
110    if (successful)
111    {
112        printf("OK\n");
113    }
114    else
115    {
116        printf("FAILED\n");
117    }
118
119    return successful;
120}
121
122static cJSON_bool test_generate_test(cJSON *test)
123{
124    cJSON *doc = NULL;
125    cJSON *patch = NULL;
126    cJSON *expected = NULL;
127    cJSON *disabled = NULL;
128
129    cJSON *object = NULL;
130    cJSON_bool successful = false;
131
132    char *printed_patch = NULL;
133
134    disabled = cJSON_GetObjectItemCaseSensitive(test, "disabled");
135    if (cJSON_IsTrue(disabled))
136    {
137        printf("SKIPPED\n");
138        return true;
139    }
140
141    doc = cJSON_GetObjectItemCaseSensitive(test, "doc");
142    TEST_ASSERT_NOT_NULL_MESSAGE(doc, "No \"doc\" in the test.");
143
144    /* Make a working copy of 'doc' */
145    object = cJSON_Duplicate(doc, true);
146    TEST_ASSERT_NOT_NULL(object);
147
148    expected = cJSON_GetObjectItemCaseSensitive(test, "expected");
149    if (expected == NULL)
150    {
151        cJSON_Delete(object);
152        /* if there is no expected output, this test doesn't make sense */
153        return true;
154    }
155
156    patch = cJSONUtils_GeneratePatchesCaseSensitive(doc, expected);
157    TEST_ASSERT_NOT_NULL_MESSAGE(patch, "Failed to generate patches.");
158
159    printed_patch = cJSON_Print(patch);
160    printf("%s\n", printed_patch);
161    free(printed_patch);
162
163    /* apply the generated patch */
164    TEST_ASSERT_EQUAL_INT_MESSAGE(0, cJSONUtils_ApplyPatchesCaseSensitive(object, patch), "Failed to apply generated patch.");
165
166    successful = cJSON_Compare(object, expected, true);
167
168    cJSON_Delete(patch);
169    cJSON_Delete(object);
170
171    if (successful)
172    {
173        printf("generated patch: OK\n");
174    }
175    else
176    {
177        printf("generated patch: FAILED\n");
178    }
179
180    return successful;
181}
182
183static void cjson_utils_should_pass_json_patch_test_tests(void)
184{
185    cJSON *tests = parse_test_file("json-patch-tests/tests.json");
186    cJSON *test = NULL;
187
188    cJSON_bool failed = false;
189    cJSON_ArrayForEach(test, tests)
190    {
191        failed |= !test_apply_patch(test);
192        failed |= !test_generate_test(test);
193    }
194
195    cJSON_Delete(tests);
196
197    TEST_ASSERT_FALSE_MESSAGE(failed, "Some tests failed.");
198}
199
200static void cjson_utils_should_pass_json_patch_test_spec_tests(void)
201{
202    cJSON *tests = parse_test_file("json-patch-tests/spec_tests.json");
203    cJSON *test = NULL;
204
205    cJSON_bool failed = false;
206    cJSON_ArrayForEach(test, tests)
207    {
208        failed |= !test_apply_patch(test);
209        failed |= !test_generate_test(test);
210    }
211
212    cJSON_Delete(tests);
213
214    TEST_ASSERT_FALSE_MESSAGE(failed, "Some tests failed.");
215}
216
217static void cjson_utils_should_pass_json_patch_test_cjson_utils_tests(void)
218{
219    cJSON *tests = parse_test_file("json-patch-tests/cjson-utils-tests.json");
220    cJSON *test = NULL;
221
222    cJSON_bool failed = false;
223    cJSON_ArrayForEach(test, tests)
224    {
225        failed |= !test_apply_patch(test);
226        failed |= !test_generate_test(test);
227    }
228
229    cJSON_Delete(tests);
230
231    TEST_ASSERT_FALSE_MESSAGE(failed, "Some tests failed.");
232}
233
234int main(void)
235{
236    UNITY_BEGIN();
237
238    RUN_TEST(cjson_utils_should_pass_json_patch_test_tests);
239    RUN_TEST(cjson_utils_should_pass_json_patch_test_spec_tests);
240    RUN_TEST(cjson_utils_should_pass_json_patch_test_cjson_utils_tests);
241
242    return UNITY_END();
243}
244