#!/bin/bash

cvmfs_test_name="Hide extended attributes"
cvmfs_test_suites="quick"

cvmfs_run_test() {
  logfile=$1

  cvmfs_mount grid.cern.ch "CVMFS_MAGIC_XATTRS_VISIBILITY=always" || return 1
  local attributes=$(list_xattrs /cvmfs/grid.cern.ch)
  echo "Extended attributes: $attributes"
  if [ "x$attributes" = "x" ]; then
    return 10
  fi

  cvmfs_umount grid.cern.ch || return 2
  cvmfs_mount grid.cern.ch "CVMFS_MAGIC_XATTRS_VISIBILITY=never" || return 3
  attributes=$(list_xattrs /cvmfs/grid.cern.ch)
  if [ "x$attributes" != "x" ]; then
    echo "Extended attributes (none expected): $attributes"
    return 20
  fi

  # Attributes are now hidden but they should still be retrievable
  local fqrn=$(get_xattr fqrn /cvmfs/grid.cern.ch)
  echo "Fqrn retrieved: $fqrn"
  if [ "x$fqrn" != "xgrid.cern.ch" ]; then
    return 30
  fi

  # CVMFS_MAGIC_XATTRS_VISIBILITY should have precedence over CVMFS_HIDE_MAGIC_XATTRS
  cvmfs_umount grid.cern.ch || return 40
  cvmfs_mount grid.cern.ch "CVMFS_HIDE_MAGIC_XATTRS=yes" \
    "CVMFS_MAGIC_XATTRS_VISIBILITY=rootonly" || return 41
  attributes=$(list_xattrs /cvmfs/grid.cern.ch)
  if [ "x$attributes" = "x" ]; then
    echo "Expected extended attributes on root node"
    return 42
  fi
  local afile=$(find /cvmfs/grid.cern.ch -type f | head -1)
  attributes=$(list_xattrs $afile)
  if [ "x$attributes" != "x" ]; then
    echo "Extended attributes (none expected for $afile): $attributes"
    return 43
  fi

  return 0
}
