LCOV - code coverage report
Current view: top level - test - test-user-util.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 167 167 100.0 %
Date: 2019-08-23 13:36:53 Functions: 12 12 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 118 224 52.7 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include "alloc-util.h"
       4                 :            : #include "format-util.h"
       5                 :            : #include "log.h"
       6                 :            : #include "macro.h"
       7                 :            : #include "path-util.h"
       8                 :            : #include "string-util.h"
       9                 :            : #include "user-util.h"
      10                 :            : 
      11                 :         16 : static void test_uid_to_name_one(uid_t uid, const char *name) {
      12         [ +  + ]:         16 :         _cleanup_free_ char *t = NULL;
      13                 :            : 
      14         [ +  - ]:         16 :         log_info("/* %s("UID_FMT", \"%s\") */", __func__, uid, name);
      15                 :            : 
      16         [ -  + ]:         16 :         assert_se(t = uid_to_name(uid));
      17   [ +  -  +  + ]:         16 :         if (!synthesize_nobody() && streq(name, NOBODY_USER_NAME)) {
      18         [ +  - ]:          4 :                 log_info("(skipping detailed tests because nobody is not synthesized)");
      19                 :          4 :                 return;
      20                 :            :         }
      21         [ -  + ]:         12 :         assert_se(streq_ptr(t, name));
      22                 :            : }
      23                 :            : 
      24                 :         20 : static void test_gid_to_name_one(gid_t gid, const char *name) {
      25         [ +  + ]:         20 :         _cleanup_free_ char *t = NULL;
      26                 :            : 
      27         [ +  - ]:         20 :         log_info("/* %s("GID_FMT", \"%s\") */", __func__, gid, name);
      28                 :            : 
      29         [ -  + ]:         20 :         assert_se(t = gid_to_name(gid));
      30   [ +  -  +  + ]:         20 :         if (!synthesize_nobody() && streq(name, NOBODY_GROUP_NAME)) {
      31         [ +  - ]:          4 :                 log_info("(skipping detailed tests because nobody is not synthesized)");
      32                 :          4 :                 return;
      33                 :            :         }
      34         [ -  + ]:         16 :         assert_se(streq_ptr(t, name));
      35                 :            : }
      36                 :            : 
      37                 :          4 : static void test_parse_uid(void) {
      38                 :            :         int r;
      39                 :            :         uid_t uid;
      40                 :            : 
      41         [ +  - ]:          4 :         log_info("/* %s */", __func__);
      42                 :            : 
      43                 :          4 :         r = parse_uid("100", &uid);
      44         [ -  + ]:          4 :         assert_se(r == 0);
      45         [ -  + ]:          4 :         assert_se(uid == 100);
      46                 :            : 
      47                 :          4 :         r = parse_uid("65535", &uid);
      48         [ -  + ]:          4 :         assert_se(r == -ENXIO);
      49                 :            : 
      50                 :          4 :         r = parse_uid("asdsdas", &uid);
      51         [ -  + ]:          4 :         assert_se(r == -EINVAL);
      52                 :          4 : }
      53                 :            : 
      54                 :          4 : static void test_uid_ptr(void) {
      55         [ +  - ]:          4 :         log_info("/* %s */", __func__);
      56                 :            : 
      57                 :            :         assert_se(UID_TO_PTR(0) != NULL);
      58                 :            :         assert_se(UID_TO_PTR(1000) != NULL);
      59                 :            : 
      60                 :            :         assert_se(PTR_TO_UID(UID_TO_PTR(0)) == 0);
      61                 :            :         assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000);
      62                 :          4 : }
      63                 :            : 
      64                 :          4 : static void test_valid_user_group_name(void) {
      65         [ +  - ]:          4 :         log_info("/* %s */", __func__);
      66                 :            : 
      67         [ -  + ]:          4 :         assert_se(!valid_user_group_name(NULL));
      68         [ -  + ]:          4 :         assert_se(!valid_user_group_name(""));
      69         [ -  + ]:          4 :         assert_se(!valid_user_group_name("1"));
      70         [ -  + ]:          4 :         assert_se(!valid_user_group_name("65535"));
      71         [ -  + ]:          4 :         assert_se(!valid_user_group_name("-1"));
      72         [ -  + ]:          4 :         assert_se(!valid_user_group_name("-kkk"));
      73         [ -  + ]:          4 :         assert_se(!valid_user_group_name("rööt"));
      74         [ -  + ]:          4 :         assert_se(!valid_user_group_name("."));
      75         [ -  + ]:          4 :         assert_se(!valid_user_group_name("eff.eff"));
      76         [ -  + ]:          4 :         assert_se(!valid_user_group_name("foo\nbar"));
      77         [ -  + ]:          4 :         assert_se(!valid_user_group_name("0123456789012345678901234567890123456789"));
      78         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("aaa:bbb"));
      79                 :            : 
      80         [ -  + ]:          4 :         assert_se(valid_user_group_name("root"));
      81         [ -  + ]:          4 :         assert_se(valid_user_group_name("lennart"));
      82         [ -  + ]:          4 :         assert_se(valid_user_group_name("LENNART"));
      83         [ -  + ]:          4 :         assert_se(valid_user_group_name("_kkk"));
      84         [ -  + ]:          4 :         assert_se(valid_user_group_name("kkk-"));
      85         [ -  + ]:          4 :         assert_se(valid_user_group_name("kk-k"));
      86                 :            : 
      87         [ -  + ]:          4 :         assert_se(valid_user_group_name("some5"));
      88         [ -  + ]:          4 :         assert_se(!valid_user_group_name("5some"));
      89         [ -  + ]:          4 :         assert_se(valid_user_group_name("INNER5NUMBER"));
      90                 :          4 : }
      91                 :            : 
      92                 :          4 : static void test_valid_user_group_name_or_id(void) {
      93         [ +  - ]:          4 :         log_info("/* %s */", __func__);
      94                 :            : 
      95         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id(NULL));
      96         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id(""));
      97         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("0"));
      98         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("1"));
      99         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("65534"));
     100         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("65535"));
     101         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("65536"));
     102         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("-1"));
     103         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("-kkk"));
     104         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("rööt"));
     105         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("."));
     106         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("eff.eff"));
     107         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("foo\nbar"));
     108         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("0123456789012345678901234567890123456789"));
     109         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("aaa:bbb"));
     110                 :            : 
     111         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("root"));
     112         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("lennart"));
     113         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("LENNART"));
     114         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("_kkk"));
     115         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("kkk-"));
     116         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("kk-k"));
     117                 :            : 
     118         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("some5"));
     119         [ -  + ]:          4 :         assert_se(!valid_user_group_name_or_id("5some"));
     120         [ -  + ]:          4 :         assert_se(valid_user_group_name_or_id("INNER5NUMBER"));
     121                 :          4 : }
     122                 :            : 
     123                 :          4 : static void test_valid_gecos(void) {
     124         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     125                 :            : 
     126         [ -  + ]:          4 :         assert_se(!valid_gecos(NULL));
     127         [ -  + ]:          4 :         assert_se(valid_gecos(""));
     128         [ -  + ]:          4 :         assert_se(valid_gecos("test"));
     129         [ -  + ]:          4 :         assert_se(valid_gecos("Ümläüt"));
     130         [ -  + ]:          4 :         assert_se(!valid_gecos("In\nvalid"));
     131         [ -  + ]:          4 :         assert_se(!valid_gecos("In:valid"));
     132                 :          4 : }
     133                 :            : 
     134                 :          4 : static void test_valid_home(void) {
     135         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     136                 :            : 
     137         [ -  + ]:          4 :         assert_se(!valid_home(NULL));
     138         [ -  + ]:          4 :         assert_se(!valid_home(""));
     139         [ -  + ]:          4 :         assert_se(!valid_home("."));
     140         [ -  + ]:          4 :         assert_se(!valid_home("/home/.."));
     141         [ -  + ]:          4 :         assert_se(!valid_home("/home/../"));
     142         [ -  + ]:          4 :         assert_se(!valid_home("/home\n/foo"));
     143         [ -  + ]:          4 :         assert_se(!valid_home("./piep"));
     144         [ -  + ]:          4 :         assert_se(!valid_home("piep"));
     145         [ -  + ]:          4 :         assert_se(!valid_home("/home/user:lennart"));
     146                 :            : 
     147         [ -  + ]:          4 :         assert_se(valid_home("/"));
     148         [ -  + ]:          4 :         assert_se(valid_home("/home"));
     149         [ -  + ]:          4 :         assert_se(valid_home("/home/foo"));
     150                 :          4 : }
     151                 :            : 
     152                 :         16 : static void test_get_user_creds_one(const char *id, const char *name, uid_t uid, gid_t gid, const char *home, const char *shell) {
     153                 :         16 :         const char *rhome = NULL;
     154                 :         16 :         const char *rshell = NULL;
     155                 :         16 :         uid_t ruid = UID_INVALID;
     156                 :         16 :         gid_t rgid = GID_INVALID;
     157                 :            :         int r;
     158                 :            : 
     159         [ +  - ]:         16 :         log_info("/* %s(\"%s\", \"%s\", "UID_FMT", "GID_FMT", \"%s\", \"%s\") */",
     160                 :            :                  __func__, id, name, uid, gid, home, shell);
     161                 :            : 
     162                 :         16 :         r = get_user_creds(&id, &ruid, &rgid, &rhome, &rshell, 0);
     163         [ +  - ]:         16 :         log_info_errno(r, "got \"%s\", "UID_FMT", "GID_FMT", \"%s\", \"%s\": %m",
     164                 :            :                        id, ruid, rgid, strnull(rhome), strnull(rshell));
     165   [ +  -  +  + ]:         16 :         if (!synthesize_nobody() && streq(name, NOBODY_USER_NAME)) {
     166         [ +  - ]:          8 :                 log_info("(skipping detailed tests because nobody is not synthesized)");
     167                 :          8 :                 return;
     168                 :            :         }
     169         [ -  + ]:          8 :         assert_se(r == 0);
     170         [ -  + ]:          8 :         assert_se(streq_ptr(id, name));
     171         [ -  + ]:          8 :         assert_se(ruid == uid);
     172         [ -  + ]:          8 :         assert_se(rgid == gid);
     173         [ -  + ]:          8 :         assert_se(path_equal(rhome, home));
     174         [ -  + ]:          8 :         assert_se(path_equal(rshell, shell));
     175                 :            : }
     176                 :            : 
     177                 :         16 : static void test_get_group_creds_one(const char *id, const char *name, gid_t gid) {
     178                 :         16 :         gid_t rgid = GID_INVALID;
     179                 :            :         int r;
     180                 :            : 
     181         [ +  - ]:         16 :         log_info("/* %s(\"%s\", \"%s\", "GID_FMT") */", __func__, id, name, gid);
     182                 :            : 
     183                 :         16 :         r = get_group_creds(&id, &rgid, 0);
     184         [ +  - ]:         16 :         log_info_errno(r, "got \"%s\", "GID_FMT": %m", id, rgid);
     185   [ +  -  +  + ]:         16 :         if (!synthesize_nobody() && streq(name, NOBODY_GROUP_NAME)) {
     186         [ +  - ]:          8 :                 log_info("(skipping detailed tests because nobody is not synthesized)");
     187                 :          8 :                 return;
     188                 :            :         }
     189         [ -  + ]:          8 :         assert_se(r == 0);
     190         [ -  + ]:          8 :         assert_se(streq_ptr(id, name));
     191         [ -  + ]:          8 :         assert_se(rgid == gid);
     192                 :            : }
     193                 :            : 
     194                 :          4 : static void test_make_salt(void) {
     195         [ +  - ]:          4 :         log_info("/* %s */", __func__);
     196                 :            : 
     197                 :          4 :         _cleanup_free_ char *s, *t;
     198                 :            : 
     199         [ -  + ]:          4 :         assert_se(make_salt(&s) == 0);
     200         [ +  - ]:          4 :         log_info("got %s", s);
     201                 :            : 
     202         [ -  + ]:          4 :         assert_se(make_salt(&t) == 0);
     203         [ +  - ]:          4 :         log_info("got %s", t);
     204                 :            : 
     205         [ -  + ]:          4 :         assert(!streq(s, t));
     206                 :          4 : }
     207                 :            : 
     208                 :          4 : int main(int argc, char *argv[]) {
     209                 :          4 :         test_uid_to_name_one(0, "root");
     210                 :          4 :         test_uid_to_name_one(UID_NOBODY, NOBODY_USER_NAME);
     211                 :          4 :         test_uid_to_name_one(0xFFFF, "65535");
     212                 :          4 :         test_uid_to_name_one(0xFFFFFFFF, "4294967295");
     213                 :            : 
     214                 :          4 :         test_gid_to_name_one(0, "root");
     215                 :          4 :         test_gid_to_name_one(GID_NOBODY, NOBODY_GROUP_NAME);
     216                 :          4 :         test_gid_to_name_one(TTY_GID, "tty");
     217                 :          4 :         test_gid_to_name_one(0xFFFF, "65535");
     218                 :          4 :         test_gid_to_name_one(0xFFFFFFFF, "4294967295");
     219                 :            : 
     220                 :          4 :         test_get_user_creds_one("root", "root", 0, 0, "/root", "/bin/sh");
     221                 :          4 :         test_get_user_creds_one("0", "root", 0, 0, "/root", "/bin/sh");
     222                 :          4 :         test_get_user_creds_one(NOBODY_USER_NAME, NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN);
     223                 :          4 :         test_get_user_creds_one("65534", NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN);
     224                 :            : 
     225                 :          4 :         test_get_group_creds_one("root", "root", 0);
     226                 :          4 :         test_get_group_creds_one("0", "root", 0);
     227                 :          4 :         test_get_group_creds_one(NOBODY_GROUP_NAME, NOBODY_GROUP_NAME, GID_NOBODY);
     228                 :          4 :         test_get_group_creds_one("65534", NOBODY_GROUP_NAME, GID_NOBODY);
     229                 :            : 
     230                 :          4 :         test_parse_uid();
     231                 :          4 :         test_uid_ptr();
     232                 :            : 
     233                 :          4 :         test_valid_user_group_name();
     234                 :          4 :         test_valid_user_group_name_or_id();
     235                 :          4 :         test_valid_gecos();
     236                 :          4 :         test_valid_home();
     237                 :            : 
     238                 :          4 :         test_make_salt();
     239                 :            : 
     240                 :          4 :         return 0;
     241                 :            : }

Generated by: LCOV version 1.14