source: TI12-security/trunk/NDG_XACML/ndg/xacml/test/test_context.py @ 6778

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDG_XACML/ndg/xacml/test/test_context.py@6778
Revision 6778, 4.7 KB checked in by pjkersha, 9 years ago (diff)

Split unit test module into policy, context, match function test related modules.

Line 
1"""NDG XACML Context unit test package
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "26/03/10"
7__copyright__ = "(C) 2010 Science and Technology Facilities Council"
8__contact__ = "Philip.Kershaw@stfc.ac.uk"
9__license__ = "BSD - see LICENSE file in top-level directory"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = "$Id: $"
12import unittest
13from os import path
14import logging
15logging.basicConfig(level=logging.DEBUG)
16
17from ndg.xacml.test import XACML_NDGTEST1_FILEPATH
18from ndg.xacml.parsers.etree.factory import ReaderFactory
19from ndg.xacml.core.context.pdpinterface import PDPInterface
20from ndg.xacml.core.context.pdp import PDP
21from ndg.xacml.core.context.handler import AbstractContextHandler
22from ndg.xacml.core.attribute import Attribute
23from ndg.xacml.core.attributevalue import AttributeValue
24from ndg.xacml.core.context.request import Request
25from ndg.xacml.core.context.response import Response
26from ndg.xacml.core.context.result import Result, Decision
27from ndg.xacml.core.context.subject import Subject
28from ndg.xacml.core.context.resource import Resource
29from ndg.xacml.core.context.action import Action
30
31               
32class TestContextHandler(AbstractContextHandler):
33    """Test implementation of Context Handler"""
34   
35    def __init__(self):
36        super(TestContextHandler, self).__init__()
37        self.pip = None       
38       
39    def handlePEPRequest(self, myRequest):
40       
41        # Convert myRequest to XACML context request
42        request = myRequest
43       
44        if self.pdp is None:
45            raise TypeError('No "pdp" attribute set')
46       
47        response = self.pdp.evaluate(request)
48       
49        # Convert XACML context response to domain specific request
50        myResponse = response
51       
52        return myResponse
53
54
55class XACMLContextTestCase(unittest.TestCase):
56    """Test PDP, PAP, PIP and Context handler"""
57   
58    def _createRequestCtx(self):
59        request = Request()
60       
61        subject = Subject()
62        subjectAttribute = Attribute()
63        subject.attributes.append(subjectAttribute)
64        subjectAttribute.attributeId = \
65                            "urn:oasis:names:tc:xacml:1.0:subject:subject-id"
66        subjectAttribute.dataType = \
67                            "urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name"
68        subjectAttribute.attributeValue = AttributeValue()
69        subjectAttribute.attributeValue.value = 'bs@simpsons.com'
70       
71        request.subjects.append(subject)
72       
73        resource = Resource()
74        resourceAttribute = Attribute()
75        resource.attributes.append(resourceAttribute)
76       
77        resourceAttribute.attributeId = \
78                            "urn:oasis:names:tc:xacml:1.0:resource:resource-id"
79                           
80        resourceAttribute.dataType = "http://www.w3.org/2001/XMLSchema#anyURI"
81        resourceAttribute.attributeValue = AttributeValue()
82        resourceAttribute.attributeValue.value = \
83                            'file://example/med/record/patient/BartSimpson'
84
85        request.resources.append(resource)
86       
87        request.action = Action()
88        actionAttribute = Attribute()
89        request.action.attributes.append(actionAttribute)
90       
91        actionAttribute.attributeId = \
92                                "urn:oasis:names:tc:xacml:1.0:action:action-id"
93        actionAttribute.dataType = "http://www.w3.org/2001/XMLSchema#string"
94        actionAttribute.attributeValue = AttributeValue()
95        actionAttribute.attributeValue.value = 'read'
96       
97        return request
98   
99    def test01CreateRequest(self):
100        requestCtx = self._createRequestCtx()
101        self.assert_(requestCtx)
102       
103    def test02CreateResponse(self):
104        response = Response()
105        result = Result()
106        response.results.append(result)
107        result.decision = Decision.value = Decision.NOT_APPLICABLE
108       
109    def test03AbstractCtxHandler(self):
110        self.assertRaises(TypeError, AbstractContextHandler)
111       
112    def test04CreateCtxHandler(self):
113        ctxHandler = TestContextHandler()
114       
115    def test04PDPInterface(self):
116        self.assertRaises(TypeError, PDPInterface)
117       
118    def test05CreatePDP(self):
119        pdp = PDP()
120        self.assert_(pdp)
121       
122    def _createPDPfromPolicy(self):
123        pdp = PDP.fromPolicySource(XACML_NDGTEST1_FILEPATH, ReaderFactory)
124        return pdp
125       
126    def test06CreatePDPfromPolicy(self):
127        pdp = self._createPDPfromPolicy()
128        self.assert_(pdp)
129       
130    def test07EvaluatePDP(self):
131        request = self._createRequestCtx()
132        pdp = self._createPDPfromPolicy()
133        response = pdp.evaluate(request)
134        self.assert_(response)
Note: See TracBrowser for help on using the repository browser.