#!/usr/bin/env python
# test has been developed by Robert Harakaly and changed for SAM by Victor Galaktionov 
# get LFC directory/file access control lists (lfc_getacl)
# meta: proxy=true
# meta: preconfig=../../LFC-config

import os, lfc, sys, commands
from testClass import _test, _ntest, _testRunner, SAM_Run, LFC_VO, TEST_HOME

global fileName

class test_ok(_test):
    def info(self):
        return "lfc_getacl OK "
    def clean(self):
        lfc.lfc_unlink(fileName)
        lfc.lfc_rmdir(os.path.dirname(fileName))

    def prepare(self):
        lfc.lfc_mkdir(os.path.dirname(fileName),0755)
        self.guid = self.get_guid()
        self.perms = 0764
        ret = lfc.lfc_creatg(fileName,self.guid, self.perms)
        if (ret != 0):
            print "Error: cannot create testing file: ", lfc.sstrerror(lfc.cvar.serrno)
            print "Test is failed"
            return False
        lfc.lfc_chmod(fileName, 0764)
    def test(self):
        nentries, acls_list = lfc.lfc_getacl(fileName, lfc.CA_MAXACLENTRIES)
        return (acls_list,0)

    def ret(self):
        acls = []
        acl1 = lfc.lfc_acl()
        acl1.a_perm=self.perms&7
        acl2 = lfc.lfc_acl()
        acl2.a_perm=(self.perms&7*8)/8
        acl3 = lfc.lfc_acl()
        acl3.a_perm=(self.perms&7*64)/64
        acls.append(acl3)
        acls.append(acl2)
        acls.append(acl1)

        return acls

    def compare(self, testVal, retVal):
        (ret, retRetVal) = retVal
        (test, testRetVal) = testVal
        retval = True
        if (retRetVal == testRetVal):
            retval = retval & ( test[0].a_perm == ret[0].a_perm )
            retval = retval & ( test[1].a_perm == ret[1].a_perm )
            retval = retval & ( test[2].a_perm == ret[2].a_perm )
        else:
            retval = False
        return retval


class lfc_getacl_test(_testRunner):
    def __init__(self):
        global fileName
        self.name = "lfc_getacl_test"
        fileName=os.environ['LFC_HOME'] + "/python_getacl_test/python_getacl_test_file"
        self.tests=[test_ok]


#************* Interface for SAM and Python tests ***************
SAM_Run(lfc_getacl_test)

