Search in sources :

Example 6 with TreeNode

use of com.google.security.zynamics.zylib.types.trees.TreeNode in project binnavi by google.

the class PostgreSQLTagManagerFunctions method loadTagManager.

/**
   * Loads a tag manager from the database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param type Type of the tags managed by the tag manager.
   * 
   * @return The tag manager that is loaded from the database.
   * 
   * @throws CouldntLoadDataException Thrown if the tag manager could not be loaded.
   */
public static CTagManager loadTagManager(final AbstractSQLProvider provider, final TagType type) throws CouldntLoadDataException {
    Preconditions.checkNotNull(type, "IE00567: Tag type argument can't be null");
    final CConnection connection = provider.getConnection();
    // should always have a tags table
    if (!PostgreSQLHelpers.hasTable(connection, CTableNames.TAGS_TABLE)) {
        final CTag rootTag = new CTag(0, "Root Node", "", type, provider);
        return new CTagManager(new Tree<CTag>(new TreeNode<CTag>(rootTag)), type, provider);
    }
    final String query = String.format("select id, parent_id, name, description from %s where type = '%s'", CTableNames.TAGS_TABLE, PostgreSQLTagFunctions.tagToString(type));
    try {
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            final HashMap<Integer, Pair<TreeNode<CTag>, Integer>> treeNodes = new HashMap<Integer, Pair<TreeNode<CTag>, Integer>>();
            final CTag rootTag = new CTag(0, "Root Node", "", type, provider);
            final TreeNode<CTag> rootTreeNode = new TreeNode<CTag>(rootTag);
            treeNodes.put(0, new Pair<TreeNode<CTag>, Integer>(rootTreeNode, -1));
            while (resultSet.next()) {
                final int tagId = resultSet.getInt("id");
                final int parentId = resultSet.getInt("parent_id");
                final TreeNode<CTag> treeNode = new TreeNode<CTag>(new CTag(tagId, PostgreSQLHelpers.readString(resultSet, "name"), PostgreSQLHelpers.readString(resultSet, "description"), type, provider));
                final Pair<TreeNode<CTag>, Integer> pair = new Pair<TreeNode<CTag>, Integer>(treeNode, parentId);
                treeNodes.put(tagId, pair);
            }
            for (final Entry<Integer, Pair<TreeNode<CTag>, Integer>> e : treeNodes.entrySet()) {
                if (e.getKey() == 0) {
                    continue;
                }
                final TreeNode<CTag> child = e.getValue().first();
                final TreeNode<CTag> parent = treeNodes.get(e.getValue().second()).first();
                child.setParent(parent);
                parent.addChild(child);
            }
            return new CTagManager(new Tree<CTag>(rootTreeNode), type, provider);
        } finally {
            resultSet.close();
        }
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) TreeNode(com.google.security.zynamics.zylib.types.trees.TreeNode) ResultSet(java.sql.ResultSet) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 7 with TreeNode

use of com.google.security.zynamics.zylib.types.trees.TreeNode in project binnavi by google.

the class CTagNode method createChildren.

@Override
protected void createChildren() {
    if (m_database.isLoaded()) {
        final List<ITreeNode<CTag>> children = m_tag.getChildren();
        Collections.sort(children, new TagComparator());
        for (final ITreeNode<CTag> tag : children) {
            add(new CTagNode(getProjectTree(), m_database, (TreeNode<CTag>) tag));
        }
    }
}
Also used : ITreeNode(com.google.security.zynamics.zylib.types.trees.ITreeNode) CProjectTreeNode(com.google.security.zynamics.binnavi.Gui.MainWindow.ProjectTree.Nodes.CProjectTreeNode) TreeNode(com.google.security.zynamics.zylib.types.trees.TreeNode) ITreeNode(com.google.security.zynamics.zylib.types.trees.ITreeNode) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) TagComparator(com.google.security.zynamics.binnavi.Tagging.TagComparator)

Example 8 with TreeNode

use of com.google.security.zynamics.zylib.types.trees.TreeNode in project binnavi by google.

the class ViewNodeTest method setUp.

@Before
public void setUp() throws CouldntSaveDataException, CouldntLoadDataException, LoadCancelledException, FileReadException {
    ConfigManager.instance().read();
    final MockSqlProvider provider = new MockSqlProvider();
    final Database database = new Database(new MockDatabase());
    final CModule internalModule = new CModule(123, "Name", "Comment", new Date(), new Date(), CommonTestObjects.MD5, CommonTestObjects.SHA1, 55, 66, new CAddress(0x555), new CAddress(0x666), new DebuggerTemplate(1, "Mock Debugger", "localhaus", 88, provider), null, Integer.MAX_VALUE, false, provider);
    internalModule.load();
    final CTagManager mockTagManager = new CTagManager(new Tree<CTag>(new TreeNode<CTag>(new CTag(1, "Root", "", TagType.NODE_TAG, provider))), TagType.NODE_TAG, provider);
    m_nodeTagManager = new TagManager(mockTagManager);
    final TagManager viewTagManager = new TagManager(new MockTagManager(com.google.security.zynamics.binnavi.Tagging.TagType.VIEW_TAG));
    final CFunction parentFunction = new CFunction(internalModule, new MockView(), new CAddress(0x123), "Mock Function", "Mock Function", "Mock Description", 0, 0, 0, 0, FunctionType.NORMAL, "", 0, null, null, null, provider);
    final Function function = new Function(ModuleFactory.get(), parentFunction);
    final Module module = new Module(database, internalModule, m_nodeTagManager, viewTagManager) {

        @Override
        public Function getFunction(final INaviFunction internalFunction) {
            return function;
        }

        @Override
        public boolean isLoaded() {
            return true;
        }
    };
    final CFunction internalFunction = new CFunction(internalModule, new MockView(), new CAddress(0x123), "", "", "", 0, 0, 0, 0, FunctionType.NORMAL, "", 0, null, null, null, provider);
    final ITreeNode<CTag> tag = mockTagManager.addTag(mockTagManager.getRootTag(), "Initial Tag");
    m_initialTag = m_nodeTagManager.getRootTags().get(0);
    final CView internalView = internalModule.getContent().getViewContainer().createView("", "");
    final CFunctionNode node = internalView.getContent().createFunctionNode(internalFunction);
    node.setColor(Color.MAGENTA);
    node.setX(10);
    node.setY(20);
    node.tagNode(tag.getObject());
    m_view = module.getViews().get(2);
    m_node = (FunctionNode) m_view.getGraph().getNodes().get(0);
    final CFunctionNode node2 = internalView.getContent().createFunctionNode(internalFunction);
    internalView.getContent().createEdge(node, node2, EdgeType.INTER_MODULE);
}
Also used : CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) DebuggerTemplate(com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate) MockView(com.google.security.zynamics.binnavi.disassembly.MockView) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) CFunction(com.google.security.zynamics.binnavi.disassembly.CFunction) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) MockTagManager(com.google.security.zynamics.binnavi.Tagging.MockTagManager) Date(java.util.Date) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) CView(com.google.security.zynamics.binnavi.disassembly.views.CView) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) CFunction(com.google.security.zynamics.binnavi.disassembly.CFunction) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) MockTagManager(com.google.security.zynamics.binnavi.Tagging.MockTagManager) MockSqlProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider) TreeNode(com.google.security.zynamics.zylib.types.trees.TreeNode) ITreeNode(com.google.security.zynamics.zylib.types.trees.ITreeNode) MockDatabase(com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase) MockDatabase(com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) Before(org.junit.Before)

Example 9 with TreeNode

use of com.google.security.zynamics.zylib.types.trees.TreeNode in project binnavi by google.

the class InstructionTest method testCreate.

@Test
public void testCreate() throws CouldntLoadDataException, LoadCancelledException {
    final SQLProvider provider = new MockSqlProvider();
    final CModule internalModule = new CModule(123, "Name", "Comment", new Date(), new Date(), "12345678123456781234567812345678", "1234567812345678123456781234567812345678", 55, 66, new CAddress(0x555), new CAddress(0x666), new DebuggerTemplate(1, "Mock Debugger", "localhaus", 88, provider), null, Integer.MAX_VALUE, false, provider);
    internalModule.load();
    final Database database = new Database(new MockDatabase());
    final CTagManager mockTagManager = new CTagManager(new Tree<CTag>(new TreeNode<CTag>(new CTag(1, "Root", "", TagType.NODE_TAG, provider))), TagType.NODE_TAG, provider);
    final TagManager nodeTagManager = new TagManager(mockTagManager);
    final TagManager viewTagManager = new TagManager(new MockTagManager(com.google.security.zynamics.binnavi.Tagging.TagType.VIEW_TAG));
    final Module module = new Module(database, internalModule, nodeTagManager, viewTagManager);
    final List<Operand> operands = new ArrayList<Operand>();
    final OperandExpression ex2 = OperandExpression.create(module, "eax", ExpressionType.Register);
    final OperandExpression ex4 = OperandExpression.create(module, "ebx", ExpressionType.Register);
    operands.add(Operand.create(module, ex2));
    operands.add(Operand.create(module, ex4));
    final Instruction instruction = Instruction.create(module, new Address(0x123), "mov", operands, new byte[] { 1, 2, 3 }, "x86-32");
    assertEquals(0x123, instruction.getAddress().toLong());
    assertEquals(null, instruction.getComment());
    assertArrayEquals(new byte[] { 1, 2, 3 }, instruction.getData());
    assertEquals("mov", instruction.getMnemonic());
    assertEquals(2, instruction.getOperands().size());
    assertEquals("eax", instruction.getOperands().get(0).getRootNode().getChildren().get(0).getValue());
    assertEquals("ebx", instruction.getOperands().get(1).getRootNode().getChildren().get(0).getValue());
    assertEquals("123  mov eax, ebx", instruction.toString());
}
Also used : CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) DebuggerTemplate(com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ArrayList(java.util.ArrayList) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) MockTagManager(com.google.security.zynamics.binnavi.Tagging.MockTagManager) CInstruction(com.google.security.zynamics.binnavi.disassembly.CInstruction) SQLProvider(com.google.security.zynamics.binnavi.Database.Interfaces.SQLProvider) Date(java.util.Date) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) MockTagManager(com.google.security.zynamics.binnavi.Tagging.MockTagManager) MockSqlProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider) TreeNode(com.google.security.zynamics.zylib.types.trees.TreeNode) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) MockDatabase(com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase) MockDatabase(com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) Test(org.junit.Test)

Example 10 with TreeNode

use of com.google.security.zynamics.zylib.types.trees.TreeNode in project binnavi by google.

the class CTaggedGraphNodesContainerNodeTest method test1Simple.

@Test
public void test1Simple() throws FileReadException, CouldntLoadDataException, LoadCancelledException, CouldntSaveDataException {
    final ZyGraph graph = ZyGraphFactory.generateTestGraph();
    final CTagsTreeModel model = new CTagsTreeModel(null);
    final CTag tag = new CTag(0, "tag", "description", TagType.NODE_TAG, new MockSqlProvider());
    final TreeNode<CTag> treeNode = new TreeNode<CTag>(tag);
    final CTaggedGraphNodesContainerNode containerNode = new CTaggedGraphNodesContainerNode(graph, model, treeNode);
    assertEquals("Tagged Nodes (0/0/1/1)", containerNode.toString());
    assertEquals(null, containerNode.getIconName());
    containerNode.getIcon();
}
Also used : ZyGraph(com.google.security.zynamics.binnavi.yfileswrap.zygraph.ZyGraph) MockSqlProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider) TreeNode(com.google.security.zynamics.zylib.types.trees.TreeNode) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) CTagsTreeModel(com.google.security.zynamics.binnavi.Gui.GraphWindows.NodeTaggingTree.CTagsTreeModel) Test(org.junit.Test)

Aggregations

CTag (com.google.security.zynamics.binnavi.Tagging.CTag)14 TreeNode (com.google.security.zynamics.zylib.types.trees.TreeNode)14 ITreeNode (com.google.security.zynamics.zylib.types.trees.ITreeNode)11 CTagManager (com.google.security.zynamics.binnavi.Tagging.CTagManager)10 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)9 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)5 Test (org.junit.Test)5 MockDatabase (com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase)4 CModule (com.google.security.zynamics.binnavi.disassembly.Modules.CModule)4 Date (java.util.Date)4 Before (org.junit.Before)4 TagManager (com.google.security.zynamics.binnavi.API.disassembly.TagManager)3 SQLProvider (com.google.security.zynamics.binnavi.Database.Interfaces.SQLProvider)3 MockTagManager (com.google.security.zynamics.binnavi.Tagging.MockTagManager)3 MockView (com.google.security.zynamics.binnavi.disassembly.MockView)3 Tree (com.google.security.zynamics.zylib.types.trees.Tree)3 Database (com.google.security.zynamics.binnavi.API.disassembly.Database)2 Module (com.google.security.zynamics.binnavi.API.disassembly.Module)2 MockDebugger (com.google.security.zynamics.binnavi.Debug.Debugger.MockDebugger)2 CProjectTreeNode (com.google.security.zynamics.binnavi.Gui.MainWindow.ProjectTree.Nodes.CProjectTreeNode)2