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 07B3CA00C5;
	Fri,  9 Sep 2022 11:35:57 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 8835E42802;
	Fri,  9 Sep 2022 11:35:49 +0200 (CEST)
Received: from mga14.intel.com (mga14.intel.com [192.55.52.115])
 by mails.dpdk.org (Postfix) with ESMTP id BF6B44003F
 for <dev@dpdk.org>; Fri,  9 Sep 2022 11:35:44 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple;
 d=intel.com; i=@intel.com; q=dns/txt; s=Intel;
 t=1662716144; x=1694252144;
 h=from:to:cc:subject:date:message-id:in-reply-to:
 references:mime-version:content-transfer-encoding;
 bh=gZWLrdtU0OzHT+9MXNKB2CVPrd9knLEq6KTVr/zgowQ=;
 b=PfoYLQmwPrRXOIGB7tCqJrki974YhLBFgJzqcFqucdCfMR2o/IMRJbH6
 H1Y7PJIJ1zrAsB3ClNptEXYuPduJJ0I5vcbtDcStKN3PokEwKlVp7Dvs+
 8fM6aUaTOPL/HBM/P2QHPcxl+8nXQV8rSNOfJLZ9npvG1NyeShZf1APVm
 0hrEjV5/f2cFv5p6DnGo6Oe13eQDjeyk8wz5nCoQJ14gG9RNqU2CvT+xi
 zFXt4YLg55orhRupKSYIyjhdtmhSRFKAc3dgD1viwLEsgfG7FeDDL+s8T
 s/1TAq0psZiZCTwh1wRDidYM3VTx/H9rURO11qd+3xe+zh+fVBGcXH6ck g==;
X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="297437123"
X-IronPort-AV: E=Sophos;i="5.93,302,1654585200"; d="scan'208";a="297437123"
Received: from orsmga004.jf.intel.com ([10.7.209.38])
 by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;
 09 Sep 2022 02:35:44 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.93,302,1654585200"; d="scan'208";a="740996364"
Received: from silpixa00401385.ir.intel.com ([10.237.214.161])
 by orsmga004.jf.intel.com with ESMTP; 09 Sep 2022 02:35:43 -0700
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>,
 Ciara Power <ciara.power@intel.com>,
 =?UTF-8?q?Morten=20Br=C3=B8rup?= <mb@smartsharesystems.com>
Subject: [PATCH v3 04/13] test/telemetry_json: add test for string character
 escaping
Date: Fri,  9 Sep 2022 10:35:14 +0100
Message-Id: <20220909093523.471727-5-bruce.richardson@intel.com>
X-Mailer: git-send-email 2.34.1
In-Reply-To: <20220909093523.471727-1-bruce.richardson@intel.com>
References: <20220623164245.561371-1-bruce.richardson@intel.com>
 <20220909093523.471727-1-bruce.richardson@intel.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
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 unit test to validate that when creating a string response in json,
that characters such as \n or quotes are properly escaped.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Ciara Power <ciara.power@intel.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
 app/test/test_telemetry_json.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/app/test/test_telemetry_json.c b/app/test/test_telemetry_json.c
index 748b7cfe5a..955c2e5b1b 100644
--- a/app/test/test_telemetry_json.c
+++ b/app/test/test_telemetry_json.c
@@ -125,6 +125,22 @@ test_large_obj_element(void)
 	return strncmp(expected, buf, sizeof(buf));
 }
 
+static int
+test_string_char_escaping(void)
+{
+	static const char str[] = "A string across\ntwo lines and \"with quotes\"!";
+	const char *expected = "\"A string across\\ntwo lines and \\\"with quotes\\\"!\"";
+	char buf[sizeof(str) + 10];
+	int used = 0;
+
+	used = rte_tel_json_str(buf, sizeof(buf), used, str);
+	printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
+	if (used != (int)strlen(expected))
+		return -1;
+
+	return strncmp(expected, buf, sizeof(buf));
+}
+
 typedef int (*test_fn)(void);
 
 static int
@@ -138,6 +154,7 @@ test_telemetry_json(void)
 			test_overflow_obj,
 			test_large_array_element,
 			test_large_obj_element,
+			test_string_char_escaping,
 	};
 	for (i = 0; i < RTE_DIM(fns); i++)
 		if (fns[i]() == 0)
-- 
2.34.1