1#!/bin/bash
2# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
3# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
4
5[ -f testing.sh ] && . testing.sh
6
7if [ "$(id -u)" -ne 0 ]
8then
9  echo "$SHOWSKIP: groupadd (not root)"
10  return 2>/dev/null
11  exit
12fi
13
14# 70 characters long string; hereafter, we will use it as per our need.
15_s70="abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
16
17echo "# usage: addgroup [-g GID] [USER] GROUP
18# Add a group or add a user to a group."
19
20# Redirecting all output to /dev/null for grep and delgroup
21arg="&>/dev/null"
22
23#testing "name" "command" "result" "infile" "stdin"
24
25testing "group_name (text)" "groupadd toyTestGroup &&
26   grep '^toyTestGroup:' /etc/group $arg && groupdel toyTestGroup $arg &&
27   echo 'yes'" "yes\n" "" ""
28testing "group_name (alphanumeric)" "groupadd toy1Test2Group3 &&
29   grep '^toy1Test2Group3:' /etc/group $arg && groupdel toy1Test2Group3 $arg &&
30   echo 'yes'" "yes\n" "" ""
31testing "group_name (numeric)" "groupadd 987654321 &&
32   grep '^987654321:' /etc/group $arg && groupdel 987654321 $arg &&
33   echo 'yes'" "yes\n" "" ""
34testing "group_name (with ./-)" "groupadd toy.1Test-2Group.3 &&
35   grep '^toy.1Test-2Group.3:' /etc/group $arg &&
36   groupdel toy.1Test-2Group.3 $arg && echo 'yes'" "yes\n" "" ""
37
38_s210=`echo $_s70$_s70$_s70`
39testing "group_name (long string)" "groupadd $_s210 &&
40   grep '^$_s210:' /etc/group $arg && groupdel $_s210 $arg && echo 'yes'" \
41  "yes\n" "" ""
42testing "group_name with group_id" "groupadd -g 49999 toyTestGroup &&
43   grep '^toyTestGroup:' /etc/group $arg && groupdel toyTestGroup $arg &&
44   echo 'yes'" "yes\n" "" ""
45testing "group_name with group_id (system_group)" \
46  "groupadd -g 49999 -S toyTestGroup && grep '^toyTestGroup:' /etc/group $arg &&
47   groupdel toyTestGroup $arg && echo 'yes'" "yes\n" "" ""
48testing "group_name (system_group)" "groupadd -S toyTestGroup &&
49   grep '^toyTestGroup:' /etc/group $arg && groupdel toyTestGroup $arg &&
50   echo 'yes'" "yes\n" "" ""
51testing "group_name (add/del user)" "groupadd toyTestGroup &&
52   grep '^toyTestGroup:' /etc/group $arg && groupadd $USER toyTestGroup &&
53   grep '^toyTestGroup:.*:.*:.*$USER.*' /etc/group $arg &&
54   groupdel $USER toyTestGroup $arg || groupdel toyTestGroup &&
55   grep '^toyTestGroup:' /etc/group $arg || echo 'yes'" "yes\n" "" ""
56
57 echo "Testing to add single group multiple times after removing it..."
58 for each in {01..20}
59 do
60   testing "group_name ($each)" "groupadd toyTestGroup &&
61      grep '^toyTestGroup:' /etc/group $arg && groupdel toyTestGroup $arg &&
62      echo 'yes'" "yes\n" "" ""
63 done
64