1/* 2 Copyright (c) 2009-2019 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 31 32static void cjson_minify_should_not_overflow_buffer(void) 33{ 34 char unclosed_multiline_comment[] = "/* bla"; 35 char pending_escape[] = "\"\\"; 36 37 cJSON_Minify(unclosed_multiline_comment); 38 TEST_ASSERT_EQUAL_STRING("", unclosed_multiline_comment); 39 40 cJSON_Minify(pending_escape); 41 TEST_ASSERT_EQUAL_STRING("\"\\", pending_escape); 42} 43 44static void cjson_minify_should_remove_single_line_comments(void) 45{ 46 const char to_minify[] = "{// this is {} \"some kind\" of [] comment /*, don't you see\n}"; 47 48 char* minified = (char*) malloc(sizeof(to_minify)); 49 TEST_ASSERT_NOT_NULL(minified); 50 strcpy(minified, to_minify); 51 52 cJSON_Minify(minified); 53 TEST_ASSERT_EQUAL_STRING("{}", minified); 54 55 free(minified); 56} 57 58static void cjson_minify_should_remove_spaces(void) 59{ 60 const char to_minify[] = "{ \"key\":\ttrue\r\n }"; 61 62 char* minified = (char*) malloc(sizeof(to_minify)); 63 TEST_ASSERT_NOT_NULL(minified); 64 strcpy(minified, to_minify); 65 66 cJSON_Minify(minified); 67 TEST_ASSERT_EQUAL_STRING("{\"key\":true}", minified); 68 69 free(minified); 70} 71 72static void cjson_minify_should_remove_multiline_comments(void) 73{ 74 const char to_minify[] = "{/* this is\n a /* multi\n //line \n {comment \"\\\" */}"; 75 76 char* minified = (char*) malloc(sizeof(to_minify)); 77 TEST_ASSERT_NOT_NULL(minified); 78 strcpy(minified, to_minify); 79 80 cJSON_Minify(minified); 81 TEST_ASSERT_EQUAL_STRING("{}", minified); 82 83 free(minified); 84} 85 86static void cjson_minify_should_not_modify_strings(void) 87{ 88 const char to_minify[] = "\"this is a string \\\" \\t bla\""; 89 90 char* minified = (char*) malloc(sizeof(to_minify)); 91 TEST_ASSERT_NOT_NULL(minified); 92 strcpy(minified, to_minify); 93 94 cJSON_Minify(minified); 95 TEST_ASSERT_EQUAL_STRING(to_minify, minified); 96 97 free(minified); 98} 99 100static void cjson_minify_should_minify_json(void) { 101 const char to_minify[] = 102 "{\n" 103 " \"glossary\": { // comment\n" 104 " \"title\": \"example glossary\",\n" 105 " /* multi\n" 106 " line */\n" 107 " \"GlossDiv\": {\n" 108 " \"title\": \"S\",\n" 109 " \"GlossList\": {\n" 110 " \"GlossEntry\": {\n" 111 " \"ID\": \"SGML\",\n" 112 " \"SortAs\": \"SGML\",\n" 113 " \"Acronym\": \"SGML\",\n" 114 " \"Abbrev\": \"ISO 8879:1986\",\n" 115 " \"GlossDef\": {\n" 116 " \"GlossSeeAlso\": [\"GML\", \"XML\"]\n" 117 " },\n" 118 " \"GlossSee\": \"markup\"\n" 119 " }\n" 120 " }\n" 121 " }\n" 122 " }\n" 123 "}"; 124 const char* minified = 125 "{" 126 "\"glossary\":{" 127 "\"title\":\"example glossary\"," 128 "\"GlossDiv\":{" 129 "\"title\":\"S\"," 130 "\"GlossList\":{" 131 "\"GlossEntry\":{" 132 "\"ID\":\"SGML\"," 133 "\"SortAs\":\"SGML\"," 134 "\"Acronym\":\"SGML\"," 135 "\"Abbrev\":\"ISO 8879:1986\"," 136 "\"GlossDef\":{" 137 "\"GlossSeeAlso\":[\"GML\",\"XML\"]" 138 "}," 139 "\"GlossSee\":\"markup\"" 140 "}" 141 "}" 142 "}" 143 "}" 144 "}"; 145 146 char *buffer = (char*) malloc(sizeof(to_minify)); 147 strcpy(buffer, to_minify); 148 149 cJSON_Minify(buffer); 150 TEST_ASSERT_EQUAL_STRING(minified, buffer); 151 152 free(buffer); 153} 154 155static void cjson_minify_should_not_loop_infinitely(void) { 156 char string[] = { '8', ' ', '/', ' ', '5', '\n', '\0' }; 157 /* this should not be an infinite loop */ 158 cJSON_Minify(string); 159} 160 161int CJSON_CDECL main(void) 162{ 163 UNITY_BEGIN(); 164 165 RUN_TEST(cjson_minify_should_not_overflow_buffer); 166 RUN_TEST(cjson_minify_should_minify_json); 167 RUN_TEST(cjson_minify_should_remove_single_line_comments); 168 RUN_TEST(cjson_minify_should_remove_multiline_comments); 169 RUN_TEST(cjson_minify_should_remove_spaces); 170 RUN_TEST(cjson_minify_should_not_modify_strings); 171 RUN_TEST(cjson_minify_should_not_loop_infinitely); 172 173 return UNITY_END(); 174} 175