Path: blob/main/lib/libc/tests/gen/fts_blocks_test.c
39491 views
/*-1* Copyright (c) 2025 Klara, Inc.2*3* SPDX-License-Identifier: BSD-2-Clause4*/56#include <sys/stat.h>78#include <fcntl.h>9#include <fts.h>1011#include <atf-c.h>1213/*14* Create two directories with three files each in lexicographical order,15* then call FTS with a sort block that sorts in reverse lexicographical16* order. This has the least chance of getting a false positive due to17* differing file system semantics. UFS will return the files in the18* order they were created while ZFS will sort them lexicographically; in19* both cases, the order we expect is the reverse.20*/21ATF_TC(fts_blocks_test);22ATF_TC_HEAD(fts_blocks_test, tc)23{24atf_tc_set_md_var(tc, "descr",25"Test FTS with a block in lieu of a comparison function");26}27ATF_TC_BODY(fts_blocks_test, tc)28{29char *args[] = {30"bar", "foo", NULL31};32char *paths[] = {33"foo", "z", "y", "x", "foo",34"bar", "c", "b", "a", "bar",35NULL36};37char **expect = paths;38FTS *fts;39FTSENT *ftse;4041ATF_REQUIRE_EQ(0, mkdir("bar", 0755));42ATF_REQUIRE_EQ(0, close(creat("bar/a", 0644)));43ATF_REQUIRE_EQ(0, close(creat("bar/b", 0644)));44ATF_REQUIRE_EQ(0, close(creat("bar/c", 0644)));45ATF_REQUIRE_EQ(0, mkdir("foo", 0755));46ATF_REQUIRE_EQ(0, close(creat("foo/x", 0644)));47ATF_REQUIRE_EQ(0, close(creat("foo/y", 0644)));48ATF_REQUIRE_EQ(0, close(creat("foo/z", 0644)));49fts = fts_open_b(args, 0,50^(const FTSENT * const *a, const FTSENT * const *b) {51return (strcmp((*b)->fts_name, (*a)->fts_name));52});53ATF_REQUIRE_MSG(fts != NULL, "fts_open_b(): %m");54while ((ftse = fts_read(fts)) != NULL && *expect != NULL) {55ATF_CHECK_STREQ(*expect, ftse->fts_name);56expect++;57}58ATF_CHECK_EQ(NULL, ftse);59ATF_CHECK_EQ(NULL, *expect);60ATF_REQUIRE_EQ_MSG(0, fts_close(fts), "fts_close(): %m");61}6263ATF_TP_ADD_TCS(tp)64{65ATF_TP_ADD_TC(tp, fts_blocks_test);66return (atf_no_error());67}686970