From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id B28B4A0093;
	Thu, 23 Jun 2022 18:43:41 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 1EFE342B70;
	Thu, 23 Jun 2022 18:43:09 +0200 (CEST)
Received: from mga12.intel.com (mga12.intel.com [192.55.52.136])
 by mails.dpdk.org (Postfix) with ESMTP id 751484282D
 for <dev@dpdk.org>; Thu, 23 Jun 2022 18:43:05 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple;
 d=intel.com; i=@intel.com; q=dns/txt; s=Intel;
 t=1656002585; x=1687538585;
 h=from:to:cc:subject:date:message-id:in-reply-to:
 references:mime-version:content-transfer-encoding;
 bh=+qyS8o8pNJCdFShqPIAOC4g6Y3w7aNO9gF8yuDVnr24=;
 b=Axl79FWBbX0NPH1/3hT/cOgevOpf4AmMUVu+4gCvZfg4GOGiU+FZPz43
 qhEThHj0NqilTEfSZOhD69SpFyGk8dWJHN/m15rLgXcTCd5cFSTQV9X94
 3QR8A0bFwf4KaUr+7DBSf6hIeaz0Z+BP8jADfrgvn3BAxW015YfljHm02
 u0u+zas8DcacRhIj1trWkEIeAB3LpjR2TUBbDG9Wh0SNeWpGi+NiNfNmL
 BfvDZaY+r7hwQpQEv4IcN0evhFueEuY+jqq0zL+8mNJv4EAr3UJ9jWGat
 FIL4GZeJh5wOXmrZg1vu7cErMmFYCzyvAyItSy/viqac2ka26l+q56pvV g==;
X-IronPort-AV: E=McAfee;i="6400,9594,10386"; a="260589107"
X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="260589107"
Received: from fmsmga005.fm.intel.com ([10.253.24.32])
 by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;
 23 Jun 2022 09:43:05 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="915267958"
Received: from silpixa00401385.ir.intel.com (HELO
 silpixa00401385.ger.corp.intel.com.) ([10.237.223.125])
 by fmsmga005.fm.intel.com with ESMTP; 23 Jun 2022 09:43:03 -0700
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: ciara.power@intel.com, fengchengwen@huawei.com, mb@smartsharesystems.com,
 Bruce Richardson <bruce.richardson@intel.com>
Subject: [RFC PATCH 6/6] test/telemetry-json: add test case for escaping
 strings in arrays
Date: Thu, 23 Jun 2022 17:42:45 +0100
Message-Id: <20220623164245.561371-7-bruce.richardson@intel.com>
X-Mailer: git-send-email 2.34.1
In-Reply-To: <20220623164245.561371-1-bruce.richardson@intel.com>
References: <20220623164245.561371-1-bruce.richardson@intel.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

Add test-case to validate that when adding strings to arrays, the
strings are properly escaped to remove any invalid characters.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 app/test/test_telemetry_json.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/app/test/test_telemetry_json.c b/app/test/test_telemetry_json.c
index 955c2e5b1b..642ae9d6e1 100644
--- a/app/test/test_telemetry_json.c
+++ b/app/test/test_telemetry_json.c
@@ -141,6 +141,29 @@ test_string_char_escaping(void)
 	return strncmp(expected, buf, sizeof(buf));
 }
 
+static int
+test_array_char_escaping(void)
+{
+	/* "meaning of life", with tab between first two words, \n at end, and "life" in quotes,
+	 * followed by "all the fish" in quotes */
+	const char *expected = "[\"meaning\\tof \\\"life\\\"\\n\",\"\\\"all the fish\\\"\"]";
+	char buf[1024];
+	int used = 0;
+
+	printf("%s: ", __func__);
+	used = rte_tel_json_empty_array(buf, sizeof(buf), used);
+	if (used != 2 || strcmp(buf, "[]"))
+		return -1;
+
+	used = rte_tel_json_add_array_string(buf, sizeof(buf), used, "meaning\tof \"life\"\n");
+	used = rte_tel_json_add_array_string(buf, sizeof(buf), used, "\"all the fish\"");
+
+	printf("buf = '%s', expected = '%s'\n", buf, expected);
+	if (used != (int)strlen(expected))
+		return -1;
+	return strncmp(expected, buf, sizeof(buf));
+}
+
 typedef int (*test_fn)(void);
 
 static int
@@ -155,6 +178,7 @@ test_telemetry_json(void)
 			test_large_array_element,
 			test_large_obj_element,
 			test_string_char_escaping,
+			test_array_char_escaping,
 	};
 	for (i = 0; i < RTE_DIM(fns); i++)
 		if (fns[i]() == 0)
-- 
2.34.1