#!/bin/bash
# meta: proxy=True
# meta: preconfig=../../DPM-config
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2010.
# 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: Alejandro Alvarez Ayllon <aalvarez@cern.ch>
#          Dimitar Shiyachki <Dimitar.Shiyachki@cern.ch>
#
##############################################################################

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

PROXY_NEEDED

TEMP_FILE=/tmp/tempout
FILE=f`date +%s`
DIR=d`date +%s`

# Clean function
function clean {
  rm -f /tmp/tempout
  DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-modifyusrmap \
            --uid $OWNER_UID --user \"$OWNER_USERNAME\" --status 0"
  lcg-del -l --vo $VO -b -D srmv2 srm://$DPM_HOST:8446/srm/managerv2\?SFN=$DPNS_HOME/$FILE
}

function exitFailure {
  clean
  echo "FAILURE"
  exit 1
}

# CREATE
echo "1. Create and read a file"

lcg-cp -v -b -D srmv2 --vo $VO /bin/bash srm://$DPM_HOST:8446/srm/managerv2\?SFN=$DPNS_HOME/$FILE
if [ $? -ne 0 ]; then
  echo "Error creating the file"
  exit 1
fi
OWNER_UID=$(dpns-ls -l $DPNS_HOME/$FILE | sed -r "s/[ \t]+/ /g" | cut -d ' ' -f 3)
OWNER_USERNAME=$(dpns-listusrmap --uid $OWNER_UID | sed -e 's/\s*[0-9]*\s*//')
GSI_TURL=$(lcg-gt -v -b -D srmv2 srm://$DPM_HOST:8446/srm/managerv2\?SFN=$DPNS_HOME/$FILE gsiftp| head -1)
echo "The username is '$OWNER_USERNAME'; the uid is $OWNER_UID"
echo "GSIFTP TURL is $GSI_TURL"
echo "OK"

echo "1.1 lcg-cp"
lcg-cp -v -b -D srmv2 --vo $VO srm://$DPM_HOST:8446/srm/managerv2\?SFN=$DPNS_HOME/$FILE /tmp/trash
if [ $? -ne 0 ]; then
  echo "Error reading with lcg-cp"
  exitFailure
fi
echo "OK"

echo "1.2 dpns-setacl"
dpns-setacl -m u:$OWNER_UID:rwx,m::rwx $DPNS_HOME/$FILE
if [ $? -ne 0 ]; then
  echo "Error accessing with dpns-setacl"
  exitFailure
fi
echo "OK"

echo "1.3 rfio"
rfcat $DPNS_HOME/$FILE >/dev/null
if [ $? -ne 0 ]; then
  echo "Error reading with rfio"
  exitFailure
fi
echo "OK"

# BAN
echo
echo "2. Ban the user associated with the proxy"
echo $OWNER_UID
echo $OWNER_USERNAME
DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-modifyusrmap \
          --uid $OWNER_UID --user \"$OWNER_USERNAME\" --status LOCAL_BAN"
if [ $? -ne 0 ]; then
  echo "Error banning the user"
  exitFailure
fi
echo "OK"

# CHECK
echo
echo "3. Read the same file"
echo "3.1 lcg-cp, srmv2 daemon"
lcg-cp -v -b -D srmv2 --vo $VO srm://$DPM_HOST:8446/srm/managerv2\?SFN=$DPNS_HOME/$FILE /tmp/trash
if [ $? -eq 0 ]; then
  echo "lcg-cp is not supposed to succeed!!"
  exitFailure
fi
echo "OK"

echo "3.2 lcg-cp, srmv2 daemon"
lcg-cp -v -b -D srmv2 --vo $VO srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=$DPNS_HOME/$FILE /tmp/trash
if [ $? -eq 0 ]; then
  echo "lcg-cp is not supposed to succeed!!"
  exitFailure
fi
echo "OK"

echo "3.3 dpns-setacl"
dpns-setacl -m u:$OWNER_UID:rwx,m::rwx $DPNS_HOME/$FILE
if [ $? -eq 0 ]; then
  echo "dpns-setacl is not supposed to succeed!!"
  exitFailure
fi
echo "OK"

echo "3.4 rfio"
rfcat $DPNS_HOME/$FILE >/dev/null
if [ $? -eq 0 ]; then
  echo "rfio is not supposed to succeed!!"
  exitFailure
fi
echo "OK"

processOutput ()
{
   L_COMMAND_NAME=$1
   L_COMMAND_RET=$2
   L_CHECK_FOR=$3
   L_ERROR_MSG=$4
   L_OUTPUT_FILE=$5
   L_ERROR_MSG_2=$6

   if [ $L_COMMAND_RET -ne 0 ]; then
      echo -n "   $L_COMMAND_NAME exited with error."
      if grep -q "$L_ERROR_MSG" "$L_OUTPUT_FILE" || \
         ( [ -n "$L_ERROR_MSG_2" ] && grep -q "$L_ERROR_MSG_2" "$L_OUTPUT_FILE" ); then
         if [ "x$L_CHECK_FOR" == "xFOR_ALLOW" ]; then
            echo "Access was denied. FAIL."
            clean
            TEST_FAILED " Access was denied. FAIL."
         else
            echo " Access was denied. OK."
         fi
      else
         echo "Unexpected error message. FAIL."
         clean
         TEST_FAILED "Unexpected error message. FAIL."
      fi
   else
      if [ "x$L_CHECK_FOR" == "xFOR_ALLOW" ]; then
         echo "   $L_COMMAND_NAME exited with no error. Access was allowed. OK."
      else
         echo "   $L_COMMAND_NAME exited with no error. FAIL."
         clean
         TEST_FAILED "   $L_COMMAND_NAME exited with no error. FAIL."
      fi
   fi
}

#
# Since lcg-cp is supposed to fail at SRM stage, dpm-gsiftp will not tried
# This probes dpm-gsiftp directlty
#
echo
echo "3.5 Checking DPM-GSIFTP access"
globus-url-copy $GSI_TURL file:///tmp/trash >$TEMP_FILE 2>&1
processOutput "globus-url-copy" $? "$1" "Could not get virtual id" "$TEMP_FILE"

echo
echo "4. Create directory"
dpns-mkdir $DPNS_HOME/$DIR
if [ $? -eq 0 ]; then
  echo "dpns-mkdir is not supposed to succeed!!"
  exitFailure
fi
echo "OK"

# UNBAN
echo
echo "5. Unban the user"
DPM_HOST_EXEC "export DPNS_HOST=localhost; export PATH=$PATH:/opt/lcg/bin; dpns-modifyusrmap \
          --uid $OWNER_UID --user \"$OWNER_USERNAME\" --status 0"
if [ $? -ne 0 ]; then
  echo "Error unbanning the user"
  exitFailure
fi

# CHECK
echo
echo "6. Read file"
echo "6.1 lcg-cp, srmv2"
lcg-cp -v -b -D srmv2 --vo $VO srm://$DPM_HOST:8446/srm/managerv2\?SFN=$DPNS_HOME/$FILE /tmp/trash
if [ $? -ne 0 ]; then
  echo "Error reading with lcg-cp"
  exitFailure
fi
echo "OK"

echo "6.2 lcg-cp, srmv2"
lcg-cp -v -b -D srmv2 --vo $VO srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=$DPNS_HOME/$FILE /tmp/trash
if [ $? -ne 0 ]; then
  echo "Error reading with lcg-cp"
  exitFailure
fi
echo "OK"

echo "6.3 dpns-setacl"
dpns-setacl -m u:$OWNER_UID:rwx,m::rwx $DPNS_HOME/$FILE
if [ $? -ne 0 ]; then
  echo "Error accessing with dpns-setacl"
  exitFailure
fi
echo "OK"

echo "6.4 rfio"
rfcat $DPNS_HOME/$FILE >/dev/null
if [ $? -ne 0 ]; then
  echo "Error reading with rfio"
  exitFailure
fi
echo "OK"

# CLEAN
echo
echo "7. Cleaning"
clean

TEST_PASSED

