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

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