''' 
	Access Control Lists testing based on newpynfs framework 
	Testing default ACL's
	Aurelien Charbon - Bull SA
'''
from optparse import OptionParser
from random_gen import *
import commands
import os
import threading
import time
import random

alphabet='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789_-()'
t_alphabet=len(alphabet)

def test_acl_default(path):
	splitedresult = []
	testdir = 'dir0'
	testfile = 'file0'
	testuser = 'user1'
	testmask = 'r'
	# set default acl on the test directory	
	u = commands.getoutput('mkdir ' + path + "/" + testdir)
	u = commands.getoutput('setfacl -d -m u:'+ testuser + ':' + testmask + ' ' + path + "/" + testdir)
	u = commands.getoutput('getfacl ' + path + "/" + testdir)
	splitedresult = u.split('\n')
	acl=[]
	for i in range (len(splitedresult)-1):
		splitedline = splitedresult[i].split(':')
		if splitedline[0] == 'default':
			name = splitedline[2]
			entry = splitedline[3]
			NameOK = re.match("user",name)
			if NameOK != None:
				acl.append([name,entry])

	# create a file in this directory
	u = commands.getoutput('touch ' + path + "/" + testdir + '/' + testfile)
	# get the file's ACL and verify
	u = commands.getoutput('getfacl ' + path + "/" + testdir + testfile)
	splitedresult = u.split('\n')
	acl2=[]
	for i in range (len(splitedresult)-1):
		NameOK = re.match("#",splitedresult[i])
		if NameOK != None:
			NameOK = re.match("getfacl",splitedresult[i])
			if NameOK != None:
				splitedline = splitedresult[i].split(':')
				name = splitedline[1]
				entry = splitedline[2]
				acl2.append(name,entry)
	print acl
	print acl2
	result_final = True
	while i < len(acl2):
		result = False
		while j < len(acl2) and result == False:
			if acl2[i] == acl[j]:
				result = True
		if result == False:
			result_final = False
	
	print "test default acl [" + str(result_final) + "]"

parser = OptionParser()
parser.add_option("-p", "--path", dest="path",
                  help="path on which the test is executed")
(options, args) = parser.parse_args()
test_acl_default(options.path)

