From patchwork Tue Aug 6 06:16:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 95346 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7A75D385C6C8 for ; Tue, 6 Aug 2024 06:18:04 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 9A4903858401 for ; Tue, 6 Aug 2024 06:16:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A4903858401 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9A4903858401 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722924977; cv=none; b=jG8OYzRdBe9qL/+wdcgQXL6I9ANCwmpRqPqM0qDaO/2LrXlClStW3xjybzxmiwQ/AHqxXcqBEZO/wFaRO0dbba9ACz+5U6yncpndKuXRsucICib9efHCnWdmdkQ/iJ4arn6v1ZzrF55Qa4K0dd9dvIK6usTSNOJQesRTa6dn7YU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722924977; c=relaxed/simple; bh=BMn7zLVNPfG+1a3AC8itBynwfPVxcqOGrPU69R7svfk=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=kfvXAxaJbUM5kab+Ffcc346elHSHGpfZC12y8eVqKyLQv0ItuAjJ4Lk6+kdw+DV0lsrjCpNzDuaruwpvtsV0LpXupqXtuhYbDMtIVZuldRi/OwPblw9mN6+A4YiAJDobEueokLeOUFnPEIHOeEnbI076wi6l82eh8JcU/k2CImE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722924975; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=yOTFDqNcnDNCPB+HZ0EhpRYoXOckAuIE7a2A6WO0bnA=; b=I1/atc5/FH3oT5696+jF5ulXDbaKrsQKR3jyGm3E9/sYs+OQZp/0ldsxq4PATfaNSw8Tko QAZdo4jFcBVbJcy495MFDCDLq4hCZAVL3EPKNXfb6yqYTMfucw9m2HwJjjDf1SBS5Szzj7 67gmTnfBi0jeFRA6De0H6miiuRpufNk= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-528-i1Hqm5PbMY-_WEjrKAnRVg-1; Tue, 06 Aug 2024 02:16:13 -0400 X-MC-Unique: i1Hqm5PbMY-_WEjrKAnRVg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0008A19560A1 for ; Tue, 6 Aug 2024 06:16:12 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.40]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 243D11955F40 for ; Tue, 6 Aug 2024 06:16:11 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 1/7] iconv: Base tests for buffer management In-Reply-To: Message-ID: <4bd28169831b2376c6d89e17dd535b41ea32f4fa.1722924862.git.fweimer@redhat.com> References: X-From-Line: 4bd28169831b2376c6d89e17dd535b41ea32f4fa Mon Sep 17 00:00:00 2001 Date: Tue, 06 Aug 2024 08:16:08 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org --- iconv/Makefile | 12 ++- iconv/tst-iconv_prog-buffer.sh | 177 +++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 iconv/tst-iconv_prog-buffer.sh Reviewed-by: DJ Delorie diff --git a/iconv/Makefile b/iconv/Makefile index 65b4a44ab8..b0fa550141 100644 --- a/iconv/Makefile +++ b/iconv/Makefile @@ -76,8 +76,11 @@ include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left)) ifeq ($(run-built-tests),yes) xtests-special += $(objpfx)test-iconvconfig.out -tests-special += $(objpfx)tst-iconv_prog.out -tests-special += $(objpfx)tst-translit-mchar.out +tests-special += \ + $(objpfx)tst-iconv_prog-buffer.out \ + $(objpfx)tst-iconv_prog.out \ + $(objpfx)tst-translit-mchar.out \ + # tests-special endif # Make a copy of the file because gconv module names are constructed @@ -141,3 +144,8 @@ $(objpfx)tst-translit-mchar.out: tst-translit-mchar.sh \ '$(run-program-env)' '$(run-program-prefix-after-env)' \ $< > $@; \ $(evaluate-test) + +$(objpfx)tst-iconv_prog-buffer.out: \ + tst-iconv_prog-buffer.sh $(objpfx)iconv_prog + $(BASH) $< $(common-objdir) '$(test-program-prefix)' > $@; \ + $(evaluate-test) diff --git a/iconv/tst-iconv_prog-buffer.sh b/iconv/tst-iconv_prog-buffer.sh new file mode 100644 index 0000000000..a27107f02b --- /dev/null +++ b/iconv/tst-iconv_prog-buffer.sh @@ -0,0 +1,177 @@ +#!/bin/bash +# Test for iconv (the program) buffer management. +# Copyright (C) 2024 Free Software Foundation, Inc. +# This file is part of the GNU C Library. + +# The GNU C 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. + +# The GNU C 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 the GNU C Library; if not, see +# . + +exec 2>&1 +set -e + +exec {logfd}>&1 + +codir=$1 +test_program_prefix="$2" + +# Use internal converters to avoid issues with module loading. +iconv_args="-f ASCII -t UTF-8" + +failure=false + +tmp=`mktemp -d` +trap 'rm -rf "$tmp"' 0 +echo ABC > "$tmp/abc" +echo DEF > "$tmp/def" +echo GGG > "$tmp/ggg" +echo HH > "$tmp/hh" +echo XY > "$tmp/xy" +echo ZT > "$tmp/zt" +echo OUT > "$tmp/out-template" +printf '\xff' > "$tmp/0xff" +cat "$tmp/xy" "$tmp/0xff" "$tmp/zt" > "$tmp/0xff-wrapped" + +run_iconv () { + local c=0 + if test "${FUNCNAME[2]}" = main; then + c=1 + fi + echo "${BASH_SOURCE[$c]}:${BASH_LINENO[$c]}: iconv $iconv_args $@" >&$logfd + $test_program_prefix $codir/iconv/iconv_prog $iconv_args "$@" +} + +check_out_expected () { + if ! cmp -s "$tmp/out" "$tmp/expected" ; then + echo "error: iconv output difference" >&$logfd + echo "*** expected ***" >&$logfd + cat "$tmp/expected" >&$logfd + echo "*** actual ***" >&$logfd + cat "$tmp/out" >&$logfd + failure=true + fi +} + +expect_files () { + local f + ! test -z "$1" + cp "$tmp/$1" "$tmp/expected" + shift + for f in "$@" ; do + cat "$tmp/$f" >> "$tmp/expected" + done + check_out_expected +} + +check_out () { + cat > "$tmp/expected" + check_out_expected +} + +expect_exit () { + local expected=$1 + shift + # Prevent failure for stopping the script. + if "$@" ; then + actual=$? + else + actual=$? + fi + if test "$actual" -ne "$expected"; then + echo "error: expected exit status $expected, not $actual" >&$logfd + exit 1 + fi +} + +ignore_failure () { + set +e + "$@" + status=$? + set -e +} + +# Concatentation test. +run_iconv -o "$tmp/out" "$tmp/abc" "$tmp/def" +expect_files abc def + +# Single-file in-place conversion. +run_iconv -o "$tmp/out" "$tmp/out" +expect_files abc def + +# Multiple input files with in-place conversion. + +run_iconv -o "$tmp/out" "$tmp/out" "$tmp/abc" +expect_files abc def abc + +# But not if we are writing to standard output. + +cp "$tmp/out-template" "$tmp/out" +run_iconv >"$tmp/out" +expect_files out-template + +cp "$tmp/out-template" "$tmp/out" +run_iconv - >"$tmp/out" +expect_files out-template + +cp "$tmp/out-template" "$tmp/out" +run_iconv /dev/null >>"$tmp/out" +expect_files out-template + +# Conversion errors should avoid clobbering an existing file if +# it is also an input file. + +cp "$tmp/0xff" "$tmp/out" +expect_exit 1 run_iconv -o "$tmp/out" "$tmp/out" +expect_files 0xff + +cp "$tmp/0xff" "$tmp/out" +expect_exit 1 run_iconv -o "$tmp/out" < "$tmp/out" +expect_files 0xff + +cp "$tmp/0xff" "$tmp/out" +expect_exit 1 run_iconv -o "$tmp/out" - < "$tmp/out" +expect_files 0xff + +# If errors are ignored, the file should be overwritten. + +cp "$tmp/out-template" "$tmp/out" +expect_exit 1 \ + run_iconv -c -o "$tmp/out" "$tmp/abc" "$tmp/0xff" "$tmp/def" 2>"$tmp/err" +! test -s "$tmp/err" +expect_files abc def + +# FIXME: This is not correct, -c should not change the exit status. +cp "$tmp/out-template" "$tmp/out" +run_iconv -c -o "$tmp/out" \ + "$tmp/abc" "$tmp/0xff-wrapped" "$tmp/def" 2>"$tmp/err" +! test -s "$tmp/err" +expect_files abc xy zt def + +# If the file does not exist yet, it should not be created on error. + +rm "$tmp/out" +expect_exit 1 run_iconv -o "$tmp/out" "$tmp/0xff" +! test -e "$tmp/out" + +expect_exit 1 run_iconv -o "$tmp/out" < "$tmp/0xff" +! test -e "$tmp/out" + +expect_exit 1 run_iconv -o "$tmp/out" "$tmp/abc" "$tmp/0xff" "$tmp/def" +! test -e "$tmp/out" + +expect_exit 1 run_iconv -o "$tmp/out" "$tmp/abc" - < "$tmp/0xff" "$tmp/def" +! test -e "$tmp/out" + +if $failure ; then + exit 1 +fi