From patchwork Wed Sep 25 13:51:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Kolosov X-Patchwork-Id: 1989417 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=ALRtDE/a; 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 4XDJCY4G20z1xt4 for ; Wed, 25 Sep 2024 23:55:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6AEB23858D33 for ; Wed, 25 Sep 2024 13:55:35 +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 6CC7A3858D35 for ; Wed, 25 Sep 2024 13:55:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6CC7A3858D35 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 6CC7A3858D35 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=1727272521; cv=none; b=N0xktC6aePRRUEr56+LHQKr/9UXkJCohkX66B9A6qd8J5efbiSP6Xhy/A6D8M/2/+A61yflJau3C9tNdvBOlDofkGHD2WdfAZi3I1lvHTUfrf3qxe/eTQ9c28iOk0dEc87/6OvsbCxytHxGl2G0hoqgZuSjyvhaabepowk6JxgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727272521; c=relaxed/simple; bh=wyASushpfmsY89H2P8pajuVWdoFEPwMQqvH2jGS3XOY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=SXgC0uwk7jFsz2TRI0N7pnslp50VnTLcyo5ZTkn4RjWHtvQwM1G3TqxDkSBk9bwA+KQmWKJTKPPi71IpZj2woVBEac8bkFFft47PwqzU4zwjwjEBWLoyt/MfiPppX+LxEIwzA2tub3VxjRiCDnX0+3JyVGm4IDSRx/BA7FuDFU8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727272518; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xvKu8tSsIg5Geg1S/SYaEYAGrsFcTFp6CEsFG0HI260=; b=ALRtDE/acaAoZBckkRG5U41wNjx+3eiTBkcVQwn63KzyRV1B/oQFhAYSgd9S78Sz1RncH/ a7YTx/44cu4LM1IQy4QUul2huY1qrqvh2WhyyjrXysWuDoF4YyKipRgNGcs7AAOqviItsx maeVbjpM+e+DBTuZoyOP2aGQjZQ8L4Y= 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-494-WNkM99vIMi-80rkeNj9tQQ-1; Wed, 25 Sep 2024 09:55:17 -0400 X-MC-Unique: WNkM99vIMi-80rkeNj9tQQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.12]) (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 4490D1977323 for ; Wed, 25 Sep 2024 13:55:16 +0000 (UTC) Received: from sk-carbon-x9.redhat.com (unknown [10.45.226.68]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4A26419560AA; Wed, 25 Sep 2024 13:55:15 +0000 (UTC) From: Sergey Kolosov To: libc-alpha@sourceware.org Cc: Sergey Kolosov Subject: [PATCH v2] stdio-common: Add new test for fdopen Date: Wed, 25 Sep 2024 15:51:23 +0200 Message-ID: <20240925135507.27703-1-skolosov@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_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 This commit adds fdopen test with all modes. --- Change from v1: Fix fread buffer size in do_test_fdopen_w() and do_test_fdopen_a(). --- stdio-common/Makefile | 1 + stdio-common/tst-fdopen2.c | 246 +++++++++++++++++++++++++++++++++++++ 2 files changed, 247 insertions(+) create mode 100644 stdio-common/tst-fdopen2.c diff --git a/stdio-common/Makefile b/stdio-common/Makefile index e3e5f634c5..89f263894c 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -207,6 +207,7 @@ tests := \ tst-cookie \ tst-dprintf-length \ tst-fdopen \ + tst-fdopen2 \ tst-ferror \ tst-fgets \ tst-fgets2 \ diff --git a/stdio-common/tst-fdopen2.c b/stdio-common/tst-fdopen2.c new file mode 100644 index 0000000000..0c6625f258 --- /dev/null +++ b/stdio-common/tst-fdopen2.c @@ -0,0 +1,246 @@ +/* Test the fdopen function. + 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 + . */ + +#include +#include +#include +#include +#include +#include +#include + +char *tmp_dir; +char *path_to_file; + +void +prepare_tmp_dir (void) +{ + tmp_dir = support_create_temp_directory ("tst-fdopen2"); + path_to_file = xasprintf ("%s/tst-fdopen2.txt", tmp_dir); +} + +/* open temp file descriptor with mode. */ +int +open_tmp_fd (int mode) +{ + int fd = xopen (path_to_file, mode, 0644); + return fd; +} + + +/* close and remove temp file with close. */ +void +close_tmp_fd (int fd) +{ + xclose (fd); + xunlink (path_to_file); +} + +/* close and remove temp file with fclose. */ +void +close_tmp_fp (FILE *fp) +{ + fclose (fp); + xunlink (path_to_file); +} + +/* test "w" fdopen mode. */ +void +do_test_fdopen_w (void) +{ + int fd, ret; + FILE *fp; + fd = open_tmp_fd (O_WRONLY | O_CREAT | O_TRUNC); + + /* test mode mismatch. */ + fp = fdopen (fd, "r"); + if (fp != NULL || errno != EINVAL) + { + close_tmp_fd (fd); + FAIL_EXIT1 ("fdopen (%d, r) should fail with EINVAL: %m", fd); + } + + fp = fdopen (fd, "w"); + if (fp == NULL) + { + close_tmp_fd (fd); + FAIL_EXIT1 ("fdopen (%d, w): %m", fd); + } + + const void *buf = "AAAA"; + ret = fwrite (buf, 1, 4, fp); + if (ret != 4) + { + close_tmp_fp (fp); + FAIL_EXIT1 ("fwrite (): %m"); + } + + unsigned char buf2[4]; + rewind (fp); + clearerr (fp); + /* fread should fail in "w" mode */ + ret = fread (buf2, 1, 4, fp); + if (ret != 0 || ferror (fp) == 0) + { + close_tmp_fp (fp); + FAIL_EXIT1 ("fread should fail in \"w\" mode"); + } + + fclose (fp); +} + +/* test "r" fdopen mode. */ +void +do_test_fdopen_r (void) +{ + int fd, ret; + FILE *fp; + fd = open_tmp_fd (O_RDONLY); + + /* test mode mismatch. */ + fp = fdopen (fd, "w"); + if (fp != NULL || errno != EINVAL) + { + close_tmp_fd (fd); + FAIL_EXIT1 ("fdopen (%d, w) should fail with EINVAL: %m", fd); + } + + fp = fdopen (fd, "r"); + if (fp == NULL) + { + close_tmp_fd (fd); + FAIL_EXIT1 ("fdopen (%d, w): %m", fd); + } + + const void *buf = "BBBB"; + /* fwrite should fail in "r" mode. */ + ret = fwrite (buf, 1, 4, fp); + if (ret != 0 || ferror (fp) == 0) + { + close_tmp_fp (fp); + FAIL_EXIT1 ("fwrite should fail in \"r\" mode"); + } + + unsigned char buf2[4]; + ret = fread (buf2, 1, 4, fp); + if (ret != 4) + { + close_tmp_fp (fp); + FAIL_EXIT1 ("fread (): %m"); + } + + fclose (fp); +} + +/* test "a" fdopen mode. */ +void +do_test_fdopen_a (void) +{ + int fd, ret; + FILE *fp; + fd = open_tmp_fd (O_WRONLY | O_CREAT | O_APPEND); + + /* test mode mismatch. */ + fp = fdopen (fd, "r+"); + if (fp != NULL || errno != EINVAL) + { + close_tmp_fd (fd); + FAIL_EXIT1 ("fdopen (%d, \"r+\") should fail with EINVAL: %m", fd); + } + + fp = fdopen (fd, "a"); + if (fp == NULL) + { + close_tmp_fd (fd); + FAIL_EXIT1 ("fdopen (%d, w): %m", fd); + } + + const void *buf = "CCCC"; + ret = fwrite (buf, 1, 4, fp); + if (ret != 4) + { + close_tmp_fp (fp); + FAIL_EXIT1 ("fwrite (): %m"); + } + + /* fread should fail in "a" mode. */ + unsigned char buf2[4]; + clearerr (fp); + ret = fread (buf2, 1, 4, fp); + if (ret != 0 || ferror (fp) == 0) + { + close_tmp_fp (fp); + FAIL_EXIT1 ("fread should fail \"a\" mode"); + } + + fclose (fp); +} + +void +do_test_fdopen_mode (int mode, const char *fmode) +{ + int fd, ret; + FILE *fp; + fd = open_tmp_fd (mode); + + fp = fdopen (fd, fmode); + if (fp == NULL) + { + close_tmp_fd (fd); + FAIL_EXIT1 ("fdopen (%d, %s): %m", fd, fmode); + } + + const void *buf = "EEEE"; + ret = fwrite (buf, 1, 4, fp); + if (ret != 4) + { + close_tmp_fp (fp); + FAIL_EXIT1 ("fwrite () in mode:%s returns %d: %m", fmode, ret); + } + + rewind (fp); + unsigned char buf2[4]; + ret = fread (buf2, 1, 4, fp); + if (ret != 4) + { + close_tmp_fp (fp); + FAIL_EXIT1 ("fread () in mode:%s returns %d: %m", fmode, ret); + } + + fclose (fp); +} + +static int +do_test (void) +{ + + prepare_tmp_dir (); + + do_test_fdopen_w (); + do_test_fdopen_r (); + do_test_fdopen_a (); + + /* test r+ w+ a+ fdopen modes. */ + do_test_fdopen_mode (O_RDWR, "r+"); + do_test_fdopen_mode (O_RDWR | O_CREAT | O_TRUNC, "w+"); + do_test_fdopen_mode (O_RDWR | O_CREAT | O_APPEND, "a+"); + xunlink (path_to_file); + return 0; +} + +#include