|
| 1 | +from importlib import import_module |
| 2 | +import time |
| 3 | +import warnings |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +# the tests need the requirements of the server (donate-cpu-server-requirements.txt) and httpx |
| 8 | +pytest.importorskip('fastapi') |
| 9 | +pytest.importorskip('jinja2') |
| 10 | +pytest.importorskip('uvicorn') |
| 11 | +pytest.importorskip('httpx') |
| 12 | + |
| 13 | +with warnings.catch_warnings(): |
| 14 | + # newer Starlette versions warn that 'httpx2' should be used instead of 'httpx' |
| 15 | + warnings.filterwarnings('ignore', message='.*httpx2') |
| 16 | + from fastapi.testclient import TestClient # pylint: disable=wrong-import-position |
| 17 | + |
| 18 | +server = import_module('donate-cpu-server-rest') |
| 19 | + |
| 20 | +PKG_A = 'ftp://ftp.de.debian.org/debian/pool/main/a/aaa/aaa_1.0.orig.tar.gz' |
| 21 | +PKG_B = 'ftp://ftp.de.debian.org/debian/pool/main/b/bbb/bbb_1.0.orig.tar.gz' |
| 22 | +PKG_C = 'ftp://ftp.de.debian.org/debian/pool/main/c/ccc/ccc_1.0.orig.tar.xz' |
| 23 | + |
| 24 | +RESULT_CRASH = '''{url} |
| 25 | +cppcheck-options: --library=posix |
| 26 | +platform: Linux-7 |
| 27 | +python: 3.14.4 |
| 28 | +client-version: 1.3.67 |
| 29 | +compiler: g++ 15 |
| 30 | +cppcheck: head {old} |
| 31 | +head-info: abc |
| 32 | +count: Crash! 5 |
| 33 | +elapsed-time: -1.0 3.0 |
| 34 | +head results: |
| 35 | +Program received signal SIGSEGV, Segmentation fault. |
| 36 | +0x000055 in Foo::bar (this=0x0) at lib/foo.cpp:12 |
| 37 | +12 return x->y; |
| 38 | +#0 0x000055 in Foo::bar (this=0x0) at lib/foo.cpp:12 |
| 39 | +#1 0x000066 in main (argc=1) at cli/main.cpp:3 |
| 40 | +
|
| 41 | +aaa/x.c:1:1: error: <script>alert(1)</script> [syntaxError] |
| 42 | +diff: |
| 43 | +'''.format(url=PKG_A, old=server.OLD_VERSION) |
| 44 | + |
| 45 | +INFO = '''{url} |
| 46 | +cppcheck: head {old} |
| 47 | +info messages: |
| 48 | +aaa/x.c:1:1: information: --check-library: There is no matching configuration for function foo() [checkLibraryFunction] |
| 49 | +aaa/x.c:2:1: information: --check-library: There is no matching configuration for function foo() [checkLibraryFunction] |
| 50 | +aaa/x.c:3:1: information: --check-library: Function bar() should have <noreturn> configuration [checkLibraryNoReturn] |
| 51 | +aaa/x.c:4:1: information: --check-library: There is no matching configuration for unsigned int [checkLibraryCheckType] |
| 52 | +'''.format(url=PKG_A, old=server.OLD_VERSION) |
| 53 | + |
| 54 | +# result file as written by donate-cpu-server.py |
| 55 | +RESULT_FILE_C = '''2026-11-01 10:00 |
| 56 | +{url} |
| 57 | +cppcheck: head {old} |
| 58 | +count: 3 2 |
| 59 | +elapsed-time: 10.0 2.0 |
| 60 | +head results: |
| 61 | +ccc/a.c:1:2: error: Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds] |
| 62 | +ccc/b.c:4:1: error: There is an unknown macro here somewhere. Configuration is required. If FOO is a macro then please configure it. [unknownMacro] |
| 63 | +
|
| 64 | +diff: |
| 65 | +head ccc/a.c:1:2: error: Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds] |
| 66 | +{old} ccc/c.c:1:2: style: blah [knownConditionTrueFalse] |
| 67 | +'''.format(url=PKG_C, old=server.OLD_VERSION) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture(name='work_path') |
| 71 | +def fixture_work_path(tmp_path): |
| 72 | + (tmp_path / 'donated-results').mkdir() |
| 73 | + (tmp_path / 'packages.txt').write_text('\n'.join([PKG_A, PKG_B, PKG_C]) + '\n') |
| 74 | + (tmp_path / 'donated-results' / 'ccc').write_text(RESULT_FILE_C) |
| 75 | + return tmp_path |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture(name='client') |
| 79 | +def fixture_client(work_path): |
| 80 | + app = server.create_app(str(work_path), rescan_interval=0.2) |
| 81 | + with TestClient(app) as client: |
| 82 | + for _ in range(100): |
| 83 | + if client.get('/api/v1/status').json()['index_ready']: |
| 84 | + break |
| 85 | + time.sleep(0.05) |
| 86 | + yield client |
| 87 | + |
| 88 | + |
| 89 | +def test_parse_result(): |
| 90 | + entry = server.parse_result(RESULT_FILE_C) |
| 91 | + assert entry['date'] == '2026-11-01 10:00' |
| 92 | + assert entry['url'] == PKG_C |
| 93 | + assert entry['versions'] == ['head', server.OLD_VERSION] |
| 94 | + assert entry['count'] == ['3', '2'] |
| 95 | + assert entry['elapsed'] == [10.0, 2.0] |
| 96 | + assert entry['head_ids'] == {'arrayIndexOutOfBounds': 1, 'unknownMacro': 1} |
| 97 | + assert entry['diff'] == {'arrayIndexOutOfBounds': [0, 1], 'knownConditionTrueFalse': [1, 0]} |
| 98 | + assert entry['names'] == {'unknownMacro': {'FOO': 1}} |
| 99 | + assert entry['crash'] is None |
| 100 | + |
| 101 | + |
| 102 | +def test_parse_crash(): |
| 103 | + entry = server.parse_result('2026-11-27 10:00\n' + RESULT_CRASH) |
| 104 | + assert entry['crash'] == { |
| 105 | + 'crash_line': '0x000055 in Foo::bar (this=0x0) at lib/foo.cpp:12', |
| 106 | + 'code_line': '12\t return x->y;', |
| 107 | + 'stack_trace': ['#0 Foo::bar(...) at lib/foo.cpp:12', '#1 main(...) at cli/main.cpp:3'], |
| 108 | + } |
| 109 | + assert entry['head_ids'] == {'syntaxError': 1} |
| 110 | + |
| 111 | + |
| 112 | +def test_parse_info(): |
| 113 | + entry = server.parse_info('2026-11-27 10:00\n' + INFO) |
| 114 | + assert entry['info_ids'] == {'checkLibraryFunction': 2, 'checkLibraryNoReturn': 1, 'checkLibraryCheckType': 1} |
| 115 | + assert entry['names'] == { |
| 116 | + 'checkLibraryFunction': {'foo()': 2}, |
| 117 | + 'checkLibraryNoReturn': {'bar()': 1}, |
| 118 | + 'checkLibraryCheckType': {'unsigned int': 1}, |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +def test_existing_results_are_indexed(client): |
| 123 | + assert client.get('/api/v1/status').json()['results'] == 1 |
| 124 | + data = client.get('/api/v1/reports/time').json() |
| 125 | + assert [p['package'] for p in data['packages']] == ['ccc'] |
| 126 | + assert data['packages'][0]['factor'] == 5.0 |
| 127 | + |
| 128 | + |
| 129 | +def test_packages(client): |
| 130 | + assert client.get('/api/v1/versions').json() == {'versions': ['head', server.OLD_VERSION]} |
| 131 | + assert client.get('/api/v1/packages').json() == {'count': 3} |
| 132 | + assert client.post('/api/v1/packages/next').json() == {'index': 0, 'url': PKG_A} |
| 133 | + assert client.get('/api/v1/packages/2').json() == {'index': 2, 'url': PKG_C} |
| 134 | + assert client.get('/api/v1/packages/9').status_code == 404 |
| 135 | + assert client.post('/api/v1/nodata', json={'url': PKG_B}).status_code == 200 |
| 136 | + # PKG_B is not handed out any more |
| 137 | + assert [client.post('/api/v1/packages/next').json()['index'] for _ in range(3)] == [2, 0, 2] |
| 138 | + |
| 139 | + |
| 140 | +def test_upload(client): |
| 141 | + r = client.post('/api/v1/results', content=RESULT_CRASH + '\nDONE') |
| 142 | + assert r.status_code == 201 and r.json()['package'] == 'aaa' |
| 143 | + assert client.post('/api/v1/info', content=INFO).status_code == 201 |
| 144 | + |
| 145 | + crash = client.get('/api/v1/reports/crash').json() |
| 146 | + assert [p['package'] for p in crash['packages']] == ['aaa'] |
| 147 | + assert crash['stack_traces'][0]['packages'] == ['aaa'] |
| 148 | + assert client.get('/api/v1/reports/crash?packages_only=true').text == PKG_A + '\n' |
| 149 | + |
| 150 | + names = client.get('/api/v1/reports/names/checkLibraryFunction').json() |
| 151 | + assert names['names'] == [{'name': 'foo()', 'count': 2}] |
| 152 | + |
| 153 | + |
| 154 | +def test_upload_errors(client, work_path): |
| 155 | + wrong_version = RESULT_CRASH.replace('head ' + server.OLD_VERSION, 'head 1.0') |
| 156 | + assert client.post('/api/v1/results', content=wrong_version).status_code == 422 |
| 157 | + assert client.post('/api/v1/results', content='ftp://evil/x/../../y.tar.gz\nfoo').status_code == 400 |
| 158 | + unknown = RESULT_CRASH.replace('/aaa/aaa_1.0', '/zzz/zzz_1.0') |
| 159 | + assert client.post('/api/v1/results', content=unknown).status_code == 404 |
| 160 | + |
| 161 | + # too large data removes the existing result |
| 162 | + big = PKG_C + '\ncppcheck: head ' + server.OLD_VERSION + '\n' + 'x' * server.MAX_RESULT_SIZE |
| 163 | + assert client.post('/api/v1/results', content=big).status_code == 413 |
| 164 | + assert not (work_path / 'donated-results' / 'ccc').exists() |
| 165 | + |
| 166 | + |
| 167 | +def test_files_written_by_old_server_are_picked_up(client, work_path): |
| 168 | + (work_path / 'donated-results' / 'aaa').write_text('2026-11-27 10:00\n' + RESULT_CRASH) |
| 169 | + for _ in range(100): |
| 170 | + if client.get('/api/v1/status').json()['results'] == 2: |
| 171 | + break |
| 172 | + time.sleep(0.05) |
| 173 | + assert client.get('/api/v1/status').json()['results'] == 2 |
| 174 | + |
| 175 | + |
| 176 | +def test_html_pages(client): |
| 177 | + client.post('/api/v1/results', content=RESULT_CRASH) |
| 178 | + client.post('/api/v1/info', content=INFO) |
| 179 | + pages = ['/', '/crash.html', '/timeout.html', '/stale.html', '/diff.html', '/diff-knownConditionTrueFalse', |
| 180 | + '/head.html', '/headinfo.html', '/head-syntaxError', '/headtoday-syntaxError', '/headinfo-checkLibraryFunction', |
| 181 | + '/latest.html', '/time_lt.html', '/time_gt.html', '/time_slow.html', '/clients.html', |
| 182 | + '/check_library_function_report.html', '/check_library_check_type_report.html', '/unknown_macro.html', |
| 183 | + '/value_flow_bailout_incomplete_var.html'] |
| 184 | + for page in pages: |
| 185 | + r = client.get(page) |
| 186 | + assert r.status_code == 200, page |
| 187 | + assert r.headers['content-type'].startswith('text/html'), page |
| 188 | + assert '<script>' not in r.text, page |
| 189 | + |
| 190 | + assert '<script>' in client.get('/head-syntaxError').text |
| 191 | + assert client.get('/head-syntaxError?pkgs=1').text == PKG_A + '\n' |
| 192 | + assert 'unknownMacro' in client.get('/head.html').text |
| 193 | + |
| 194 | + |
| 195 | +def test_html_name_pages(client): |
| 196 | + client.post('/api/v1/info', content=INFO) |
| 197 | + assert 'Function bar() should have' in client.get('/check_library-bar%28%29').text |
| 198 | + # '+' is a space, as in the links of donate-cpu-server.py |
| 199 | + assert 'configuration for unsigned int' in client.get('/check_library-unsigned+int').text |
| 200 | + assert 'please configure it' in client.get('/unknown_macro-FOO').text |
| 201 | + |
| 202 | + |
| 203 | +def test_raw_files(client): |
| 204 | + client.post('/api/v1/info', content=INFO) |
| 205 | + r = client.get('/ccc') |
| 206 | + assert r.status_code == 200 and r.text == RESULT_FILE_C |
| 207 | + assert client.get('/info_output/aaa').status_code == 200 |
| 208 | + assert client.get('/nonexistent').status_code == 404 |
| 209 | + assert client.get('/..%2F..%2Fetc%2Fpasswd').status_code == 404 |
0 commit comments