def testDiff(self): f = files.RegularFile(None) f.inode.perms.set(0444) f.inode.mtime.set(0100) f.inode.owner.set("root") f.inode.group.set("root") f.contents = files.RegularFileStream() s = 'hi' f.contents.size.set(len(s)) f.contents.sha1.set(sha1helper.sha1String(s)) f.flags.set(0) s = files.SymbolicLink(None) s.inode.perms.set(0604) s.inode.mtime.set(0100) s.inode.owner.set('daemon') s.inode.group.set('uucp') # to make sure that referenced names "exist" files.userCache.nameCache['daemon'] = 2 files.groupCache.nameCache['uucp'] = 14 s.flags.set(0) s.target.set("/some/target") # when the lsTag is different, the diff should just the frozen # file object assert(s.diff(f) == s.freeze())
def testContentsChanged(self): f = files.RegularFile(None) f.inode.perms.set(0444) f.inode.mtime.set(0100) f.inode.owner.set("root") f.inode.group.set("root") f.contents = files.RegularFileStream() s = 'hi' f.contents.size.set(len(s)) f.contents.sha1.set(sha1helper.sha1String(s)) f.flags.set(0) # this file stream diff has no contents change. verify that # contentsChanged returns the correct value diff = f.diff(f) assert(files.contentsChanged(diff) == False) f2 = f.copy() s = 'bye' f2.contents.size.set(len(s)) f2.contents.sha1.set(sha1helper.sha1String(s)) # this diff should have changed contents diff = f.diff(f2) assert(files.contentsChanged(diff) == True) # non-regular files should always return False s = files.SymbolicLink(None) s.inode.perms.set(0604) s.inode.mtime.set(0100) s.inode.owner.set('daemon') s.inode.group.set('uucp') # to make sure that referenced names "exist" files.userCache.nameCache['daemon'] = 2 files.groupCache.nameCache['uucp'] = 14 s.flags.set(0) s.target.set("/some/target") s2 = s.copy() s2.target.set("/some/other/target") diff = s.diff(s2) assert(files.contentsChanged(diff) == False) diff = s.diff(s) assert(files.contentsChanged(diff) == False) # test some pre-generated diffs - no change diff = '\x01-\x01\x00\x00\x05\x00"\x01\x00\x02\x01\xa4\x02\x00\x04B\x82=4\x03\x00\x08kvandine\x04\x00\x08kvandine' assert(files.contentsChanged(diff) == False) # this one has contents changed diff = '\x01-\x01\x00"\x01\x00\x08\x00\x00\x00\x00\x00\x00\x02q\x02\x00\x14\xac\x87%\xeb1a/&\xdf\x81\xb9O\xee\xf9\x895\xd4\xb8i\xd4\x05\x00\x1d\x02\x00\x04B\x82Ec\x03\x00\x08kvandine\x04\x00\x08kvandine' assert(files.contentsChanged(diff) == True)
def testJournal(self): class Journal: def __init__(self): self.perms = [] self.devnodes = [] def lchown(self, root, target, uid, gid): self.perms.append((root, target, uid, gid)) def mknod(self, root, target, devtype, major, minor, mode, uid, gid): self.devnodes.append((root, target, devtype, major, minor, mode, uid, gid)) path = tempfile.mkdtemp() try: journal = Journal() filelist = [] for (name, cls) in (('/dev/block', files.BlockDevice), ('/dev/char', files.CharacterDevice)): d = cls(None) d.inode.perms.set(0604) d.inode.mtime.set(0100) d.inode.owner.set("daemon") d.inode.group.set("uucp") # to make sure that referenced names "exist" files.userCache.nameCache['daemon'] = 2 files.groupCache.nameCache['uucp'] = 14 d.flags.set(0) d.devt.major.set(3) d.devt.minor.set(1) filelist.append((name, d)) for name, d in filelist: p = path + name d.restore(None, path, p, journal=journal) assert(journal.devnodes == [(path, path + '/dev/block', 'b', 3, 1, 0604, 'daemon', 'uucp'), (path, path + '/dev/char', 'c', 3, 1, 0604, 'daemon', 'uucp')]) d = files.RegularFile(None) d.inode.perms.set(1755) d.inode.owner.set('root') d.inode.group.set('root') d.inode.mtime.set(0100) contents = filecontents.FromString('Hello, world') d.restore(contents, path, path + '/sbin/ping', journal=journal) assert(journal.perms == [(path, path + '/sbin/ping', 'root', 'root')]) finally: shutil.rmtree(path)
def testFileId(self): # this test verifies that the value produced as the fileId # of a known stream matches its pre-calculated value. f = files.RegularFile(None) f.inode.perms.set(0604) f.inode.mtime.set(0100) f.inode.owner.set("daemon") f.inode.group.set("uucp") # to make sure that referenced names "exist" files.userCache.nameCache['daemon'] = 2 files.groupCache.nameCache['uucp'] = 14 s = "hello world" contents = filecontents.FromString(s) f.contents = files.RegularFileStream() f.contents.size.set(len(s)) f.contents.sha1.set(sha1helper.sha1String(s)) f.flags.set(0) expectedId = '567355867fbbcb2be55d35c3d229a7df8152fdbc' self.assertEqual(f.freeze(), '-\x01\x00"\x01\x00\x08\x00\x00\x00\x00\x00\x00\x00\x0b\x02\x00\x14*\xael5\xc9O\xcf\xb4\x15\xdb\xe9_@\x8b\x9c\xe9\x1e\xe8F\xed\x03\x00\x04\x00\x00\x00\x00\x05\x00\x1c\x01\x00\x02\x01\x84\x02\x00\x04\x00\x00\x00@\x03\x00\x06daemon\x04\x00\x04uucp') self.assertEqual(sha1helper.sha1ToString(f.fileId()), expectedId)
def testOwnership(self): f = files.RegularFile(None) f.inode.perms.set(0604) f.inode.mtime.set(0100) f.inode.owner.set("daemon") f.inode.group.set("uucp") # to make sure that referenced names "exist" files.userCache.nameCache['daemon'] = 2 files.userCache.nameCache['uucp'] = 10 files.groupCache.nameCache['uucp'] = 14 s = "hello world" contents = filecontents.FromString(s) f.contents = files.RegularFileStream() f.contents.size.set(len(s)) f.contents.sha1.set(sha1helper.sha1String(s)) f.flags.set(0) # and setuid root fr = files.RegularFile(None) fr.inode.perms.set(06755) fr.inode.mtime.set(0100) fr.inode.owner.set("root") fr.inode.group.set("root") fr.contents = files.RegularFileStream() fr.contents.size.set(len(s)) fr.contents.sha1.set(sha1helper.sha1String(s)) fr.flags.set(0) # and unwriteable fo = files.RegularFile(None) fo.inode.perms.set(0444) fo.inode.mtime.set(0100) fo.inode.owner.set("root") fo.inode.group.set("root") fo.contents = files.RegularFileStream() fo.contents.size.set(len(s)) fo.contents.sha1.set(sha1helper.sha1String(s)) fo.flags.set(0) # and secret fs = files.RegularFile(None) fs.inode.perms.set(0400) fs.inode.mtime.set(0100) fs.inode.owner.set("root") fs.inode.group.set("root") fs.contents = files.RegularFileStream() fs.contents.size.set(len(s)) fs.contents.sha1.set(sha1helper.sha1String(s)) fs.flags.set(0) f2 = f.copy() assert(f == f2) d = tempfile.mkdtemp() # before we mimic root, test a non-root of setu/gid file pr = d+"/setuid" fr.restore(contents, d, pr) assert not os.stat(pr).st_mode & 04000 try: self.mimicRoot() p = d + "/file" f.restore(contents, d, p) assert self.compareChownLog([ (p, 2, 14) ]) self.chownLog = [] f.inode.owner.set("rootroot") self.logCheck(f.restore, (contents, d, p), "warning: user rootroot does not exist - using root") assert self.compareChownLog([ (p, 0, 14) ]) self.chownLog = [] f.inode.owner.set("uucp") f.inode.group.set("grpgrp") self.logCheck(f.restore, (contents, d, p), "warning: group grpgrp does not exist - using root") assert self.compareChownLog([ (p, 10, 0) ]) self.chmodLog = [] pr = d+"/setuid" fr.restore(contents, d, pr) assert self.compareChmodLog([ (pr, 06755) ]) assert os.stat(pr).st_mode & 07777 == 06755 self.chmodLog = [] po = d+"/unwriteable" fo.restore(contents, d, po) assert self.compareChmodLog([ (po, 0444) ]) assert os.stat(po).st_mode & 07777 == 0444 self.chmodLog = [] ps = d+"/secret" fs.restore(contents, d, ps) assert self.compareChmodLog([ (ps, 0400) ]) assert os.stat(ps).st_mode & 07777 == 0400 self.chmodLog = [] finally: self.realRoot() shutil.rmtree(d)