def take_add_event_listener_edge(event_type): assert len(relevant_edges) > 0 edge = relevant_edges.pop() assert pg_edge_data(edge, 'edge type') == 'add event listener' assert pg_edge_data(edge, 'key') == event_type event_listener_id = pg_edge_data(edge, 'event listener id') event_listener_script_id = pg_edge_data(edge, 'script id') assert event_listener_id not in event_listeners event_listeners[event_listener_id] = event_listener_script_id return edge
def take_remove_event_listener_edge(event_type): assert len(relevant_edges) > 0 edge = relevant_edges.pop() assert pg_edge_data(edge, 'edge type') == 'remove event listener' assert pg_edge_data(edge, 'key') == event_type event_listener_id = pg_edge_data(edge, 'event listener id') event_listener_script_id = pg_edge_data(edge, 'script id') assert event_listener_id in event_listeners assert event_listeners[event_listener_id] == event_listener_script_id del event_listeners[event_listener_id] return edge
def test(page_graph, html, tab, headless): script_nodes = pg_find_html_element_node( page_graph, 'script', generate_script_text_selector('fetch')) assert len(script_nodes) == 1 html_element = script_nodes[0] # there should be an execute edge to the actual script scripts = pg_nodes_directly_reachable_from_with_edge_type( page_graph, html_element, 'execute') assert len(scripts) == 1 actual_script = scripts[0] # there should be a request start edge to a resource node, # as well as a request complete edge back all_edges_from_script = [] all_edges_to_script = [] for edge in page_graph.edges(data=True): if pg_edge_out(edge) == actual_script: all_edges_from_script.append(edge) elif pg_edge_in(edge) == actual_script: all_edges_to_script.append(edge) # we have on edge from the script... assert len(all_edges_from_script) == 1 # ... and it should be a request start edge ... assert pg_edge_data(all_edges_from_script[0], 'edge type') == 'request start' # ... and the target should be a resource node. assert pg_get_node_data( page_graph, all_edges_from_script[0][1])['node type'] == 'resource' # we should have two edges coming to the script (execute and request complete)... assert len(all_edges_to_script) == 2 for edge in all_edges_to_script: assert (pg_edge_data(edge, 'edge type') == 'execute' or pg_edge_data(edge, 'edge type') == 'request complete') # ... where the edge from the resource should be a request complete resource_node = all_edges_from_script[0][1] for edge in all_edges_to_script: if pg_edge_out(edge) == resource_node: assert pg_edge_data(edge, 'edge type') == 'request complete'
def test(page_graph, html, tab, headless): form = pg_find_html_element_node(page_graph, 'form', lambda page_graph, node: True)[0] expected_edge_type = 'add event listener' event_edge_in = [ edge for edge in page_graph.in_edges(form, data=True) if pg_edge_data(edge, 'edge type') == expected_edge_type ] assert len(event_edge_in) == 1 event_edge_in = event_edge_in[0] parser_node = list(page_graph.nodes)[0] assert parser_node == pg_edge_out(event_edge_in) assert form == pg_edge_in(event_edge_in) # check the edge from the form to the event handler expected_edge_type_out = 'event listener' event_edge_out = [ edge for edge in page_graph.out_edges(form, data=True) if pg_edge_data(edge, 'edge type') == expected_edge_type_out ] assert len(event_edge_out) == 1 event_edge_out = event_edge_out[0] event_function_node = pg_find_html_element_node( page_graph, 'script', lambda page_graph, node: True)[0] successors = list(page_graph.successors(event_function_node)) assert len(successors) == 2 # the text node, and a script node # actual_function_node = successors[1] # form has an edge from itself to the event handler assert form == pg_edge_out(event_edge_out) # assert actual_function_node == pg_edge_in(event_edge_out) # it should be the same event listener id on the two edges assert pg_edge_data(event_edge_in, 'event listener id') == pg_edge_data( event_edge_out, 'event listener id')
def test(page_graph, html, tab, headless): div_node = pg_find_html_element_node(page_graph, 'div', lambda page_graph, node: True)[0] relevant_edge_types = {'add event listener', 'remove event listener'} relevant_edges = [ edge for edge in page_graph.in_edges(div_node, data=True) if pg_edge_data(edge, 'edge type') in relevant_edge_types ] relevant_edges.reverse() script_nodes = [] script_ids = [] event_listeners = {} def take_add_event_listener_edge(event_type): assert len(relevant_edges) > 0 edge = relevant_edges.pop() assert pg_edge_data(edge, 'edge type') == 'add event listener' assert pg_edge_data(edge, 'key') == event_type event_listener_id = pg_edge_data(edge, 'event listener id') event_listener_script_id = pg_edge_data(edge, 'script id') assert event_listener_id not in event_listeners event_listeners[event_listener_id] = event_listener_script_id return edge def take_remove_event_listener_edge(event_type): assert len(relevant_edges) > 0 edge = relevant_edges.pop() assert pg_edge_data(edge, 'edge type') == 'remove event listener' assert pg_edge_data(edge, 'key') == event_type event_listener_id = pg_edge_data(edge, 'event listener id') event_listener_script_id = pg_edge_data(edge, 'script id') assert event_listener_id in event_listeners assert event_listeners[event_listener_id] == event_listener_script_id del event_listeners[event_listener_id] return edge # Check the "add event listener" edges from the first script node. # addEventListener('click', foo): edge = take_add_event_listener_edge('click') script_nodes.append(pg_edge_out(edge)) script_ids.append(page_graph.nodes[script_nodes[0]]['script id']) assert script_ids[0] == pg_edge_data(edge, 'script id') # A duplicate addEventListener('click', foo) call should be ignored here... # addEventListener('click', boundFoo): edge = take_add_event_listener_edge('click') assert script_nodes[0] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # addEventListener('click', boundBoundFoo): edge = take_add_event_listener_edge('click') assert script_nodes[0] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # addEventListener('click', bar): edge = take_add_event_listener_edge('click') assert script_nodes[0] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # addEventListener('click', baz): edge = take_add_event_listener_edge('click') assert script_nodes[0] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') assert len(event_listeners) == 5 # Check the "remove event listener" edges from the first script node. # removeEventListener('click', foo): edge = take_remove_event_listener_edge('click') assert script_nodes[0] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # A duplicate removeEventListener('click', foo) call should be ignored here... # removeEventListener('click', boundFoo): edge = take_remove_event_listener_edge('click') assert script_nodes[0] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # removeEventListener('click', boundBoundFoo): edge = take_remove_event_listener_edge('click') assert script_nodes[0] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # removeEventListener('click', bar): edge = take_remove_event_listener_edge('click') assert script_nodes[0] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # removeEventListener('click', baz): edge = take_remove_event_listener_edge('click') assert script_nodes[0] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') assert len(event_listeners) == 0 # Check the "add event listener" edges from the second script node. # addEventListener('click', foo): edge = take_add_event_listener_edge('click') script_nodes.append(pg_edge_out(edge)) assert script_nodes.count(script_nodes[1]) == 1 script_ids.append(page_graph.nodes[script_nodes[1]]['script id']) assert script_ids.count(script_ids[1]) == 1 assert script_ids[0] == pg_edge_data(edge, 'script id') # A duplicate addEventListener('click', foo) call should be ignored here... # addEventListener('click', boundFoo): edge = take_add_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # addEventListener('click', boundBoundFoo): edge = take_add_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # addEventListener('click', boundBoundBoundFoo): edge = take_add_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # addEventListener('click', bar): edge = take_add_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # addEventListener('click', baz): edge = take_add_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') assert len(event_listeners) == 6 # Check the "remove event listener" edges from the second script node. # removeEventListener('click', foo): edge = take_remove_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # A duplicate removeEventListener('click', foo) call should be ignored here... # removeEventListener('click', boundFoo): edge = take_remove_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # removeEventListener('click', boundBoundFoo): edge = take_remove_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # removeEventListener('click', boundBoundBoundFoo): edge = take_remove_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # removeEventListener('click', bar): edge = take_remove_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # removeEventListener('click', baz): edge = take_remove_event_listener_edge('click') assert script_nodes[1] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') assert len(event_listeners) == 0 # Check attribute event listener behavior for the third script node. # onclick = quux edge = take_add_event_listener_edge('click') script_nodes.append(pg_edge_out(edge)) assert script_nodes.count(script_nodes[2]) == 1 script_ids.append(page_graph.nodes[script_nodes[2]]['script id']) assert script_ids.count(script_ids[2]) == 1 assert script_ids[2] == pg_edge_data(edge, 'script id') # onclick = foo (removing quux) edge = take_remove_event_listener_edge('click') assert script_nodes[2] == pg_edge_out(edge) assert script_ids[2] == pg_edge_data(edge, 'script id') # onclick = foo (adding foo) edge = take_add_event_listener_edge('click') assert script_nodes[2] == pg_edge_out(edge) assert script_ids[0] == pg_edge_data(edge, 'script id') # Check attribute event listener behavior for the fourth script node. # onclick = quux (removing foo) edge = take_remove_event_listener_edge('click') script_nodes.append(pg_edge_out(edge)) assert script_nodes.count(script_nodes[3]) == 1 script_ids.append(page_graph.nodes[script_nodes[3]]['script id']) assert script_ids.count(script_ids[3]) == 1 assert script_ids[0] == pg_edge_data(edge, 'script id') # onclick = quux (adding quux) edge = take_add_event_listener_edge('click') assert script_nodes[3] == pg_edge_out(edge) assert script_ids[2] == pg_edge_data(edge, 'script id')