/usr/share/pyshared/zope/securitypolicy/tests/test_securitymap.py is in python-zope.securitypolicy 3.7.0-0ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | ##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
#############################################################################
"""Test SecurityMap implementations
"""
import unittest
from zope.securitypolicy.securitymap import SecurityMap
from zope.securitypolicy.securitymap import PersistentSecurityMap
from zope.security.management import setSecurityPolicy, getInteraction
from zope.security.management import newInteraction, endInteraction
class InteractionStub:
invalidated = 0
def invalidate_cache(self):
self.invalidated += 1
class TestSecurityMap(unittest.TestCase):
def setUp(self):
self.oldpolicy = setSecurityPolicy(InteractionStub)
newInteraction()
def tearDown(self):
endInteraction()
setSecurityPolicy(self.oldpolicy)
def _getSecurityMap(self):
return SecurityMap()
def test_addCell(self):
map = self._getSecurityMap()
self.assertEqual(getInteraction().invalidated, 0)
map.addCell(0, 0, 'aa')
self.assertEqual(getInteraction().invalidated, 1)
self.assertEqual(map._byrow[0][0], 'aa')
self.assertEqual(map._bycol[0][0], 'aa')
map.addCell(1, 0, 'ba')
self.assertEqual(getInteraction().invalidated, 2)
self.assertEqual(map._byrow[1][0], 'ba')
self.assertEqual(map._bycol[0][1], 'ba')
map.addCell(5, 3, 'fd')
self.assertEqual(getInteraction().invalidated, 3)
self.assertEqual(map._byrow[5][3], 'fd')
self.assertEqual(map._bycol[3][5], 'fd')
def test_addCell_noninteger(self):
map = self._getSecurityMap()
map.addCell(0.3, 0.4, 'entry')
self.assertEqual(map._byrow[0.3][0.4], 'entry')
self.assertEqual(map._bycol[0.4][0.3], 'entry')
marker = object()
map.addCell('a', 'b', marker)
self.assertEqual(map._byrow['a']['b'], marker)
self.assertEqual(map._bycol['b']['a'], marker)
def test_delCell(self):
map = self._getSecurityMap()
self.assertEqual(getInteraction().invalidated, 0)
map._byrow[0] = {}
map._bycol[1] = {}
map._byrow[0][1] = 'aa'
map._bycol[1][0] = 'aa'
map.delCell(0, 1)
self.assertEqual(getInteraction().invalidated, 1)
self.assertEqual(map._byrow.get(0), None)
self.assertEqual(map._bycol.get(1), None)
def test_queryCell(self):
map = self._getSecurityMap()
map._byrow[0] = {}
map._bycol[1] = {}
map._byrow[0][1] = 'aa'
map._bycol[1][0] = 'aa'
marker = object()
self.assertEqual(map.queryCell(0, 1), 'aa')
self.assertEqual(map.queryCell(1, 0), None)
self.assertEqual(map.queryCell(1, 0, marker), marker)
def test_getCell(self):
map = self._getSecurityMap()
map._byrow[0] = {}
map._bycol[1] = {}
map._byrow[0][1] = 'aa'
map._bycol[1][0] = 'aa'
self.assertEqual(map.getCell(0, 1), 'aa')
self.assertRaises(KeyError, map.getCell, 1, 0)
def test_getRow(self):
map = self._getSecurityMap()
map._byrow[0] = {}
map._byrow[0][1] = 'ab'
map._byrow[0][2] = 'ac'
map._byrow[1] = {}
map._byrow[1][1] = 'bb'
map._bycol[1] = {}
map._bycol[1][0] = 'ab'
map._bycol[1][1] = 'bb'
map._bycol[2] = {}
map._bycol[2][0] = 'ac'
self.assertEqual(map.getRow(0), [(1, 'ab'), (2, 'ac')])
self.assertEqual(map.getRow(1), [(1, 'bb')])
self.assertEqual(map.getRow(2), [])
def test_getCol(self):
map = self._getSecurityMap()
map._byrow[0] = {}
map._byrow[0][1] = 'ab'
map._byrow[0][2] = 'ac'
map._byrow[1] = {}
map._byrow[1][1] = 'bb'
map._bycol[1] = {}
map._bycol[1][0] = 'ab'
map._bycol[1][1] = 'bb'
map._bycol[2] = {}
map._bycol[2][0] = 'ac'
self.assertEqual(map.getCol(1), [(0, 'ab'), (1, 'bb')])
self.assertEqual(map.getCol(2), [(0, 'ac')])
self.assertEqual(map.getCol(0), [])
def test_getAllCells(self):
map = self._getSecurityMap()
map._byrow[0] = {}
map._byrow[0][1] = 'ab'
map._byrow[0][2] = 'ac'
map._byrow[1] = {}
map._byrow[1][1] = 'bb'
map._bycol[1] = {}
map._bycol[1][0] = 'ab'
map._bycol[1][1] = 'bb'
map._bycol[2] = {}
map._bycol[2][0] = 'ac'
self.assertEqual(map.getCol(1), [(0, 'ab'), (1, 'bb')])
self.assertEqual(map.getCol(2), [(0, 'ac')])
self.assertEqual(map.getCol(0), [])
class TestPersistentSecurityMap(TestSecurityMap):
def _getSecurityMap(self):
return PersistentSecurityMap()
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(TestSecurityMap),
unittest.makeSuite(TestPersistentSecurityMap),
))
|