From patchwork Fri Oct 11 12:28:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= X-Patchwork-Id: 1996103 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=b6mVyh2P; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XQ5XS2zTNz1xsc for ; Fri, 11 Oct 2024 23:29:12 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 92EF33857820 for ; Fri, 11 Oct 2024 12:29:10 +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.129.124]) by sourceware.org (Postfix) with ESMTP id 2EC4B3858D26 for ; Fri, 11 Oct 2024 12:28:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2EC4B3858D26 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 2EC4B3858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728649735; cv=none; b=KdwYale2VAg/SHEtiDGREdo0Ccr9Q5U4/tZOfJnjCgnGoEidjctD1O0f5gkBF9gf1e/CffBYW7QPcseUtkFEBmqd6y9xSh2IOMppjSNEO8CWAjwOhp8/QLk4HvjOMkXkffYTlyaFEio8CtYdgc2btDumPPhoutbR/QLPcxqHefU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728649735; c=relaxed/simple; bh=eZTCz2UPs20/GjaRpTZwXyywSZYsSxDJU1q1X1BrLoU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=R0Q/w80qqHwEaWTxSCyQXff9v4N3IvVLFd7/iy/mpeItcCIurd7UwkxIoeNhn7/0f/Zlki+7ot0b0cWmSO2ymAMHxakGpWmPw81dv7DyzvbOmThJVuV5Ud5yhPi9cZs4WpikGXF6+f/OIv+W73+v2AUGswScrHmnHXCe1f4QBu0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728649732; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yGY0WTxnnXyY8Kjy98/48oYsVzz2eBE5fVkbzOQbbJM=; b=b6mVyh2PjQ2cn550kiJ/Y4huXfm2mD8c0jtnRlpon0h+5pfd36bHVkdEbGRREoz3sQ+9PE dGvEMS4misSGCyuewvySHShHEbQqACpxCpYnqrpLLpgmfcMEIiGfOzPgEHjX4rooP1czNS Cgp/SZlMjJ7Lq6fwHB+PRmZC+iy7E8g= Received: from mx-prod-mc-03.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-677-c83ulJxqMtiNSodU1M1Erg-1; Fri, 11 Oct 2024 08:28:51 -0400 X-MC-Unique: c83ulJxqMtiNSodU1M1Erg-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 342A719560B8 for ; Fri, 11 Oct 2024 12:28:51 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.192.32]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7412130001A3 for ; Fri, 11 Oct 2024 12:28:50 +0000 (UTC) From: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= To: libc-alpha@sourceware.org Subject: [PATCH] libio: Add test case for fflush Date: Fri, 11 Oct 2024 14:28:45 +0200 Message-ID: <20241011122845.2350960-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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_H3, 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~incoming=patchwork.ozlabs.org@sourceware.org Hello, This patch adds a test to verify that `fflush (FILE)` and `fflush (NULL)` are semantically equivalent from the FILE perspective. Fred. --- 8< --- Subject: [PATCH] libio: Add test case for fflush Since one path uses _IO_SYNC and the other _IO_OVERFLOW, the newly added test case verifies that `fflush (FILE)` and `fflush (NULL)` are semantically equivalent from the FILE perspective. --- libio/Makefile | 1 + libio/tst-fflush.c | 171 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 libio/tst-fflush.c diff --git a/libio/Makefile b/libio/Makefile index ae704d8767..dff4b603e7 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -98,6 +98,7 @@ tests = \ tst-fclose-unopened \ tst-fclose-unopened2 \ tst-fdopen-seek-failure \ + tst-fflush \ tst-fgetc-after-eof \ tst-fgetwc \ tst-fgetws \ diff --git a/libio/tst-fflush.c b/libio/tst-fflush.c new file mode 100644 index 0000000000..498fa94884 --- /dev/null +++ b/libio/tst-fflush.c @@ -0,0 +1,171 @@ +/* Test that fflush (FILE) and fflush (NULL) are semantically equivalent. + + 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 + . */ + +/* A success on this test doesn't imply the effectiveness of fflush as + we can't ensure that the file wasn't already in the expected state + before the call of the function. It only ensures that, if the test + fails, fflush is broken. */ + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#define CONTENT_SZ_MAX 32 +#define TEST_FILE_COUNT 10 + +#define FILE_FLUSH 0 +#define GLOBAL_FLUSH 1 + +#define FILE_UNCHANGED 0 +#define FILE_CHANGED 1 + +struct +{ + FILE *file; + char *name; + int fd; + char *mfile; +} files[TEST_FILE_COUNT]; + +static void +base_init (int file) +{ + files[file].file = NULL; + files[file].fd = -1; + files[file].name = NULL; + files[file].mfile = NULL; +} + +static void +file_init (int file) +{ + int fd = -1; + + base_init (file); + + if (file >= TEST_FILE_COUNT) + return; + + xclose (create_temp_file ("tst-fflush", &files[file].name)); + + fd = xopen (files[file].name, O_RDONLY, 0); + files[file].mfile = (char *) xmmap (NULL, CONTENT_SZ_MAX, PROT_READ, + MAP_SHARED, fd); + xclose (fd); + + files[file].file = xfopen (files[file].name, "w"); + TEST_VERIFY_EXIT (files[file].file != NULL); + files[file].fd = fileno (files[file].file); +} + +static void +file_cleanup (int file) +{ + xfclose (files[file].file); + free (files[file].name); + xmunmap (files[file].mfile, CONTENT_SZ_MAX); + + base_init (file); +} + +static int +file_changed (int to_check, int global_flush) +{ + struct stat stats = { }; + bool content_matches = 0; + char expected[CONTENT_SZ_MAX] = { }; + + verbose_printf ("Check that %s (%d) exactly contains the data we put in\n", + files[to_check].name, to_check); + + /* File should contain "N:M" where both N and M are one digit exactly. */ + snprintf (expected, sizeof (expected), "%d:%d", global_flush, to_check); + content_matches + = (strncmp (files[to_check].mfile, expected, sizeof (expected)) == 0); + + TEST_VERIFY_EXIT (content_matches != 0); + + TEST_VERIFY_EXIT (fstat (files[to_check].fd, &stats) >= 0); + TEST_VERIFY_EXIT (stats.st_size == 3); + + /* Not reached if the data doesn't match. */ + return FILE_CHANGED; +} + +static void +file_flush (int global_flush) +{ + for (int i = 0; i < TEST_FILE_COUNT; i++) + file_init (i); + + /* Print a unique identifier in each file, that is not too long nor contain + new line to not trigger _IO_OVERFLOW/_IO_SYNC. */ + for (int i = 0; i < TEST_FILE_COUNT; i++) + fprintf (files[i].file, "%d:%d", global_flush, i); + + if (global_flush) + { + fflush (NULL); + } + else + { + for (int i = 0; i < TEST_FILE_COUNT; i++) + fflush (files[i].file); + } + + for (int i = 0; i < TEST_FILE_COUNT; i++) + { + int changed = file_changed (i, global_flush); + + verbose_printf ("Check that file %s has been modified after fflush\n", + files[i].name); + verbose_printf ("File %s; changed %d; global fflush %d\n", + files[i].name, changed, !!global_flush); + TEST_VERIFY_EXIT (changed != FILE_UNCHANGED); + } + + for (int i = 0; i < TEST_FILE_COUNT; i++) + file_cleanup (i); +} + +static int +do_test (void) +{ + verbose_printf ("Checking fflush(FILE)\n"); + file_flush (FILE_FLUSH); + + verbose_printf ("checking fflush(NULL)\n"); + file_flush (GLOBAL_FLUSH); + + return 0; +} + +#include