#!/usr/bin/env bats load test_helper # configure.d/lib/pam.sh and the security key and fingerprint items, on PAM # files in the test's own directories, with a fake sudo setup() { common_setup export COREDESKTOP="$REPO" export PAM_DIR="$BATS_TEST_TMPDIR/pam.d" export PAM_VENDOR_DIR="$BATS_TEST_TMPDIR/vendor/pam.d" mkdir -p "$PAM_DIR" "$PAM_VENDOR_DIR" printf '#%%PAM-1.0\nauth\t\tinclude\t\tsystem-auth\naccount\t\tinclude\t\tsystem-auth\nsession\t\tinclude\t\tsystem-auth\n' \ > "$PAM_DIR/sudo" printf '#%%PAM-1.0\n\nauth include system-auth\naccount include system-auth\n' \ > "$PAM_VENDOR_DIR/polkit-1" stub sudo '"$@"' export USER=me } @test "pam_add puts the lines before the first auth line, once" { source "$REPO/configure.d/lib/pam.sh" pam_add sudo pam_test.so "auth sufficient pam_test.so a" "auth sufficient pam_other.so" pam_add sudo pam_test.so "auth sufficient pam_test.so a" [ "$(sed -n 2p "$PAM_DIR/sudo")" = "auth sufficient pam_test.so a" ] [ "$(sed -n 3p "$PAM_DIR/sudo")" = "auth sufficient pam_other.so" ] [[ "$(sed -n 4p "$PAM_DIR/sudo")" == auth*system-auth ]] [ "$(grep -c pam_test.so "$PAM_DIR/sudo")" -eq 1 ] } @test "pam_add copies a vendor PAM file before changing it" { source "$REPO/configure.d/lib/pam.sh" pam_add polkit-1 pam_test.so "auth sufficient pam_test.so" pam_has polkit-1 pam_test.so refute grep -q pam_test.so "$PAM_VENDOR_DIR/polkit-1" grep -q '^account include system-auth$' "$PAM_DIR/polkit-1" } @test "pam_has ignores commented-out lines" { source "$REPO/configure.d/lib/pam.sh" echo "#auth sufficient pam_test.so" >> "$PAM_DIR/sudo" refute pam_has sudo pam_test.so } @test "pam_remove takes out only the matching lines" { source "$REPO/configure.d/lib/pam.sh" local before before="$(cat "$PAM_DIR/sudo")" pam_add sudo pam_test.so "auth sufficient pam_test.so" pam_remove sudo 'pam_test\.so' [ "$(cat "$PAM_DIR/sudo")" = "$before" ] } @test "pam_add fails on a PAM file that doesn't exist anywhere" { source "$REPO/configure.d/lib/pam.sh" run pam_add nothing pam_test.so "auth sufficient pam_test.so" [ "$status" -eq 1 ] } # --- Security key ------------------------------------------------------------ key_setup() { export U2F_MAPPINGS="$BATS_TEST_TMPDIR/u2f_mappings" export HIDRAW="$BATS_TEST_TMPDIR/hidraw" mkdir -p "$HIDRAW/hidraw0/device" # A keyboard's report descriptor: usage page 1 printf '\x05\x01\x09\x06\xa1\x01' > "$HIDRAW/hidraw0/device/report_descriptor" source "$REPO/configure.d/70-security-key.sh" stub pacman stub fido2-token 'echo "/dev/hidraw1: vendor=0x1050, product=0x0407 (Yubico YubiKey OTP+FIDO+CCID)"' stub pamu2fcfg 'echo ":KEY1"' } @test "the security key item shows only when a key is plugged in or set up" { key_setup refute is_installed mkdir -p "$HIDRAW/hidraw1/device" printf '\x06\xd0\xf1\x09\x01\xa1\x01' > "$HIDRAW/hidraw1/device/report_descriptor" is_installed } @test "setting up a security key registers it and adds it to sudo and polkit" { key_setup run run_configure [ "$status" -eq 0 ] [ "$(cat "$U2F_MAPPINGS")" = "me:KEY1" ] grep -q "pam_u2f.so cue origin=pam://coredesktop appid=pam://coredesktop authfile=$U2F_MAPPINGS" "$PAM_DIR/sudo" pam_has polkit-1 pam_u2f.so is_configured } @test "a second key goes on the same line" { key_setup echo "other:KEYX" > "$U2F_MAPPINGS" run run_configure stub pamu2fcfg 'echo ":KEY2"' run run_configure <<< 1 [ "$status" -eq 0 ] [ "$(cat "$U2F_MAPPINGS")" = "$(printf 'other:KEYX\nme:KEY1:KEY2')" ] [ "$(grep -c pam_u2f.so "$PAM_DIR/sudo")" -eq 1 ] } @test "stopping security keys takes them out of PAM and removes your keys" { key_setup echo "other:KEYX" > "$U2F_MAPPINGS" run run_configure run run_configure <<< 2 [ "$status" -eq 0 ] refute pam_has sudo pam_u2f.so refute pam_has polkit-1 pam_u2f.so [ "$(cat "$U2F_MAPPINGS")" = "other:KEYX" ] } @test "setting up a security key fails without one plugged in, changing nothing" { key_setup stub fido2-token run run_configure [ "$status" -eq 1 ] [[ "$output" == *"No security key found"* ]] refute pam_has sudo pam_u2f.so [ ! -e "$U2F_MAPPINGS" ] } # --- Fingerprint ------------------------------------------------------------- fingerprint_setup() { export USB_DEVICES="$BATS_TEST_TMPDIR/usb" export LID_CLOSED="$BATS_TEST_TMPDIR/lid-closed" mkdir -p "$USB_DEVICES" source "$REPO/configure.d/71-fingerprint.sh" stub pacman stub fprintd-list 'if [ -e "$BATS_TEST_TMPDIR/enrolled" ]; then echo "Fingerprints for user me on Goodix:"; echo " - #0: right-index-finger"; else echo "User me has no fingers enrolled for Goodix."; fi' stub fprintd-enroll 'touch "$BATS_TEST_TMPDIR/enrolled"' } # usb_device NAME VENDOR [PRODUCT] [DRIVER] usb_device() { local dir="$USB_DEVICES/$1" mkdir -p "$dir/$1:1.0" echo "$2" > "$dir/idVendor" [ -n "$3" ] && echo "$3" > "$dir/product" if [ -n "$4" ]; then mkdir -p "$BATS_TEST_TMPDIR/drivers/$4" ln -s "$BATS_TEST_TMPDIR/drivers/$4" "$dir/$1:1.0/driver" fi return 0 } @test "the fingerprint item finds readers by name or by vendor" { fingerprint_setup refute is_installed usb_device 1-1 046d "USB Keyboard" usbhid refute is_installed usb_device 1-2 04f3 "ELAN:ARM-M4" is_installed rm -rf "${USB_DEVICES:?}"/* usb_device 1-3 27c6 is_installed } @test "the fingerprint item doesn't take a reader vendor's touchpad for a reader" { fingerprint_setup usb_device 1-1 06cb "" usbhid refute is_installed # libusb's claim, while fprintd uses the reader usb_device 1-2 06cb "" usbfs is_installed } @test "setting up the fingerprint enrolls a finger and adds it to sudo and polkit" { fingerprint_setup usb_device 1-1 27c6 run run_configure [ "$status" -eq 0 ] called fprintd-enroll me [ -x "$LID_CLOSED" ] # The lid check comes right before pam_fprintd, so its success=1 skips it [ "$(grep -A1 "pam_exec.so quiet $LID_CLOSED" "$PAM_DIR/sudo" | tail -n 1 | awk '{ print $3 }')" = pam_fprintd.so ] pam_has polkit-1 pam_fprintd.so is_configured } @test "setting up the fingerprint doesn't enroll again a finger enrolled in Settings" { fingerprint_setup touch "$BATS_TEST_TMPDIR/enrolled" run run_configure [ "$status" -eq 0 ] not_called fprintd-enroll pam_has sudo pam_fprintd.so } @test "stopping the fingerprint takes it and the lid check out of PAM" { fingerprint_setup local before before="$(cat "$PAM_DIR/sudo")" run run_configure run run_configure <<< 1 [ "$status" -eq 0 ] [ "$(cat "$PAM_DIR/sudo")" = "$before" ] refute grep -q lid-closed "$PAM_DIR/polkit-1" }