#!/usr/bin/env python
# test has been developed by Robert Harakaly and changed for SAM by Victor Galaktionov
# open a LFC directory, having the specified GUID, in the name server (lfc_opendirg) 
# 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_name(_test):
    def info(self):
	return "Test open directory: "
    def prepare(self):
        self.name= LFC_VO + "/lfc_opendirg_test"
        lfc.lfc_mkdir(self.name, 0775)
    def clean(self):
        lfc.lfc_rmdir(self.name)
    def test(self):
        dir = lfc.lfc_opendirg(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_OK_guid(_test):
    def info(self):
        return "Test open directory: "
    def prepare(self):
        self.name= LFC_VO + "/lfc_opendirg_test"
        self.guid=self.get_guid()
        lfc.lfc_mkdirg(self.name, self.guid, 0775)
    def clean(self):
        lfc.lfc_rmdir(self.name)
    def test(self):
        dir = lfc.lfc_opendirg("",self.guid)
        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_OK_both(_test):
    def info(self):
        return "Test open directory: "
    def prepare(self):
        self.name= LFC_VO + "/lfc_opendirg_test"
        self.guid=self.get_guid()
        lfc.lfc_mkdirg(self.name, self.guid, 0775)
    def clean(self):
        lfc.lfc_rmdir(self.name)
    def test(self):
        dir = lfc.lfc_opendirg(self.name,self.guid)
        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

#	add test if name and guid do not match

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_opendirg(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"
        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_opendirg(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"
        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_opendirg(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_ENOENT(_ntest):
    def info(self):
        return "Opendir on nonexisting file (ENOENT): "
    def test(self):
        dir = lfc.lfc_opendirg("/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_opendirg_test(_testRunner):
    def __init__(self):
        self.name = "lfc_addreplica_test"
        self.tests=[test_OK_name, test_OK_guid, test_OK_both, test_ENAMETOOLONG, test_EACCES1, test_EACCES2, test_ENOENT]



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

