#!/usr/bin/perl
#*********************************************************************
#   Copyright (C) International Business Machines  Corp., 2003
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this pronram;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#  FILE   : group01
#
#  PURPOSE: Tests that changes to the data in /etc/group takes effect.
#
#  SETUP: This script requires perl, as well as the Expect module
#           for perl.  The script must be run by "root". 
#
#  HISTORY:
#    03/03 originated by Dustin Kirkland (k1rkland@us.ibm.com)
#
#*********************************************************************
#!/usr/bin/perl

use Expect;
use strict;
use warnings;

require "utils.plib";

my $username1 = "cg_user1";
my $username2 = "cg_user2";
my $initial_password1 = "ltp_test_pass";
my $initial_encrypted_password1 = "\\\$1\\\$1yzzszzz\\\$7P9AphbzAN43pTktT\/kpp\/";
my $group1 = "cg_group1";
my $newgroup1 = "new_" . $group1;

my @result;
my $error = 0;
my $test = 0;
my $exit_code = 0;
my %ARGV;
foreach $_ (@ARGV) {
  $ARGV{$_} = 1;
}


#################
# Begin Testing #
#################

print("========================================================================\n");
print("Begin execution of group01\n");
print("Testing that changes to the data in /etc/group takes effect.\n"); 
print("========================================================================\n");

print("Username1: [$username1]\n");
print("Password1: [$initial_password1]\n");


##############
# Test 0     #
# usermod -G #
##############

$error = 0;
print("\n-> Test \#$test : Adding a valid user to a valid group\n");

# Create the users for this test
print("Creating users...\n");
$Expect::Log_Stdout = 0;
create_user($username1);
set_encrypted_password($username1, $initial_encrypted_password1);
$Expect::Log_Stdout = 1;

# Create the group for this test
print("Creating group...\n");
`groupadd $group1`;

# Show groups user currently belongs to
print("Showing groups user currently belongs to...\n");
my $output = `groups $username1`;
print("$output");

# Add the user to the group
print("Adding the user to the group...\n");
`usermod -G $group1 $username1`;

# Show groups user now belongs to
print("Showing groups user now belongs to...\n");
$output = `groups $username1`;
print("$output");
if ($output !~ /\s$group1\s/) {
  $error = 1;
}

# Delete the users that were created for this test
print("Deleting the users that were created...\n");
$Expect::Log_Stdout = 0;
delete_user($username1);
$Expect::Log_Stdout = 1;

# Delete the group that was created for this test
print("Deleting the group that was created...\n");
`groupdel $group1`;

if ($error == 0) {
  $result[$test] .= "==> Test \#$test : PASS (adding valid user to valid group)\n";
} else {
  $result[$test] .= "==> Test \#$test : FAIL (adding valid user to valid group)\n";
  $exit_code = 1;
}
print("$result[0]");
$test++;


##############
# Test 1     #
# usermod -G #
##############

$error = 0;
print("\n-> Test \#$test : Adding a valid user to a valid group, and then removing them from that group\n");

# Create the users for this test
print("Creating users...\n");
$Expect::Log_Stdout = 0;
create_user($username1);
set_encrypted_password($username1, $initial_encrypted_password1);
$Expect::Log_Stdout = 1;

# Create the group for this test
print("Creating group...\n");
`groupadd $group1`;

# Show groups user currently belongs to
print("Showing groups user currently belongs to...\n");
$output = `groups $username1`;
print("$output");

# Add the user to the group
print("Adding the user to the group...\n");
`usermod -G $group1 $username1`;

# Show groups user now belongs to
print("Showing groups user now belongs to...\n");
$output = `groups $username1`;
print("$output");
if ($output !~ /\s$group1\s/) {
  $error = 1;
}

# Remove the user from the group
print("Removing the user from the group...\n");
`usermod -G users $username1`;

# Show groups user now belongs to
print("Showing groups user now belongs to...\n");
$output = `groups $username1`;
print("$output");
if ($output =~ /\s$group1\s/) {
  $error = 1;
}


# Delete the users that were created for this test
print("Deleting the users that were created...\n");
$Expect::Log_Stdout = 0;
delete_user($username1);
$Expect::Log_Stdout = 1;

# Delete the group that was created for this test
print("Deleting the group that was created...\n");
`groupdel $group1`;

if ($error == 0) {
  $result[$test] .= "==> Test \#$test : PASS (removing a valid user from a valid group)\n";
} else {
  $result[$test] .= "==> Test \#$test : FAIL (removing a valid user from a valid group)\n";
  $exit_code = 1;
}
print("$result[$test]");
$test++;


##############
# Test 2     #
# groupmod   #
##############

$error = 0;
print("\n-> Test \#$test : Modifying a group as non-root user\n");

# Create the users for this test
print("Creating users...\n");
$Expect::Log_Stdout = 0;
create_user($username1);
set_encrypted_password($username1, $initial_encrypted_password1);
create_user($username2);
set_encrypted_password($username2, $initial_encrypted_password1);
$Expect::Log_Stdout = 1;

# Create the group for this test
print("Creating group...\n");
`groupadd $group1`;

# Show groups user currently belongs to
print("Showing groups user currently belongs to...\n");
$output = `groups $username1`;
print("$output");

# Add the user to the group
print("Adding the user to the group...\n");
`usermod -G $group1 $username1`;

# Show groups user now belongs to
print("Showing groups user now belongs to...\n");
$output = `groups $username1`;
print("$output");
if ($output !~ /\s$group1\s/) {
  $error = 1;
}

# User tries to modify group
print("User trying to modify group...\n");
my $status = run_as_user($username1, $initial_password1, "/usr/sbin/groupmod -A $username2 $group1");
my $lower = $status / 256;
my $upper = $status % 256;
#########################################
# if (($upper != 0) || ($lower != 10)) {
#
# This test case used to look for error code "10".
# However, this case yielded different error codes
# on different systems that has not yet been resolved.
#########################################
if (($upper != 0) || ($lower == 0)) {
  $error = 1;
}

# Delete the users that were created for this test
print("Deleting the users that were created...\n");
$Expect::Log_Stdout = 0;
delete_user($username1);
delete_user($username2);
$Expect::Log_Stdout = 1;

# Delete the group that was created for this test
print("Deleting the group that was created...\n");
`groupdel $group1`;
`groupdel $newgroup1`;

if ($error == 0) {
  $result[$test] .= "==> Test \#$test : PASS (user tries to modify group)\n";
} else {
  $result[$test] .= "==> Test \#$test : FAIL (user tries to modify group)\n";
  $exit_code = 1;
}
print("$result[$test]");
$test++;


##############
# Test 3     #
# groupmod   #
##############

$error = 0;
print("\n-> Test \#$test : Modifying a group as root user\n");

# Create the users for this test
print("Creating users...\n");
$Expect::Log_Stdout = 0;
create_user($username1);
set_encrypted_password($username1, $initial_encrypted_password1);
$Expect::Log_Stdout = 1;

# Create the group for this test
print("Creating group...\n");
`groupadd $group1`;

# Show groups user currently belongs to
print("Showing groups user currently belongs to...\n");
$output = `groups $username1`;
print("$output");

# Add the user to the group
print("Adding the user to the group...\n");
`usermod -G $group1 $username1`;

# Show groups user now belongs to
print("Showing groups user now belongs to...\n");
$output = `groups $username1`;
print("$output");
if ($output !~ /\s$group1\s/) {
  $error = 1;
}

# Root tries to modify group
print("Root trying to modify group...\n");
`groupmod -n new_$group1 $group1`;
if ($? != 0) {
  print("[$?]\n");
  $error = 1;
}

# Show groups user now belongs to
print("Showing groups user now belongs to...\n");
$output = `groups $username1`;
print("$output");
if ($output !~ /\s$newgroup1\s/) {
  $error = 1;
}


# Delete the group that was created for this test
print("Deleting the group that was created...\n");
`groupdel $group1`;
`groupdel $newgroup1`;

# Delete the users that were created for this test
print("Deleting the users that were created...\n");
$Expect::Log_Stdout = 0;
delete_user($username1);
$Expect::Log_Stdout = 1;

if ($error == 0) {
  $result[$test] .= "==> Test \#$test : PASS (root tries to modify group)\n";
} else {
  $result[$test] .= "==> Test \#$test : FAIL (root tries to modify group)\n";
  $exit_code = 1;
}
print("$result[$test]");
$test++;


###############
# End Testing #
###############

# Print rolled up results
print("\nSummary of Results\n");
foreach my $result (@result) {
  if ($result) {
    print($result);
  }
}

print("========================================================================\n");
print("End execution of group01\n");
print("========================================================================\n");


exit $exit_code;
