def test_CreateRandomGraph( node_x_dimensionality: int, node_y_dimensionality: int, graph_x_dimensionality: int, graph_y_dimensionality: int, ): """Black-box test of generator properties.""" g = random_networkx_generator.CreateRandomGraph( node_x_dimensionality=node_x_dimensionality, node_y_dimensionality=node_y_dimensionality, graph_x_dimensionality=graph_x_dimensionality, graph_y_dimensionality=graph_y_dimensionality, ) for _, data in g.nodes(data=True): assert len(data["x"]) == node_x_dimensionality assert len(data["y"]) == node_y_dimensionality assert len(g.graph["x"]) == graph_x_dimensionality assert len(g.graph["y"]) == graph_y_dimensionality
def test_fuzz_graph_tuple_networkx(): """Fuzz graph tuples with randomly generated graphs.""" node_x_dimensionality = random.randint(1, 3) node_y_dimensionality = random.randint(0, 3) graph_x_dimensionality = random.randint(0, 3) graph_y_dimensionality = random.randint(0, 3) graph_in = random_networkx_generator.CreateRandomGraph( node_x_dimensionality=node_x_dimensionality, node_y_dimensionality=node_y_dimensionality, graph_x_dimensionality=graph_x_dimensionality, graph_y_dimensionality=graph_y_dimensionality, ) t = graph_tuple.GraphTuple.CreateFromNetworkX(graph_in) try: assert t.node_count == graph_in.number_of_nodes() assert t.edge_count == graph_in.number_of_edges() assert t.node_x_dimensionality == node_x_dimensionality assert t.node_y_dimensionality == node_y_dimensionality assert t.graph_x_dimensionality == graph_x_dimensionality assert t.graph_y_dimensionality == graph_y_dimensionality assert t.edge_position_max == max( position for _, _, position in graph_in.edges(data="position")) except AssertionError: fs.Write("/tmp/graph_in.pickle", pickle.dumps(graph_in)) fs.Write("/tmp/graph_tuple_out.pickle", pickle.dumps(t)) app.Error("Assertion failed! Wrote graphs to /tmp/graphs_in.pickle " "and /tmp/graph_tuple_out.pickle") raise g_out = t.ToNetworkx() try: assert graph_in.number_of_nodes() == g_out.number_of_nodes() assert graph_in.number_of_edges() == g_out.number_of_edges() assert len(g_out.nodes[0]["x"]) == node_x_dimensionality assert len(g_out.nodes[0]["y"]) == node_y_dimensionality assert len(g_out.graph["x"]) == graph_x_dimensionality assert len(g_out.graph["y"]) == graph_y_dimensionality except AssertionError: fs.Write("/tmp/graph_in.pickle", pickle.dumps(graph_in)) fs.Write("/tmp/graph_out.pickle", pickle.dumps(g_out)) app.Error("Assertion failed! Wrote graphs to /tmp/graphs_in.pickle " "and /tmp/graphs_out.pickle") raise
def test_fuzz_GraphTuple_CreateFromNetworkX( db_session: graph_tuple_database.Database.SessionType, ): """Fuzz the networkx -> proto conversion using randomly generated graphs.""" g = random_networkx_generator.CreateRandomGraph() t = graph_tuple_database.GraphTuple.CreateFromNetworkX( g=g, ir_id=random.randint(0, int(4e6))) # Test the derived properties of the generated graph tuple. assert t.edge_count == (t.control_edge_count + t.data_edge_count + t.call_edge_count) assert len(t.sha1) == 40 assert t.node_count == g.number_of_nodes() assert t.edge_count == g.number_of_edges() assert t.tuple.node_count == g.number_of_nodes() assert t.tuple.edge_count == g.number_of_edges() assert len(t.tuple.adjacencies) == 3 assert len(t.tuple.edge_positions) == 3 # Add it to the database to catch SQL integrity errors. db_session.add(t) db_session.commit()
def CreateRandomGraphTuple( disjoint_graph_count: int = 1, node_x_dimensionality: int = 1, node_y_dimensionality: int = 0, graph_x_dimensionality: int = 0, graph_y_dimensionality: int = 0, node_count: int = None, ) -> graph_tuple.GraphTuple: """Generate a random graph tuple. This generates a random graph which has sensible values for fields, but does not have meaningful semantics, e.g. there may be data flow edges between identifiers, etc. For speed, this generator guarantees only that: 1. There is a 'root' node with outgoing call edges. 2. Nodes are either statements, identifiers, or immediates. 3. Nodes have text, preprocessed_text, and a single node_x value. 4. Edges are either control, data, or call. 5. Edges have positions. 6. The graph is strongly connected. """ graphs = [ random_networkx_generator.CreateRandomGraph( node_x_dimensionality=node_x_dimensionality, node_y_dimensionality=node_y_dimensionality, graph_x_dimensionality=graph_x_dimensionality, graph_y_dimensionality=graph_y_dimensionality, node_count=node_count, ) for _ in range(disjoint_graph_count) ] graph_tuples = [ graph_tuple.GraphTuple.CreateFromNetworkX(graph) for graph in graphs ] if len(graph_tuples) > 1: return graph_tuple.GraphTuple.FromGraphTuples(graph_tuples) else: return graph_tuples[0]
def test_CreateRandomGraph_node_count(node_count: int): """Test generating protos with specific node counts.""" g = random_networkx_generator.CreateRandomGraph(node_count=node_count) assert g.number_of_nodes() == node_count