|
1 | 1 | from importlib import import_module |
| 2 | +import json |
2 | 3 | import time |
3 | 4 | import warnings |
4 | 5 |
|
@@ -233,7 +234,7 @@ def test_html_pages(client): |
233 | 234 | client.post('/api/v1/results', content=RESULT_CRASH) |
234 | 235 | client.post('/api/v1/info', content=INFO) |
235 | 236 | pages = ['/', '/crash.html', '/timeout.html', '/stale.html', '/diff.html', '/diff-knownConditionTrueFalse', |
236 | | - '/head.html', '/headinfo.html', '/head-syntaxError', '/headtoday-syntaxError', '/headinfo-checkLibraryFunction', |
| 237 | + '/head.html', '/headinfo.html', '/head-syntaxError', '/headtoday-syntaxError', '/headinfo-checkLibraryFunction', '/triage.html', |
237 | 238 | '/latest.html', '/time_lt.html', '/time_gt.html', '/time_slow.html', '/clients.html', |
238 | 239 | '/check_library_function_report.html', '/check_library_check_type_report.html', '/unknown_macro.html', |
239 | 240 | '/value_flow_bailout_incomplete_var.html'] |
@@ -295,3 +296,227 @@ def test_raw_files(client): |
295 | 296 | assert client.get('/info_output/aaa').status_code == 200 |
296 | 297 | assert client.get('/nonexistent').status_code == 404 |
297 | 298 | assert client.get('/..%2F..%2Fetc%2Fpasswd').status_code == 404 |
| 299 | + |
| 300 | + |
| 301 | +WARNING_OOB = "ccc/a.c:1:2: error: Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]" |
| 302 | +WARNING_MACRO = 'ccc/b.c:4:1: error: There is an unknown macro here somewhere. Configuration is required. ' \ |
| 303 | + 'If FOO is a macro then please configure it. [unknownMacro]' |
| 304 | +WARNING_OLD = server.OLD_VERSION + ' ccc/c.c:1:2: style: blah [knownConditionTrueFalse]' |
| 305 | + |
| 306 | + |
| 307 | +def triage(warning, verdict, url=PKG_C): |
| 308 | + return {'url': url, 'warning': warning, 'verdict': verdict} |
| 309 | + |
| 310 | + |
| 311 | +def triage_warnings(work_path): |
| 312 | + return [item['warning'] for item in json.loads((work_path / 'triage.json').read_text())] |
| 313 | + |
| 314 | + |
| 315 | +def upload_triage(client): |
| 316 | + r = client.post('/api/v1/triage', json=[ |
| 317 | + triage('head ' + WARNING_OOB, 'Unintentional ; True positive - not a False positive \u00e5'), |
| 318 | + triage(WARNING_MACRO, 'Intentional ; False positive - x'), |
| 319 | + triage(WARNING_OLD, 'False positive'), |
| 320 | + triage(WARNING_OOB + '\n' + WARNING_OLD, 'rephrazed')]) |
| 321 | + assert r.status_code == 200 |
| 322 | + assert r.json() == {'added': 4, 'updated': 0, 'unchanged': 0, 'skipped': 0} |
| 323 | + |
| 324 | + |
| 325 | +def test_triage_report(client): |
| 326 | + upload_triage(client) |
| 327 | + data = client.get('/api/v1/reports/triage').json() |
| 328 | + assert data['head']['message_ids'] == { |
| 329 | + 'arrayIndexOutOfBounds': {'tp': 1, 'fp': 0, 'other': 1, 'tp_ratio': 1.0, 'fp_ratio': 0.0}, |
| 330 | + 'unknownMacro': {'tp': 0, 'fp': 1, 'other': 0, 'tp_ratio': 0.0, 'fp_ratio': 1.0}, |
| 331 | + } |
| 332 | + assert data['head']['total'] == {'tp': 1, 'fp': 1, 'other': 1, 'tp_ratio': 0.5, 'fp_ratio': 0.5} |
| 333 | + assert list(data['old']['message_ids']) == ['knownConditionTrueFalse'] |
| 334 | + html = client.get('/triage.html').text |
| 335 | + assert '/head-arrayIndexOutOfBounds' in html and '/diff-knownConditionTrueFalse' in html and '50%' in html |
| 336 | + |
| 337 | + |
| 338 | +def test_triage_merge(client, work_path): |
| 339 | + upload_triage(client) |
| 340 | + # with and without 'head' is the same warning, and notes are ignored |
| 341 | + r = client.post('/api/v1/triage', json=[ |
| 342 | + triage(WARNING_OOB + '\nccc/a.c:1:1: note: array a [arrayIndexOutOfBounds]', 'False positive'), |
| 343 | + triage(WARNING_OLD, 'False positive'), |
| 344 | + triage('ccc/a.c:1:1: note: array a', 'True positive'), |
| 345 | + triage('no message id', 'True positive'), |
| 346 | + triage(WARNING_OOB, 'True positive', PKG_C.replace('ccc_1.0', 'ccc_9.0')), |
| 347 | + triage(WARNING_OOB, 'True positive', 'http://example.com/x.tar.gz')]) |
| 348 | + assert r.json() == {'added': 0, 'updated': 1, 'unchanged': 1, 'skipped': 4} |
| 349 | + assert len(triage_warnings(work_path)) == 4 |
| 350 | + assert client.get('/api/v1/reports/triage').json()['head']['total']['fp'] == 2 |
| 351 | + |
| 352 | + # the triage results are loaded after a restart |
| 353 | + with TestClient(server.create_app(str(work_path), rescan_interval=0.2)) as client2: |
| 354 | + assert client2.get('/api/v1/reports/triage').json()['head']['total']['fp'] == 2 |
| 355 | + |
| 356 | + |
| 357 | +def test_triage_upload_errors(client, work_path, monkeypatch): |
| 358 | + for data in ('', '{}', '[{"url": "x"}]', '[{"url": "x", "warning": 1, "verdict": "x"}]', '[1'): |
| 359 | + assert client.post('/api/v1/triage', content=data).status_code == 422 |
| 360 | + monkeypatch.setattr(server, 'MAX_TRIAGE_SIZE', 10) |
| 361 | + assert client.post('/api/v1/triage', json=[triage(WARNING_OOB, 'True positive')]).status_code == 413 |
| 362 | + assert not (work_path / 'triage.json').exists() |
| 363 | + |
| 364 | + |
| 365 | +def test_triage_broken_file(work_path): |
| 366 | + for text in ('[{', '[1]', '[{"url": "x"}]', '{"a": 1}'): |
| 367 | + (work_path / 'triage.json').write_text(text) |
| 368 | + with pytest.raises(RuntimeError): |
| 369 | + server.create_app(str(work_path)) |
| 370 | + assert (work_path / 'triage.json').read_text() == text |
| 371 | + |
| 372 | + |
| 373 | +def test_triage_removed_when_results_are_uploaded(client, work_path): |
| 374 | + upload_triage(client) |
| 375 | + results = RESULT_FILE_C.split('\n', 1)[1] |
| 376 | + |
| 377 | + # results for another package, or same results => nothing is removed |
| 378 | + assert client.post('/api/v1/results', content=RESULT_CRASH).status_code == 201 |
| 379 | + assert client.post('/api/v1/results', content=results).status_code == 201 |
| 380 | + assert len(triage_warnings(work_path)) == 4 |
| 381 | + |
| 382 | + # a missing warning says nothing if there was a crash or a timeout |
| 383 | + for count in ('Crash! 2', '3 TO!'): |
| 384 | + data = results.replace('count: 3 2', 'count: ' + count).replace(WARNING_OOB, '') |
| 385 | + assert client.post('/api/v1/results', content=data).status_code == 201 |
| 386 | + assert len(triage_warnings(work_path)) == 4 |
| 387 | + |
| 388 | + # the old warning is not in the diff anymore |
| 389 | + data = results.replace(WARNING_OLD + '\n', '') |
| 390 | + assert client.post('/api/v1/results', content=data).status_code == 201 |
| 391 | + assert triage_warnings(work_path) == [WARNING_OOB, WARNING_MACRO] |
| 392 | + |
| 393 | + data = data.replace(WARNING_OOB, WARNING_OOB.replace(':1:2:', ':2:2:')) |
| 394 | + assert client.post('/api/v1/results', content=data).status_code == 201 |
| 395 | + assert triage_warnings(work_path) == [WARNING_MACRO] |
| 396 | + assert client.get('/api/v1/reports/triage').json()['head']['total']['fp'] == 1 |
| 397 | + |
| 398 | + |
| 399 | +def test_triage_removed_when_old_server_stores_results(client, work_path): |
| 400 | + upload_triage(client) |
| 401 | + (work_path / 'donated-results' / 'ccc').write_text(RESULT_FILE_C.replace(WARNING_MACRO, '')) |
| 402 | + for _ in range(100): |
| 403 | + if len(triage_warnings(work_path)) == 3: |
| 404 | + break |
| 405 | + time.sleep(0.05) |
| 406 | + assert WARNING_MACRO not in triage_warnings(work_path) |
| 407 | + assert len(triage_warnings(work_path)) == 3 |
| 408 | + |
| 409 | + |
| 410 | +def test_triage_removed_when_package_url_changes(work_path): |
| 411 | + new_url = PKG_C.replace('ccc_1.0', 'ccc_1.1') |
| 412 | + with open(str(work_path / 'packages.txt'), 'at') as f: |
| 413 | + f.write(new_url + '\n') |
| 414 | + with TestClient(server.create_app(str(work_path), rescan_interval=0.2)) as client: |
| 415 | + upload_triage(client) |
| 416 | + results = RESULT_FILE_C.split('\n', 1)[1].replace(PKG_C, new_url) |
| 417 | + assert client.post('/api/v1/results', content=results).status_code == 201 |
| 418 | + assert triage_warnings(work_path) == [] |
| 419 | + |
| 420 | + |
| 421 | +def suggestions(client): |
| 422 | + r = client.get('/api/v1/triage/suggestions') |
| 423 | + assert r.status_code == 200 |
| 424 | + assert r.json()['complete'] is True |
| 425 | + return r.json()['warnings'] |
| 426 | + |
| 427 | + |
| 428 | +def result_file(head, diff, count='3 2', url=PKG_C): |
| 429 | + return '2026-11-01 10:00\n{}\ncppcheck: head {}\ncount: {}\nelapsed-time: 1.0 1.0\nhead results:\n{}\n\ndiff:\n{}\n'.format( |
| 430 | + url, server.OLD_VERSION, count, '\n'.join(head), '\n'.join(diff)) |
| 431 | + |
| 432 | + |
| 433 | +def style(line, message_id='id1'): |
| 434 | + return 'ccc/a.c:{}:1: style: x [{}]'.format(line, message_id) |
| 435 | + |
| 436 | + |
| 437 | +def test_triage_suggestions(client): |
| 438 | + assert suggestions(client) == [ |
| 439 | + {'message_id': 'arrayIndexOutOfBounds', 'section': 'diff', 'package': 'ccc', 'url': PKG_C, 'warning': 'head ' + WARNING_OOB}, |
| 440 | + {'message_id': 'knownConditionTrueFalse', 'section': 'diff', 'package': 'ccc', 'url': PKG_C, 'warning': WARNING_OLD}, |
| 441 | + {'message_id': 'unknownMacro', 'section': 'head', 'package': 'ccc', 'url': PKG_C, 'warning': WARNING_MACRO}, |
| 442 | + ] |
| 443 | + # the suggestions can be uploaded when there is a verdict; after that they are not suggested anymore |
| 444 | + items = [dict(w, verdict='True positive') for w in suggestions(client)] |
| 445 | + assert client.post('/api/v1/triage', json=items[:1]).json()['added'] == 1 |
| 446 | + assert [w['warning'] for w in suggestions(client)] == [WARNING_OLD, WARNING_MACRO] |
| 447 | + assert client.post('/api/v1/triage', json=items[1:]).json()['added'] == 2 |
| 448 | + assert suggestions(client) == [] |
| 449 | + |
| 450 | + |
| 451 | +def test_triage_suggestions_triaged(client): |
| 452 | + upload_triage(client) |
| 453 | + assert suggestions(client) == [] |
| 454 | + |
| 455 | + |
| 456 | +def test_triage_suggestions_selection(client, work_path, monkeypatch): |
| 457 | + head = [style(n) for n in range(1, 31)] + [style(n, 'id2') for n in range(1, 31)] + [style(7, 'id2')] |
| 458 | + head += ['ccc/a.c:1:1: debug: x [dbg]', 'ccc/a.c:9:1: note: x [id1]'] |
| 459 | + diff = ['head ' + style(n) for n in (1, 2)] + [server.OLD_VERSION + ' ' + style(99)] |
| 460 | + diff += ['head ' + style(n, 'id2') for n in range(1, 21)] |
| 461 | + client.post('/api/v1/results', content=result_file(head, diff).split('\n', 1)[1]) |
| 462 | + client.post('/api/v1/triage', json=[triage(style(2), 'False positive')]) |
| 463 | + |
| 464 | + first = suggestions(client) |
| 465 | + id1 = [w for w in first if w['message_id'] == 'id1'] |
| 466 | + id2 = [w for w in first if w['message_id'] == 'id2'] |
| 467 | + assert len(first) == 10 and len(id1) == 5 and len(id2) == 5 |
| 468 | + # all the diff warnings without triage result, and then head warnings. No duplicates. |
| 469 | + assert {w['warning'] for w in id1 if w['section'] == 'diff'} == {'head ' + style(1), server.OLD_VERSION + ' ' + style(99)} |
| 470 | + assert len({w['warning'] for w in id1 if w['section'] == 'head'} - {style(1), style(2)}) == 3 |
| 471 | + # enough warnings in the diff |
| 472 | + assert {w['section'] for w in id2} == {'diff'} |
| 473 | + assert len({w['warning'] for w in id2}) == 5 |
| 474 | + |
| 475 | + # same list the same day, new random list the next day |
| 476 | + assert suggestions(client) == first |
| 477 | + monkeypatch.setattr(server, 'today_str', lambda: '2100-01-01') |
| 478 | + lists = [suggestions(client)] |
| 479 | + assert suggestions(client) == lists[0] |
| 480 | + for day in range(2, 7): |
| 481 | + monkeypatch.setattr(server, 'today_str', lambda day=day: '2100-01-0{}'.format(day)) |
| 482 | + lists.append(suggestions(client)) |
| 483 | + assert any(x != first for x in lists) |
| 484 | + |
| 485 | + |
| 486 | +@pytest.mark.parametrize('old,new', [ |
| 487 | + ('count: 3 2', 'count: Crash! 2'), |
| 488 | + ('count: 3 2', 'count: 3 TO!'), |
| 489 | + ('cppcheck: head ' + server.OLD_VERSION, 'cppcheck: head 1.0'), |
| 490 | + ('ccc_1.0', 'ccc_9.0')]) |
| 491 | +def test_triage_suggestions_ignored_results(work_path, old, new): |
| 492 | + # incomplete results, results for another old version, unknown package url |
| 493 | + assert old in RESULT_FILE_C |
| 494 | + (work_path / 'donated-results' / 'ccc').write_text(RESULT_FILE_C.replace(old, new)) |
| 495 | + with TestClient(server.create_app(str(work_path), rescan_interval=0.2)) as client: |
| 496 | + for _ in range(100): |
| 497 | + if client.get('/api/v1/status').json()['index_ready']: |
| 498 | + break |
| 499 | + time.sleep(0.05) |
| 500 | + assert client.get('/api/v1/status').json()['results'] == 1 |
| 501 | + assert suggestions(client) == [] |
| 502 | + |
| 503 | + |
| 504 | +def test_triage_suggestions_index_not_ready(work_path): |
| 505 | + # without lifespan the index is not built => incomplete and not reused |
| 506 | + client = TestClient(server.create_app(str(work_path))) |
| 507 | + data = client.get('/api/v1/triage/suggestions').json() |
| 508 | + assert data['complete'] is False and data['warnings'] == [] |
| 509 | + with client: |
| 510 | + for _ in range(100): |
| 511 | + if client.get('/api/v1/status').json()['index_ready']: |
| 512 | + break |
| 513 | + time.sleep(0.05) |
| 514 | + assert len(suggestions(client)) == 3 |
| 515 | + |
| 516 | + |
| 517 | +def test_random_order(): |
| 518 | + rng = server.random.Random(1) |
| 519 | + assert list(server.random_order(0, rng)) == [] |
| 520 | + orders = {tuple(server.random_order(4, rng)) for _ in range(1000)} |
| 521 | + assert len(orders) == 24 |
| 522 | + assert all(sorted(order) == [0, 1, 2, 3] for order in orders) |
0 commit comments