From nobody Wed May 14 20:15:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 153503963974445.983333502641244; Thu, 23 Aug 2018 08:53:59 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 79E7630832C2; Thu, 23 Aug 2018 15:53:57 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3ED802010D15; Thu, 23 Aug 2018 15:53:57 +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 E40A5181A130; Thu, 23 Aug 2018 15:53:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w7NFrpaZ028075 for ; Thu, 23 Aug 2018 11:53:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id E330610CD893; Thu, 23 Aug 2018 15:53:50 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.43.2.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5648310CD890; Thu, 23 Aug 2018 15:53:50 +0000 (UTC) From: Simon Kobyda To: libvir-list@redhat.com Date: Thu, 23 Aug 2018 17:53:43 +0200 Message-Id: <20180823155343.29044-4-skobyda@redhat.com> In-Reply-To: <20180823155343.29044-1-skobyda@redhat.com> References: <20180823155343.29044-1-skobyda@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com Cc: Simon Kobyda Subject: [libvirt] [PATCH v5 3/3] vsh: Added tests X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Thu, 23 Aug 2018 15:53:58 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 For now, there are 9 test cases - testVshTableNew: Creating table with empty header - testVshTableHeader: Printing table with/without header - testVshTableRowAppend: Appending row with various number of cells. Only row with same number of cells as in header is accepted. - testUnicode: Printing table with unicode characters. Checking correct alignment. - testUnicodeArabic: test opposite (right to left) writing - testUnicodeZeroWidthChar - testUnicodeCombiningChar - testUnicodeNonPrintableChar, - testNTables: Create and print varios types of tables - one column, one row table, table without content, standart table... Signed-off-by: Simon Kobyda --- tests/Makefile.am | 8 + tests/vshtabletest.c | 377 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 385 insertions(+) create mode 100644 tests/vshtabletest.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 21a6c823d9..136fe16f71 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -206,6 +206,7 @@ test_programs =3D virshtest sockettest \ virhostdevtest \ virnetdevtest \ virtypedparamtest \ + vshtabletest \ $(NULL) =20 test_libraries =3D libshunload.la \ @@ -938,6 +939,13 @@ metadatatest_SOURCES =3D \ testutils.c testutils.h metadatatest_LDADD =3D $(LDADDS) $(LIBXML_LIBS) =20 +vshtabletest_SOURCES =3D \ + vshtabletest.c \ + testutils.c testutils.h +vshtabletest_LDADD =3D \ + $(LDADDS) \ + ../tools/libvirt_shell.la + virshtest_SOURCES =3D \ virshtest.c \ testutils.c testutils.h diff --git a/tests/vshtabletest.c b/tests/vshtabletest.c new file mode 100644 index 0000000000..1b07c37c56 --- /dev/null +++ b/tests/vshtabletest.c @@ -0,0 +1,377 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include +#include +#include + +#include "internal.h" +#include "testutils.h" +#include "viralloc.h" +#include "../tools/vsh-table.h" + +static int +testVshTableNew(const void *opaque ATTRIBUTE_UNUSED) +{ + if (vshTableNew(NULL)) { + fprintf(stderr, "expected failure when passing null to vshTableNew= \n"); + return -1; + } + + return 0; +} + +static int +testVshTableHeader(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + char *act =3D NULL; + const char *exp =3D +" 1 fedora28 running \n" +" 2 rhel7.5 running \n"; + const char *exp2 =3D +" Id Name State \n" +"--------------------------\n" +" 1 fedora28 running \n" +" 2 rhel7.5 running \n"; + + vshTablePtr table =3D vshTableNew("Id", "Name", "State", + NULL); //to ask about return + if (!table) + goto cleanup; + + vshTableRowAppend(table, "1", "fedora28", "running", NULL); + vshTableRowAppend(table, "2", "rhel7.5", "running", + NULL); + + act =3D vshTablePrintToString(table, false); + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + VIR_FREE(act); + act =3D vshTablePrintToString(table, true); + if (virTestCompareToString(exp2, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +static int +testVshTableRowAppend(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + + vshTablePtr table =3D vshTableNew("Id", "Name", NULL); + if (!table) + goto cleanup; + + if (vshTableRowAppend(table, NULL) >=3D 0) { + fprintf(stderr, "Appending NULL shouldn't work\n"); + ret =3D -1; + } + + if (vshTableRowAppend(table, "2", NULL) >=3D 0) { + fprintf(stderr, "Appending less items than in header\n"); + ret =3D -1; + } + + if (vshTableRowAppend(table, "2", "rhel7.5", "running", + NULL) >=3D 0) { + fprintf(stderr, "Appending more items than in header\n"); + ret =3D -1; + } + + if (vshTableRowAppend(table, "2", "rhel7.5", NULL) < 0) { + fprintf(stderr, "Appending same number of items as in header" + " should not return NULL\n"); + ret =3D -1; + } + + cleanup: + vshTableFree(table); + return ret; +} + +static int +testUnicode(const void *opaque ATTRIBUTE_UNUSED) +{ + + int ret =3D 0; + char *act =3D NULL; + + const char *exp =3D +" Id =E5=90=8D=E7=A8=B1 =D0=B3=D0=BE=D1=81=D1=83=D0=B4= =D0=B0=D1=80=D1=81=D1=82=D0=B2=D0=BE \n" +"-----------------------------------------\n" +" 1 fedora28 running \n" +" 2 =F0=9F=99=8A=F0=9F=99=89=F0=9F=99=88rhel7.5=F0=9F=99=86=F0=9F=99=86= =F0=9F=99=85 running \n"; + vshTablePtr table; + + table =3D vshTableNew("Id", "=E5=90=8D=E7=A8=B1", "=D0=B3=D0=BE=D1=81= =D1=83=D0=B4=D0=B0=D1=80=D1=81=D1=82=D0=B2=D0=BE", NULL); + if (!table) + goto cleanup; + + vshTableRowAppend(table, "1", "fedora28", "running", NULL); + vshTableRowAppend(table, "2", "=F0=9F=99=8A=F0=9F=99=89=F0=9F=99=88rhe= l7.5=F0=9F=99=86=F0=9F=99=86=F0=9F=99=85", "running", + NULL); + + act =3D vshTablePrintToString(table, true); + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +/* Point of this test is to see how table behaves with right to left writi= ng*/ +static int +testUnicodeArabic(const void *opaque ATTRIBUTE_UNUSED) +{ + + int ret =3D 0; + char *act =3D NULL; + + const char *exp =3D +" =EF=BB=A1=EF=BA=8D =EF=BB=A2=EF=BB=A3=EF=BA=8D =EF=BB=95=EF=BA=8E=EF=BA= =8C=EF=BB=A3=EF=BA=93 =EF=BA=93 =EF=BA=8E=EF=BB=A0=EF=BA=BC=EF=BB=8B = =EF=BA=8D=EF=BB=B8=EF=BA=9C=EF= =BB=A7=EF=BA=8E=EF=BB=A7 \n" +"-------------------------------------------------------------------------= ------------------\n" +" 1 =EF=BB=89=EF=BA=AA=EF=BB=B4=EF=BB=9F =EF=BA=8E=EF=BB=A0= =EF=BA=9C=EF=BB=98=EF=BB=B4=EF=BB=9F =EF=BB=95=EF=BA=8E=EF=BB=A3 =EF=BB=8A= =EF=BB=A7, =D9=A3=D9=A0 =EF=BB=8E=EF=BB=B4=EF=BB=A8=EF=BB=B3=EF=BA=8D =EF= =BB=AE=EF=BA=98=EF=BB=A7=EF=BA=8E=EF=BB=A4=EF=BA=97 =EF=BA=8E=EF=BB=A0=EF= =BA=9B=EF=BA=8E=EF=BB=A0=EF=BA=9B=D8=8C =EF=BA=84=EF=BA=B3=EF=BA=AD, =EF=BA= =A9=EF=BB=AE=EF=BB=9F =EF=BA=A9=EF=BB=AE=EF=BB=9F. =EF=BA=84=EF=BB=A3=EF= =BA=8E=EF=BB=A3 =EF=BA=8D =EF=BA=8E=EF=BB=A7 =EF=BB=B2=EF=BB=9C=EF=BB=A7 \= n" +" =EF=BA=BA=EF=BB=94=EF=BA=A3=EF=BA=93 =EF=BA=96=EF=BB=9C=EF=BA= =98=EF=BB=B4=EF=BB=9B=EF=BA=8D=D9=8B =EF=BB=8A=EF=BB=9F, =EF=BA=8E=EF=BB=A0= =EF=BA=A0=EF=BB=A7=EF=BB=AD=EF=BA=A9 =EF=BB=AD=EF=BA=8E=EF=BB=A0=EF=BB=8C= =EF=BA=97=EF=BA=8D=EF=BA=A9 =EF=BA=B5=EF=BA=AD= \n"; + vshTablePtr table; + + table =3D vshTableNew("=EF=BB=A1=EF=BA=8D =EF=BB=A2=EF=BB=A3=EF=BA=8D = =EF=BB=95=EF=BA=8E=EF=BA=8C=EF=BB=A3=EF=BA=93", "=EF=BA=93 =EF=BA=8E=EF=BB= =A0=EF=BA=BC=EF=BB=8B", "=EF=BA=8D=EF=BB=B8=EF=BA=9C=EF=BB=A7=EF=BA=8E=EF= =BB=A7", NULL); + if (!table) + goto cleanup; + vshTableRowAppend(table, + "1", + "=EF=BB=89=EF=BA=AA=EF=BB=B4=EF=BB=9F =EF=BA=8E=EF= =BB=A0=EF=BA=9C=EF=BB=98=EF=BB=B4=EF=BB=9F =EF=BB=95=EF=BA=8E=EF=BB=A3 =EF= =BB=8A=EF=BB=A7, =D9=A3=D9=A0 =EF=BB=8E=EF=BB=B4=EF=BB=A8=EF=BB=B3=EF=BA=8D= =EF=BB=AE=EF=BA=98=EF=BB=A7=EF=BA=8E=EF=BB=A4=EF=BA=97 =EF=BA=8E=EF=BB=A0= =EF=BA=9B=EF=BA=8E=EF=BB=A0=EF=BA=9B=D8=8C =EF=BA=84=EF=BA=B3=EF=BA=AD, =EF= =BA=A9=EF=BB=AE=EF=BB=9F", + "=EF=BA=A9=EF=BB=AE=EF=BB=9F. =EF=BA=84=EF=BB=A3=EF= =BA=8E=EF=BB=A3 =EF=BA=8D =EF=BA=8E=EF=BB=A7 =EF=BB=B2=EF=BB=9C=EF=BB=A7", + NULL); + vshTableRowAppend(table, "=EF=BA=BA=EF=BB=94=EF=BA=A3=EF=BA=93", "=EF= =BA=96=EF=BB=9C=EF=BA=98=EF=BB=B4=EF=BB=9B=EF=BA=8D=D9=8B =EF=BB=8A=EF=BB= =9F, =EF=BA=8E=EF=BB=A0=EF=BA=A0=EF=BB=A7=EF=BB=AD=EF=BA=A9 =EF=BB=AD=EF=BA= =8E=EF=BB=A0=EF=BB=8C=EF=BA=97=EF=BA=8D=EF=BA=A9", "=EF=BA=B5=EF=BA=AD", + NULL); + act =3D vshTablePrintToString(table, true); + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +/* Testing zero-width characters by inserting few zero-width spaces */ +static int +testUnicodeZeroWidthChar(const void *opaque ATTRIBUTE_UNUSED) +{ + + int ret =3D 0; + vshTablePtr table =3D NULL; + const char *exp =3D +" I\u200Bd Name \u200BStatus \n" +"--------------------------\n" +" 1\u200B fedora28 run\u200Bning \n" +" 2 rhel7.5 running \n"; + char *act =3D NULL; + + table =3D vshTableNew("I\u200Bd", "Name", "\u200BStatus", NULL); + if (!table) + goto cleanup; + vshTableRowAppend(table, "1\u200B", "fedora28", "run\u200Bning", NULL); + vshTableRowAppend(table, "2", "rhel7.5", "running", NULL); + act =3D vshTablePrintToString(table, true); + + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +static int +testUnicodeCombiningChar(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + vshTablePtr table =3D NULL; + const char *exp =3D +" Id N=C3=A1me =E2=93=88tatus \n" +"--------------------------\n" +" 1 f=C4=9Bdora28 running \n" +" 2 rhel running \n"; + char *act =3D NULL; + + table =3D vshTableNew("Id", "N=C3=A1me", "=E2=93=88tatus", NULL); + if (!table) + goto cleanup; + vshTableRowAppend(table, "1", "f=C4=9Bdora28", "running", NULL); + vshTableRowAppend(table, "2", "rhel", "running", NULL); + act =3D vshTablePrintToString(table, true); + + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +/* Testing zero-width characters by inserting few zero-width spaces */ +static int +testUnicodeNonPrintableChar(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + vshTablePtr table =3D NULL; + const char *exp =3D +" I\\x09d Name Status \n" +"----------------------------------\n" +" 1 f\\x07edora28 running \n" +" 2 rhel7.5 running \n"; + char *act =3D NULL; + + table =3D vshTableNew("I\td", "Name", "Status", NULL); + if (!table) + goto cleanup; + vshTableRowAppend(table, "1", "f\aedora28", "running", NULL); + vshTableRowAppend(table, "2", "rhel7.5", "running", NULL); + act =3D vshTablePrintToString(table, true); + + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +static int +testNTables(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + vshTablePtr table1 =3D NULL; + vshTablePtr table2 =3D NULL; + vshTablePtr table3 =3D NULL; + const char *exp1 =3D +" Id Name Status \n" +"--------------------------\n" +" 1 fedora28 running \n" +" 2 rhel7.5 running \n"; + const char *exp2 =3D +" Id Name Status \n" +"---------------------\n"; + const char *exp3 =3D +" Id \n" +"-----\n" +" 1 \n" +" 2 \n" +" 3 \n" +" 4 \n"; + char *act1 =3D NULL; + char *act2 =3D NULL; + char *act3 =3D NULL; + + table1 =3D vshTableNew("Id", "Name", "Status", NULL); + if (!table1) + goto cleanup; + vshTableRowAppend(table1, "1", "fedora28", "running", NULL); + vshTableRowAppend(table1, "2", "rhel7.5", "running", NULL); + act1 =3D vshTablePrintToString(table1, true); + + table2 =3D vshTableNew("Id", "Name", "Status", NULL); + if (!table2) + goto cleanup; + act2 =3D vshTablePrintToString(table2, true); + + table3 =3D vshTableNew("Id", NULL); + if (!table3) + goto cleanup; + vshTableRowAppend(table3, "1", NULL); + vshTableRowAppend(table3, "2", NULL); + vshTableRowAppend(table3, "3", NULL); + vshTableRowAppend(table3, "4", NULL); + act3 =3D vshTablePrintToString(table3, true); + + if (virTestCompareToString(exp1, act1) < 0) + ret =3D -1; + if (virTestCompareToString(exp2, act2) < 0) + ret =3D -1; + if (virTestCompareToString(exp3, act3) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act1); + VIR_FREE(act2); + VIR_FREE(act3); + vshTableFree(table1); + vshTableFree(table2); + vshTableFree(table3); + return ret; +} + +static int +mymain(void) +{ + int ret =3D 0; + + if (!setlocale(LC_CTYPE, "en_US.UTF-8")) + return EXIT_AM_SKIP; + + if (virTestRun("testVshTableNew", testVshTableNew, NULL) < 0) + ret =3D -1; + + if (virTestRun("testVshTableHeader", testVshTableHeader, NULL) < 0) + ret =3D -1; + + if (virTestRun("testVshTableRowAppend", testVshTableRowAppend, NULL) <= 0) + ret =3D -1; + + if (virTestRun("testUnicode", testUnicode, NULL) < 0) + ret =3D -1; + + if (virTestRun("testUnicodeArabic", testUnicodeArabic, NULL) < 0) + ret =3D -1; + + if (virTestRun("testUnicodeZeroWidthChar", + testUnicodeZeroWidthChar, + NULL) < 0) + ret =3D -1; + + if (virTestRun("testUnicodeCombiningChar", + testUnicodeCombiningChar, + NULL) < 0) + ret =3D -1; + + if (virTestRun("testUnicodeNonPrintableChar", + testUnicodeNonPrintableChar, + NULL) < 0) + ret =3D -1; + + if (virTestRun("testNTables", testNTables, NULL) < 0) + ret =3D -1; + + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIR_TEST_MAIN(mymain) --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list