Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: move gvm_json_string_escape to a new file #852

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ if (BUILD_TESTS AND NOT SKIP_SRC)
DEPENDS array-test alivedetection-test boreas_error-test boreas_io-test
cli-test cpeutils-test cvss-test ping-test sniffer-test util-test networking-test
passwordbasedauthentication-test xmlutils-test version-test versionutils-test
osp-test nvti-test hosts-test jsonpull-test compressutils-test
osp-test nvti-test hosts-test json-test jsonpull-test compressutils-test
openvasd-test)

endif (BUILD_TESTS AND NOT SKIP_SRC)
Expand Down
19 changes: 17 additions & 2 deletions util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ endif (BUILD_WITH_LDAP)
include_directories (${GLIB_INCLUDE_DIRS} ${GPGME_INCLUDE_DIRS} ${GCRYPT_INCLUDE_DIRS}
${LIBXML2_INCLUDE_DIRS})

set (FILES cpeutils.c passwordbasedauthentication.c compressutils.c fileutils.c gpgmeutils.c jsonpull.c kb.c
set (FILES cpeutils.c passwordbasedauthentication.c compressutils.c fileutils.c gpgmeutils.c json.c jsonpull.c kb.c
ldaputils.c nvticache.c mqtt.c radiusutils.c serverutils.c sshutils.c uuidutils.c versionutils.c
xmlutils.c)

set (HEADERS cpeutils.h passwordbasedauthentication.h authutils.h compressutils.h fileutils.h gpgmeutils.h
jsonpull.h kb.h ldaputils.h nvticache.h mqtt.h radiusutils.h serverutils.h sshutils.h
json.h jsonpull.h kb.h ldaputils.h nvticache.h mqtt.h radiusutils.h serverutils.h sshutils.h
uuidutils.h versionutils.h xmlutils.h)

if (BUILD_STATIC)
Expand Down Expand Up @@ -148,6 +148,21 @@ endif (BUILD_SHARED)
## Tests

if (BUILD_TESTS)
add_executable (json-test
EXCLUDE_FROM_ALL
json_tests.c)

add_test (json-test json-test)

target_include_directories (json-test PRIVATE ${CGREEN_INCLUDE_DIRS})

target_link_libraries (json-test ${CGREEN_LIBRARIES}
${GLIB_LDFLAGS} ${CJSON_LDFLAGS})

add_custom_target (tests-json
DEPENDS json-test)


add_executable (jsonpull-test
EXCLUDE_FROM_ALL
jsonpull_tests.c)
Expand Down
64 changes: 64 additions & 0 deletions util/json.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "json.h"

/**
* @brief Escapes a string according to the JSON or JSONPath standard
*
* @param[in] string The string to escape
* @param[in] single_quote Whether to escape single quotes
*
* @return The escaped string
*/
gchar *
gvm_json_string_escape (const char *string, gboolean single_quote)
{
gchar *point;
if (string == NULL)
return NULL;

GString *escaped = g_string_sized_new (strlen (string));
for (point = (char *) string; *point != 0; point++)
{
unsigned char character = *point;

if ((character > 31) && (character != '\\')
&& (single_quote ? (character != '\'') : (character != '\"')))
{
g_string_append_c (escaped, character);
}
else
{
g_string_append_c (escaped, '\\');
switch (*point)
{
case '\\':
case '\'':
case '\"':
g_string_append_c (escaped, *point);
break;
case '\b':
g_string_append_c (escaped, 'b');
break;
case '\f':
g_string_append_c (escaped, 'f');
break;
case '\n':
g_string_append_c (escaped, 'n');
break;
case '\r':
g_string_append_c (escaped, 'r');
break;
case '\t':
g_string_append_c (escaped, 't');
break;
default:
g_string_append_printf (escaped, "u%04x", character);
}
}
}
return g_string_free (escaped, FALSE);
}
17 changes: 17 additions & 0 deletions util/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#ifndef _GVM_JSON_H
#define _GVM_JSON_H

#define _GNU_SOURCE

#include <cjson/cJSON.h>
#include <glib.h>

gchar *
gvm_json_string_escape (const char *, gboolean);

#endif /* _GVM_JSON_H */
51 changes: 51 additions & 0 deletions util/json_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* SPDX-FileCopyrightText: 2019-2023 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "json.c"

#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <stdio.h>

Describe (json);
BeforeEach (json)
{
}
AfterEach (json)
{
}

Ensure (json, can_json_escape_strings)
{
const char *unescaped_string = "\"'Abc\\\b\f\n\r\t\001Äöü'\"";
const char *escaped_string_dq = "\\\"'Abc\\\\\\b\\f\\n\\r\\t\\u0001Äöü'\\\"";
const char *escaped_string_sq = "\"\\'Abc\\\\\\b\\f\\n\\r\\t\\u0001Äöü\\'\"";

gchar *escaped_string = NULL;
escaped_string = gvm_json_string_escape (NULL, FALSE);
assert_that (escaped_string, is_null);

escaped_string = gvm_json_string_escape (unescaped_string, FALSE);
assert_that (escaped_string, is_equal_to_string (escaped_string_dq));
g_free (escaped_string);

escaped_string = gvm_json_string_escape (unescaped_string, TRUE);
assert_that (escaped_string, is_equal_to_string (escaped_string_sq));
g_free (escaped_string);
}

int
main (int argc, char **argv)
{
TestSuite *suite;

suite = create_test_suite ();

add_test_with_context (suite, json, can_json_escape_strings);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());

Check warning on line 49 in util/json_tests.c

View check run for this annotation

Codecov / codecov/patch

util/json_tests.c#L49

Added line #L49 was not covered by tests
return run_test_suite (suite, create_text_reporter ());
}
58 changes: 0 additions & 58 deletions util/jsonpull.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,6 @@
#define GVM_JSON_CHAR_ERROR -2 ///< Error reading file
#define GVM_JSON_CHAR_UNDEFINED -3 ///< Undefined state

/**
* @brief Escapes a string according to the JSON or JSONPath standard
*
* @param[in] string The string to escape
* @param[in] single_quote Whether to escape single quotes
*
* @return The escaped string
*/
gchar *
gvm_json_string_escape (const char *string, gboolean single_quote)
{
gchar *point;
if (string == NULL)
return NULL;

GString *escaped = g_string_sized_new (strlen (string));
for (point = (char *) string; *point != 0; point++)
{
unsigned char character = *point;

if ((character > 31) && (character != '\\')
&& (single_quote ? (character != '\'') : (character != '\"')))
{
g_string_append_c (escaped, character);
}
else
{
g_string_append_c (escaped, '\\');
switch (*point)
{
case '\\':
case '\'':
case '\"':
g_string_append_c (escaped, *point);
break;
case '\b':
g_string_append_c (escaped, 'b');
break;
case '\f':
g_string_append_c (escaped, 'f');
break;
case '\n':
g_string_append_c (escaped, 'n');
break;
case '\r':
g_string_append_c (escaped, 'r');
break;
case '\t':
g_string_append_c (escaped, 't');
break;
default:
g_string_append_printf (escaped, "u%04x", character);
}
}
}
return g_string_free (escaped, FALSE);
}

/**
* @brief Creates a new JSON path element.
*
Expand Down
6 changes: 2 additions & 4 deletions util/jsonpull.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

#define _GNU_SOURCE

#include <cjson/cJSON.h>
#include "json.h"

#include <glib.h>
#include <stdio.h>

Expand Down Expand Up @@ -97,9 +98,6 @@ typedef struct
size_t parse_buffer_limit; ///< Maximum parse buffer size
} gvm_json_pull_parser_t;

gchar *
gvm_json_string_escape (const char *, gboolean);

gvm_json_path_elem_t *
gvm_json_pull_path_elem_new (gvm_json_pull_container_type_t, int);

Expand Down
22 changes: 1 addition & 21 deletions util/jsonpull_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "json.c"
#include "jsonpull.c"

#include <cgreen/cgreen.h>
Expand Down Expand Up @@ -72,25 +73,6 @@ read_with_error_on_eof (void *stream_cookie, char *buf, size_t size)

#define JSON_READ_ERROR "error reading JSON stream: Input/output error"

Ensure (jsonpull, can_json_escape_strings)
{
const char *unescaped_string = "\"'Abc\\\b\f\n\r\t\001Äöü'\"";
const char *escaped_string_dq = "\\\"'Abc\\\\\\b\\f\\n\\r\\t\\u0001Äöü'\\\"";
const char *escaped_string_sq = "\"\\'Abc\\\\\\b\\f\\n\\r\\t\\u0001Äöü\\'\"";

gchar *escaped_string = NULL;
escaped_string = gvm_json_string_escape (NULL, FALSE);
assert_that (escaped_string, is_null);

escaped_string = gvm_json_string_escape (unescaped_string, FALSE);
assert_that (escaped_string, is_equal_to_string (escaped_string_dq));
g_free (escaped_string);

escaped_string = gvm_json_string_escape (unescaped_string, TRUE);
assert_that (escaped_string, is_equal_to_string (escaped_string_sq));
g_free (escaped_string);
}

Ensure (jsonpull, can_init_parser_with_defaults)
{
gvm_json_pull_parser_t parser;
Expand Down Expand Up @@ -1132,8 +1114,6 @@ main (int argc, char **argv)

suite = create_test_suite ();

add_test_with_context (suite, jsonpull, can_json_escape_strings);

add_test_with_context (suite, jsonpull, can_init_parser_with_defaults);

add_test_with_context (suite, jsonpull, can_parse_false);
Expand Down
Loading