From 2c6af5856b4dd5a767af8cfe67352ce0209af913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Hrdina?= Date: Tue, 9 Jul 2024 09:19:27 +0200 Subject: [PATCH] Add basic test --- Sanity/basic/main.fmf | 20 ++++++++++++ Sanity/basic/test.sh | 34 +++++++++++++++++++ Sanity/basic/test_lockdev.c | 65 +++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 Sanity/basic/main.fmf create mode 100755 Sanity/basic/test.sh create mode 100644 Sanity/basic/test_lockdev.c diff --git a/Sanity/basic/main.fmf b/Sanity/basic/main.fmf new file mode 100644 index 0000000..9ef5f3f --- /dev/null +++ b/Sanity/basic/main.fmf @@ -0,0 +1,20 @@ +summary: Basic test +description: Tests basic functionality of lockdev +contact: FrantiĊĦek Hrdina +component: + - lockdev +test: ./test.sh +framework: beakerlib +recommend: + - lockdev + - gcc + - lockdev-devel +duration: 5m +enabled: true +tag: + - Tier1 +tier: '1' +adjust: + - enabled: false + when: distro < rhel-7 + continue: false diff --git a/Sanity/basic/test.sh b/Sanity/basic/test.sh new file mode 100755 index 0000000..3277a05 --- /dev/null +++ b/Sanity/basic/test.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k +. /usr/share/beakerlib/beakerlib.sh || exit 1 + +rlJournalStart + rlPhaseStartSetup + rlAssertRpm lockdev + + tmpDir=$(pwd) + + rlIsRHEL && repos="--enablerepo=rhel-CRB --enablerepo=rhel-buildroot" + rlIsCentOS && repos="--enablerepo=crb" + rlIsRHELLike && rlRun "dnf install -y $(rlGetRecommended) $repos" + rlCheckDependencies + + rlRun "tmp=\$(mktemp -d)" 0 "Create tmp directory" + rlRun "pushd $tmp" + rlRun "gcc -o test_lockdev $tmpDir/test_lockdev.c -llockdev" + rlRun "chmod +x test_lockdev" + rlPhaseEnd + + rlPhaseStartTest + rlRun "./test_lockdev &> lockdev.log" + rlAssertNotGrep "No tty device found tty" lockdev.log + rlAssertGrep "Device in use" lockdev.log + rlAssertGrep "Device is locked" lockdev.log + rlAssertGrep "Device is unlocked" lockdev.log + rlPhaseEnd + + rlPhaseStartCleanup + rlRun "popd" + rlRun "rm -r $tmp" 0 "Remove tmp directory" + rlPhaseEnd +rlJournalEnd diff --git a/Sanity/basic/test_lockdev.c b/Sanity/basic/test_lockdev.c new file mode 100644 index 0000000..72ec1f8 --- /dev/null +++ b/Sanity/basic/test_lockdev.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_PATH "/dev/" + +int main() { + struct dirent *entry; + DIR *dp; + char device[256]; + int fd; + + dp = opendir(DEVICE_PATH); + if (dp == NULL) { + perror("opendir"); + return 1; + } + + while ((entry = readdir(dp))) { + if (strncmp(entry->d_name, "ttyS", 4) == 0) { + snprintf(device, sizeof(device), "%s%s", DEVICE_PATH, entry->d_name); + break; + } + } + + closedir(dp); + + if (entry == NULL) { + fprintf(stderr, "No tty device found ttyS*\n"); + return 1; + } + + printf("Device in use: %s\n", device); + + fd = open(device, O_RDWR); + if (fd == -1) { + perror("open"); + return 1; + } + + if (dev_lock(device) == -1) { + perror("dev_lock"); + close(fd); + return 1; + } + + printf("Device is locked.\n"); + + if (dev_unlock(device, 0) == -1) { + perror("dev_unlock"); + close(fd); + return 1; + } + + printf("Device is unlocked.\n"); + + close(fd); + return 0; +}