#!/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`
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-rmusrmap --user $DUMMY_USERNAME"
   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_UID=$(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_UID

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

echo
echo "Scenario 1: Listing all mappings as non-admin user"
dpns-listusrmap >$TEMP_FILE 2>&1
if [ $? -ne 0 ]; then
   echo "dpns-listusrmap exited with error."
   if grep -q "nslistusrmap: 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-listusrmap 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-listusrmap" &> $TEMP_FILE
if [ $? -ne 0 ]; then
   echo "dpns-listusrmap exited with error."
   if grep -q "nslistusrmap: 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_UID; 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 user mapping as a regular user"
export PATH=$PATH:/opt/lcg/bin; dpns-enterusrmap --user $DUMMY_USERNAME
ret=$?
if [ $ret -eq 0 ]; then
   cleanup
   TEST_FAILED "Usermap creation completed successfully (non-admin user exec). Test has failed."
fi

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

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

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

echo
echo -n "Try to update the username for the same UID"
DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-modifyusrmap --uid $DUMMY_UID \
                                                                --user x${DUMMY_USERNAME}"
if [ $? -ne 0 ]; then
   cleanup
   TEST_FAILED "dpns-updateusrmap did not complete successfully. Test has failed."
fi
echo "...ok"

if ! dpns-listusrmap --uid $DUMMY_UID | grep -q x${DUMMY_USERNAME}; then
   cleanup
   TEST_FAILED "Update cannot be confirmed by dpns-listusrmap"
fi

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

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

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

cleanup
echo
echo "OVERALL RESULT:"
TEST_PASSED


