This file is indexed.

/usr/lib/python2.7/dist-packages/nss_cache/lock_test.py is in nsscache 0.33-3.

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
# Copyright 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

"""Unit tests for nss_cache/lock.py."""

__author__ = 'vasilios@google.com (Vasilios Hoffman)'


import __builtin__
import errno
import fcntl
import os
import re
import shutil
import signal
import stat
import sys
import tempfile
import unittest

import mox

from nss_cache import lock


class TestPidFile(mox.MoxTestBase):
  """Unit tests for PidFile class in lock.py."""

  # Note that we do not test whether fcntl actually works as expected.
  # That is outside the scope of unit tests and I'm not going to fork
  # a child to test this, at least not now.
  #
  # Rest assured, it works as expected and fcntl throws an exception if
  # another process has the lock.
  #
  # We also do not test if os.kill works as expected :)

  def setUp(self):
    super(TestPidFile, self).setUp()
    self.workdir = tempfile.mkdtemp()
    self.filename = '%s/%s' % (self.workdir, 'pidfile')

  def tearDown(self):
    shutil.rmtree(self.workdir)
    super(TestPidFile, self).tearDown()

  def testInit(self):
    locker = lock.PidFile()

    pid = os.getpid()
    filename = os.path.basename(sys.argv[0])
    filename = '%s/%s' % (locker.STATE_DIR, filename)

    self.assertTrue(isinstance(locker, lock.PidFile))
    self.assertEquals(locker.pid, pid)
    self.assertEquals(locker.filename, filename)
    self.assertEquals(locker._locked, False)
    self.assertEquals(locker._file, None)

    # also check the case where argv[0] is empty (interactively loaded)
    full_path = sys.argv[0]
    sys.argv[0] = ''
    self.assertRaises(TypeError, lock.PidFile)
    sys.argv[0] = full_path

  def testHandleArgumentsProperly(self):
    filename = 'TEST'
    pid = 10
    locker = lock.PidFile(filename=filename, pid=pid)
    self.assertEquals(locker.filename, filename)
    self.assertEquals(locker.pid, pid)

  def testDestructorUnlocks(self):
    yes = lock.PidFile()
    self.mox.StubOutWithMock(yes, 'Locked')
    self.mox.StubOutWithMock(yes, 'Unlock')
    yes.Locked().AndReturn(True)
    yes.Unlock()

    no = lock.PidFile()
    self.mox.StubOutWithMock(no, 'Locked')
    no.Locked().AndReturn(False)

    self.mox.ReplayAll()

    # test the case where locked returns True.
    yes.__del__()

    # test the case where self.Locked() returns False.
    no.__del__()

  def testOpenCreatesAppropriateFileWithPerms(self):
    locker = lock.PidFile(filename=self.filename)
    locker._Open()

    self.assertTrue(os.path.exists(self.filename))

    file_mode = os.stat(self.filename)[stat.ST_MODE]
    correct_mode = (stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP
                    | stat.S_IROTH)
    self.assertEquals(file_mode, correct_mode)

    os.remove(self.filename)

  def testLockCreatesPidfiles(self):
    locker = lock.PidFile()
    self.mox.StubOutWithMock(locker, '_Open')
    locker._Open().AndRaise(NotImplementedError)
    self.mox.ReplayAll()

    self.assertRaises(NotImplementedError, locker.Lock)

    # Note that testing when self._file is not None is covered below.

  def testLockLocksWithFcntl(self):
    locker = lock.PidFile(pid='PID')

    self.mox.StubOutWithMock(locker, '_file')
    locker._file.truncate()
    locker._file.write('PID\n')
    locker._file.flush()

    self.mox.StubOutWithMock(fcntl, 'lockf')
    fcntl.lockf(locker._file, fcntl.LOCK_EX | fcntl.LOCK_NB)

    self.mox.ReplayAll()

    locker.Lock()
    self.assertTrue(locker._locked)

    # force __del__ to skip Unlock()
    locker._locked = False

  def testLockStoresPid(self):
    locker = lock.PidFile(filename=self.filename, pid='PID')
    locker.Lock()

    pid_file = open(self.filename, 'r')

    self.assertEquals(pid_file.read(), 'PID\n')

    os.remove(self.filename)

  def testLockTrapsPermissionDeniedOnly(self):
    locker = lock.PidFile()
    self.mox.StubOutWithMock(locker, '_Open')
    locker._Open().AndRaise(IOError(errno.EACCES, ''))
    locker._Open().AndRaise(IOError(errno.EIO, ''))
    self.mox.ReplayAll()

    self.assertEquals(False, locker.Lock())
    self.assertRaises(IOError, locker.Lock)

  def testForceLockTerminatesAndClearsLock(self):
    locker = lock.PidFile(pid='PID')
    self.mox.StubOutWithMock(locker, 'SendTerm')
    locker.SendTerm()
    self.mox.StubOutWithMock(locker, 'ClearLock')
    locker.ClearLock()
    self.mox.StubOutWithMock(locker, '_file')
    self.mox.StubOutWithMock(fcntl, 'lockf')
    fcntl.lockf(locker._file,
                fcntl.LOCK_EX | fcntl.LOCK_NB).AndRaise(
                    IOError(fcntl.F_GETSIG, ''))
    fcntl.lockf(locker._file,
                fcntl.LOCK_EX | fcntl.LOCK_NB).AndRaise(
                    IOError(fcntl.F_GETSIG, ''))
    self.mox.ReplayAll()

    # This is a little weird due to recursion.
    # The first time through lockf throws an error and we retry the lock.
    # The 2nd time through we should fail, because lockf will still throw
    # an error, so we expect False back and the above mock objects
    # invoked.
    self.assertFalse(locker.Lock(force=True))

  def testSendTermMatchesCommandAndSendsTerm(self):
    locker = lock.PidFile()
    self.mox.StubOutWithMock(locker, '_file')
    locker._file.read().AndReturn('1234')
    locker._file.seek(0)

    # Mock used in place of an re.compile() pattern -- expects the contents
    # of our proc_file!
    mock_re = self.mox.CreateMockAnything()
    mock_re.match('TEST').AndReturn(True)
    self.mox.StubOutWithMock(re, 'compile')
    re.compile('.*nsscache').AndReturn(mock_re)

    self.mox.StubOutWithMock(os, 'kill')
    os.kill(1234, signal.SIGTERM)

    # Create a file we open() in SendTerm().
    proc_dir = '%s/1234' % self.workdir
    proc_filename = '%s/cmdline' % proc_dir
    os.mkdir(proc_dir)
    proc_file = open(proc_filename, 'w')
    proc_file.write('TEST')
    proc_file.flush()
    proc_file.close()
    locker.PROC_DIR = self.workdir

    self.mox.ReplayAll()

    locker.SendTerm()

    os.remove(proc_filename)
    os.rmdir(proc_dir)

  def testSendTermNoPid(self):
    locker = lock.PidFile()
    self.mox.StubOutWithMock(locker, '_file')
    locker._file.read().AndReturn('\n')
    locker.PROC = self.workdir

    self.mox.ReplayAll()

    locker.SendTerm()

  def testSendTermNonePid(self):
    locker = lock.PidFile()
    self.mox.StubOutWithMock(locker, '_file')
    locker._file.read().AndReturn(None)
    locker.PROC = self.workdir

    self.mox.ReplayAll()

    locker.SendTerm()

  def testSendTermTrapsENOENT(self):
    locker = lock.PidFile()
    self.mox.StubOutWithMock(locker, '_file')
    locker._file.read().AndReturn('1234\n')
    locker._file.seek(0)
    locker.PROC = self.workdir

    self.mox.StubOutWithMock(__builtin__, 'open')
    __builtin__.open(mox.IgnoreArg(), 'r').AndRaise(IOError(errno.ENOENT, ''))

    self.mox.ReplayAll()

    # self.workdir/1234/cmdline should not exist :)
    self.failIf(os.path.exists('%s/1234/cmdline' % self.workdir))

    locker.SendTerm()

  def testClearLockRemovesPidFile(self):
    # Create a pid file.
    pidfile = open(self.filename, 'w')
    pidfile.write('foo')
    pidfile.flush()

    locker = lock.PidFile(filename=self.filename)

    # Cheat instead of calling open.
    locker._file = pidfile

    locker.ClearLock()

    self.failIf(os.path.exists(self.filename))

  def testLockedPredicate(self):
    locker = lock.PidFile()

    locker._locked = True
    self.assertTrue(locker.Locked())

    locker._locked = False
    self.failIf(locker.Locked())

  def testUnlockReleasesFcntlLock(self):
    locker = lock.PidFile()
    locker._file = 'FILE_OBJECT'
    self.mox.StubOutWithMock(fcntl, 'lockf')
    fcntl.lockf('FILE_OBJECT', fcntl.LOCK_UN)

    self.mox.ReplayAll()
    locker.Unlock()

    self.failIf(locker._locked)


if __name__ == '__main__':
  unittest.main()