Skip to content

Commit 3c5f3c7

Browse files
authored
webapp: Fix tests xref API to support xref details query (#2210)
* webapp: Fix tests xref API to support xref details query Signed-off-by: Arthur Chan <[email protected]> * Fix formatting Signed-off-by: Arthur Chan <[email protected]> * Fix minor logic Signed-off-by: Arthur Chan <[email protected]> * Fix formatting Signed-off-by: Arthur Chan <[email protected]> --------- Signed-off-by: Arthur Chan <[email protected]>
1 parent 55b8cf4 commit 3c5f3c7

File tree

1 file changed

+54
-14
lines changed
  • tools/web-fuzzing-introspection/app/webapp

1 file changed

+54
-14
lines changed

tools/web-fuzzing-introspection/app/webapp/routes.py

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import json
2020
import signal
2121
import logging
22-
from typing import Any, Dict, List, Optional
22+
from typing import Any, Dict, List, Optional, Set
2323
import requests
2424

2525
from flask import render_template, request, redirect
@@ -2778,31 +2778,59 @@ def extract_project_tests(project_name,
27782778
return tests_file_list
27792779

27802780

2781-
def extract_project_tests_xref(project_name: str,
2782-
funcs: List[str]) -> Dict[str, List[str]]:
2783-
"""Extracts test files that invoke the target functions or all functions
2784-
if target functions are not provided."""
2785-
result: Dict[str, List[str]] = {}
2781+
def _load_project_tests_xref(
2782+
project_name: str) -> Dict[str, List[Dict[str, Any]]]:
2783+
"""Helper to extract the cross reference from test files."""
2784+
# Check existing test_files_xref.json
27862785
test_files: Dict[str, List[Dict[str, Any]]] = {}
27872786

2788-
# Check existing test_files_xref.json
27892787
test_json = os.path.join(
27902788
os.path.dirname(__file__),
27912789
f"../static/assets/db/db-projects/{project_name}/test_files_xref.json")
27922790
if not os.path.isfile(test_json):
2793-
return result
2791+
return test_files
27942792

27952793
with open(test_json, 'r') as f:
27962794
test_files = json.load(f)
27972795

2798-
if not test_files:
2799-
return result
2796+
return test_files
2797+
2798+
2799+
def extract_project_tests_xref(project_name: str,
2800+
funcs: List[str]) -> Dict[str, List[str]]:
2801+
"""Extracts test files that invoke the target functions or all functions
2802+
if target functions are not provided."""
2803+
result: Dict[str, Set[str]] = {}
2804+
2805+
test_files = _load_project_tests_xref(project_name)
2806+
for file, reach_list in test_files.items():
2807+
for target in reach_list:
2808+
func_name = target['function_name'].split('::')[-1]
2809+
if not funcs or func_name in funcs:
2810+
result.setdefault(func_name, set()).add(file)
2811+
2812+
return {k: list(v) for k, v in result.items()}
2813+
2814+
2815+
def extract_project_tests_xref_details(
2816+
project_name: str,
2817+
funcs: List[str]) -> Dict[str, Dict[str, List[Dict[str, Any]]]]:
2818+
"""Extracts test files that invoke the target functions or all functions
2819+
if target functions are not provided. Detail lines of the function called
2820+
in each test file is also included."""
2821+
result: Dict[str, Dict[str, List[Dict[str, Any]]]] = {}
28002822

2823+
test_files = _load_project_tests_xref(project_name)
28012824
for file, reach_list in test_files.items():
28022825
for target in reach_list:
2826+
if 'params' not in target:
2827+
# Old version of test xrefs
2828+
return {}
2829+
28032830
func_name = target['function_name'].split('::')[-1]
2804-
if not funcs or any(func == func_name for func in funcs):
2805-
result.setdefault(func_name, []).append(file)
2831+
if not funcs or func_name in funcs:
2832+
result.setdefault(func_name, {}).setdefault(file,
2833+
[]).append(target)
28062834

28072835
return result
28082836

@@ -2988,7 +3016,15 @@ def project_tests_xref(args):
29883016
# Get target functions if provided
29893017
funcs = args.get('functions', '').split(',')
29903018
funcs = [func for func in funcs if func]
2991-
test_files = extract_project_tests_xref(project, funcs)
3019+
3020+
# Determine if details are needed and extract test xref dict
3021+
test_files: Dict[str, Any] = {}
3022+
details = True
3023+
test_files = extract_project_tests_xref_details(project, funcs)
3024+
if not test_files:
3025+
details = False
3026+
test_files = extract_project_tests_xref(project, funcs)
3027+
29923028
if not test_files:
29933029
return {
29943030
'result':
@@ -2997,7 +3033,11 @@ def project_tests_xref(args):
29973033
[f'Could not find test files matching the requirements']
29983034
}
29993035

3000-
return {'result': 'success', 'test-files-xref': test_files}
3036+
return {
3037+
'result': 'success',
3038+
'test-files-xref': test_files,
3039+
'details': details
3040+
}
30013041

30023042

30033043
@api_blueprint.route('/api/addr-to-recursive-dwarf-info')

0 commit comments

Comments
 (0)