#!/bin/bash
# Run as root on a Lustre client. Mount point defaults to /mount_point.
MOUNT=${MOUNT:-/mount_point}
LCTL=${LCTL:-lctl}
groupadd -f -g 61001 testgrp
useradd -M -G testgrp -s /bin/bash testuser 2>/dev/null

# Create a directory with mode 700 + execute-only ACL
mkdir -p $MOUNT/test/d1
chmod 700 $MOUNT/test/d1
chown root:root $MOUNT/test/d1
setfacl -m "g:testgrp:--x" $MOUNT/test/d1
echo "hello" > $MOUNT/test/d1/file.txt
chmod 644 $MOUNT/test/d1/file.txt

# Drop caches to force server round-trip
sync; echo 3 > /proc/sys/vm/drop_caches
$LCTL set_param ldlm.namespaces.*.lru_size=clear 2>/dev/null

echo "--- Test: open (cat) ---"
runuser -u testuser -- cat $MOUNT/test/d1/file.txt && echo "PASS" || echo "FAIL (Issue 1)"

# Drop caches again
sync; echo 3 > /proc/sys/vm/drop_caches
$LCTL set_param ldlm.namespaces.*.lru_size=clear 2>/dev/null

echo "--- Test: stat ---"
runuser -u testuser -- stat $MOUNT/test/d1/file.txt > /dev/null 2>&1 && echo "PASS" || echo "FAIL (Issue 2)"

# Cleanup
rm -rf $MOUNT/test
userdel -f testuser; groupdel testgrp
