#!/bin/bash
##############################################################################
# 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>
#
##############################################################################
# meta: proxy=true
# meta: preconfig=../../DPM-config

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

export tokendesc="TOKEN_MULTIPLE_GROUPS_`date +%s`"
export filename="bug409500_testfile_`date +%s`"

echo "Token description: $tokendesc"
echo "Filename: $filename"

test_bug40500_pre () {
   return 0
}

test_bug40500_post () {

   echo "Cleaning up..."

   export X509_USER_PROXY=/tmp/second_proxy

   if dpns-ls /dpm/cern.ch/home/$SECOND_VO/${filename}_2 | grep -q ${filename}_2; then
      lcg-del -v -b -l -D srmv2 srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/dpm/cern.ch/home/$SECOND_VO/${filename}_2
   fi 

   if dpns-ls /dpm/cern.ch/home/$SECOND_VO/${filename}_3 | grep -q ${filename}_3; then
      lcg-del -v -b -l -D srmv2 srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/dpm/cern.ch/home/$SECOND_VO/${filename}_3 
   fi

   voms-proxy-destroy -file $X509_USER_PROXY
   unset X509_USER_PROXY

   if dpns-ls /dpm/cern.ch/home/$VO/${filename}_1 | grep -q ${filename}_1; then
      lcg-del -v -b -l -D srmv2 srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/dpm/cern.ch/home/$VO/${filename}_1
   fi

   if dpm-listspaces | grep -q $tokendesc; then
      DPM_HOST_EXEC "dpm-releasespace --token_desc $tokendesc"
   fi

   return 0
}

test_bug40500 () {

   DPM_HOST_EXEC "dpm-reservespace --gspace 32M --token_desc $tokendesc --group \"$VO,$SECOND_VO\""

   if [ $? -ne 0 ]; then
      echo "Error reserving space for multiple groups."
      return 1
   fi

   lcg-cp -v -b -D srmv2 --dst $tokendesc file:/bin/ls srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/dpm/cern.ch/home/$VO/${filename}_1

   if [ $? -ne 0 ]; then
      echo "Error copying file to reserved space."
      return 1
   fi
   echo -e "Success copying file to reserved space.\n"

   export X509_USER_PROXY=/tmp/second_proxy
   echo $PROXYPASSWD | voms-proxy-init -voms $SECOND_VO -pwstdin -out /tmp/second_proxy
   if [ $? -ne 0 ]; then
      echo "Error creating proxy for VO: $SECOND_VO"
      return 1
   fi

   lcg-cp -v -b -D srmv2 --dst $tokendesc file:/bin/ls srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/dpm/cern.ch/home/$SECOND_VO/${filename}_2

   if [ $? -ne 0 ]; then
      echo "Error copying file to reserved space that should be accessible for the VO."
      return 1
   fi
   echo -e "Success copying file with secondary VO.\n"

   unset X509_USER_PROXY

   DPM_HOST_EXEC "dpm-updatespace --token_desc $tokendesc --group \"$VO\""
   lcg-cp -v -b -D srmv2 --dst $tokendesc file:/bin/ls srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/dpm/cern.ch/home/$SECOND_VO/${filename}_3
   if [ $? -eq 0 ]; then
      echo "Error: file successully written to space when our access was revoked."
      return 1
   fi
   echo -e "Permission denied when our access was revoked. Success.\n"

   return 0
}

if (test_bug40500_pre); then
  if (test_bug40500); then
    if (test_bug40500_post); then
      TEST_PASSED
    else
      TEST_FAILED
    fi
  else
    test_bug40500
    TEST_FAILED
  fi
else
  TEST_FAILED
fi

