Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ jobs:
run: |
cd build
src/logkeys --help

- name: Run integration tests
run: |
sudo apt install python3-libevdev
scripts/simple_test.sh
1 change: 1 addition & 0 deletions depcomp
1 change: 1 addition & 0 deletions install-sh
1 change: 1 addition & 0 deletions missing
1 change: 1 addition & 0 deletions scripts/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
myconfdir=$(sysconfdir)
myconf_SCRIPTS = logkeys-start.sh logkeys-kill.sh
dist_noinst_SCRIPTS = simple_test.sh
19 changes: 19 additions & 0 deletions scripts/input_events.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file contains a sequence of input events to be fed into
# the fake input device created by the simple_test.sh script.
# Each line represents a single input event, with the format:
# <event_type> <event_code> <value>
#
# The events in this file correspond to the keystrokes "hello<enter>".

EV_KEY KEY_H 1
EV_KEY KEY_H 0
EV_KEY KEY_E 1
EV_KEY KEY_E 0
EV_key KEY_L 1
EV_key KEY_L 0
EV_key KEY_L 1
EV_key KEY_L 0
EV_key KEY_O 1
EV_key KEY_O 0
EV_KEY KEY_ENTER 1
EV_KEY KEY_ENTER 0
81 changes: 81 additions & 0 deletions scripts/simple_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

# A simple integration test for logkeys.
#
# This test creates a fake input device using libevdev,
# then runs logkeys to capture a few simulated keystrokes from this device.
# Finally, it checks that the keystrokes were logged correctly.

set -euo pipefail

INPUT_EVENTS_TXT="$(dirname "$0")/input_events.txt"
LOGKEYS_BIN="$(dirname "$0")/../build/src/logkeys"
FAKE_INPUT_DEV="/tmp/fake-input-dev"
LOG_FILE="/tmp/logkeys.log"

if [ ! -x "$LOGKEYS_BIN" ]; then
echo "Error: logkeys binary not found at $LOGKEYS_BIN"
exit 1
fi

# Clean up from previous runs
rm -f "$FAKE_INPUT_DEV" "$LOG_FILE"

# Create a fake input device and feed it some events.
# This needs to run in the background since libevdev-events-text
# will block waiting for a client to connect.
python3 -c '
import libevdev
import sys
import time

d = libevdev.Device()
d.name = "fake-input-device"
for line in sys.stdin:
line = line.strip()
if not line or line.startswith("#"):
continue
event_type, event_code, value = line.split()
event_type = getattr(libevdev.EV, event_type)
event_code = getattr(libevdev.EV, event_code)
value = int(value)
d.create_event(event_type, event_code, value)
d.create_event(libevdev.EV.EV_SYN, libevdev.EV.SYN_REPORT, 0)
with open("/tmp/fake-input-dev", "wb") as f:
f.write(d.fd.read())
' < "$INPUT_EVENTS_TXT" &
evdev_pid=$!

# Wait for the fake input device to be created
sleep 0.1

# Run logkeys in the background
"$LOGKEYS_BIN" --start --device "$FAKE_INPUT_DEV" --output "$LOG_FILE"
logkeys_pid=$(pgrep logkeys)

# Wait for logkeys to start and log some keys
sleep 0.5

# Stop logkeys
kill "$logkeys_pid"

# Stop the event generator
kill "$evdev_pid"

# Wait for processes to exit
wait "$logkeys_pid" || true
wait "$evdev_pid" || true

# Check that the log file contains the expected output
# The expected output is "hello<enter>"
EXPECTED_OUTPUT="hello<enter>"
ACTUAL_OUTPUT=$(cat "$LOG_FILE" | grep -v '^#' | tr -d '\n')
if [ "$ACTUAL_OUTPUT" = "$EXPECTED_OUTPUT" ]; then
echo "Test passed!"
exit 0
else
echo "Test failed!"
echo "Expected output: $EXPECTED_OUTPUT"
echo "Actual output: $ACTUAL_OUTPUT"
exit 1
fi
Loading