]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/ask-password-api: return "error" when dialogue is cancelled
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 6 Feb 2020 08:32:16 +0000 (09:32 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 6 Feb 2020 09:51:13 +0000 (10:51 +0100)
test-ask-password-api would crash if ^D was pressed.
If think the callers generally expect a non-empty strv as reply. Let's
return an error if we have nothing to return.

Also modernize test-ask-password-api a bit.

src/shared/ask-password-api.c
src/test/test-ask-password-api.c

index 7f6775f4a130c1c45ac04a050814022a2eab2438..a6c83e181a01f6433895fa1002f94de5c456b4bb 100644 (file)
@@ -658,11 +658,15 @@ int ask_password_tty(
                 goto finish;
 
 skipped:
-        if (keyname)
-                (void) add_to_keyring_and_log(keyname, flags, l);
-
-        *ret = TAKE_PTR(l);
-        r = 0;
+        if (strv_isempty(l))
+                r = log_debug_errno(SYNTHETIC_ERRNO(ECANCELED), "Password query was cancelled.");
+        else {
+                if (keyname)
+                        (void) add_to_keyring_and_log(keyname, flags, l);
+
+                *ret = TAKE_PTR(l);
+                r = 0;
+        }
 
 finish:
         if (ttyfd >= 0 && reset_tty) {
index fa91869cf5bb011781b309ffb27279756fd08cd0..13a1064b457898685fa906130df80812da0e2adf 100644 (file)
@@ -1,24 +1,26 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
-#include "alloc-util.h"
 #include "ask-password-api.h"
-#include "log.h"
 #include "strv.h"
+#include "tests.h"
 
-static void ask_password(void) {
+static void test_ask_password(void) {
         int r;
         _cleanup_strv_free_ char **ret = NULL;
 
         r = ask_password_tty(-1, "hello?", "da key", 0, 0, NULL, &ret);
-        assert(r >= 0);
-        assert(strv_length(ret) == 1);
-
-        log_info("Got %s", *ret);
+        if (r == -ECANCELED)
+                assert_se(ret == NULL);
+        else {
+                assert_se(r >= 0);
+                assert_se(strv_length(ret) == 1);
+                log_info("Got \"%s\"", *ret);
+        }
 }
 
 int main(int argc, char **argv) {
-        log_parse_environment();
+        test_setup_logging(LOG_DEBUG);
 
-        ask_password();
+        test_ask_password();
         return EXIT_SUCCESS;
 }