From patchwork Fri Aug 30 19:53:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 96826 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 450DB38654B2 for ; Fri, 30 Aug 2024 19:55:06 +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 3330F385020E for ; Fri, 30 Aug 2024 19:53:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3330F385020E 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 3330F385020E 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=1725047641; cv=none; b=uQmO5GSXiHM2qNRosPG1j3tNRUMlHhQ571Vc8PyQkBSSEgxDl3xXXu4yL5/Rzemd+N+11roqckh+6BvuZ2O09E+4DZZDFggj0Wn9sbo8E/TdRgRVZF67g26Jis6tRkw0bvhRfmxZgIjgyOgjofZQsIoRdad74zFmzrbNwDFt1ls= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047641; c=relaxed/simple; bh=Kabzf115xjrDGj9V3dl4i3TvQPcKB+kVIGzeIQwQ79c=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=SlKs4T7DgMs37g/qFApWYY8ZikQTgau8H1dtX/FdOZfReSik2FZ+ElbXH32+AeNwnyHqSLImQGyVF1o0y6Rr7V8JHStyOQAVRPsDUjLnSgoccVv4TJ/aRRo1ppxnFhOS3U1TvgGKpz6xRWxy45sovudD1dKunDhjpQfddyAiYI8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047637; 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=QOa9Wn6CMTbI80+htIqJSBS8Miv/9p+xavHdkFRKNg8=; b=EreBPbxV5X48GEi3MlKPpHU1xZ0/rnFuaSiSqfE43d3PszZadGhRTtL0Y7SsSv3l8pdNyJ jabTm8KnWop7pHzLbuMB43gPvYCOF7VIjcaAR6kjzeBVX0A1wXj0gtFsh29bKy0V4RLz6f s2G3eCaEwNZn8mjwWtor+3dhJZpijuI= 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-447--pnhUjilONC9TWzr5wh73g-1; Fri, 30 Aug 2024 15:53:56 -0400 X-MC-Unique: -pnhUjilONC9TWzr5wh73g-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [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 D18181955D4C for ; Fri, 30 Aug 2024 19:53:55 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CE91F19560AA for ; Fri, 30 Aug 2024 19:53:54 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 11/13] dirent: Add tst-readdir-long In-Reply-To: Message-ID: <4405f6fbaec624b13324d695907995ebb2bdf127.1725047142.git.fweimer@redhat.com> References: X-From-Line: 4405f6fbaec624b13324d695907995ebb2bdf127 Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:53:51 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) 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=-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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 It tests long names and ENAMETOOLONG handling, specifically for readdir_r. This is a regression test for bug 14699, bug 32124, and bug 32128. Reviewed-by: DJ Delorie --- dirent/Makefile | 1 + dirent/tst-readdir-long.c | 231 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 dirent/tst-readdir-long.c diff --git a/dirent/Makefile b/dirent/Makefile index f9056724f0..91edcf9e70 100644 --- a/dirent/Makefile +++ b/dirent/Makefile @@ -61,6 +61,7 @@ tests := \ tst-closedir-leaks \ tst-fdopendir \ tst-fdopendir2 \ + tst-readdir-long \ tst-scandir \ tst-scandir64 \ tst-seekdir \ diff --git a/dirent/tst-readdir-long.c b/dirent/tst-readdir-long.c new file mode 100644 index 0000000000..ec48e12300 --- /dev/null +++ b/dirent/tst-readdir-long.c @@ -0,0 +1,231 @@ +/* Test readdir (+variants) behavior with file names of varying length. + 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 +#include +#include +#include + +/* If positive, at this length an EMSGSIZE error is injected. */ +static int inject_error_at_length; + +/* Return a file name, LENGTH bytes long. */ +static char * +name_of_length (size_t length) +{ + char *result = xmalloc (length + 1); + unsigned int prefix = snprintf (result, length + 1, "%zu-", length); + for (size_t i = prefix; i < length; ++i) + result[i] = 'A' + ((length + i) % 26); + result[length] = '\0'; + return result; +} + +/* Add the directory entry at OFFSET to the stream D. */ +static uint64_t +add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset) +{ + unsigned int length = offset + 1; + if (length > 1000) + /* Longer than what is possible to produce with 256 + UTF-8-encoded Unicode code points. */ + return 0; + + char *to_free = NULL; + const char *name; + uint64_t ino = 1000 + length; /* Arbitrary value, distinct from 1. */ + uint32_t type = DT_REG; + if (offset <= 1) + { + type = DT_DIR; + name = ".." + !offset; /* "." or "..". */ + ino = 1; + } + else if (length == 1000) + name = "short"; + else + { + to_free = name_of_length (length); + name = to_free; + } + + ++offset; + bool added = support_fuse_dirstream_add (d, ino, offset, type, name); + free (to_free); + if (added) + return offset; + else + return 0; +} + +/* Set to true if getdents64 should produce only one entry. */ +static bool one_entry_per_getdents64; + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + switch (inh->opcode) + { + case FUSE_READDIR: + if (inh->nodeid == 1) + { + uint64_t offset = support_fuse_cast (READ, inh)->offset; + if (inject_error_at_length == offset + 1) + support_fuse_reply_error (f, EMSGSIZE); + else + { + struct support_fuse_dirstream *d + = support_fuse_prepare_readdir (f); + while (true) + { + offset = add_directory_entry (d, offset); + if (offset == 0 || one_entry_per_getdents64 + /* Error will be reported at next READDIR. */ + || offset + 1 == inject_error_at_length) + break; + } + support_fuse_reply_prepared (f); + } + } + else + support_fuse_reply_error (f, EIO); + break; + default: + FAIL ("unexpected event %s", support_fuse_opcode (inh->opcode)); + support_fuse_reply_error (f, EIO); + } + } +} + +/* Run the tests for the specified readdir variant OP. */ +static void +run_readdir_tests (struct support_fuse *f, enum support_readdir_op op) +{ + printf ("info: testing %s (inject_error=%d unbuffered=%d)\n", + support_readdir_function (op), inject_error_at_length, + (int) one_entry_per_getdents64); + + bool testing_r = support_readdir_r_variant (op); + + DIR *dir = xopendir (support_fuse_mountpoint (f)); + struct support_dirent e = { 0, }; + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE (e.d_ino, 1); + TEST_COMPARE_STRING (e.d_name, "."); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE (e.d_ino, 1); + TEST_COMPARE_STRING (e.d_name, ".."); + + for (unsigned int i = 3; i < 1000; ++i) + { + if (i == inject_error_at_length) + /* Error expected below. */ + break; + + if (i >= sizeof ((struct dirent) { 0, }.d_name) && testing_r) + /* This is a readir_r test. The longer names are not + available because they do not fit into struct dirent. */ + break; + + char *expected_name = name_of_length (i); + TEST_COMPARE (strlen (expected_name), i); + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE (e.d_ino, 1000 + i); + TEST_COMPARE_STRING (e.d_name, expected_name); + free (expected_name); + } + + if (inject_error_at_length == 0) + { + /* Check that the ENAMETOOLONG error does not prevent reading a + later short name. */ + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE (e.d_ino, 2000); + TEST_COMPARE_STRING (e.d_name, "short"); + + if (testing_r) + /* An earlier name was too long. */ + support_readdir_expect_error (dir, op, ENAMETOOLONG); + else + /* Entire directory read without error. */ + TEST_VERIFY (!support_readdir (dir, op, &e)); + } + else + support_readdir_expect_error (dir, op, EMSGSIZE); + + free (e.d_name); + xclosedir (dir); +} + +/* Run all readdir variants for both fully-buffered an unbuffered + (one-at-a-time) directory streams. */ +static void +run_fully_buffered_and_singleton_buffers (struct support_fuse *f) +{ + for (int do_one_entry = 0; do_one_entry < 2; ++do_one_entry) + { + one_entry_per_getdents64 = do_one_entry; + for (enum support_readdir_op op = 0; op <= support_readdir_op_last(); + ++op) + run_readdir_tests (f, op); + } +} + +static int +do_test (void) +{ + /* Smoke test for name_of_length. */ + { + char *name = name_of_length (5); + TEST_COMPARE_STRING (name, "5-HIJ"); + free (name); + + name = name_of_length (6); + TEST_COMPARE_STRING (name, "6-IJKL"); + free (name); + } + + support_fuse_init (); + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + + run_fully_buffered_and_singleton_buffers (f); + + inject_error_at_length = 100; + run_fully_buffered_and_singleton_buffers (f); + + inject_error_at_length = 300; + run_fully_buffered_and_singleton_buffers (f); + + support_fuse_unmount (f); + return 0; +} + +#include