use of com.google.security.zynamics.binnavi.disassembly.INaviModule in project binnavi by google.
the class CViewInserterTest method test.
@Test
public void test() throws CouldntLoadDataException, CPartialLoadException, LoadCancelledException, FileReadException, CouldntSaveDataException {
ConfigManager.instance().read();
final INaviModule mockModule = new MockModule();
final MockSqlProvider mockProvider = new MockSqlProvider();
final CUserManager userManager = CUserManager.get(mockProvider);
final IUser user = userManager.addUser(" VIEW INSERTER USER ");
userManager.setCurrentActiveUser(user);
final CModuleViewGenerator generator = new CModuleViewGenerator(mockProvider, mockModule);
final CView view = generator.generate(1, "", "", ViewType.NonNative, GraphType.MIXED_GRAPH, new Date(), new Date(), 0, 0, new HashSet<CTag>(), new HashSet<CTag>(), false);
view.load();
final MockFunction mockFunction = new MockFunction(mockProvider);
final CFunctionNode fnode1 = view.getContent().createFunctionNode(mockFunction);
final CFunctionNode fnode2 = view.getContent().createFunctionNode(mockFunction);
@SuppressWarnings("unused") final CNaviViewEdge edge1 = view.getContent().createEdge(fnode1, fnode2, EdgeType.JUMP_UNCONDITIONAL);
final MockInstruction instruction1 = new MockInstruction();
final CCodeNode cnode1 = view.getContent().createCodeNode(mockFunction, Lists.newArrayList(instruction1));
final CCodeNode cnode2 = view.getContent().createCodeNode(mockFunction, Lists.newArrayList(instruction1));
@SuppressWarnings("unused") final CNaviViewEdge edge2 = view.getContent().createEdge(cnode1, cnode2, EdgeType.JUMP_UNCONDITIONAL);
final ArrayList<IComment> comments = Lists.<IComment>newArrayList(new CComment(null, CommonTestObjects.TEST_USER_1, null, "Foo"));
final CTextNode tnode1 = view.getContent().createTextNode(comments);
@SuppressWarnings("unused") final CNaviViewEdge edge3 = view.getContent().createEdge(cnode1, tnode1, EdgeType.JUMP_UNCONDITIONAL);
final CGroupNode gnode1 = view.getContent().createGroupNode(Lists.newArrayList((INaviViewNode) fnode1, (INaviViewNode) fnode2));
gnode1.appendComment("TEST GROUP NODE COMMENT 1");
final CView view2 = generator.generate(2, "", "", ViewType.NonNative, GraphType.MIXED_GRAPH, new Date(), new Date(), 0, 0, new HashSet<CTag>(), new HashSet<CTag>(), false);
view2.load();
CViewInserter.insertView(view, view2);
final List<INaviViewNode> nodes = view2.getGraph().getNodes();
assertEquals(view2.getNodeCount(), 6);
assertEquals(mockFunction, ((INaviFunctionNode) nodes.get(0)).getFunction());
assertEquals(nodes.get(5), ((INaviFunctionNode) nodes.get(0)).getParentGroup());
}
use of com.google.security.zynamics.binnavi.disassembly.INaviModule in project binnavi by google.
the class CPostgreSQLZyGraphTest2 method testSave.
@Test
public void testSave() throws CouldntSaveDataException, CouldntLoadDriverException, CouldntConnectException, InvalidDatabaseException, CouldntInitializeDatabaseException, CouldntLoadDataException, InvalidExporterDatabaseFormatException, InvalidDatabaseVersionException, CPartialLoadException, LoadCancelledException {
m_view.getGraph().getNodes().get(0).setSelected(true);
m_view.getGraph().getNodes().get(1).setColor(new Color(123));
m_view.getGraph().getNodes().get(2).setX(456);
m_view.getGraph().getNodes().get(3).setY(789);
final INaviView newView = m_graph.saveAs(new CModuleContainer(m_database, m_module), "New View", "New View Description");
m_database2.connect();
m_database2.load();
final INaviModule module = m_database2.getContent().getModules().get(0);
module.load();
final Iterable<INaviView> views = CViewFilter.getFlowgraphViews(module.getContent().getViewContainer().getViews());
final INaviView loadedNewView = Iterables.getLast(views);
loadedNewView.load();
assertEquals(loadedNewView.getNodeCount(), newView.getNodeCount());
assertEquals(true, loadedNewView.getGraph().getNodes().get(0).isSelected());
assertEquals(0xFF00007B, loadedNewView.getGraph().getNodes().get(1).getColor().getRGB());
assertEquals(456, loadedNewView.getGraph().getNodes().get(2).getX(), 0);
assertEquals(789, loadedNewView.getGraph().getNodes().get(3).getY(), 0);
m_database2.close();
}
use of com.google.security.zynamics.binnavi.disassembly.INaviModule in project binnavi by google.
the class PostgreSQLAddressSpaceLoader method loadAddressSpaces.
/**
* Loads the address spaces of a project.
*
* The project, the debugger manager, and all modules in the module list must be stored in the
* database connected to by the provider argument.
*
* @param provider The SQL provider that provides the connection.
* @param project The parent project of the address spaces to load.
* @param debuggerManager Debugger manager of the database.
* @param list A list of all modules that belong to the database.
*
* @return A list that contains the address spaces of the project.
*
* @throws CouldntLoadDataException Thrown if the address spaces could not be loaded.
*/
public static List<CAddressSpace> loadAddressSpaces(final AbstractSQLProvider provider, final INaviProject project, final DebuggerTemplateManager debuggerManager, final List<INaviModule> list) throws CouldntLoadDataException {
checkArguments(provider, project);
Preconditions.checkNotNull(debuggerManager, "IE01543: Debugger provider argument can not be null");
Preconditions.checkNotNull(list, "IE01545: Modules argument can not be null");
NaviLogger.info("Loading address spaces of project %s", project.getConfiguration().getName());
final CConnection connection = provider.getConnection();
final List<CAddressSpace> addressSpaces = new ArrayList<CAddressSpace>();
final String query = "SELECT id, name, description, creation_date, modification_date, debugger_id " + " FROM " + CTableNames.ADDRESS_SPACES_TABLE + " WHERE project_id = " + project.getConfiguration().getId();
try {
final ResultSet resultSet = connection.executeQuery(query, true);
try {
while (resultSet.next()) {
final int addressSpaceId = resultSet.getInt("id");
final Map<INaviModule, IAddress> imageBases = loadImageBases(connection, addressSpaceId, list);
final String name = PostgreSQLHelpers.readString(resultSet, "name");
final String description = PostgreSQLHelpers.readString(resultSet, "description");
final Timestamp creationDate = resultSet.getTimestamp("creation_date");
final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
final DebuggerTemplate debuggerDescription = debuggerManager.findDebugger(resultSet.getInt("debugger_id"));
addressSpaces.add(new CAddressSpace(addressSpaceId, name, description, creationDate, modificationDate, imageBases, debuggerDescription, provider, project));
}
return addressSpaces;
} finally {
resultSet.close();
}
} catch (final SQLException e) {
throw new CouldntLoadDataException(e);
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviModule in project binnavi by google.
the class PostgreSQLAddressSpaceLoader method loadImageBases.
/**
* Loads the image bases of the given modules within the given address space.
*
* The address space ID and the modules in the module list must all reference items that are
* stored in the database connected to by connection argument.
*
* @param connection Connection to the database.
* @param addressSpaceId ID of the address space.
* @param list Modules whose image bases are loaded.
*
* @return A mapping of modules -> image bases for all modules in the address space.
*
* @throws CouldntLoadDataException Thrown if the image bases could not be loaded.
*/
private static Map<INaviModule, IAddress> loadImageBases(final CConnection connection, final int addressSpaceId, final List<INaviModule> list) throws CouldntLoadDataException {
final HashMap<INaviModule, IAddress> imageBases = new HashMap<INaviModule, IAddress>();
final String query = "SELECT module_id, image_base FROM " + CTableNames.SPACE_MODULES_TABLE + " WHERE address_space_id = " + addressSpaceId;
try {
final ResultSet resultSet = connection.executeQuery(query, true);
try {
while (resultSet.next()) {
try {
final INaviModule module = findModule(list, resultSet.getInt("module_id"));
imageBases.put(module, PostgreSQLHelpers.loadAddress(resultSet, "image_base"));
} catch (final MaybeNullException exception) {
// I can not think of a scenario where this can happen.
CUtilityFunctions.logException(exception);
}
}
} finally {
resultSet.close();
}
return imageBases;
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviModule in project binnavi by google.
the class PostgreSQLInstructionFunctions method createInstructions.
/**
* Saves an instruction to the database.
*
* @param provider The provider used to access the database.
* @param instructions The instruction to save.
*
* @throws SQLException Thrown if the instruction could not be created.
*/
public static void createInstructions(final SQLProvider provider, final Iterable<INaviInstruction> instructions) throws SQLException {
Preconditions.checkNotNull(provider, "IE01550: Provider argument can not be null");
Preconditions.checkNotNull(instructions, "IE01554: Instruction argument can not be null");
final String query = "INSERT INTO " + CTableNames.INSTRUCTIONS_TABLE + "(module_id, address, mnemonic, data, native, architecture, comment_id) " + "VALUES(?, ?, ?, ?, ?, ?, ?)";
final PreparedStatement insertStatement = provider.getConnection().getConnection().prepareStatement(query);
final ArrayList<INaviInstruction> instructionsWithUnsavedComments = new ArrayList<INaviInstruction>();
final List<List<COperandTree>> operands = new ArrayList<List<COperandTree>>();
for (final INaviInstruction instruction : instructions) {
final String mnemonic = instruction.getMnemonic();
final byte[] data = instruction.getData();
operands.add(instruction.getOperands());
final INaviModule module = instruction.getModule();
final IAddress address = instruction.getAddress();
final int moduleID = module.getConfiguration().getId();
final List<IComment> comments = instruction.getGlobalComment();
final Integer commentId = comments == null ? null : comments.size() == 0 ? null : Iterables.getLast(comments).getId();
if ((comments != null) && (comments.size() != 0) && (commentId == null)) {
instructionsWithUnsavedComments.add(instruction);
}
try {
insertStatement.setInt(1, moduleID);
insertStatement.setObject(2, address.toBigInteger(), Types.BIGINT);
insertStatement.setString(3, mnemonic);
insertStatement.setBytes(4, data);
insertStatement.setBoolean(5, false);
insertStatement.setObject(6, instruction.getArchitecture(), Types.OTHER);
if (commentId == null) {
insertStatement.setNull(7, Types.INTEGER);
} else {
insertStatement.setInt(7, commentId);
}
insertStatement.execute();
} finally {
insertStatement.close();
}
}
// unsaved comments.
for (final INaviInstruction instruction : instructionsWithUnsavedComments) {
final ArrayList<IComment> instructionComments = new ArrayList<IComment>();
for (final IComment comment : instruction.getGlobalComment()) {
try {
final Integer commentId = PostgreSQLInstructionFunctions.appendGlobalInstructionComment(provider, instruction, comment.getComment(), comment.getUser().getUserId());
final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
instructionComments.add(newComment);
} catch (final CouldntSaveDataException exception) {
CUtilityFunctions.logException(exception);
}
}
instruction.initializeGlobalComment(instructionComments);
}
for (final List<COperandTree> operand : operands) {
int position = 0;
for (final COperandTree operandTree : operand) {
createOperandTree(provider, operandTree, position);
position++;
}
}
}
Aggregations