From patchwork Sun Jan 12 22:55:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Petr_=C5=A0tetiar?= X-Patchwork-Id: 1221902 X-Patchwork-Delegate: ynezz@true.cz Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=true.cz Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20170209 header.b=YHDihUID; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47wsX46R10z9s29 for ; Mon, 13 Jan 2020 09:56:04 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:References: In-Reply-To:Message-Id:Date:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=biBLZXLFIDkShMTYy/XzIxeOVgDUTqHRnQZqNHXlb2c=; b=YHDihUIDpvc8TN /cHB9PZKt/A7y0D46pE6Yte+Mz8a4kIIH93Do4myo9KgnwhiE8htuArkyM1yhwb1GJpM8V04Ah5A9 lHL4hu9SyGH82j/Av6NZ/XukbKYnQz5N35/pt00/+bpGsJdKCGlJ+dtJeQpxAhu9lh2OcfbFPUoOH zubrYOvFA9c9fOlSkNi4e6MUl8y+sm5m+ioFybCJALm3sDpRC9A6zd+tdCzRRBnhbb6G53TL2WudG hJnHD+eXWRVcX/1dqvqhNQQZiopksFVA2Hk5jse1OFrCPBq3D922mP/OPVn9S/IYUSVj9YsbGXSV4 0ZuQf1v9f2A+Xq6+GM4w==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1iqm9J-0001st-Fy; Sun, 12 Jan 2020 22:56:01 +0000 Received: from smtp-out.xnet.cz ([178.217.244.18]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1iqm8m-0001CY-LC for openwrt-devel@lists.openwrt.org; Sun, 12 Jan 2020 22:55:32 +0000 Received: from meh.true.cz (meh.true.cz [108.61.167.218]) (Authenticated sender: petr@true.cz) by smtp-out.xnet.cz (Postfix) with ESMTPSA id 56F77378A; Sun, 12 Jan 2020 23:55:22 +0100 (CET) Received: by meh.true.cz (OpenSMTPD) with ESMTP id 54e3bce3; Sun, 12 Jan 2020 23:55:11 +0100 (CET) From: =?utf-8?q?Petr_=C5=A0tetiar?= To: openwrt-devel@lists.openwrt.org Date: Sun, 12 Jan 2020 23:55:13 +0100 Message-Id: <20200112225515.11654-3-ynezz@true.cz> In-Reply-To: <20200112225515.11654-1-ynezz@true.cz> References: <20200112225515.11654-1-ynezz@true.cz> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200112_145529_006553_4C5481D0 X-CRM114-Status: UNSURE ( 9.41 ) X-CRM114-Notice: Please train this message. X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [178.217.244.18 listed in list.dnswl.org] 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record Subject: [OpenWrt-Devel] [PATCH libubox 2/4] tests: blobmsg/json: add more test cases X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Petr_=C5=A0tetiar?= Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org * add missing test with sanitizers * add test case for blobmsg_add_json_from_string * add test cases for all numeric types * print types for each variable Signed-off-by: Petr Štetiar --- tests/cram/test_blobmsg.t | 255 +++++++++++++++++++++++++++++++++++--- tests/test-blobmsg.c | 47 +++++-- 2 files changed, 275 insertions(+), 27 deletions(-) diff --git a/tests/cram/test_blobmsg.t b/tests/cram/test_blobmsg.t index 3a5801a5a0f4..9ab2acf6e2d8 100644 --- a/tests/cram/test_blobmsg.t +++ b/tests/cram/test_blobmsg.t @@ -1,32 +1,255 @@ check that blobmsg is producing expected results: $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH" + $ valgrind --quiet --leak-check=full test-blobmsg + [*] blobmsg dump: + Message: Hello, world! + List: { + 0 (i8) + 100 (i8) + -128 (i8) + 127 (i8) + -32768 (i16) + 32767 (i16) + -2147483648 (i32) + 2147483647 (i32) + -9223372036854775808 (i64) + 9223372036854775807 (i64) + 133.700000 (dbl) + } + Testdata: { + \tdouble : 133.700000 (dbl) (esc) + \tfoo : 0 (i8) (esc) + \tpoo : 100 (i8) (esc) + \tmoo-min : -128 (i8) (esc) + \tmoo-max : 127 (i8) (esc) + \tbar-min : -32768 (i16) (esc) + \tbar-max : 32767 (i16) (esc) + \tbaz-min : -2147483648 (i32) (esc) + \tbaz-max : 2147483647 (i32) (esc) + \ttaz-min : -9223372036854775808 (i64) (esc) + \ttaz-max : 9223372036854775807 (i64) (esc) + \tworld : 2 (str) (esc) + } + + [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]} + + [*] blobmsg from json: + Message: Hello, world! + List: { + 0 (i8) + 1 (i8) + 1 (i8) + 1 (i8) + 32768 (i32) + 32767 (i32) + -2147483648 (i32) + 2147483647 (i32) + -2147483648 (i32) + 2147483647 (i32) + 133.700000 (dbl) + } + Testdata: { + \tdouble : 133.700000 (dbl) (esc) + \tfoo : 0 (i8) (esc) + \tpoo : 1 (i8) (esc) + \tmoo-min : 1 (i8) (esc) + \tmoo-max : 1 (i8) (esc) + \tbar-min : 32768 (i32) (esc) + \tbar-max : 32767 (i32) (esc) + \tbaz-min : -2147483648 (i32) (esc) + \tbaz-max : 2147483647 (i32) (esc) + \ttaz-min : -2147483648 (i32) (esc) + \ttaz-max : 2147483647 (i32) (esc) + \tworld : 2 (str) (esc) + } + + $ test-blobmsg-san + [*] blobmsg dump: + Message: Hello, world! + List: { + 0 (i8) + 100 (i8) + -128 (i8) + 127 (i8) + -32768 (i16) + 32767 (i16) + -2147483648 (i32) + 2147483647 (i32) + -9223372036854775808 (i64) + 9223372036854775807 (i64) + 133.700000 (dbl) + } + Testdata: { + \tdouble : 133.700000 (dbl) (esc) + \tfoo : 0 (i8) (esc) + \tpoo : 100 (i8) (esc) + \tmoo-min : -128 (i8) (esc) + \tmoo-max : 127 (i8) (esc) + \tbar-min : -32768 (i16) (esc) + \tbar-max : 32767 (i16) (esc) + \tbaz-min : -2147483648 (i32) (esc) + \tbaz-max : 2147483647 (i32) (esc) + \ttaz-min : -9223372036854775808 (i64) (esc) + \ttaz-max : 9223372036854775807 (i64) (esc) + \tworld : 2 (str) (esc) + } + + [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]} + + [*] blobmsg from json: Message: Hello, world! List: { - 0 - 1 - 2 - 133.700000 + 0 (i8) + 1 (i8) + 1 (i8) + 1 (i8) + 32768 (i32) + 32767 (i32) + -2147483648 (i32) + 2147483647 (i32) + -2147483648 (i32) + 2147483647 (i32) + 133.700000 (dbl) } Testdata: { - \tdouble : 133.700000 (esc) - \thello : 1 (esc) - \tworld : 2 (esc) + \tdouble : 133.700000 (dbl) (esc) + \tfoo : 0 (i8) (esc) + \tpoo : 1 (i8) (esc) + \tmoo-min : 1 (i8) (esc) + \tmoo-max : 1 (i8) (esc) + \tbar-min : 32768 (i32) (esc) + \tbar-max : 32767 (i32) (esc) + \tbaz-min : -2147483648 (i32) (esc) + \tbaz-max : 2147483647 (i32) (esc) + \ttaz-min : -2147483648 (i32) (esc) + \ttaz-max : 2147483647 (i32) (esc) + \tworld : 2 (str) (esc) } - json: {"message":"Hello, world!","testdata":{"double":133.700000,"hello":1,"world":"2"},"list":[0,1,2,133.700000]} $ test-blobmsg-san + [*] blobmsg dump: + Message: Hello, world! + List: { + 0 (i8) + 100 (i8) + -128 (i8) + 127 (i8) + -32768 (i16) + 32767 (i16) + -2147483648 (i32) + 2147483647 (i32) + -9223372036854775808 (i64) + 9223372036854775807 (i64) + 133.700000 (dbl) + } + Testdata: { + \tdouble : 133.700000 (dbl) (esc) + \tfoo : 0 (i8) (esc) + \tpoo : 100 (i8) (esc) + \tmoo-min : -128 (i8) (esc) + \tmoo-max : 127 (i8) (esc) + \tbar-min : -32768 (i16) (esc) + \tbar-max : 32767 (i16) (esc) + \tbaz-min : -2147483648 (i32) (esc) + \tbaz-max : 2147483647 (i32) (esc) + \ttaz-min : -9223372036854775808 (i64) (esc) + \ttaz-max : 9223372036854775807 (i64) (esc) + \tworld : 2 (str) (esc) + } + + [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]} + + [*] blobmsg from json: + Message: Hello, world! + List: { + 0 (i8) + 1 (i8) + 1 (i8) + 1 (i8) + 32768 (i32) + 32767 (i32) + -2147483648 (i32) + 2147483647 (i32) + -2147483648 (i32) + 2147483647 (i32) + 133.700000 (dbl) + } + Testdata: { + \tdouble : 133.700000 (dbl) (esc) + \tfoo : 0 (i8) (esc) + \tpoo : 1 (i8) (esc) + \tmoo-min : 1 (i8) (esc) + \tmoo-max : 1 (i8) (esc) + \tbar-min : 32768 (i32) (esc) + \tbar-max : 32767 (i32) (esc) + \tbaz-min : -2147483648 (i32) (esc) + \tbaz-max : 2147483647 (i32) (esc) + \ttaz-min : -2147483648 (i32) (esc) + \ttaz-max : 2147483647 (i32) (esc) + \tworld : 2 (str) (esc) + } + + $ test-blobmsg-san + [*] blobmsg dump: + Message: Hello, world! + List: { + 0 (i8) + 100 (i8) + -128 (i8) + 127 (i8) + -32768 (i16) + 32767 (i16) + -2147483648 (i32) + 2147483647 (i32) + -9223372036854775808 (i64) + 9223372036854775807 (i64) + 133.700000 (dbl) + } + Testdata: { + \tdouble : 133.700000 (dbl) (esc) + \tfoo : 0 (i8) (esc) + \tpoo : 100 (i8) (esc) + \tmoo-min : -128 (i8) (esc) + \tmoo-max : 127 (i8) (esc) + \tbar-min : -32768 (i16) (esc) + \tbar-max : 32767 (i16) (esc) + \tbaz-min : -2147483648 (i32) (esc) + \tbaz-max : 2147483647 (i32) (esc) + \ttaz-min : -9223372036854775808 (i64) (esc) + \ttaz-max : 9223372036854775807 (i64) (esc) + \tworld : 2 (str) (esc) + } + + [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]} + + [*] blobmsg from json: Message: Hello, world! List: { - 0 - 1 - 2 - 133.700000 + 0 (i8) + 1 (i8) + 1 (i8) + 1 (i8) + 32768 (i32) + 32767 (i32) + -2147483648 (i32) + 2147483647 (i32) + -2147483648 (i32) + 2147483647 (i32) + 133.700000 (dbl) } Testdata: { - \tdouble : 133.700000 (esc) - \thello : 1 (esc) - \tworld : 2 (esc) + \tdouble : 133.700000 (dbl) (esc) + \tfoo : 0 (i8) (esc) + \tpoo : 1 (i8) (esc) + \tmoo-min : 1 (i8) (esc) + \tmoo-max : 1 (i8) (esc) + \tbar-min : 32768 (i32) (esc) + \tbar-max : 32767 (i32) (esc) + \tbaz-min : -2147483648 (i32) (esc) + \tbaz-max : 2147483647 (i32) (esc) + \ttaz-min : -2147483648 (i32) (esc) + \ttaz-max : 2147483647 (i32) (esc) + \tworld : 2 (str) (esc) } - json: {"message":"Hello, world!","testdata":{"double":133.700000,"hello":1,"world":"2"},"list":[0,1,2,133.700000]} diff --git a/tests/test-blobmsg.c b/tests/test-blobmsg.c index 5e99dc2a42a6..58b0bc4eb574 100644 --- a/tests/test-blobmsg.c +++ b/tests/test-blobmsg.c @@ -1,4 +1,5 @@ #include +#include #include #include "blobmsg.h" @@ -35,22 +36,22 @@ static void dump_attr_data(struct blob_attr *data, int indent, int next_indent) int type = blobmsg_type(data); switch(type) { case BLOBMSG_TYPE_STRING: - indent_printf(indent, "%s\n", blobmsg_get_string(data)); + indent_printf(indent, "%s (str)\n", blobmsg_get_string(data)); break; case BLOBMSG_TYPE_INT8: - indent_printf(indent, "%d\n", blobmsg_get_u8(data)); + indent_printf(indent, "%d (i8)\n", (int8_t) blobmsg_get_u8(data)); break; case BLOBMSG_TYPE_INT16: - indent_printf(indent, "%d\n", blobmsg_get_u16(data)); + indent_printf(indent, "%d (i16)\n", (int16_t) blobmsg_get_u16(data)); break; case BLOBMSG_TYPE_INT32: - indent_printf(indent, "%d\n", blobmsg_get_u32(data)); + indent_printf(indent, "%d (i32)\n", (int32_t) blobmsg_get_u32(data)); break; case BLOBMSG_TYPE_INT64: - indent_printf(indent, "%"PRIu64"\n", blobmsg_get_u64(data)); + indent_printf(indent, "%"PRId64" (i64)\n", (int64_t) blobmsg_get_u64(data)); break; case BLOBMSG_TYPE_DOUBLE: - indent_printf(indent, "%lf\n", blobmsg_get_double(data)); + indent_printf(indent, "%lf (dbl)\n", blobmsg_get_double(data)); break; case BLOBMSG_TYPE_TABLE: case BLOBMSG_TYPE_ARRAY: @@ -117,14 +118,30 @@ fill_message(struct blob_buf *buf) tbl = blobmsg_open_table(buf, "testdata"); blobmsg_add_double(buf, "double", 1.337e2); - blobmsg_add_u32(buf, "hello", 1); + blobmsg_add_u8(buf, "foo", 0); + blobmsg_add_u8(buf, "poo", 100); + blobmsg_add_u8(buf, "moo-min", INT8_MIN); + blobmsg_add_u8(buf, "moo-max", INT8_MAX); + blobmsg_add_u16(buf, "bar-min", INT16_MIN); + blobmsg_add_u16(buf, "bar-max", INT16_MAX); + blobmsg_add_u32(buf, "baz-min", INT32_MIN); + blobmsg_add_u32(buf, "baz-max", INT32_MAX); + blobmsg_add_u64(buf, "taz-min", INT64_MIN); + blobmsg_add_u64(buf, "taz-max", INT64_MAX); blobmsg_add_string(buf, "world", "2"); blobmsg_close_table(buf, tbl); tbl = blobmsg_open_array(buf, "list"); - blobmsg_add_u32(buf, NULL, 0); - blobmsg_add_u32(buf, NULL, 1); - blobmsg_add_u32(buf, NULL, 2); + blobmsg_add_u8(buf, NULL, 0); + blobmsg_add_u8(buf, NULL, 100); + blobmsg_add_u8(buf, NULL, INT8_MIN); + blobmsg_add_u8(buf, NULL, INT8_MAX); + blobmsg_add_u16(buf, NULL, INT16_MIN); + blobmsg_add_u16(buf, NULL, INT16_MAX); + blobmsg_add_u32(buf, NULL, INT32_MIN); + blobmsg_add_u32(buf, NULL, INT32_MAX); + blobmsg_add_u64(buf, NULL, INT64_MIN); + blobmsg_add_u64(buf, NULL, INT64_MAX); blobmsg_add_double(buf, "double", 1.337e2); blobmsg_close_table(buf, tbl); } @@ -136,13 +153,21 @@ int main(int argc, char **argv) blobmsg_buf_init(&buf); fill_message(&buf); + fprintf(stderr, "[*] blobmsg dump:\n"); dump_message(&buf); json = blobmsg_format_json(buf.head, true); if (!json) exit(EXIT_FAILURE); - fprintf(stderr, "json: %s\n", json); + fprintf(stderr, "\n[*] blobmsg to json: %s\n", json); + + blobmsg_buf_init(&buf); + if (!blobmsg_add_json_from_string(&buf, json)) + exit(EXIT_FAILURE); + + fprintf(stderr, "\n[*] blobmsg from json:\n"); + dump_message(&buf); if (buf.buf) free(buf.buf);