Java Code Examples for ghidra.framework.model.DomainFolder#createFile()

The following examples show how to use ghidra.framework.model.DomainFolder#createFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractVersionControlActionTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	frontEnd = new FrontEndTestEnv();

	Program p1 = frontEnd.buildProgram(this);
	DomainFolder rootFolder = frontEnd.getRootFolder();

	// Program A created by the FrontEndTestEnv
	rootFolder.createFile(PROGRAM_B, p1, TaskMonitor.DUMMY);

	Program p2 = frontEnd.buildProgram(this);
	rootFolder.createFile(PROGRAM_C, p2, TaskMonitor.DUMMY);

	p1.release(this);
	p2.release(this);

	waitForTree();
	frontEnd.waitForTreeNode(PROGRAM_C);
}
 
Example 2
Source File: SetLanguageTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Before
	public void setUp() throws Exception {
		env = new TestEnv();

		setErrorGUIEnabled(true);
		frontEndTool = env.getFrontEndTool();
		env.showFrontEndTool();

		setLanguageAction = getAction(frontEndTool, "LanguageProviderPlugin", "Set Language");

		// NOTE: Only test translation from a supported language to another supported language

// TODO: Change test data to a supported case (e.g., MIPS-32 to MIPS-64)

		DomainFolder rootFolder = env.getProject().getProjectData().getRootFolder();

		ProgramBuilder builder = new ProgramBuilder("notepad", "x86:LE:32:default");
		Program p = builder.getProgram();

		assertEquals(new LanguageID("x86:LE:32:default"), p.getLanguageID());
		rootFolder.createFile("notepad", p, TaskMonitor.DUMMY);
		env.release(p);
		builder.dispose();

		rootFolder.createFolder("XYZ");
		GTree tree = findComponent(frontEndTool.getToolFrame(), GTree.class);
		waitForTree(tree);

		GTreeNode rootNode = tree.getViewRoot();
		xyzFolderNode = rootNode.getChild(0);
		notepadNode = rootNode.getChild(1);
		notepadFile = ((DomainFileNode) notepadNode).getDomainFile();

		waitForSwing();
	}
 
Example 3
Source File: VersionControlAction1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testRefreshFolder() throws Exception {

	doCreateVersions();

	DomainFolder rootFolder = frontEnd.getRootFolder();
	DomainFolder folder = rootFolder.createFolder("myFolder");

	Program p = frontEnd.buildProgram(this);
	folder.createFile("My_Program", p, TaskMonitor.DUMMY);
	p.release(this);

	waitForSwing();

	GTreeNode node = getNode(PROGRAM_A);
	DomainFile df = ((DomainFileNode) node).getDomainFile();
	df.copyTo(folder, TaskMonitor.DUMMY);

	waitForSwing();

	final GTreeNode fnode = getFolderNode("myFolder");
	assertNotNull(fnode);
	expandNode(fnode);
	assertEquals(2, fnode.getChildCount());
	selectNode(fnode);

	DockingActionIf action = getAction("Refresh");
	performFrontEndAction(action);
	assertEquals(2, fnode.getChildCount());
}
 
Example 4
Source File: VersionControlAction2Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testActionsEnabledForFolder() throws Exception {

	DomainFolder rootFolder = frontEnd.getRootFolder();
	DomainFolder f = rootFolder.createFolder("myFolder");
	waitForSwing();

	Program p = frontEnd.buildProgram(this);
	f.createFile("Sample", p, TaskMonitor.DUMMY);
	p.release(this);

	waitForSwing();
	GTreeNode node = getFolderNode("myFolder");
	assertNotNull(node);

	expandNode(node);
	selectNode(node);

	Set<DockingActionIf> actions = frontEnd.getFrontEndActions();
	for (DockingActionIf action : actions) {
		if (!(action instanceof VersionControlAction)) {
			continue;
		}
		if (action.getName().equals("Find Checkouts")) {
			assertTrue(action.isEnabledForContext(getDomainFileActionContext(node)));
		}
		else {
			assertTrue(!action.isEnabledForContext(getDomainFileActionContext(node)));
		}
	}
}
 
Example 5
Source File: VersionControlAction2Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindCheckoutsInSubFolder() throws Exception {

	DomainFolder rootFolder = frontEnd.getRootFolder();
	DomainFolder folder = rootFolder.createFolder("myFolder_1");
	folder = folder.createFolder("myFolder_2");

	Program p = frontEnd.buildProgram(this);
	folder.createFile("My_Program", p, TaskMonitor.DUMMY);
	p.release(this);
	waitForSwing();

	GTreeNode node = getFolderNode("myFolder_1");
	assertNotNull(node);
	node = node.getChild("myFolder_2");
	assertNotNull(node);
	node = node.getChild("My_Program");
	assertNotNull(node);
	addToVersionControl(node, true);

	selectNode(getFolderNode("myFolder_1"));
	DockingActionIf action = getAction("Find Checkouts");
	performFrontEndAction(action);

	FindCheckoutsDialog dialog = waitForDialogComponent(FindCheckoutsDialog.class);
	assertNotNull(dialog);

	final GTable table = findComponent(dialog.getComponent(), GTable.class);
	assertNotNull(table);
	waitForBusyTable(table);

	FindCheckoutsTableModel model = (FindCheckoutsTableModel) table.getModel();

	assertEquals(1, model.getRowCount());
	CheckoutInfo checkoutInfo = model.getRowObject(0);
	DomainFile file = checkoutInfo.getFile();
	DomainFolder parent = file.getParent();
	assertEquals("/myFolder_1/myFolder_2", parent.getPathname());
	pressButtonByText(dialog, "Dismiss");
}
 
Example 6
Source File: VersionControlSlowScreenShots.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindMyCheckouts() throws Exception {

	frontEnd.createMultipleCheckins();

	DomainFolder rootFolder = frontEnd.getRootFolder();
	DomainFolder folder = rootFolder.createFolder("myFolder_1");
	folder = folder.createFolder("myFolder_2");
	Program p = frontEnd.buildProgram(this);
	folder.createFile("My_Program", p, TaskMonitor.DUMMY);
	p.release(this);

	GTreeNode node = frontEnd.waitForFolderNode("myFolder_1");
	assertNotNull(node);
	node = node.getChild("myFolder_2");
	assertNotNull(node);
	node = node.getChild("My_Program");
	assertNotNull(node);
	frontEnd.addToVersionControl(node, true);

	GTreeNode rootNode = frontEnd.getRootNode();
	frontEnd.selectNodes(rootNode);
	DockingActionIf action = frontEnd.getAction("Find Checkouts");
	frontEnd.performFrontEndAction(action);

	FindCheckoutsDialog dialog = waitForDialogComponent(FindCheckoutsDialog.class);
	assertNotNull(dialog);

	captureDialog(dialog);
}
 
Example 7
Source File: FrontEndPluginActionsTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void createNotepadFile(DomainFolder f) throws Exception {
	waitForSwing();
	ProgramDB p = createDefaultProgram("p1", ProgramBuilder._TOY, this);
	f.createFile("notepad", p, TaskMonitor.DUMMY);
	p.release(this);
	waitForSwing();
}
 
Example 8
Source File: GenerateLotsOfProgramsScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void createPrograms(DomainFolder parent, int count)
		throws IOException, InvalidNameException, CancelledException {
	Processor processor = Processor.toProcessor("x86");
	Language language = getDefaultLanguage(processor);
	Program program = new ProgramDB("dummy", language, language.getDefaultCompilerSpec(), this);
	for (int i = 0; i < count; i++) {
		parent.createFile("Prog_" + i, program, monitor);
	}
	program.release(this);
}
 
Example 9
Source File: VersionControlAction1Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindMyCheckouts() throws Exception {
	doCreateVersions();

	DomainFolder rootFolder = frontEnd.getRootFolder();
	DomainFolder folder = rootFolder.createFolder("myFolder_1");
	folder = folder.createFolder("myFolder_2");
	Program p = frontEnd.buildProgram(this);
	folder.createFile("My_Program", p, TaskMonitor.DUMMY);
	p.release(this);

	GTreeNode node = getFolderNode("myFolder_1");
	assertNotNull(node);
	node = node.getChild("myFolder_2");
	assertNotNull(node);
	node = node.getChild("My_Program");
	assertNotNull(node);
	addToVersionControl(node, true);

	GTreeNode rootNode = frontEnd.getRootNode();
	selectNode(rootNode);
	DockingActionIf action = getAction("Find Checkouts");
	performFrontEndAction(action);

	FindCheckoutsDialog dialog = waitForDialogComponent(FindCheckoutsDialog.class);
	assertNotNull(dialog);

	final GTable table = findComponent(dialog.getComponent(), GTable.class);
	assertNotNull(table);
	waitForBusyTable(table);

	FindCheckoutsTableModel model = (FindCheckoutsTableModel) table.getModel();

	assertEquals(2, model.getRowCount());
	assertEquals(4, model.getColumnCount());

	DockingActionIf undoCheckoutAction = getAction("UndoCheckOut");
	DockingActionIf checkInAction = getAction("CheckIn");
	assertFalse(checkInAction.isEnabledForContext(dialog.getActionContext(null)));
	assertFalse(undoCheckoutAction.isEnabledForContext(dialog.getActionContext(null)));

	// make a selection in the table
	selectInTable(table, node);
	assertFalse(checkInAction.isEnabledForContext(dialog.getActionContext(null)));
	assertTrue(undoCheckoutAction.isEnabledForContext(dialog.getActionContext(null)));

	CheckoutInfo checkoutInfo = model.getRowObject(0);
	DomainFile file = checkoutInfo.getFile();
	DomainFolder parent = file.getParent();
	assertEquals("/myFolder_1/myFolder_2", parent.getPathname());
	pressButtonByText(dialog, "Dismiss");
}
 
Example 10
Source File: VersionControlSlowScreenShots.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testCheckedOut() throws Exception {

	frontEnd.createMultipleCheckins();

	Program p1 = frontEnd.buildProgram(this);
	DomainFolder rootFolder = frontEnd.getRootFolder();

	// Program A created by the FrontEndTestEnv
	rootFolder.createFile("Program_B", p1, TaskMonitor.DUMMY);

	FrontEndTool t = frontEnd.getFrontEndTool();
	JFrame frame = t.getToolFrame();

	captureWindow(frame, 400, 550);
}
 
Example 11
Source File: GFileSystemLoadKernelTask.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void loadKext(GFile file, TaskMonitor monitor) throws Exception {
	if (file.getLength() == 0) {
		return;
	}
	if (!file.getName().endsWith(".kext")) {
		return;
	}
	monitor.setMessage("Opening " + file.getName());

	Program program = ProgramMappingService.findMatchingProgramOpenIfNeeded(file.getFSRL(),
		this, programManager, ProgramManager.OPEN_VISIBLE);
	if (program != null) {
		program.release(this);
		return;
	}

	//File cacheFile = FileSystemService.getInstance().getFile(file.getFSRL(), monitor);

	if (file.getFilesystem() instanceof GFileSystemProgramProvider) {
		LanguageService languageService = DefaultLanguageService.getLanguageService();

		GFileSystemProgramProvider fileSystem =
			(GFileSystemProgramProvider) file.getFilesystem();
		program = fileSystem.getProgram(file, languageService, monitor, this);
	}

	if (program != null) {
		try {
			DomainFolder folder = ProjectDataUtils.createDomainFolderPath(
				AppInfo.getActiveProject().getProjectData().getRootFolder(),
				file.getParentFile().getPath());
			String fileName = ProjectDataUtils.getUniqueName(folder, program.getName());

			GhidraProgramUtilities.setAnalyzedFlag(program, true);
			ImporterUtilities.setProgramProperties(program, file.getFSRL(), monitor);

			folder.createFile(fileName, program, monitor);

			programManager.openProgram(program);
			ProgramMappingService.createAssociation(file.getFSRL(), program);
		}
		finally {
			program.release(this);
		}
	}
}
 
Example 12
Source File: FrontEndPluginActionsTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testRenameFolderInUse() throws Exception {
	// folder contains a file that is in use
	DomainFolder f = rootFolder.createFolder("myFolder");
	f = f.createFolder("A");
	f = f.createFolder("B");
	f = f.createFolder("C");

	Program p = createDefaultProgram("new", ProgramBuilder._TOY, this);

	DomainFile df = f.createFile("notepad", p, TaskMonitor.DUMMY);
	waitForSwing();

	final GTreeNode myNode = rootNode.getChild("myFolder");
	((DomainFolderNode) myNode).getDomainFolder().createFile("notepad", p, TaskMonitor.DUMMY);
	p.release(this);

	waitForSwing();
	tree.expandPath(myNode.getTreePath());
	assertNotNull(myNode.getChild("notepad"));

	setInUse(df, "/myFolder/notepad");

	setSelectionPath(myNode.getTreePath());

	final DockingActionIf renameAction = getAction("Rename");
	performAction(renameAction, getDomainFileActionContext(), true);
	waitForTree();

	// attempt to rename "myFolder"
	SwingUtilities.invokeLater(() -> {
		int row = tree.getRowForPath(myNode.getTreePath());
		DefaultTreeCellEditor cellEditor = (DefaultTreeCellEditor) tree.getCellEditor();
		JTree jTree = (JTree) getInstanceField("tree", tree);
		Container container = (Container) cellEditor.getTreeCellEditorComponent(jTree, myNode,
			true, true, false, row);
		JTextField textField = (JTextField) container.getComponent(0);

		textField.setText("My_Newfolder");
		tree.stopEditing();
	});

	waitForSwing();

	OptionDialog d = waitForDialogComponent(OptionDialog.class);
	assertNotNull(d);
	assertEquals("Rename Failed", d.getTitle());
	pressButtonByText(d.getComponent(), "OK");
	assertNotNull(rootNode.getChild("myFolder"));
}
 
Example 13
Source File: GhidraVersionTrackingScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public void saveSessionAs(String path, String name) throws Exception {
	DomainFolder folder = state.getProject().getProjectData().getFolder(path);
	folder.createFile(name, vtSession, monitor);
	vtSession.setName(name);
}
 
Example 14
Source File: AutoVersionTrackingScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void run() throws Exception {

	DomainFolder folder =
		askProjectFolder("Please choose a folder for your Version Tracking session.");
	String name = askString("Please enter a Version Tracking session name", "Session Name");

	Program sourceProgram;
	Program destinationProgram;

	boolean isCurrentProgramSourceProg = askYesNo("Current Program Source Program?",
		"Is the current program your source program?");

	if (isCurrentProgramSourceProg) {
		sourceProgram = currentProgram;
		destinationProgram = askProgram("Please select the destination (new) program");
	}
	else {
		destinationProgram = currentProgram;
		sourceProgram = askProgram("Please select the source (existing annotated) program");
	}

	// Need to end the script transaction or it interferes with vt things that need locks
	end(true);

	VTSession session =
		VTSessionDB.createVTSession(name, sourceProgram, destinationProgram, this);

	folder.createFile(name, session, monitor);

	PluginTool tool = state.getTool();
	VTPlugin vtPlugin = getPlugin(tool, VTPlugin.class);
	if (vtPlugin == null) {
		tool.addPlugin(VTPlugin.class.getName());
		vtPlugin = getPlugin(tool, VTPlugin.class);
	}

	VTController controller = new VTControllerImpl(vtPlugin);

	//String description = "AutoVTScript";

	AutoVersionTrackingCommand autoVTcmd =
		new AutoVersionTrackingCommand(controller, session, 1.0, 10.0);

	controller.getTool().executeBackgroundCommand(autoVTcmd, session);
	//destinationProgram.save(description, monitor);

	//session.save(description, monitor);
	//session.release(this);

}
 
Example 15
Source File: CreateAppliedExactMatchingSessionScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void run() throws Exception {
	DomainFolder folder =
		askProjectFolder("Please choose a folder for the session domain object");
	String name = askString("Please enter a Version Tracking session name", "Session Name");
	Program sourceProgram = askProgram("Please select the source (existing annotated) program");
	Program destinationProgram = askProgram("Please select the destination (new) program");

	VTSession session =
		VTSessionDB.createVTSession(name, sourceProgram, destinationProgram, this);

	// it seems clunky to have to create this separately, but I'm not sure how else to do it
	folder.createFile(name, session, monitor);

	String description = "CreateAppliedExactMatchingSession";

	int sessionTransaction = session.startTransaction(description);
	try {
		PluginTool serviceProvider = state.getTool();
		VTAssociationManager manager = session.getAssociationManager();

		// should we have convenience methods in VTCorrelator that don't
		// take address sets, thus implying the entire address space should be used?
		AddressSetView sourceAddressSet = sourceProgram.getMemory().getLoadedAndInitializedAddressSet();
		AddressSetView destinationAddressSet =
			destinationProgram.getMemory().getLoadedAndInitializedAddressSet();

		VTProgramCorrelatorFactory factory;

		factory = new ExactDataMatchProgramCorrelatorFactory();
		correlateAndPossiblyApply(sourceProgram, destinationProgram, session, serviceProvider,
			manager, sourceAddressSet, destinationAddressSet, factory);

		factory = new ExactMatchBytesProgramCorrelatorFactory();
		correlateAndPossiblyApply(sourceProgram, destinationProgram, session, serviceProvider,
			manager, sourceAddressSet, destinationAddressSet, factory);

		factory = new ExactMatchInstructionsProgramCorrelatorFactory();
		correlateAndPossiblyApply(sourceProgram, destinationProgram, session, serviceProvider,
			manager, sourceAddressSet, destinationAddressSet, factory);
	}
	finally {
		try {
			session.endTransaction(sessionTransaction, true);
			destinationProgram.save(description, monitor);
			session.save(description, monitor);
		}
		finally {
			session.release(this);
		}
	}
}