1#!/bin/bash
2
3# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
4# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
5
6[ -f testing.sh ] && . testing.sh
7
8if [ "$(id -u)" -ne 0 ]
9then
10  echo "$SHOWSKIP: useradd (not root)"
11  return 2>/dev/null
12  exit
13fi
14
15# Redirecting all output to /dev/null for grep, adduser and deluser
16arg="&>/dev/null"
17
18#testing "name" "command" "result" "infile" "stdin"
19
20# Default password for adding user is: 'password'
21pass=`echo -ne 'password\npassword\n'`
22
23user="toyTestUser"
24testing "(text)" "useradd $user $arg ||
25   grep '^$user:' /etc/passwd $arg && [ -d /home/$user ] &&
26   echo 'yes'" "yes\n" "" "$pass"
27userdel -r $user $arg
28
29user="toy1Test2User3"
30testing "(alphanumeric)" "useradd $user $arg ||
31   grep '^$user:' /etc/passwd $arg && [ -d /home/$user ] &&
32   echo 'yes'" "yes\n" "" "$pass"
33userdel -r $user $arg
34
35user="987654321"
36testing "(numeric)" "useradd $user $arg ||
37   grep '^$user:' /etc/passwd $arg && [ -d /home/$user ] &&
38   echo 'yes'" "yes\n" "" "$pass"
39userdel -r $user $arg
40
41user="toy.1Test-2User_3"
42testing "(with ./-/_)" "useradd $user $arg ||
43   grep '^$user:' /etc/passwd $arg && [ -d /home/$user ] &&
44   echo 'yes'" "yes\n" "" "$pass"
45userdel -r $user $arg
46
47# 70 characters long string; hereafter, we will use it as per our need.
48user="abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
49testing "(long string)" "useradd $user $arg ||
50   grep '^$user:' /etc/passwd $arg && [ -d /home/$user ] &&
51   echo 'yes'" "yes\n" "" "$pass"
52userdel -r $user $arg
53
54user="toyTestUser"
55testing "dir" "useradd -h $PWD/dir $user $arg ||
56   grep '^$user:.*dir' /etc/passwd $arg && [ -d $PWD/dir ] &&
57   echo 'yes'" "yes\n" "" "$pass"
58userdel -r $user $arg
59rm -rf $PWD/dir
60
61gecos="aaa,bbb,ccc,ddd,eee"
62testing "gecos" "useradd -g '$gecos' $user $arg ||
63   grep '^$user:.*$gecos' /etc/passwd $arg && [ -d /home/$user ] &&
64   echo 'yes'" "yes\n" "" "$pass"
65userdel -r $user $arg
66
67shl="/bin/sh"
68testing "shell" "useradd -s $shl $user $arg ||
69   grep '^$user:.*$shl$' /etc/passwd $arg && [ -d /home/$user ] &&
70   echo 'yes'" "yes\n" "" "$pass"
71userdel -r $user $arg
72
73g_name="root"
74g_id=`grep $g_name':.*:.*' /etc/group | cut -d : -f 3`
75testing "group" "useradd -G $g_name $user $arg ||
76   grep '^$user:.*:.*:$g_id:.*' /etc/passwd $arg && [ -d /home/$user ] &&
77   echo 'yes'" "yes\n" "" "$pass"
78userdel -r $user $arg
79
80testing "(system user)" "useradd -S $user $arg ||
81   grep '^$user:.*:.*:.*' /etc/passwd $arg && [ ! -e /home/$user ] &&
82   echo 'yes'" "yes\n" "" "$pass"
83userdel -r $user $arg
84
85testing "-D" "useradd -D $user $arg ||
86   grep '^$user:.*:.*:.*' /etc/passwd $arg && [ -d /home/$user ] &&
87   echo 'yes'" "yes\n" "" "$pass"
88userdel -r $user $arg
89
90testing "-H" "useradd -H $user $arg ||
91   grep '^$user:.*:.*:.*' /etc/passwd $arg && [ ! -e /home/$user ] &&
92   echo 'yes'" "yes\n" "" "$pass"
93userdel -r $user $arg
94
95testing "dir and -H" "useradd -H -h $PWD/dir $user $arg ||
96   grep '^$user:.*dir' /etc/passwd $arg && [ ! -e $PWD/dir ] &&
97   echo 'yes'" "yes\n" "" "$pass"
98userdel -r $user $arg
99
100testing "-u" "useradd -u 49999 $user $arg ||
101   grep '^$user:x:49999:.*' /etc/passwd $arg && [ -d /home/$user ] &&
102   echo 'yes'" "yes\n" "" "$pass"
103userdel -r $user $arg
104
105