def test_add_node(self):
        parent_node = TreeNode()
        parent_node.log = logging

        parent_node.add_child(None)

        assert len(parent_node.children) == 0
    def test_link_to_dest(self):
        #
        # A relation from relation_source_id to relation_dest_id
        #
        relation_source_id = "source object id"
        relation_dest_id = "dest object id"

        des_node = TreeNode()
        des_node.id = relation_dest_id
        src_node = TreeNode()
        src_node.id = relation_source_id

        # The tree has a parent node as the root and
        # and both source node and dest node are children of the parent node
        parent_node = TreeNode()
        tree = MultiRootTree(parent_node)
        parent_node.add_child(des_node)
        parent_node.add_child(src_node)



        v = RelationVisitor(src_id=relation_source_id,
                            des_id=relation_dest_id,
                            stix_objects=[],
                            multi_root_tree=tree,
                            log=logging)
        #
        # visit the des_node. Here both the src_node and the des_node
        # are already in the tree
        #
        ret = des_node.accept(v)

        #
        # A new link node will be added to the src node
        #
        assert len(src_node.children) == 1
        new_node = src_node.children[0]


        # This new node is a copy of the des node
        assert new_node.id == relation_dest_id
        assert new_node.type == des_node.type
        assert new_node.name == des_node.name
        assert not new_node.is_root
        assert new_node.is_link

        # if we found, we shall stop
        assert not ret
    def test_get_node_visitor_break(self):
        """
        parent
            child1 (with match id)
            child2
        :return:
        """
        matched_obj_id = "matched obj id"
        parent = TreeNode(obj_type=None, obj_id="fake id")
        child1 = TreeNode(obj_type=None, obj_id=matched_obj_id)
        child2 = TreeNode(obj_type=None, obj_id="fake id2")
        parent.add_child(child1)
        parent.add_child(child2)

        v = GetNodeVisitor(matched_obj_id)
        parent.accept(v)

        assert v.node == child1
Пример #4
0
    def test(self):
        """
        Use the following tree for test
        root1
            child1
                child2
                child3
                    child4
            child5
                child6
                child2(link)
        root2
            child7
        :return:
        """

        root1 = TreeNode(obj_type="indicator", obj_id="", name="root1")
        root2 = TreeNode(obj_type="identity", obj_id="", name="root2")
        #
        # 1. First build a tree
        #
        tree = MultiRootTree(root1)

        tree.add_root(root2)

        child1 = TreeNode(obj_type="malware", obj_id="", name="child1")

        root1.add_child(child1)

        child2 = TreeNode(obj_type="indicator", obj_id="", name="child2")

        child1.add_child(child2)

        child3 = TreeNode(obj_type="malware", obj_id="", name="child3")
        child1.add_child(child3)

        child4 = TreeNode(obj_type="observed-data",
                          obj_id="",
                          objects={"0": {
                              "type": "file",
                              "name": "child4"
                          }},
                          name="child4")
        child3.add_child(child4)

        child5 = TreeNode(obj_type="file", obj_id="", name="child5")

        root1.add_child(child5)

        child6 = TreeNode(
            obj_type="observed-data",
            obj_id="",
            objects={"0": {
                "type": "ipv4-addr",
                "value": "child6"
            }},
            name="child6")

        child5.add_child(child6)
        child2_link = TreeNode(obj_type="indicator", obj_id="", name="child2")
        child2_link.is_link = True
        child5.add_child(child2_link)

        child7 = TreeNode(obj_type="ipv4-addr", obj_id="", name="child7")
        root2.add_child(child7)

        #
        # Use a html_gen_visitor to parse the tree
        #
        visitor = HtmlGenVisitor(logging)

        tree.accept(visitor=visitor)

        #
        # Output the html file
        #
        with open("TestGenHtml.html", "w") as outfile:
            outfile.write(visitor.html)
            # Visually verify that the output html
            # shows a tree similar to the one above

        assert "root1" in visitor.html
        assert "root2" in visitor.html

        for x in range(1, 7, 1):
            # assert each child is there
            name = "child" + str(x)
            assert name in visitor.html