def testRenameOfRunningExecutable(self): if util.get_canonical_os_type() != 'windows': return import pkg.portable.os_windows as os_windows cwd = os.getcwdu() exefilesrc = 'C:\\Windows\\system32\\more.com' self.assertTrue(os.path.exists(exefilesrc)) # create an image, copy an executable into it, # run the executable, replace the executable tdir1 = tempfile.mkdtemp() img1 = image.Image(tdir1, imgtype=image.IMG_USER, should_exist=False, user_provided_dir=True) img1.history.client_name = "pkg-test" img1.set_attrs(False, "test", origins=["http://localhost:10000"], refresh_allowed=False) exefile = os.path.join(tdir1, 'less.com') shutil.copyfile(exefilesrc, exefile) proc = subprocess.Popen([exefile], stdin=subprocess.PIPE) self.assertRaises(OSError, os.unlink, exefile) fd1, path1 = tempfile.mkstemp(dir=tdir1) os.write(fd1, b"foo") os.close(fd1) portable.rename(path1, exefile) fd2 = os.open(exefile, os.O_RDONLY) self.assertEqual(os.read(fd2, 3), "foo") os.close(fd2) proc.communicate() # Make sure that the moved executable gets deleted # This is a white-box test # To simulate running another process, we delete the cache # and call get_trashdir as if another file was being moved # to the trash. os_windows.cached_image_info = [] os_windows.get_trashdir(exefile) self.assertTrue(not os.path.exists( os.path.join(img1.imgdir, os_windows.trashname))) # cleanup os.chdir(cwd) shutil.rmtree(tdir1)
def testRenameOfRunningExecutable(self): if util.get_canonical_os_type() != 'windows': return import pkg.portable.os_windows as os_windows cwd = os.getcwdu() exefilesrc = 'C:\\Windows\\system32\\more.com' self.assert_(os.path.exists(exefilesrc)) # create an image, copy an executable into it, # run the executable, replace the executable tdir1 = tempfile.mkdtemp() img1 = image.Image(tdir1, imgtype=image.IMG_USER, should_exist=False, user_provided_dir=True) img1.history.client_name = "pkg-test" img1.set_attrs(False, "test", origins=["http://localhost:10000"], refresh_allowed=False) exefile = os.path.join(tdir1, 'less.com') shutil.copyfile(exefilesrc, exefile) proc = subprocess.Popen([exefile], stdin = subprocess.PIPE) self.assertRaises(OSError, os.unlink, exefile) fd1, path1 = tempfile.mkstemp(dir = tdir1) os.write(fd1, "foo") os.close(fd1) portable.rename(path1, exefile) fd2 = os.open(exefile, os.O_RDONLY) self.assertEquals(os.read(fd2, 3), "foo") os.close(fd2) proc.communicate() # Make sure that the moved executable gets deleted # This is a white-box test # To simulate running another process, we delete the cache # and call get_trashdir as if another file was being moved # to the trash. os_windows.cached_image_info = [] os_windows.get_trashdir(exefile) self.assert_(not os.path.exists(os.path.join(img1.imgdir, os_windows.trashname))) # cleanup os.chdir(cwd) shutil.rmtree(tdir1)