This file is indexed.

/usr/lib/python2.7/dist-packages/twext/who/test/test_util.py is in python-twext 0.1.b2.dev15059-1.

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
##
# Copyright (c) 2013-2015 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Directory service utility tests.
"""

from itertools import chain

from twisted.trial import unittest
from twisted.python.constants import (
    Names, NamedConstant, Values, ValueConstant, Flags, FlagConstant
)

from ..idirectory import DirectoryServiceError
from ..util import ConstantsContainer, uniqueResult, describe



class Tools(Names):
    hammer = NamedConstant()
    hammer.description = u"nail pounder"

    screwdriver = NamedConstant()
    screwdriver.description = u"screw twister"


    @staticmethod
    def isPounder(tool):
        if getattr(tool, "description", "").endswith("pounder"):
            return True
        return False



class MoreTools(Names):
    saw = NamedConstant()
    saw.description = u"z maker"

    mallet = NamedConstant()
    mallet.description = u"soft pounder"



class Instruments(Names):
    hammer = NamedConstant()
    chisel = NamedConstant()



class Statuses(Values):
    OK = ValueConstant(200)
    NOT_OK = ValueConstant(500)


    @staticmethod
    def isError(status):
        return status.value < 500



class MoreStatuses(Values):
    MOOLAH = ValueConstant(402)


    @staticmethod
    def isError(status):
        return status.value < 400



class Switches(Flags):
    r = FlagConstant()
    g = FlagConstant()
    b = FlagConstant()

    r.description = u"red"
    g.description = u"green"
    b.description = u"blue"

    black = FlagConstant()



class ConstantsContainerTest(unittest.TestCase):
    """
    Tests for L{ConstantsContainer}.
    """

    def test_constants_from_constants(self):
        """
        Initialize a container from some constants.
        """
        constants = set((Tools.hammer, Tools.screwdriver, Instruments.chisel))
        self.assertEquals(
            set(ConstantsContainer(constants).iterconstants()),
            constants,
        )


    def test_constants_from_containers(self):
        """
        Initialize a container from other containers.
        """
        self.assertEquals(
            set(ConstantsContainer((Tools, MoreTools)).iterconstants()),
            set(chain(Tools.iterconstants(), MoreTools.iterconstants())),
        )


    def test_constants_from_constantsContainers(self):
        """
        Initialize a container from other L{ConstantsContainer}s.
        """
        self.assertEquals(
            set(
                ConstantsContainer((
                    ConstantsContainer((Tools,)),
                    ConstantsContainer((MoreTools,)),
                )).iterconstants()
            ),
            set(chain(Tools.iterconstants(), MoreTools.iterconstants())),
        )


    def test_constants_from_iterables(self):
        """
        Initialize a container from iterables of constants.
        """
        self.assertEquals(
            set(
                ConstantsContainer((
                    Tools.iterconstants(), MoreTools.iterconstants()
                )).iterconstants()
            ),
            set(chain(Tools.iterconstants(), MoreTools.iterconstants())),
        )


    def test_conflictingClasses(self):
        """
        A container can't contain two constants with different types.
        """
        self.assertRaises(TypeError, ConstantsContainer, (Tools, Switches))


    def test_conflictingNames_different(self):
        """
        A container can't contain two different constants with the same name.
        """
        self.assertRaises(ValueError, ConstantsContainer, (Tools, Instruments))


    def test_conflictingNames_same(self):
        """
        A container can combine containers which contain the same constants.
        """
        ConstantsContainer((Tools, Tools))


    def test_notConstantClass(self):
        """
        A container can't contain random classes.
        """
        self.assertRaises(TypeError, ConstantsContainer, (self.__class__,))


    def test_attrs(self):
        """
        Constants are assessible via attributes.
        """
        container = ConstantsContainer((
            Tools.hammer, Tools.screwdriver, Instruments.chisel
        ))

        self.assertEquals(container.hammer, Tools.hammer)
        self.assertEquals(container.screwdriver, Tools.screwdriver)
        self.assertEquals(container.chisel, Instruments.chisel)
        self.assertRaises(AttributeError, lambda: container.plugh)


    def test_iterconstants(self):
        """
        L{ConstantsContainer.iterconstants}C{()} produces the contained
        constants.
        """
        constants = set((Tools.hammer, Tools.screwdriver, Instruments.chisel))
        container = ConstantsContainer(constants)

        self.assertEquals(
            set(container.iterconstants()),
            constants,
        )


    def test_staticmethod(self):
        """
        Static methods from source containers are accessible via attributes.
        """
        container = ConstantsContainer((Tools, MoreTools))

        self.assertTrue(container.isPounder(container.hammer))
        self.assertTrue(container.isPounder(container.mallet))
        self.assertFalse(container.isPounder(container.screwdriver))
        self.assertFalse(container.isPounder(container.saw))


    def test_conflictingMethods(self):
        """
        A container can't contain two static methods with the same name.
        """
        self.assertRaises(
            ValueError, ConstantsContainer, (Statuses, MoreStatuses)
        )


    def test_lookupByName(self):
        """
        Constants are assessible via L{ConstantsContainer.lookupByName}.
        """
        constants = set((
            Instruments.hammer,
            Tools.screwdriver,
            Instruments.chisel,
        ))
        container = ConstantsContainer(constants)

        self.assertEquals(
            container.lookupByName("hammer"),
            Instruments.hammer,
        )
        self.assertEquals(
            container.lookupByName("screwdriver"),
            Tools.screwdriver,
        )
        self.assertEquals(
            container.lookupByName("chisel"),
            Instruments.chisel,
        )

        self.assertRaises(
            ValueError,
            container.lookupByName, "plugh",
        )


    def test_lookupByValue(self):
        """
        Containers with L{ValueConstant}s are assessible via
        L{ConstantsContainer.lookupByValue}.
        """
        container = ConstantsContainer((Statuses,))

        self.assertEquals(container.lookupByValue(200), Statuses.OK)
        self.assertEquals(container.lookupByValue(500), Statuses.NOT_OK)

        self.assertRaises(ValueError, container.lookupByValue, 999)



class UtilTest(unittest.TestCase):
    """
    Miscellaneous tests.
    """

    def test_uniqueResult(self):
        """
        L{uniqueResult} returns the single value it is given, raises
        L{DirectoryServiceError} is given more than one, or returns L{None}
        if given on values.
        """
        self.assertEquals(None, uniqueResult(()))
        self.assertEquals(1, uniqueResult((1,)))
        self.assertRaises(DirectoryServiceError, uniqueResult, (1, 2, 3))


    def test_describeConstant(self):
        """
        L{describe} will look up the C{description} attribute on constants and
        fall back to the C{name} attribute.
        """
        self.assertEquals(u"nail pounder", describe(Tools.hammer))
        self.assertEquals(u"hammer", describe(Instruments.hammer))


    def test_describeFlags(self):
        """
        L{describe} on flags will describe the contained flags by joining their
        descriptions with C{"|"}.
        """
        self.assertEquals(u"blue", describe(Switches.b))
        self.assertEquals(u"green|red", describe(Switches.r | Switches.g))
        self.assertEquals(u"black|blue", describe(Switches.b | Switches.black))


    def test_describeObject(self):
        """
        L{describe} will cast non-cosntant objects to L{unicode}.
        """
        for obj in (object(), u"string", b"data"):
            self.assertEquals(describe(obj), unicode(obj))