]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-terminal-util.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / test / test-terminal-util.c
index 508f0c03ee332909ad60858a5c8939598937fc40..0b46cad9827a40b81adf46964c01b8d719d1d6e0 100644 (file)
@@ -3,17 +3,18 @@
 #include <fcntl.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <sys/stat.h>
 #include <unistd.h>
 
 #include "alloc-util.h"
 #include "fd-util.h"
+#include "fs-util.h"
 #include "macro.h"
 #include "path-util.h"
 #include "strv.h"
 #include "terminal-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
-#include "util.h"
 
 #define LOREM_IPSUM "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " \
         "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " \
@@ -21,9 +22,7 @@
         "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " \
         "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
 
-static void test_default_term_for_tty(void) {
-        log_info("/* %s */", __func__);
-
+TEST(default_term_for_tty) {
         puts(default_term_for_tty("/dev/tty23"));
         puts(default_term_for_tty("/dev/ttyS23"));
         puts(default_term_for_tty("/dev/tty0"));
@@ -38,13 +37,11 @@ static void test_default_term_for_tty(void) {
         puts(default_term_for_tty("console"));
 }
 
-static void test_read_one_char(void) {
+TEST(read_one_char) {
         _cleanup_fclose_ FILE *file = NULL;
         char r;
         bool need_nl;
-        char name[] = "/tmp/test-read_one_char.XXXXXX";
-
-        log_info("/* %s */", __func__);
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-read_one_char.XXXXXX";
 
         assert_se(fmkostemp_safe(name, "r+", &file) == 0);
 
@@ -64,15 +61,11 @@ static void test_read_one_char(void) {
         assert_se(fputs("\n", file) >= 0);
         rewind(file);
         assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
-
-        assert_se(unlink(name) >= 0);
 }
 
-static void test_getttyname_malloc(void) {
+TEST(getttyname_malloc) {
         _cleanup_free_ char *ttyname = NULL;
-        _cleanup_close_ int master = -1;
-
-        log_info("/* %s */", __func__);
+        _cleanup_close_ int master = -EBADF;
 
         assert_se((master = posix_openpt(O_RDWR|O_NOCTTY)) >= 0);
         assert_se(getttyname_malloc(master, &ttyname) >= 0);
@@ -129,16 +122,12 @@ static const Color colors[] = {
         { "highlight-grey-underline", ansi_highlight_grey_underline },
 };
 
-static void test_colors(void) {
-        log_info("/* %s */", __func__);
-
+TEST(colors) {
         for (size_t i = 0; i < ELEMENTSOF(colors); i++)
                 printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal());
 }
 
-static void test_text(void) {
-        log_info("/* %s */", __func__);
-
+TEST(text) {
         for (size_t i = 0; !streq(colors[i].name, "underline"); i++) {
                 bool blwh = strstr(colors[i].name, "black")
                         || strstr(colors[i].name, "white");
@@ -153,14 +142,38 @@ static void test_text(void) {
         }
 }
 
-int main(int argc, char *argv[]) {
-        test_setup_logging(LOG_INFO);
+TEST(get_ctty) {
+        _cleanup_free_ char *ctty = NULL;
+        struct stat st;
+        dev_t devnr;
+        int r;
 
-        test_default_term_for_tty();
-        test_read_one_char();
-        test_getttyname_malloc();
-        test_colors();
-        test_text();
+        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");
+}
+
+TEST(get_default_background_color) {
+        double red, green, blue;
+        int r;
 
-        return 0;
+        r = get_default_background_color(&red, &green, &blue);
+        if (r < 0)
+                log_notice_errno(r, "Can't get terminal default background color: %m");
+        else
+                log_notice("R=%g G=%g B=%g", red, green, blue);
 }
+
+DEFINE_TEST_MAIN(LOG_INFO);