#!/usr/bin/env python
# test has been developed by Robert Harakaly and changed for SAM by Victor Galaktionov 
# open a LFC directory, in the name server (lfc_opendir)
# meta: proxy=true
# meta: preconfig=../../LFC-config

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

class test_OK(_test):
    def info(self):
	return "Test open directory: "
    def prepare(self):
        self.name= LFC_VO + "/lfc_opendir_test" #   "/grid/dteam/lfc_opendir_test"
        lfc.lfc_mkdir(self.name, 0775)
    def clean(self):
        lfc.lfc_rmdir(self.name)
    def test(self):
        dir = lfc.lfc_opendir(self.name)
        return (dir,0)
    def ret(self):
        retval=lfc.lfc_DIR()
        return retval
    def compare(self, testVal, retVal):
        (ret, retRetVal) = retVal
        (test, testRetVal) = testVal
        if (test != None):
            retval = True
        else:
            retval = False
        return retval


class test_ENAMETOOLONG(_ntest):
    def info(self):
        return "Test with path too long (ENAMETOOLONG): "
    def prepare(self):
        self.path = "a"
        for a in range(0,lfc.CA_MAXPATHLEN):
            self.path = self.path + "a"
    def test(self):
        dir = lfc.lfc_opendir(self.path)
        return (dir,lfc.cvar.serrno,-1)
    def ret(self):
        return (None, errno.ENAMETOOLONG)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        (test, testerr, testRetVal) = testVal
        if ((ret == test) & (reterr == testerr)):
            retval = True
        else:
            retval = False
        return retval

class test_EACCES1(_ntest):
    def info(self):
        return "Test permission denied on path no read (EACCES): "
    def prepare(self):
        self.path=  LFC_VO + "/python_opendir_test/testfile"  # "/grid/dteam/python_opendir_test/testfile"
        guid = self.get_guid()
        lfc.lfc_mkdir(os.path.dirname(self.path),0755)
        lfc.lfc_creatg(self.path,guid,0644)
        lfc.lfc_chmod(os.path.dirname(self.path),0111)
    def clean(self):
        lfc.lfc_chmod(os.path.dirname(self.path),0755)
        lfc.lfc_unlink(self.path)
        lfc.lfc_rmdir(os.path.dirname(self.path))
    def test(self):
        dir = lfc.lfc_opendir(os.path.dirname(self.path))
        return (dir,lfc.cvar.serrno,-1)
    def ret(self):
        return (None, errno.EACCES)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        (test, testerr, testRetVal) = testVal
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = True
        else:
            retval = False
        return retval

class test_EACCES2(_ntest):
    def info(self):
        return "Test permission denied on path nosearch (EACCES): "
    def prepare(self):
        self.path=     LFC_VO + "/python_opendir_test/testfile"  # "/grid/dteam/python_opendir_test/testfile"
        lfc.lfc_mkdir(os.path.dirname(self.path),0755)
        lfc.lfc_mkdir(self.path, 0644)
    def clean(self):
        lfc.lfc_rmdir(self.path)
        lfc.lfc_rmdir(os.path.dirname(self.path))
    def test(self):
        dir = lfc.lfc_opendir(self.path)
        return (dir,lfc.cvar.serrno,-1)
    def ret(self):
        return (None, errno.EACCES)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        (test, testerr, testRetVal) = testVal
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = True
        else:
            retval = False
        return retval


class test_EINVAL3(_ntest):
    def info(self):
        return "Test SE name length exeeds CA_MAXGUILEN (EINVAL): "
    def prepare(self):
        self.guid=self.get_guid()
        self.name= LFC_VO + "lfc_getreplica_test" # "/grid/dteam/lfc_getreplica_test"
        lfc.lfc_creatg(self.name, self.guid, 0664)
    def clean(self):
        lfc.lfc_unlink(self.name)
    def test(self):
        se = ""
        sfn = "sfn://test-se.cern.ch" + LFC_VO + "/hary/lfc_getreplica_test"
        for i in range (0,lfc.CA_MAXNAMELEN+1):
            se = se + "a"
        ret = lfc.lfc_addreplica(self.guid, None, se, sfn,"-","D","","")
        return (None,lfc.cvar.serrno,ret)
    def ret(self):
        return (None, errno.EINVAL) 
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        (test, testerr, testRetVal) = testVal
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = True
        else:
            retval = False
        return retval

class test_ENOENT(_ntest):
    def info(self):
        return "Opendir on nonexisting file (ENOENT): "
    def test(self):
        dir = lfc.lfc_opendir("/nonexistingdirectory")
        return (dir,lfc.cvar.serrno,-1)
    def ret(self):
        return (None, errno.ENOENT)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        (test, testerr, testRetVal) = testVal
        if ((ret == test) & (reterr == testerr)):
            retval = True
        else:
            retval = False
        return retval


class lfc_opendir_test(_testRunner):
    def __init__(self):
        self.name = "lfc_addreplica_test"
        self.tests=[test_OK, test_ENAMETOOLONG, test_EACCES1, test_EACCES2, test_ENOENT]


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

