Exemplo n.º 1
0
    def testIndexCreation(self):
        cache = files.FilesPasswdMapHandler(self.config)
        entries = [
            passwd.PasswdMapEntry(dict(name='foo', uid=10, gid=10)),
            passwd.PasswdMapEntry(dict(name='bar', uid=11, gid=11)),
            passwd.PasswdMapEntry(dict(name='quux', uid=12, gid=11)),
        ]
        pmap = passwd.PasswdMap(entries)
        cache.Write(pmap)
        cache.WriteIndex()

        index_filename = cache.GetCacheFilename() + '.ixname'
        self.failUnless(os.path.exists(index_filename),
                        'Index not created %s' % index_filename)
        f = open(index_filename)
        self.assertEqual('bar\x0015\x00\x00\n', f.readline())
        self.assertEqual('foo\x000\x00\x00\x00\n', f.readline())
        self.assertEqual('quux\x0030\x00\n', f.readline())

        index_filename = cache.GetCacheFilename() + '.ixuid'
        self.failUnless(os.path.exists(index_filename),
                        'Index not created %s' % index_filename)
        f = open(index_filename)
        self.assertEqual('10\x000\x00\x00\n', f.readline())
        self.assertEqual('11\x0015\x00\n', f.readline())
        self.assertEqual('12\x0030\x00\n', f.readline())
Exemplo n.º 2
0
  def testFullUpdateOnEmptySource(self):
    """A full update as above, but instead, the initial source is empty."""
    original_modify_stamp = time.gmtime(1)
    new_modify_stamp = time.gmtime(2)
    # Construct an updater
    self.updater = files_updater.FileMapUpdater(config.MAP_PASSWORD,
                                                self.workdir,
                                                {'name': 'files',
                                                 'dir': self.workdir2})
    self.updater.WriteModifyTimestamp(original_modify_stamp)

    # Construct a cache
    cache = files.FilesPasswdMapHandler({'dir': self.workdir2})
    map_entry = passwd.PasswdMapEntry({'name': 'foo', 'uid': 10, 'gid': 10})
    password_map = passwd.PasswdMap()
    password_map.SetModifyTimestamp(new_modify_stamp)
    password_map.Add(map_entry)
    cache.Write(password_map)

    source_mock = self.mox.CreateMock(source.FileSource)
    source_mock.GetFile(config.MAP_PASSWORD,
                        mox.IgnoreArg(),
                        current_file=mox.IgnoreArg(),
                        location=None).AndReturn(None)
    self.mox.ReplayAll()
    self.assertRaises(error.EmptyMap,
                      self.updater.UpdateCacheFromSource,
                      cache,
                      source_mock,
                      force_write=False,
                      location=None)
    self.assertNotEqual(new_modify_stamp, self.updater.GetModifyTimestamp())
    self.assertEqual(None, self.updater.GetUpdateTimestamp())
Exemplo n.º 3
0
  def testFullUpdateOnEmptyCache(self):
    """A full update as above, but the initial cache is empty."""
    original_modify_stamp = time.gmtime(1)
    new_modify_stamp = time.gmtime(2)
    # Construct an updater
    self.updater = files_updater.FileMapUpdater(config.MAP_PASSWORD,
                                                self.workdir,
                                                {'name': 'files',
                                                 'dir': self.workdir2})
    self.updater.WriteModifyTimestamp(original_modify_stamp)

    # Construct a cache
    cache = files.FilesPasswdMapHandler({'dir': self.workdir2})

    def GetFileEffects(map_name, dst_file, current_file, location):
      f = open(dst_file, 'w')
      f.write('root:x:0:0:root:/root:/bin/bash\n')
      f.close()
      os.utime(dst_file, (1, 2))
      return dst_file

    source_mock = self.mox.CreateMock(source.FileSource)
    source_mock.GetFile(config.MAP_PASSWORD, mox.IgnoreArg(), mox.IgnoreArg(), location=None).WithSideEffects(GetFileEffects)

    #source_mock = MockSource()
    self.assertEqual(0, self.updater.UpdateCacheFromSource(cache,
                                                           source_mock,
                                                           force_write=False,
                                                           location=None))
    self.assertEqual(new_modify_stamp, self.updater.GetModifyTimestamp())
    self.assertNotEqual(None, self.updater.GetUpdateTimestamp())
Exemplo n.º 4
0
 def testWrite(self):
     cache = files.FilesPasswdMapHandler(self.config)
     entry = passwd.PasswdMapEntry({'name': 'foo', 'uid': 10, 'gid': 10})
     pmap = passwd.PasswdMap([entry])
     written = cache.Write(pmap)
     self.assertTrue('foo' in written)
     self.assertFalse(entry in pmap)  # we emptied pmap to avoid mem leaks
     self.assertFalse(cache.temp_cache_file.closed)
Exemplo n.º 5
0
    def testFullUpdate(self):
        original_modify_stamp = time.gmtime(1)
        new_modify_stamp = time.gmtime(2)

        # Construct a fake source.
        def GetFile(map_name, dst_file, current_file, location):
            print(("GetFile: %s" % dst_file))
            f = open(dst_file, 'w')
            f.write('root:x:0:0:root:/root:/bin/bash\n')
            f.close()
            os.utime(dst_file, (1, 2))
            os.system("ls -al %s" % dst_file)
            return dst_file

        dst_file = mox.Value()
        source_mock = self.mox.CreateMock(source.FileSource)
        source_mock.GetFile(config.MAP_PASSWORD,
                            mox.Remember(dst_file),
                            current_file=mox.IgnoreArg(),
                            location=mox.IgnoreArg()).WithSideEffects(
                                GetFile).AndReturn(dst_file)

        # Construct the cache.
        cache = files.FilesPasswdMapHandler({'dir': self.workdir2})
        map_entry = passwd.PasswdMapEntry({
            'name': 'foo',
            'uid': 10,
            'gid': 10
        })
        password_map = passwd.PasswdMap()
        password_map.SetModifyTimestamp(new_modify_stamp)
        password_map.Add(map_entry)
        cache.Write(password_map)

        updater = files_updater.FileMapUpdater(config.MAP_PASSWORD,
                                               self.workdir, {
                                                   'name': 'files',
                                                   'dir': self.workdir2
                                               })
        updater.WriteModifyTimestamp(original_modify_stamp)

        self.mox.ReplayAll()

        self.assertEqual(
            0,
            updater.UpdateCacheFromSource(cache,
                                          source_mock,
                                          force_write=False,
                                          location=None))

        self.assertEqual(new_modify_stamp, updater.GetModifyTimestamp())
        self.assertNotEqual(None, updater.GetUpdateTimestamp())
Exemplo n.º 6
0
    def testWriteCacheAndIndex(self):
        cache = files.FilesPasswdMapHandler(self.config)
        entries = [
            passwd.PasswdMapEntry(dict(name='foo', uid=10, gid=10)),
            passwd.PasswdMapEntry(dict(name='bar', uid=11, gid=11)),
        ]
        pmap = passwd.PasswdMap(entries)
        written = cache.Write(pmap)
        cache.WriteIndex()

        self.assertTrue('foo' in written)
        self.assertTrue('bar' in written)
        index_filename = cache.GetCacheFilename() + '.ixname'
        self.assertTrue(os.path.exists(index_filename),
                        'Index not created %s' % index_filename)
        index_filename = cache.GetCacheFilename() + '.ixuid'
        self.assertTrue(os.path.exists(index_filename),
                        'Index not created %s' % index_filename)

        entries = [
            passwd.PasswdMapEntry(dict(name='foo', uid=10, gid=10)),
            passwd.PasswdMapEntry(dict(name='bar', uid=11, gid=11)),
            passwd.PasswdMapEntry(dict(name='quux', uid=12, gid=11)),
        ]
        pmap = passwd.PasswdMap(entries)
        written = cache.Write(pmap)
        self.assertTrue('foo' in written)
        self.assertTrue('bar' in written)
        self.assertTrue('quux' in written)

        index_filename = cache.GetCacheFilename() + '.ixname'
        with open(index_filename) as f:
            self.assertEqual('bar\x0015\x00\n', f.readline())
            self.assertEqual('foo\x000\x00\x00\n', f.readline())

        index_filename = cache.GetCacheFilename() + '.ixuid'
        with open(index_filename) as f:
            self.assertEqual('10\x000\x00\x00\n', f.readline())
            self.assertEqual('11\x0015\x00\n', f.readline())

        cache.WriteIndex()
        index_filename = cache.GetCacheFilename() + '.ixname'
        with open(index_filename) as f:
            self.assertEqual('bar\x0015\x00\x00\n', f.readline())
            self.assertEqual('foo\x000\x00\x00\x00\n', f.readline())
            self.assertEqual('quux\x0030\x00\n', f.readline())

        index_filename = cache.GetCacheFilename() + '.ixuid'
        with open(index_filename) as f:
            self.assertEqual('10\x000\x00\x00\n', f.readline())
            self.assertEqual('11\x0015\x00\n', f.readline())
            self.assertEqual('12\x0030\x00\n', f.readline())
Exemplo n.º 7
0
    def testWritePasswdEntry(self):
        """We correctly write a typical entry in /etc/passwd format."""
        cache = files.FilesPasswdMapHandler(self.config)
        file_mock = self.mox.CreateMock(sys.stdout)
        file_mock.write('root:x:0:0:Rootsy:/root:/bin/bash\n')

        map_entry = passwd.PasswdMapEntry()
        map_entry.name = 'root'
        map_entry.passwd = 'x'
        map_entry.uid = 0
        map_entry.gid = 0
        map_entry.gecos = 'Rootsy'
        map_entry.dir = '/root'
        map_entry.shell = '/bin/bash'

        self.mox.ReplayAll()

        cache._WriteData(file_mock, map_entry)