From patchwork Tue Mar 6 22:56:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 10263083 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4D3636055D for ; Tue, 6 Mar 2018 22:57:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3E29928BE6 for ; Tue, 6 Mar 2018 22:57:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3289328C02; Tue, 6 Mar 2018 22:57:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3FD1528BED for ; Tue, 6 Mar 2018 22:57:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4F17B356E2; Tue, 6 Mar 2018 22:57:34 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1DBC45D962; Tue, 6 Mar 2018 22:57:34 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E5B844A46E; Tue, 6 Mar 2018 22:57:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w26MvWVS027693 for ; Tue, 6 Mar 2018 17:57:32 -0500 Received: by smtp.corp.redhat.com (Postfix) id 9F4716090D; Tue, 6 Mar 2018 22:57:32 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx01.extmail.prod.ext.phx2.redhat.com [10.5.110.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8EB4F60851; Tue, 6 Mar 2018 22:57:29 +0000 (UTC) Received: from smtp.nue.novell.com (smtp.nue.novell.com [195.135.221.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 413AC81DE8; Tue, 6 Mar 2018 22:57:27 +0000 (UTC) Received: from emea4-mta.ukb.novell.com ([10.120.13.87]) by smtp.nue.novell.com with ESMTP (TLS encrypted); Tue, 06 Mar 2018 23:57:25 +0100 Received: from apollon.suse.de.de (nwb-a10-snat.microfocus.com [10.120.13.202]) by emea4-mta.ukb.novell.com with ESMTP (TLS encrypted); Tue, 06 Mar 2018 22:57:13 +0000 From: Martin Wilck To: Christophe Varoqui Date: Tue, 6 Mar 2018 23:56:29 +0100 Message-Id: <20180306225633.12028-3-mwilck@suse.com> In-Reply-To: <20180306225633.12028-1-mwilck@suse.com> References: <20180306225633.12028-1-mwilck@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 06 Mar 2018 22:57:28 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 06 Mar 2018 22:57:28 +0000 (UTC) for IP:'195.135.221.5' DOMAIN:'smtp.nue.novell.com' HELO:'smtp.nue.novell.com' FROM:'mwilck@suse.com' RCPT:'' X-RedHat-Spam-Score: -2.301 (RCVD_IN_DNSWL_MED, SPF_PASS) 195.135.221.5 smtp.nue.novell.com 195.135.221.5 smtp.nue.novell.com X-Scanned-By: MIMEDefang 2.83 on 10.5.110.25 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: dm-devel@redhat.com Cc: Xose Vazquez Perez , dm-devel@redhat.com, Martin Wilck Subject: [dm-devel] [PATCH 2/6] tests: add unit tests for config file parser X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 06 Mar 2018 22:57:34 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP Add test cases for parsing the config file. Some of these tests currently fail unless the CPP macros at the top are set to 1. The patches that follow fix them. Signed-off-by: Martin Wilck --- tests/Makefile | 2 +- tests/globals.c | 1 + tests/parser.c | 497 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 499 insertions(+), 1 deletion(-) create mode 100644 tests/parser.c diff --git a/tests/Makefile b/tests/Makefile index f6b55836a434..231f73bc7332 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -3,7 +3,7 @@ include ../Makefile.inc CFLAGS += $(BIN_CFLAGS) -I$(multipathdir) -I$(mpathcmddir) LIBDEPS += -L$(multipathdir) -lmultipath -lcmocka -TESTS := uevent +TESTS := uevent parser .SILENT: $(TESTS:%=%.o) .PRECIOUS: $(TESTS:%=%-test) diff --git a/tests/globals.c b/tests/globals.c index 96a56515fd09..80f57bd3639a 100644 --- a/tests/globals.c +++ b/tests/globals.c @@ -6,6 +6,7 @@ struct udev *udev; int logsink = 0; struct config conf = { .uid_attrs = "sd:ID_BOGUS", + .verbosity = 4, }; struct config *get_multipath_config(void) diff --git a/tests/parser.c b/tests/parser.c new file mode 100644 index 000000000000..e77d7ef56caf --- /dev/null +++ b/tests/parser.c @@ -0,0 +1,497 @@ +/* + * Copyright (c) 2018 SUSE Linux GmbH + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * + */ + +#include +#include +#include +#include +#include +#include +// #include "list.h" +#include "parser.h" +#include "vector.h" + +#include "globals.c" + +/* Set these to 1 to get success for current broken behavior */ +/* Strip leading whitespace between quotes */ +#define LSTRIP_QUOTED_WSP 0 +/* Stop parsing at 2nd quote */ +#define TWO_QUOTES_ONLY 0 + +#if TWO_QUOTES_ONLY +static const char quote_marker[] = { '"', '\0' }; +#else +static const char quote_marker[] = { '\0', '"', '\0' }; +#endif +static bool is_quote(const char* token) +{ + return !memcmp(token, quote_marker, sizeof(quote_marker)); +} + +static char *test_file = "test.conf"; + +/* Missing declaration */ +int validate_config_strvec(vector strvec, char *file); + +/* Stringify helpers */ +#define _str_(x) #x +#define str(x) _str_(x) + +static int setup(void **state) +{ + return 0; +} + +static int teardown(void **state) +{ + return 0; +} + +static void test01(void **state) +{ + vector v = alloc_strvec("keyword value"); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 2); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_string_equal(VECTOR_SLOT(v, 1), "value"); + + val = set_value(v); + assert_string_equal(val, "value"); + + free(val); + free_strvec(v); +} + +static void test02(void **state) +{ + vector v = alloc_strvec("keyword \"value\""); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), "value"); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + + val = set_value(v); + assert_string_equal(val, "value"); + + free(val); + free_strvec(v); +} + +static void test03(void **state) +{ + vector v = alloc_strvec("keyword value\n"); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 2); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_string_equal(VECTOR_SLOT(v, 1), "value"); + + val = set_value(v); + assert_string_equal(val, "value"); + + free(val); + free_strvec(v); +} + +static void test04(void **state) +{ + vector v = alloc_strvec("keyword \t value \t \n "); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 2); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_string_equal(VECTOR_SLOT(v, 1), "value"); + + val = set_value(v); + assert_string_equal(val, "value"); + + free(val); + free_strvec(v); +} + +static void test05(void **state) +{ + vector v = alloc_strvec("keyword \t value \t ! comment "); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 2); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_string_equal(VECTOR_SLOT(v, 1), "value"); + + val = set_value(v); + assert_string_equal(val, "value"); + + free(val); + free_strvec(v); +} + +static void test06(void **state) +{ + vector v = alloc_strvec("keyword \t value # \n comment "); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 2); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_string_equal(VECTOR_SLOT(v, 1), "value"); + + val = set_value(v); + assert_string_equal(val, "value"); + + free(val); + free_strvec(v); +} + +static void test07(void **state) +{ + vector v = alloc_strvec("keyword \t value more "); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 3); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_string_equal(VECTOR_SLOT(v, 1), "value"); + assert_string_equal(VECTOR_SLOT(v, 2), "more"); + + val = set_value(v); + assert_string_equal(val, "value"); + + free(val); + free_strvec(v); +} + +static void test08(void **state) +{ +#define QUOTED08 " value more " +#define QUOTED08B "value more " + vector v = alloc_strvec("keyword \t \"" QUOTED08 "\""); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; +#if LSTRIP_QUOTED_WSP + assert_string_equal(VECTOR_SLOT(v, 2), QUOTED08B); +#else + assert_string_equal(VECTOR_SLOT(v, 2), QUOTED08); +#endif + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + + val = set_value(v); +#if LSTRIP_QUOTED_WSP + assert_string_equal(val, QUOTED08B); +#else + assert_string_equal(val, QUOTED08); +#endif + free(val); + free_strvec(v); +} + +static void test09(void **state) +{ +#define QUOTED09 "value # more" + vector v = alloc_strvec("keyword \"" QUOTED09 "\""); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), QUOTED09); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + + val = set_value(v); + assert_string_equal(val, QUOTED09); + + free(val); + free_strvec(v); +} + +static void test10(void **state) +{ +#define QUOTED10 "value ! more" + vector v = alloc_strvec("keyword \"" QUOTED10 "\""); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), QUOTED10); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + + val = set_value(v); + assert_string_equal(val, QUOTED10); + + free(val); + free_strvec(v); +} + +static void test11(void **state) +{ +#define QUOTED11 "value comment" + vector v = alloc_strvec("keyword\"" QUOTED11 "\""); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), QUOTED11); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + + val = set_value(v); + assert_string_equal(val, QUOTED11); + + free(val); + free_strvec(v); +} + +static void test12(void **state) +{ + vector v = alloc_strvec("key\"word\""); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "key"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), "word"); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + + val = set_value(v); + assert_string_equal(val, "word"); + + free(val); + free_strvec(v); +} + +static void test13(void **state) +{ + vector v = alloc_strvec("keyword value \"quoted\""); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 5); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_string_equal(VECTOR_SLOT(v, 1), "value"); + assert_true(is_quote(VECTOR_SLOT(v, 2)));; + assert_string_equal(VECTOR_SLOT(v, 3), "quoted"); + assert_true(is_quote(VECTOR_SLOT(v, 4)));; + + val = set_value(v); + assert_string_equal(val, "value"); + + free(val); + free_strvec(v); +} + +static void test14(void **state) +{ + vector v = alloc_strvec("keyword \"value \" comment\"\""); + char *val; + + assert_int_equal(validate_config_strvec(v, test_file), 0); +#if TWO_QUOTES_ONLY + assert_int_equal(VECTOR_SIZE(v), 7); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), "value "); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + assert_string_equal(VECTOR_SLOT(v, 4), "comment"); + assert_true(is_quote(VECTOR_SLOT(v, 5)));; + assert_true(is_quote(VECTOR_SLOT(v, 6)));; + + val = set_value(v); + assert_string_equal(val, "value "); + +#else + assert_int_equal(VECTOR_SIZE(v), 5); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), "value "); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + assert_string_equal(VECTOR_SLOT(v, 4), " comment\""); + + val = set_value(v); + assert_string_equal(val, "value "); + +#endif + free(val); + free_strvec(v); +} + +static void test15(void **state) +{ +#define QUOTED15 "word value\n comment" + vector v = alloc_strvec("key\"" QUOTED15 "\""); + char *val; + + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "key"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), QUOTED15); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + assert_int_equal(validate_config_strvec(v, test_file), 0); + + val = set_value(v); + assert_string_equal(val, QUOTED15); + + free(val); + free_strvec(v); +} + +static void test16(void **state) +{ + vector v = alloc_strvec("keyword \"2.5\"\" SSD\""); + char *val; + +#if TWO_QUOTES_ONLY + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 6); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), "2.5"); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + assert_string_equal(VECTOR_SLOT(v, 4), "SSD"); + assert_true(is_quote(VECTOR_SLOT(v, 5)));; + + val = set_value(v); + assert_string_equal(val, "2.5"); +#else + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), "2.5\" SSD"); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + + val = set_value(v); + assert_string_equal(val, "2.5\" SSD"); +#endif + free(val); + free_strvec(v); +} + +static void test17(void **state) +{ + vector v = alloc_strvec("keyword \"\"\"\"\" is empty\""); + char *val; +#if TWO_QUOTES_ONLY + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 6); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_true(is_quote(VECTOR_SLOT(v, 2)));; + assert_true(is_quote(VECTOR_SLOT(v, 3)));; +#if LSTRIP_QUOTED_WSP + assert_string_equal(VECTOR_SLOT(v, 4), "is empty"); +#else + assert_string_equal(VECTOR_SLOT(v, 4), " is empty"); +#endif + assert_true(is_quote(VECTOR_SLOT(v, 5)));; + + val = set_value(v); + assert_string_equal(val, ""); +#else + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), "\"\" is empty"); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + + val = set_value(v); + assert_string_equal(val, "\"\" is empty"); +#endif + free(val); + free_strvec(v); +} + +static void test18(void **state) +{ + vector v = alloc_strvec("keyword \"\"\"\""); + char *val; +#if TWO_QUOTES_ONLY + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 5); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_true(is_quote(VECTOR_SLOT(v, 2)));; + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + assert_true(is_quote(VECTOR_SLOT(v, 4)));; + + val = set_value(v); + assert_string_equal(val, ""); +#else + assert_int_equal(validate_config_strvec(v, test_file), 0); + assert_int_equal(VECTOR_SIZE(v), 4); + assert_string_equal(VECTOR_SLOT(v, 0), "keyword"); + assert_true(is_quote(VECTOR_SLOT(v, 1)));; + assert_string_equal(VECTOR_SLOT(v, 2), "\""); + assert_true(is_quote(VECTOR_SLOT(v, 3)));; + + val = set_value(v); + assert_string_equal(val, "\""); +#endif + free(val); + free_strvec(v); +} + +int test_config_parser(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test01), + cmocka_unit_test(test02), + cmocka_unit_test(test03), + cmocka_unit_test(test04), + cmocka_unit_test(test05), + cmocka_unit_test(test06), + cmocka_unit_test(test07), + cmocka_unit_test(test08), + cmocka_unit_test(test09), + cmocka_unit_test(test10), + cmocka_unit_test(test11), + cmocka_unit_test(test12), + cmocka_unit_test(test13), + cmocka_unit_test(test14), + cmocka_unit_test(test15), + cmocka_unit_test(test16), + cmocka_unit_test(test17), + cmocka_unit_test(test18), + }; + return cmocka_run_group_tests(tests, setup, teardown); +} + +int main(void) +{ + int ret = 0; + + ret += test_config_parser(); + return ret; +}