summaryrefslogtreecommitdiff
path: root/src/test/test-terminal-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-05-12 16:05:40 +0200
committerLennart Poettering <lennart@poettering.net>2021-05-19 17:58:01 +0200
commit11f3c130aa6a61021f2e4106609df4d052e28a52 (patch)
tree336a7d16f29c4d01bd67ee4de83f50320642337b /src/test/test-terminal-util.c
parent0ba976e8da8f4878e478d02468a93977a9d3ee35 (diff)
downloadsystemd-11f3c130aa6a61021f2e4106609df4d052e28a52.tar.gz
terminal: don't hardcode major number of PTYs
Hardcoding major numbers sucks. And we generally don't do it, except when determining whether something is a PTY. Thing though is that we don't actually need to do that here either, hence don#t.
Diffstat (limited to 'src/test/test-terminal-util.c')
-rw-r--r--src/test/test-terminal-util.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c
index 508f0c03ee..e793a74b13 100644
--- a/src/test/test-terminal-util.c
+++ b/src/test/test-terminal-util.c
@@ -3,6 +3,7 @@
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
+#include <sys/stat.h>
#include <unistd.h>
#include "alloc-util.h"
@@ -153,6 +154,29 @@ static void test_text(void) {
}
}
+static void test_get_ctty(void) {
+ _cleanup_free_ char *ctty = NULL;
+ struct stat st;
+ dev_t devnr;
+ int r;
+
+ r = get_ctty(0, &devnr, &ctty);
+ if (r < 0) {
+ log_notice_errno(r, "Apparently called without a controlling TTY, cutting get_ctty() test short: %m");
+ return;
+ }
+
+ /* In almost all cases STDIN will match our controlling TTY. Let's verify that and then compare paths */
+ assert_se(fstat(STDIN_FILENO, &st) >= 0);
+ if (S_ISCHR(st.st_mode) && st.st_rdev == devnr) {
+ _cleanup_free_ char *stdin_name = NULL;
+
+ assert_se(getttyname_malloc(STDIN_FILENO, &stdin_name) >= 0);
+ assert_se(path_equal(stdin_name, ctty));
+ } else
+ log_notice("Not invoked with stdin == ctty, cutting get_ctty() test short");
+}
+
int main(int argc, char *argv[]) {
test_setup_logging(LOG_INFO);
@@ -161,6 +185,7 @@ int main(int argc, char *argv[]) {
test_getttyname_malloc();
test_colors();
test_text();
+ test_get_ctty();
return 0;
}