protected MiscContentItem createMiscItem(final String name, final List<String> path, final byte[] newHash, final boolean isDirectory) { return new MiscContentItem(name, path, newHash, isDirectory); }
protected MiscContentItem createMiscItem(final String name, final List<String> path, final byte[] newHash, final boolean isDirectory) { return new MiscContentItem(name, path, newHash, isDirectory); }
private static MiscContentItem createMiscItem(String path, byte[] hash, boolean directory, boolean affectsRuntime) { final String[] s = path.split(PATH_DELIMITER); final int length = s.length; final String name = s[length - 1]; final String[] itemPath = Arrays.copyOf(s, length - 1); return new MiscContentItem(name, itemPath, hash, directory, affectsRuntime); }
private static MiscContentItem createMiscItem(String path, byte[] hash, boolean directory, boolean affectsRuntime) { final String[] s = path.split(PATH_DELIMITER); final int length = s.length; final String name = s[length - 1]; final String[] itemPath = Arrays.copyOf(s, length - 1); return new MiscContentItem(name, itemPath, hash, directory, affectsRuntime); }
static ContentModification createRollbackItem(String name, List<String> path, byte[] backupHash, boolean directory) { final MiscContentItem backupItem = new MiscContentItem(name, path, backupHash, directory); return new ContentModification(backupItem, NO_CONTENT, ModificationType.ADD); } }
static ContentModification createRollbackItem(String name, List<String> path, byte[] backupHash, boolean directory) { final MiscContentItem backupItem = new MiscContentItem(name, path, backupHash, directory); return new ContentModification(backupItem, NO_CONTENT, ModificationType.ADD); } }
static ModificationCondition getCondition(FSPathElement optionalPaths, DistributionContentItem item) { if(optionalPaths.children.isEmpty()) { return null; } final FSPathElement e = new FSPathElement(optionalPaths); final List<String> path = matchOptionalPath(e, item); if(path == null) { return null; } final MiscContentItem misc; if(e.requires != null) { misc = new MiscContentItem(e.requires[0], Arrays.asList(Arrays.copyOf(e.requires, e.requires.length - 1)), null, false); } else { misc = new MiscContentItem(e.name, path, null, true); } return ModificationCondition.Factory.exists(misc); }
public static ContentModification removeMisc(File existingFile, String... fileSegments) throws IOException { byte[] existingHash = hashFile(existingFile); String[] subdir = new String[0]; if (fileSegments.length > 0) { subdir = new String[fileSegments.length -1]; System.arraycopy(fileSegments, 0, subdir, 0, fileSegments.length - 1); } ContentModification fileRemoved = new ContentModification(new MiscContentItem(existingFile.getName(), subdir, NO_CONTENT), existingHash, REMOVE); return fileRemoved; }
@Override ContentModification createRollbackEntry(ContentModification original, byte[] targetHash, byte[] itemHash) { final MiscContentItem item = new MiscContentItem(contentItem.getName(), contentItem.getPath(), itemHash, contentItem.isDirectory(), contentItem.isAffectsRuntime()); return createRollbackEntry(original, item, targetHash); }
@Override ContentModification createRollbackEntry(ContentModification original, byte[] targetHash, byte[] itemHash) { final MiscContentItem item = new MiscContentItem(contentItem.getName(), contentItem.getPath(), itemHash, contentItem.isDirectory(), contentItem.isAffectsRuntime()); return createRollbackEntry(original, item, targetHash); }
static RollbackInfo createRollbackInfo(String id, byte[] oih, byte[] oth, byte[] rih, byte[] rth) { // final MiscContentItem oi = new MiscContentItem(name, path, oih); final MiscContentItem ri = new MiscContentItem(name, path, rih); // final Patch o = createPatch(id, Patch.PatchType.ONE_OFF, new ContentModification(oi, oth, ModificationType.MODIFY)); final Patch r = createPatch(id, Patch.PatchType.ONE_OFF, new ContentModification(ri, rth, ModificationType.MODIFY)); // return new RollbackInfo(o, r); }
public static ContentModification addMisc(File patchDir, String patchElementID, String content, String[] contentSegments, String[] requiredSegments) throws IOException { File miscDir = newFile(patchDir, patchElementID, MISC); File addedFile = touch(miscDir, contentSegments); dump(addedFile, content); byte[] newHash = hashFile(addedFile); String[] subdir = new String[contentSegments.length -1]; System.arraycopy(contentSegments, 0, subdir, 0, contentSegments.length - 1); final MiscContentItem contentItem = new MiscContentItem(addedFile.getName(), subdir, newHash); ModificationCondition condition = null; if(requiredSegments != null && requiredSegments.length > 0) { subdir = new String[requiredSegments.length -1]; System.arraycopy(requiredSegments, 0, subdir, 0, requiredSegments.length - 1); condition = ModificationCondition.Factory.exists(new MiscContentItem(requiredSegments[requiredSegments.length - 1], subdir, null)); } return new ContentModification(contentItem, NO_CONTENT, ADD, condition); }
public T removeFile(final String name, final String[] path, final byte[] existingHash, final boolean isDirectory, final String[] requiredPath) { final ContentItem item = createMiscItem(name, Arrays.asList(path), NO_CONTENT, isDirectory); ModificationCondition condition = null; if(requiredPath != null && requiredPath.length > 0) { final String[] subdir = new String[requiredPath.length -1]; System.arraycopy(requiredPath, 0, subdir, 0, requiredPath.length - 1); condition = ModificationCondition.Factory.exists(new MiscContentItem(requiredPath[requiredPath.length - 1], subdir, null)); } return addContentModification(new ContentModification(item, existingHash, ModificationType.REMOVE, condition)); }
public static ContentModification modifyMisc(File patchDir, String patchElementID, String modifiedContent, byte[] existingHash, String[] fileSegments, String[] requiredSegments) throws IOException { File miscDir = newFile(patchDir, patchElementID, MISC); File modifiedFile = touch(miscDir, fileSegments); dump(modifiedFile, modifiedContent); byte[] modifiedHash = hashFile(modifiedFile); String[] subdir = new String[0]; if (fileSegments.length > 0) { subdir = new String[fileSegments.length -1]; System.arraycopy(fileSegments, 0, subdir, 0, fileSegments.length - 1); } final MiscContentItem item = new MiscContentItem(modifiedFile.getName(), subdir, modifiedHash); ModificationCondition condition = null; if(requiredSegments != null && requiredSegments.length > 0) { subdir = new String[requiredSegments.length -1]; System.arraycopy(requiredSegments, 0, subdir, 0, requiredSegments.length - 1); condition = ModificationCondition.Factory.exists(new MiscContentItem(requiredSegments[requiredSegments.length - 1], subdir, null)); } return new ContentModification(item, existingHash, MODIFY, condition); } }
@Test public void testAddDirectory() throws Exception { final ContentItem item = new MiscContentItem("dir", new String[] { "test"}, NO_CONTENT, true, false); final ContentModification addDir = new ContentModification(item, NO_CONTENT, ModificationType.ADD); final String patchID = randomString(); final Patch patch = PatchBuilder.create() .setPatchId(patchID) .setDescription(randomString()) .oneOffPatchIdentity(productConfig.getProductName(), productConfig.getProductVersion()) .getParent() .addContentModification(addDir) .build(); // create the patch final File patchDir = mkdir(tempDir, patch.getPatchId()); createPatchXMLFile(patchDir, patch); final File zippedPatch = createZippedPatchFile(patchDir, patch.getPatchId()); // Apply PatchingResult result = executePatch(zippedPatch); assertPatchHasBeenApplied(result, patch); final File test = new File(env.getInstalledImage().getJbossHome(), "test"); assertTrue(test.exists()); assertTrue(test.isDirectory()); final File dir = new File(test, "dir"); assertTrue(dir.exists()); assertTrue(dir.isDirectory()); rollback(patchID); }
@Before public void setUp() throws Exception{ // with a file in it File binDir = mkdir(env.getInstalledImage().getJbossHome(), "bin"); String fileName = "standalone.sh"; removedFile = touch(binDir, fileName); dump(removedFile, "modified script to run standalone AS"); expectedModifiedHash = hashFile(removedFile); // let's simulate that the file has been modified by the users by using a hash that is not the file checksum byte[] unmodifiedHash = randomString().getBytes(StandardCharsets.UTF_8); String patchID = randomString(); File patchDir = mkdir(tempDir, patchID); File updatedFile = touch(patchDir, "misc", "bin", fileName); dump(updatedFile, "updated script"); // build a one-off patch for the base installation // with 1 removed file fileRemoved = new ContentModification(new MiscContentItem(fileName, new String[] { "bin" }, NO_CONTENT), unmodifiedHash, REMOVE); patch = PatchBuilder.create() .setPatchId(patchID) .setDescription(randomString()) .oneOffPatchIdentity(productConfig.getProductName(), productConfig.getProductVersion()) .getParent() .addContentModification(fileRemoved) .build(); // create the patch createPatchXMLFile(patchDir, patch); zippedPatch = createZippedPatchFile(patchDir, patch.getPatchId()); runner = newPatchTool(); }
@Test public void testWrongMiscContent() throws Exception { final PatchingTestBuilder builder = createDefaultBuilder(); ContentModificationUtils.addMisc(builder.getRoot(), "oo2", "test-content", "wrong-content"); final MiscContentItem item = new MiscContentItem("wrong-content", new String[0], WRONG_HASH); final ContentModification wrongModification = new ContentModification(item, IoUtils.NO_CONTENT, ModificationType.ADD); final PatchingTestStepBuilder step1 = builder.createStepBuilder(); step1.oneOffPatchIdentity(PRODUCT_VERSION) .setPatchId("oo2") .oneOffPatchElement("base-patch-002", "base", false) .addModuleWithRandomContent("other.test", null) .getParent() .addFileWithRandomContent(null, "test", "content") .addContentModification(wrongModification) ; // try { apply(step1); Assert.fail("should have failed"); } catch (PatchingException e) { Assert.assertFalse(builder.hasFile("test", "content")); Assert.assertFalse(builder.hasFile("wrong-content")); final InstalledIdentity identity = loadInstallationManager().getDefaultIdentity(); final PatchableTarget base = identity.getLayer("base"); Assert.assertFalse(base.getDirectoryStructure().getModulePatchDirectory("base-patch-002").exists()); Assert.assertFalse(identity.getInstalledImage().getPatchHistoryDir("oo2").exists()); } }
@Test public void testPreserveExisting() throws Exception { // content-item 'two' replacing 'one', but kept 'four' final RollbackInfo patch01 = createRollbackInfo("patch01", two, one, four, four); // content-item 'three' replacing 'two' final RollbackInfo patch02 = createRollbackInfo("patch02", three, four); // [patch-two, patch-one] final ContentTaskDefinitions defs = process(patch02, patch01); Assert.assertEquals(1, defs.size()); final PatchingTasks.ContentTaskDefinition def = defs.get(new Location(new MiscContentItem(name, path, one))); Assert.assertNotNull(def); Assert.assertTrue(def.hasConflicts()); // We want to got back to four Assert.assertEquals(four, def.getTarget().getItem().getContentHash()); // The recorded action was preserving four Assert.assertEquals(four, def.getTarget().getTargetHash()); // The current content however is three Assert.assertEquals(three, def.getLatest().getTargetHash()); // And originally replaced four Assert.assertEquals(four, def.getLatest().getItem().getContentHash()); // The resulting operation should replace 'three' with 'four' final ContentModification modification = PatchingTaskDescription.resolveDefinition(def); Assert.assertEquals(four, modification.getItem().getContentHash()); Assert.assertEquals(three, modification.getTargetHash()); }
@Test public void testSimple() throws Exception { // content-item 'two' replacing 'one' final RollbackInfo patch01 = createRollbackInfo("patch01", two, one); // content-item 'three' replacing 'two' final RollbackInfo patch02 = createRollbackInfo("patch02", three, two); // [patch-two, patch-one] final ContentTaskDefinitions defs = process(patch02, patch01); Assert.assertEquals(1, defs.size()); final PatchingTasks.ContentTaskDefinition def = defs.get(new Location(new MiscContentItem(name, path, one))); Assert.assertNotNull(def); Assert.assertFalse(def.hasConflicts()); // We want to restore one (from the backup) Assert.assertEquals(one, def.getTarget().getItem().getContentHash()); // The original target was two Assert.assertEquals(two, def.getTarget().getTargetHash()); // The current content however is three Assert.assertEquals(three, def.getLatest().getTargetHash()); // And originally replaced two Assert.assertEquals(two, def.getLatest().getItem().getContentHash()); // The resulting operation should replace 'three' with 'one' final ContentModification modification = PatchingTaskDescription.resolveDefinition(def); Assert.assertEquals(one, modification.getItem().getContentHash()); Assert.assertEquals(three, modification.getTargetHash()); }
@Test public void testOverrideExisting() throws Exception { // content-item 'two' replacing 'four', originally targeting 'one' final RollbackInfo patch01 = createRollbackInfo("patch01", two, one, four, two); // content-item 'three' replacing 'two' final RollbackInfo patch02 = createRollbackInfo("patch02", three, two); // [patch-two, patch-one] final ContentTaskDefinitions defs = process(patch02, patch01); Assert.assertEquals(1, defs.size()); final PatchingTasks.ContentTaskDefinition def = defs.get(new Location(new MiscContentItem(name, path, one))); Assert.assertNotNull(def); Assert.assertTrue(def.hasConflicts()); // We want to restore four (from the backup) Assert.assertEquals(four, def.getTarget().getItem().getContentHash()); // The original target was two Assert.assertEquals(two, def.getTarget().getTargetHash()); // The current content however is three Assert.assertEquals(three, def.getLatest().getTargetHash()); // And originally replaced two Assert.assertEquals(two, def.getLatest().getItem().getContentHash()); // The resulting operation should replace 'three' with 'four' final ContentModification modification = PatchingTaskDescription.resolveDefinition(def); Assert.assertEquals(four, modification.getItem().getContentHash()); Assert.assertEquals(three, modification.getTargetHash()); }