1 | #!/usr/bin/env python |
---|
2 | """NDG XACML Context unit test package |
---|
3 | |
---|
4 | NERC 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: $" |
---|
13 | import unittest |
---|
14 | from os import path |
---|
15 | import logging |
---|
16 | logging.basicConfig(level=logging.DEBUG) |
---|
17 | |
---|
18 | from ndg.xacml.test import XACML_NDGTEST1_FILEPATH |
---|
19 | from ndg.xacml.parsers.etree.factory import ReaderFactory |
---|
20 | from ndg.xacml.core.context.pdpinterface import PDPInterface |
---|
21 | from ndg.xacml.core.context.pdp import PDP |
---|
22 | from ndg.xacml.core.context.handler import AbstractContextHandler |
---|
23 | from ndg.xacml.core.attribute import Attribute |
---|
24 | from ndg.xacml.core.attributevalue import AttributeValueFactory, AttributeValue |
---|
25 | from ndg.xacml.core.context.request import Request |
---|
26 | from ndg.xacml.core.context.response import Response |
---|
27 | from ndg.xacml.core.context.result import Result, Decision |
---|
28 | from ndg.xacml.core.context.subject import Subject |
---|
29 | from ndg.xacml.core.context.resource import Resource |
---|
30 | from ndg.xacml.core.context.action import Action |
---|
31 | |
---|
32 | |
---|
33 | class 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 | |
---|
56 | class XACMLContextTestCase(unittest.TestCase): |
---|
57 | """Test PDP, PAP, PIP and Context handler""" |
---|
58 | |
---|
59 | def _createRequestCtx(self): |
---|
60 | request = Request() |
---|
61 | subject = Subject() |
---|
62 | |
---|
63 | attributeValueFactory = AttributeValueFactory() |
---|
64 | |
---|
65 | openidSubjectAttribute = Attribute() |
---|
66 | roleAttribute = Attribute() |
---|
67 | |
---|
68 | openidSubjectAttribute.attributeId = "urn:esg:openid" |
---|
69 | openidSubjectAttribute.dataType = \ |
---|
70 | 'http://www.w3.org/2001/XMLSchema#anyURI' |
---|
71 | |
---|
72 | StringAttributeValue = attributeValueFactory( |
---|
73 | 'http://www.w3.org/2001/XMLSchema#anyURI') |
---|
74 | openidSubjectAttribute.attributeValues.append(StringAttributeValue()) |
---|
75 | |
---|
76 | openidSubjectAttribute.attributeValues[-1].dataType = \ |
---|
77 | 'http://www.w3.org/2001/XMLSchema#anyURI' |
---|
78 | openidSubjectAttribute.attributeValues[-1].value = \ |
---|
79 | 'https://my.name.somewhere.ac.uk' |
---|
80 | |
---|
81 | subject.attributes.append(openidSubjectAttribute) |
---|
82 | |
---|
83 | roleAttribute.attributeId = "urn:ndg:security:authz:1.0:attr" |
---|
84 | roleAttribute.dataType = 'http://www.w3.org/2001/XMLSchema#string' |
---|
85 | roleAttribute.attributeValues.append(StringAttributeValue()) |
---|
86 | roleAttribute.attributeValues[-1].dataType = \ |
---|
87 | 'http://www.w3.org/2001/XMLSchema#string' |
---|
88 | roleAttribute.attributeValues[-1].value = 'staff' |
---|
89 | |
---|
90 | subject.attributes.append(roleAttribute) |
---|
91 | |
---|
92 | request.subjects.append(subject) |
---|
93 | |
---|
94 | resource = Resource() |
---|
95 | resourceAttribute = Attribute() |
---|
96 | resource.attributes.append(resourceAttribute) |
---|
97 | |
---|
98 | resourceAttribute.attributeId = \ |
---|
99 | "urn:oasis:names:tc:xacml:1.0:resource:resource-id" |
---|
100 | |
---|
101 | resourceAttribute.dataType = "http://www.w3.org/2001/XMLSchema#anyURI" |
---|
102 | resourceAttribute.attributeValues.append(AttributeValue()) |
---|
103 | resourceAttribute.attributeValues[-1].value = \ |
---|
104 | 'http://www.localhost/test_securedURI' |
---|
105 | |
---|
106 | request.resources.append(resource) |
---|
107 | |
---|
108 | request.action = Action() |
---|
109 | actionAttribute = Attribute() |
---|
110 | request.action.attributes.append(actionAttribute) |
---|
111 | |
---|
112 | actionAttribute.attributeId = \ |
---|
113 | "urn:oasis:names:tc:xacml:1.0:action:action-id" |
---|
114 | actionAttribute.dataType = "http://www.w3.org/2001/XMLSchema#string" |
---|
115 | actionAttribute.attributeValues.append(AttributeValue()) |
---|
116 | actionAttribute.attributeValues[-1].value = 'read' |
---|
117 | |
---|
118 | return request |
---|
119 | |
---|
120 | def test01CreateRequest(self): |
---|
121 | requestCtx = self._createRequestCtx() |
---|
122 | self.assert_(requestCtx) |
---|
123 | |
---|
124 | def test02CreateResponse(self): |
---|
125 | response = Response() |
---|
126 | result = Result() |
---|
127 | response.results.append(result) |
---|
128 | result.decision = Decision() |
---|
129 | result.decision.value = Decision.NOT_APPLICABLE |
---|
130 | |
---|
131 | def test03AbstractCtxHandler(self): |
---|
132 | self.assertRaises(TypeError, AbstractContextHandler) |
---|
133 | |
---|
134 | def test04CreateCtxHandler(self): |
---|
135 | ctxHandler = TestContextHandler() |
---|
136 | |
---|
137 | def test04PDPInterface(self): |
---|
138 | self.assertRaises(TypeError, PDPInterface) |
---|
139 | |
---|
140 | def test05CreatePDP(self): |
---|
141 | pdp = PDP() |
---|
142 | self.assert_(pdp) |
---|
143 | |
---|
144 | def _createPDPfromPolicy(self): |
---|
145 | pdp = PDP.fromPolicySource(XACML_NDGTEST1_FILEPATH, ReaderFactory) |
---|
146 | return pdp |
---|
147 | |
---|
148 | def test06CreatePDPfromPolicy(self): |
---|
149 | pdp = self._createPDPfromPolicy() |
---|
150 | self.assert_(pdp) |
---|
151 | |
---|
152 | def test07EvaluatePDP(self): |
---|
153 | request = self._createRequestCtx() |
---|
154 | pdp = self._createPDPfromPolicy() |
---|
155 | response = pdp.evaluate(request) |
---|
156 | self.assert_(response) |
---|
157 | |
---|
158 | |
---|
159 | if __name__ == "__main__": |
---|
160 | unittest.main() |
---|