#!/bin/bash
# meta: proxy=True
# meta: preconfig=../../DPM-config
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# AUTHORS: Dimitar Shiyachki <Dimitar.Shiyachki@cern.ch>
#
##############################################################################

SCRIPTDIR="$(dirname "$(readlink -f ${BASH_SOURCE})")"
source "${SCRIPTDIR}/../../Macros"

PROXY_NEEDED

TEST_DIR=d`date +%s%N`
DUMMY_USERNAME=u`date +%s%N`
DUMMY_GROUPNAME=g`date +%s%N`
TEMP_FILE=/tmp/_dpm_test_temp`date +%s%N`

cleanup ()
{
   echo; echo "Cleaning up..."
   DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-rmgrpmap --group $DUMMY_GROUPNAME"
   lcg-del --nolfc -v -b -D srmv2 srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/$DPNS_HOME/$TEST_DIR/bash_copy_1
   dpns-rm -r $TEST_DIR
   rm -f $TEMP_FILE
}

echo "Creating directory $TEST_DIR"
dpns-mkdir -p $TEST_DIR

echo "Copying a file in $TEST_DIR"
lcg-cp -v -b -D srmv2 file:/bin/bash srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/$DPNS_HOME/$TEST_DIR/bash_copy_1

echo
echo -n "Looking up owning user id: "
OWNER_GID=$(dpns-ls -l $DPNS_HOME/$TEST_DIR | \
            grep bash_copy_1 | \
            sed -e 's/[rwxd-]*\s*[0-9]*\s*\([0-9]*\)\s*.*/\1/')
echo $OWNER_GID

echo
echo -n "Getting the subject corresponding to the owner user id: "
OWNER_USERNAME=$(dpns-listgrpmap --gid $OWNER_GID | \
                 sed -e 's/\s*[0-9]*\s*//')
echo $OWNER_USERNAME

echo
echo "Scenario 1: Listing all mappings as non-admin user"
dpns-listgrpmap >$TEMP_FILE 2>&1
if [ $? -ne 0 ]; then
   echo "dpns-listgrpmap exited with error."
   if grep -q "nslistgrpmap: Permission denied" $TEMP_FILE; then
      echo "Access was denied. Test OK."
   else
      cleanup
      TEST_FAILED "Unexpected error occured (no access denied warning). Test failed."
   fi
else
   cleanup 
   TEST_FAILED "dpns-listgrpmap exited with no error. Test failed."
fi

echo
echo "Scenario 2: Listing all mappings as admin user"

DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-listgrpmap" &> $TEMP_FILE
if [ $? -ne 0 ]; then
   echo "dpns-listgrpmap exited with error."
   if grep -q "nslistgrpmap: Permission denied" $TEMP_FILE; then
      echo "Access was denied for root. Test has failed."
   else
      echo "Unexpected error occured (no access denied warning). Test failed."
   fi
   cleanup
   TEST_FAILED
else
   if grep "$OWNER_USERNAME" $TEMP_FILE | grep -q $OWNER_GID; then
      echo "Output contains our mapping. Test OK"
   else
      cleanup
      TEST_FAILED "Output does not contain current user. Test failed."
   fi
fi

echo
echo "Scenario 3: Create a custom group mapping as a regular user"
export PATH=$PATH:/opt/lcg/bin; dpns-entergrpmap --group $DUMMY_GROUPNAME
ret=$?
if [ $ret -eq 0 ]; then
   echo "Groupmap creation completed successfully (non-admin user exec). Test has failed."
   cleanup
   TEST_FAILED
fi

echo
echo "Scenario 4: Create a custom group mapping as DPM admin"
DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-entergrpmap --group $DUMMY_GROUPNAME"
ret=$?
if [ $ret -ne 0 ]; then
   echo "Groupmap creation failed (admin user exec). Test has failed."
   cleanup
   TEST_FAILED
fi

echo
echo -n "Looking up the GID of the dummy user: "
DUMMY_GID=$(dpns-listgrpmap --group "$DUMMY_GROUPNAME" | sed -e 's/\s*\([0-9]*\)\s*.*/\1/')
if [ $? -ne 0 ]; then
   echo "dpns-listgrpmap failed"
   cleanup
   TEST_FAILED
fi
echo $DUMMY_GID

echo
echo "Try to create a new map for the same groupname"
DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-entergrpmap --group $DUMMY_GROUPNAME"
if [ $? -eq 0 ]; then
   echo "dpns-entergrpmap has completed successfully. Test has failed"
   cleanup
   TEST_FAILED
fi

echo
echo -n "Try to update the groupname for the same GID"
DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-modifygrpmap --gid $DUMMY_GID \
                                                                --group x${DUMMY_GROUPNAME}"
if [ $? -ne 0 ]; then
   echo; echo "dpns-updategrpmap did not complete successfully. Test has failed."
   cleanup
   TEST_FAILED
fi
echo "...ok"

if ! dpns-listgrpmap --gid $DUMMY_GID | grep -q x${DUMMY_GROUPNAME}; then
   echo "Update cannot be confirmed by dpns-listgrpmap"
   cleanup
   TEST_FAILED
fi

echo
echo -n "Removing map for x$DUMMY_GROUPNAME"
DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-rmgrpmap --group x${DUMMY_GROUPNAME}"
if [ $? -ne 0 ]; then
   echo; echo "dpns-rmgrpmap did not complete successfully. Test has failed."
   cleanup
   TEST_FAILED
fi
echo "...ok"

echo "Checking whether the mapping still exists with dpns-listgrpmap"
if dpns-listgrpmap --gid $DUMMY_GID | grep -q x${DUMMY_GROUPNAME}; then
   echo "Removal was not successfull. Test has failed."
   cleanup
   TEST_FAILED
fi

DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-entergrpmap --group v$DUMMY_GROUPNAME"
C_GID=$(dpns-listgrpmap --group "v$DUMMY_GROUPNAME" | sed -e 's/\s*\([0-9]*\)\s*.*/\1/')
echo; echo "Removing a user map for uid=$C_GID"
DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-rmgrpmap --gid ${C_GID}"
if [ $? -ne 0 ]; then
   echo "Removal by uid has failed."
   cleanup
   TEST_FAILED
fi
echo "Removal OK."

cleanup
echo
echo "OVERALL RESULT:"
TEST_PASSED


