Harald Hoyer 800881
From 87e08d21857b2b2e59f3480e6d0538b535a5897c Mon Sep 17 00:00:00 2001
Harald Hoyer 800881
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Harald Hoyer 800881
Date: Mon, 16 Sep 2013 11:04:20 -0500
Harald Hoyer 800881
Subject: [PATCH] login: fix login_is_valid test
Harald Hoyer 800881
Harald Hoyer 800881
---
Harald Hoyer 800881
 Makefile.am                   | 10 +++++++++-
Harald Hoyer 800881
 src/login/login-shared.c      | 23 ++++++++++++++++++++++-
Harald Hoyer 800881
 src/login/login-shared.h      | 21 +++++++++++++++++++++
Harald Hoyer 800881
 src/login/test-login-shared.c | 38 ++++++++++++++++++++++++++++++++++++++
Harald Hoyer 800881
 4 files changed, 90 insertions(+), 2 deletions(-)
Harald Hoyer 800881
 create mode 100644 src/login/test-login-shared.c
Harald Hoyer 800881
Harald Hoyer 800881
diff --git a/Makefile.am b/Makefile.am
Harald Hoyer 800881
index 6014521..eae001b 100644
Harald Hoyer 800881
--- a/Makefile.am
Harald Hoyer 800881
+++ b/Makefile.am
Harald Hoyer 800881
@@ -3836,6 +3836,13 @@ test_login_LDADD = \
Harald Hoyer 800881
 	libsystemd-login-internal.la \
Harald Hoyer 800881
 	libsystemd-shared.la
Harald Hoyer 800881
 
Harald Hoyer 800881
+test_login_shared_SOURCES = \
Harald Hoyer 800881
+	src/login/test-login-shared.c
Harald Hoyer 800881
+
Harald Hoyer 800881
+test_login_shared_LDADD = \
Harald Hoyer 800881
+	libsystemd-login-internal.la \
Harald Hoyer 800881
+	libsystemd-shared.la
Harald Hoyer 800881
+
Harald Hoyer 800881
 test_inhibit_SOURCES = \
Harald Hoyer 800881
 	src/login/test-inhibit.c
Harald Hoyer 800881
 
Harald Hoyer 800881
@@ -3862,7 +3869,8 @@ manual_tests += \
Harald Hoyer 800881
 	test-inhibit
Harald Hoyer 800881
 
Harald Hoyer 800881
 tests += \
Harald Hoyer 800881
-	test-login-tables
Harald Hoyer 800881
+	test-login-tables \
Harald Hoyer 800881
+	test-login-shared
Harald Hoyer 800881
 
Harald Hoyer 800881
 libsystemd_login_la_SOURCES = \
Harald Hoyer 800881
 	src/login/sd-login.c \
Harald Hoyer 800881
diff --git a/src/login/login-shared.c b/src/login/login-shared.c
Harald Hoyer 800881
index ff13c28..054c775 100644
Harald Hoyer 800881
--- a/src/login/login-shared.c
Harald Hoyer 800881
+++ b/src/login/login-shared.c
Harald Hoyer 800881
@@ -1,8 +1,29 @@
Harald Hoyer 800881
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
Harald Hoyer 800881
+
Harald Hoyer 800881
+/***
Harald Hoyer 800881
+  This file is part of systemd.
Harald Hoyer 800881
+
Harald Hoyer 800881
+  Copyright 2013 Zbigniew Jędrzejewski-Szmek
Harald Hoyer 800881
+
Harald Hoyer 800881
+  systemd is free software; you can redistribute it and/or modify it
Harald Hoyer 800881
+  under the terms of the GNU Lesser General Public License as published by
Harald Hoyer 800881
+  the Free Software Foundation; either version 2.1 of the License, or
Harald Hoyer 800881
+  (at your option) any later version.
Harald Hoyer 800881
+
Harald Hoyer 800881
+  systemd is distributed in the hope that it will be useful, but
Harald Hoyer 800881
+  WITHOUT ANY WARRANTY; without even the implied warranty of
Harald Hoyer 800881
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Harald Hoyer 800881
+  Lesser General Public License for more details.
Harald Hoyer 800881
+
Harald Hoyer 800881
+  You should have received a copy of the GNU Lesser General Public License
Harald Hoyer 800881
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
Harald Hoyer 800881
+***/
Harald Hoyer 800881
+
Harald Hoyer 800881
 #include "login-shared.h"
Harald Hoyer 800881
 #include "def.h"
Harald Hoyer 800881
 
Harald Hoyer 800881
 bool session_id_valid(const char *id) {
Harald Hoyer 800881
         assert(id);
Harald Hoyer 800881
 
Harald Hoyer 800881
-        return id + strspn(id, LETTERS DIGITS) == '\0';
Harald Hoyer 800881
+        return id[0] && id[strspn(id, LETTERS DIGITS)] == '\0';
Harald Hoyer 800881
 }
Harald Hoyer 800881
diff --git a/src/login/login-shared.h b/src/login/login-shared.h
Harald Hoyer 800881
index 728ef00..b2787c9 100644
Harald Hoyer 800881
--- a/src/login/login-shared.h
Harald Hoyer 800881
+++ b/src/login/login-shared.h
Harald Hoyer 800881
@@ -1,3 +1,24 @@
Harald Hoyer 800881
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
Harald Hoyer 800881
+
Harald Hoyer 800881
+/***
Harald Hoyer 800881
+  This file is part of systemd.
Harald Hoyer 800881
+
Harald Hoyer 800881
+  Copyright 2013 Zbigniew Jędrzejewski-Szmek
Harald Hoyer 800881
+
Harald Hoyer 800881
+  systemd is free software; you can redistribute it and/or modify it
Harald Hoyer 800881
+  under the terms of the GNU Lesser General Public License as published by
Harald Hoyer 800881
+  the Free Software Foundation; either version 2.1 of the License, or
Harald Hoyer 800881
+  (at your option) any later version.
Harald Hoyer 800881
+
Harald Hoyer 800881
+  systemd is distributed in the hope that it will be useful, but
Harald Hoyer 800881
+  WITHOUT ANY WARRANTY; without even the implied warranty of
Harald Hoyer 800881
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Harald Hoyer 800881
+  Lesser General Public License for more details.
Harald Hoyer 800881
+
Harald Hoyer 800881
+  You should have received a copy of the GNU Lesser General Public License
Harald Hoyer 800881
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
Harald Hoyer 800881
+***/
Harald Hoyer 800881
+
Harald Hoyer 800881
 #include <stdbool.h>
Harald Hoyer 800881
 
Harald Hoyer 800881
 bool session_id_valid(const char *id);
Harald Hoyer 800881
diff --git a/src/login/test-login-shared.c b/src/login/test-login-shared.c
Harald Hoyer 800881
new file mode 100644
Harald Hoyer 800881
index 0000000..2df6029
Harald Hoyer 800881
--- /dev/null
Harald Hoyer 800881
+++ b/src/login/test-login-shared.c
Harald Hoyer 800881
@@ -0,0 +1,38 @@
Harald Hoyer 800881
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
Harald Hoyer 800881
+
Harald Hoyer 800881
+/***
Harald Hoyer 800881
+  This file is part of systemd.
Harald Hoyer 800881
+
Harald Hoyer 800881
+  Copyright 2013 Zbigniew Jędrzejewski-Szmek
Harald Hoyer 800881
+
Harald Hoyer 800881
+  systemd is free software; you can redistribute it and/or modify it
Harald Hoyer 800881
+  under the terms of the GNU Lesser General Public License as published by
Harald Hoyer 800881
+  the Free Software Foundation; either version 2.1 of the License, or
Harald Hoyer 800881
+  (at your option) any later version.
Harald Hoyer 800881
+
Harald Hoyer 800881
+  systemd is distributed in the hope that it will be useful, but
Harald Hoyer 800881
+  WITHOUT ANY WARRANTY; without even the implied warranty of
Harald Hoyer 800881
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Harald Hoyer 800881
+  Lesser General Public License for more details.
Harald Hoyer 800881
+
Harald Hoyer 800881
+  You should have received a copy of the GNU Lesser General Public License
Harald Hoyer 800881
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
Harald Hoyer 800881
+***/
Harald Hoyer 800881
+
Harald Hoyer 800881
+#include "macro.h"
Harald Hoyer 800881
+#include "login-shared.h"
Harald Hoyer 800881
+
Harald Hoyer 800881
+static void test_session_id_valid(void) {
Harald Hoyer 800881
+        assert_se(session_id_valid("c1"));
Harald Hoyer 800881
+        assert_se(session_id_valid("1234"));
Harald Hoyer 800881
+
Harald Hoyer 800881
+        assert_se(!session_id_valid("1-2"));
Harald Hoyer 800881
+        assert_se(!session_id_valid(""));
Harald Hoyer 800881
+        assert_se(!session_id_valid("\tid"));
Harald Hoyer 800881
+}
Harald Hoyer 800881
+
Harald Hoyer 800881
+int main(int argc, char* argv[]) {
Harald Hoyer 800881
+        test_session_id_valid();
Harald Hoyer 800881
+
Harald Hoyer 800881
+        return 0;
Harald Hoyer 800881
+}