quot: Add tests

To facilitate the task, we change the comparison function so that users
with equal filesystem usage are sorted by UID, and add an undocumented
option that prevents quot from replacing numerical UIDs with names.  We
also switch from getfsfile(3) to getmntpoint(3) so the first line is
identical regardless of whether we pass quot a mountpoint or a device.

Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D53133

(cherry picked from commit aa870a1935)
(cherry picked from commit b5f3f1a661)
(cherry picked from commit b41551753a)
This commit is contained in:
Dag-Erling Smørgrav 2025-10-17 13:55:12 +02:00
parent 6ec3d4402a
commit 29343c4b8f
5 changed files with 120 additions and 3 deletions

View file

@ -1177,6 +1177,8 @@
..
pw
..
quot
..
rpcbind
..
sa

View file

@ -1,7 +1,10 @@
.include <src.opts.mk>
PROG= quot
MAN= quot.8
LIBADD= ufs
LIBADD= ufs util
HAS_TESTS=
SUBDIR.${MK_TESTS}= tests
WARNS?= 2

View file

@ -55,6 +55,7 @@
/* some flags of what to do: */
static char estimate;
static char count;
static char noname;
static char unused;
static void (*func)(int, struct fs *, char *);
static long blocksize;
@ -281,7 +282,7 @@ user(uid_t uid)
usr--) {
if (!usr->name) {
usr->uid = uid;
if (!(pwd = getpwuid(uid))) {
if (noname || !(pwd = getpwuid(uid))) {
asprintf(&usr->name, "#%u", uid);
} else {
usr->name = strdup(pwd->pw_name);
@ -308,7 +309,10 @@ cmpusers(const void *v1, const void *v2)
u1 = (const struct user *)v1;
u2 = (const struct user *)v2;
return u2->space - u1->space;
return (u2->space > u1->space ? 1 :
u2->space < u1->space ? -1 :
u1->uid > u2->uid ? 1 :
u1->uid < u2->uid ? -1 : 0);
}
#define sortusers(users) (qsort((users),nusers,sizeof(struct user), \
@ -578,6 +582,9 @@ main(int argc, char *argv[])
while (--argc > 0 && **++argv == '-') {
while (*++*argv) {
switch (**argv) {
case 'N':
noname = 1;
break;
case 'n':
func = donames;
break;

View file

@ -0,0 +1,4 @@
PACKAGE= tests
ATF_TESTS_SH= quot_test
.include <bsd.test.mk>

View file

@ -0,0 +1,101 @@
#
# Copyright (c) 2025 Dag-Erling Smørgrav <des@FreeBSD.org>
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Create and mount a UFS filesystem on a small memory disk
quot_setup()
{
atf_check -o save:dev mdconfig -t malloc -s 16M
local dev=$(cat dev)
atf_check -o ignore newfs "$@" /dev/$dev
atf_check mkdir mnt
local mnt=$(realpath mnt)
atf_check mount /dev/$dev "$mnt"
echo "/dev/$dev:" >expect
printf "%5d\t%5d\t%-8s\n" 8 2 "#0" >>expect
}
# Create a directory owned by a given UID
quot_adduid()
{
local uid=$1
atf_check install -d -o $uid -g 0 mnt/$uid
printf "%5d\t%5d\t%-8s\n" 4 1 "#$uid" >>expect
}
# Perform the tests
quot_test()
{
local dev=$(cat dev)
# Create inodes owned by a large number of users to exercise
# hash collisions and rehashing. The code uses an open hash
# table that starts out with only 8 entries and doubles every
# time it fills up.
local uid
for uid in $(seq 1 32); do
quot_adduid $uid
done
# Also create inodes owned by users with long UIDs, up to the
# highest possible value (2^32 - 2, because chown(2) and
# friends interpret 2^32 - 1 as “leave unchanged”).
local shift
for shift in $(seq 6 32); do
quot_adduid $(((1 << shift) - 2))
done
# Since quot operates directly on the underlying device, not
# on the mounted filesystem, we remount read-only to ensure
# that everything gets flushed to the memory disk.
atf_check mount -ur /dev/$dev
atf_check -o file:expect quot -fkN /dev/$dev
}
# Unmount and release the memory disk
quot_cleanup()
{
if [ -d mnt ]; then
umount mnt || true
fi
if [ -f dev ]; then
mdconfig -d -u $(cat dev) || true
fi
}
atf_test_case ufs1 cleanup
ufs1_head()
{
atf_set descr "Test quot on UFS1"
atf_set require.user root
}
ufs1_body()
{
quot_setup -O1
quot_test
}
ufs1_cleanup()
{
quot_cleanup
}
atf_test_case ufs2 cleanup
ufs2_head()
{
atf_set descr "Test quot on UFS2"
atf_set require.user root
}
ufs2_body()
{
quot_setup -O2
quot_test
}
ufs2_cleanup()
{
quot_cleanup
}
atf_init_test_cases()
{
atf_add_test_case ufs1
atf_add_test_case ufs2
}