Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
utils: add a test suite for json_escape
Baptiste Daroussin committed 4 years ago
commit 2d2315986d13a99c545e3cdc5b1f6ffca380f26f
parent 825ab21
4 files changed +39 -30
modified libpkg/pkg_event.c
@@ -38,21 +38,6 @@
static pkg_event_cb _cb = NULL;
static void *_data = NULL;

-
static char *
-
buf_json_escape(const char *str)
-
{
-
	xstring *buf = xstring_new();
-

-
	while (str != NULL && *str != '\0') {
-
		if (*str == '"' || *str == '\\')
-
			fputc('\\', buf->fp);
-
		fputc(*str, buf->fp);
-
		str++;
-
	}
-

-
	return (xstring_get(buf));
-
}
-

static void
pipeevent(struct pkg_event *ev)
{
@@ -71,25 +56,25 @@ pipeevent(struct pkg_event *ev)
		    "\"data\": {"
		    "\"msg\": \"%s(%s): %s\","
		    "\"errno\": %d}}",
-
		    buf_json_escape(ev->e_errno.func),
-
		    buf_json_escape(ev->e_errno.arg),
-
		    buf_json_escape(strerror(ev->e_errno.no)),
+
		    json_escape(ev->e_errno.func),
+
		    json_escape(ev->e_errno.arg),
+
		    json_escape(strerror(ev->e_errno.no)),
		    ev->e_errno.no);
		break;
	case PKG_EVENT_ERROR:
		fprintf(msg->fp, "{ \"type\": \"ERROR\", "
		    "\"data\": {\"msg\": \"%s\"}}",
-
		    buf_json_escape(ev->e_pkg_error.msg));
+
		    json_escape(ev->e_pkg_error.msg));
		break;
	case PKG_EVENT_NOTICE:
		fprintf(msg->fp, "{ \"type\": \"NOTICE\", "
		    "\"data\": {\"msg\": \"%s\"}}",
-
		    buf_json_escape(ev->e_pkg_notice.msg));
+
		    json_escape(ev->e_pkg_notice.msg));
		break;
	case PKG_EVENT_DEVELOPER_MODE:
		fprintf(msg->fp, "{ \"type\": \"ERROR\", "
		    "\"data\": {\"msg\": \"DEVELOPER_MODE: %s\"}}",
-
		    buf_json_escape(ev->e_pkg_error.msg));
+
		    json_escape(ev->e_pkg_error.msg));
		break;
	case PKG_EVENT_UPDATE_ADD:
		fprintf(msg->fp, "{ \"type\": \"INFO_UPDATE_ADD\", "
@@ -116,7 +101,7 @@ pipeevent(struct pkg_event *ev)
		    "\"data\": { "
		    "\"url\": \"%s\" "
		    "}}",
-
		    buf_json_escape(ev->e_fetching.url)
+
		    json_escape(ev->e_fetching.url)
		    );
		break;
	case PKG_EVENT_FETCH_FINISHED:
@@ -124,7 +109,7 @@ pipeevent(struct pkg_event *ev)
		    "\"data\": { "
		    "\"url\": \"%s\" "
		    "}}",
-
		    buf_json_escape(ev->e_fetching.url)
+
		    json_escape(ev->e_fetching.url)
		    );
		break;
	case PKG_EVENT_INSTALL_BEGIN:
@@ -158,7 +143,7 @@ pipeevent(struct pkg_event *ev)
		    ev->e_install_finished.pkg,
		    ev->e_install_finished.pkg,
			ev->e_install_finished.pkg->message ?
-
				buf_json_escape(ev->e_install_finished.pkg->message->str) :
+
				json_escape(ev->e_install_finished.pkg->message->str) :
				"");
		break;
	case PKG_EVENT_INTEGRITYCHECK_BEGIN:
@@ -303,7 +288,7 @@ pipeevent(struct pkg_event *ev)
		    "}}",
		    ev->e_file_mismatch.pkg,
		    ev->e_file_mismatch.pkg,
-
		    buf_json_escape(ev->e_file_mismatch.file->path));
+
		    json_escape(ev->e_file_mismatch.file->path));
		break;
	case PKG_EVENT_PLUGIN_ERRNO:
		fprintf(msg->fp, "{ \"type\": \"ERROR_PLUGIN\", "
@@ -313,9 +298,9 @@ pipeevent(struct pkg_event *ev)
		    "\"errno\": %d"
		    "}}",
		    pkg_plugin_get(ev->e_plugin_errno.plugin, PKG_PLUGIN_NAME),
-
		    buf_json_escape(ev->e_plugin_errno.func),
-
		    buf_json_escape(ev->e_plugin_errno.arg),
-
		    buf_json_escape(strerror(ev->e_plugin_errno.no)),
+
		    json_escape(ev->e_plugin_errno.func),
+
		    json_escape(ev->e_plugin_errno.arg),
+
		    json_escape(strerror(ev->e_plugin_errno.no)),
		    ev->e_plugin_errno.no);
		break;
	case PKG_EVENT_PLUGIN_ERROR:
@@ -325,7 +310,7 @@ pipeevent(struct pkg_event *ev)
		    "\"msg\": \"%s\""
		    "}}",
		    pkg_plugin_get(ev->e_plugin_error.plugin, PKG_PLUGIN_NAME),
-
		    buf_json_escape(ev->e_plugin_error.msg));
+
		    json_escape(ev->e_plugin_error.msg));
		break;
	case PKG_EVENT_PLUGIN_INFO:
		fprintf(msg->fp, "{ \"type\": \"INFO_PLUGIN\", "
@@ -334,7 +319,7 @@ pipeevent(struct pkg_event *ev)
		    "\"msg\": \"%s\""
		    "}}",
		    pkg_plugin_get(ev->e_plugin_info.plugin, PKG_PLUGIN_NAME),
-
		    buf_json_escape(ev->e_plugin_info.msg));
+
		    json_escape(ev->e_plugin_info.msg));
		break;
	case PKG_EVENT_INCREMENTAL_UPDATE:
		fprintf(msg->fp, "{ \"type\": \"INFO_INCREMENTAL_UPDATE\", "
modified libpkg/private/utils.h
@@ -116,5 +116,6 @@ char *rtrimspace(char *buf);
bool copy_file(int from, int to);
void hidden_tempfile(char *buf, int buflen, const char *path);
void append_random_suffix(char *buf, int buflen, int suffixlen);
+
char *json_escape(const char *str);

#endif
modified libpkg/utils.c
@@ -986,3 +986,18 @@ hidden_tempfile(char *buf, int buflen, const char *path)
	append_random_suffix(buf, nbuflen, suffixlen);
}

+
char *
+
json_escape(const char *str)
+
{
+
	xstring *buf = xstring_new();
+

+
	while (str != NULL && *str != '\0') {
+
		if (*str == '"' || *str == '\\')
+
			fputc('\\', buf->fp);
+
		fputc(*str, buf->fp);
+
		str++;
+
	}
+

+
	return (xstring_get(buf));
+
}
+

modified tests/lib/utils.c
@@ -29,9 +29,11 @@

ATF_TC(hidden_tempfile);
ATF_TC(random_suffix);
+
ATF_TC(json_escape);

ATF_TC_HEAD(hidden_tempfile, tc) {}
ATF_TC_HEAD(random_suffix, tc) {}
+
ATF_TC_HEAD(json_escape, tc) {}

ATF_TC_BODY(hidden_tempfile, tc) {
	const char *filename = "plop";
@@ -64,10 +66,16 @@ ATF_TC_BODY(random_suffix, tc) {
	ATF_REQUIRE_EQ_MSG(strlen(buf), 13, "suffix not long enough %lu", strlen(buf));
}

+
ATF_TC_BODY(json_escape, tc) {
+
	char *m = json_escape("entry1\"\"\\ ");
+
	ATF_REQUIRE_STREQ_MSG(m, "entry1\\\"\\\"\\\\ ", "Invalid escaping");
+
}
+

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, hidden_tempfile);
	ATF_TP_ADD_TC(tp, random_suffix);
+
	ATF_TP_ADD_TC(tp, json_escape);

	return (atf_no_error());
}