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
7# 70 characters long string; hereafter, we will use it as per our need.
8_s70="abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
9
10echo "# usage: addgroup [-g GID] [USER] GROUP
11# Add a group or add a user to a group."
12
13# Redirecting all output to /dev/null for grep and delgroup
14arg="&>/dev/null"
15
16#testing "name" "command" "result" "infile" "stdin"
17
18testing "group_name (text)" "groupadd toyTestGroup &&
19   grep '^toyTestGroup:' /etc/group $arg && groupdel toyTestGroup $arg &&
20   echo 'yes'" "yes\n" "" ""
21testing "group_name (alphanumeric)" "groupadd toy1Test2Group3 &&
22   grep '^toy1Test2Group3:' /etc/group $arg && groupdel toy1Test2Group3 $arg &&
23   echo 'yes'" "yes\n" "" ""
24testing "group_name (numeric)" "groupadd 987654321 &&
25   grep '^987654321:' /etc/group $arg && groupdel 987654321 $arg &&
26   echo 'yes'" "yes\n" "" ""
27testing "group_name (with ./-)" "groupadd toy.1Test-2Group.3 &&
28   grep '^toy.1Test-2Group.3:' /etc/group $arg &&
29   groupdel toy.1Test-2Group.3 $arg && echo 'yes'" "yes\n" "" ""
30
31_s210=`echo $_s70$_s70$_s70`
32testing "group_name (long string)" "groupadd $_s210 &&
33   grep '^$_s210:' /etc/group $arg && groupdel $_s210 $arg && echo 'yes'" \
34  "yes\n" "" ""
35testing "group_name with group_id" "groupadd -g 49999 toyTestGroup &&
36   grep '^toyTestGroup:' /etc/group $arg && groupdel toyTestGroup $arg &&
37   echo 'yes'" "yes\n" "" ""
38testing "group_name with group_id (system_group)" \
39  "groupadd -g 49999 -S toyTestGroup && grep '^toyTestGroup:' /etc/group $arg &&
40   groupdel toyTestGroup $arg && echo 'yes'" "yes\n" "" ""
41testing "group_name (system_group)" "groupadd -S toyTestGroup &&
42   grep '^toyTestGroup:' /etc/group $arg && groupdel toyTestGroup $arg &&
43   echo 'yes'" "yes\n" "" ""
44testing "group_name (add/del user)" "groupadd toyTestGroup &&
45   grep '^toyTestGroup:' /etc/group $arg && groupadd $USER toyTestGroup &&
46   grep '^toyTestGroup:.*:.*:.*$USER.*' /etc/group $arg &&
47   groupdel $USER toyTestGroup $arg || groupdel toyTestGroup &&
48   grep '^toyTestGroup:' /etc/group $arg || echo 'yes'" "yes\n" "" ""
49
50 echo "Testing to add single group multiple times after removing it..."
51 for each in {01..20}
52 do
53   testing "group_name ($each)" "groupadd toyTestGroup &&
54      grep '^toyTestGroup:' /etc/group $arg && groupdel toyTestGroup $arg &&
55      echo 'yes'" "yes\n" "" ""
56 done
57