def test_collector_refresh(tmpdir, monkeypatch, capsys): '''Test signature downloads''' # create a test signature zip test2 = tmpdir.join('test2.signature').strpath with open(test2, 'w') as fp: fp.write('test2') with zipfile.ZipFile(tmpdir.join('out.zip').strpath, 'w') as zf: zf.write(test2, 'test2.signature') # create an old signature tmpdir.mkdir('sigs') with open(tmpdir.join('sigs', 'test1.signature').strpath, 'w'): pass with open(tmpdir.join('sigs', 'other.txt').strpath, 'w'): pass assert {f.basename for f in tmpdir.join('sigs').listdir()} == {'test1.signature', 'other.txt'} with open(tmpdir.join('out.zip').strpath, 'rb') as fp: class response_t(object): status_code = requests.codes["ok"] text = "OK" raw = fp # this asserts the expected arguments and returns the open handle to out.zip as 'raw' which is read by refresh() def myget(_session, url, stream=None, headers=None): assert url == 'gopher://aol.com:70/crashmanager/rest/signatures/download/' assert stream is True assert headers == {'Authorization': 'Token token'} return response_t() monkeypatch.setattr(requests.Session, 'get', myget) # create Collector collector = Collector(sigCacheDir=tmpdir.join('sigs').strpath, serverHost='aol.com', serverPort=70, serverProtocol='gopher', serverAuthToken='token', clientId='test-fuzzer1', tool='test-tool') # call refresh collector.refresh() # check that it worked assert {f.basename for f in tmpdir.join('sigs').listdir()} == {'test2.signature', 'other.txt'} with open(tmpdir.join('sigs', 'test2.signature').strpath) as fp: assert fp.read() == 'test2' assert 'other.txt' in capsys.readouterr()[1] # should have had a warning about unrecognized file # check that 404 raises monkeypatch.undo() class response_t(object): # noqa status_code = requests.codes["not found"] text = "Not found" def myget(_session, _url, stream=None, headers=None): return response_t() monkeypatch.setattr(requests.Session, 'get', myget) with pytest.raises(RuntimeError, match='Server unexpectedly responded'): collector.refresh() # check that bad zips raise errors monkeypatch.undo() with open(tmpdir.join('sigs', 'other.txt').strpath, 'rb') as fp: class response_t(object): # noqa status_code = requests.codes["ok"] text = "OK" raw = fp def myget(_session, _url, stream=None, headers=None): return response_t() monkeypatch.setattr(requests.Session, 'get', myget) with pytest.raises(zipfile.BadZipfile, match='not a zip file'): collector.refresh() monkeypatch.undo() with open(tmpdir.join('out.zip').strpath, 'r+b') as fp: # corrupt the CRC field for the signature file in the zip fp.seek(0x42) fp.write(b'\xFF') with open(tmpdir.join('out.zip').strpath, 'rb') as fp: class response_t(object): # noqa status_code = requests.codes["ok"] text = "OK" raw = fp def myget(_session, _url, stream=None, headers=None): return response_t() monkeypatch.setattr(requests.Session, 'get', myget) with pytest.raises(RuntimeError, match='Bad CRC'): collector.refresh()
def test_collector_refresh(tmpdir, monkeypatch, capsys): '''Test signature downloads''' # create a test signature zip test2 = tmpdir.join('test2.signature').strpath with open(test2, 'w') as fp: fp.write('test2') with zipfile.ZipFile(tmpdir.join('out.zip').strpath, 'w') as zf: zf.write(test2, 'test2.signature') # create an old signature tmpdir.mkdir('sigs') with open(tmpdir.join('sigs', 'test1.signature').strpath, 'w'): pass with open(tmpdir.join('sigs', 'other.txt').strpath, 'w'): pass assert {f.basename for f in tmpdir.join('sigs').listdir() } == {'test1.signature', 'other.txt'} with open(tmpdir.join('out.zip').strpath, 'rb') as fp: class response_t(object): status_code = requests.codes["ok"] text = "OK" raw = fp # this asserts the expected arguments and returns the open handle to out.zip as 'raw' which is read by refresh() def myget(_session, url, stream=None, auth=None): assert url == 'gopher://aol.com:70/crashmanager/files/signatures.zip' assert stream is True assert len(auth) == 2 assert auth[0] == 'fuzzmanager' assert auth[1] == 'token' return response_t() monkeypatch.setattr(requests.Session, 'get', myget) # create Collector collector = Collector(sigCacheDir=tmpdir.join('sigs').strpath, serverHost='aol.com', serverPort=70, serverProtocol='gopher', serverAuthToken='token', clientId='test-fuzzer1', tool='test-tool') # call refresh collector.refresh() # check that it worked assert {f.basename for f in tmpdir.join('sigs').listdir() } == {'test2.signature', 'other.txt'} with open(tmpdir.join('sigs', 'test2.signature').strpath) as fp: assert fp.read() == 'test2' assert 'other.txt' in capsys.readouterr()[ 1] # should have had a warning about unrecognized file # check that 404 raises monkeypatch.undo() class response_t(object): # noqa status_code = requests.codes["not found"] text = "Not found" def myget(_session, _url, stream=None, auth=None): return response_t() monkeypatch.setattr(requests.Session, 'get', myget) with pytest.raises(RuntimeError, match='Server unexpectedly responded'): collector.refresh() # check that bad zips raise errors monkeypatch.undo() with open(tmpdir.join('sigs', 'other.txt').strpath, 'rb') as fp: class response_t(object): # noqa status_code = requests.codes["ok"] text = "OK" raw = fp def myget(_session, _url, stream=None, auth=None): return response_t() monkeypatch.setattr(requests.Session, 'get', myget) with pytest.raises(zipfile.BadZipfile, match='not a zip file'): collector.refresh() monkeypatch.undo() with open(tmpdir.join('out.zip').strpath, 'r+b') as fp: # corrupt the CRC field for the signature file in the zip fp.seek(0x42) fp.write(b'\xFF') with open(tmpdir.join('out.zip').strpath, 'rb') as fp: class response_t(object): # noqa status_code = requests.codes["ok"] text = "OK" raw = fp def myget(_session, _url, stream=None, auth=None): return response_t() monkeypatch.setattr(requests.Session, 'get', myget) with pytest.raises(RuntimeError, match='Bad CRC'): collector.refresh()
def test_collector_refresh(capsys, tmp_path): '''Test signature downloads''' # create a test signature zip test2_path = tmp_path / 'test2.signature' with test2_path.open('w') as fp: fp.write('test2') outzip_path = tmp_path / "out.zip" with zipfile.ZipFile(str(outzip_path), 'w') as zf: zf.write(str(test2_path), 'test2.signature') # create an old signature sigs_path = tmp_path / 'sigs' sigs_path.mkdir() (sigs_path / 'test1.signature').touch() (sigs_path / 'other.txt').touch() assert {f.name for f in sigs_path.iterdir()} == {'test1.signature', 'other.txt'} with outzip_path.open('rb') as fp: class response_t(object): status_code = requests.codes["ok"] text = "OK" raw = fp # this asserts the expected arguments and returns the open handle to out.zip as 'raw' which is read by refresh() def myget(url, stream=None, headers=None): assert url == 'gopher://aol.com:70/crashmanager/rest/signatures/download/' assert stream is True assert headers == {'Authorization': 'Token token'} return response_t() # create Collector collector = Collector(sigCacheDir=str(sigs_path), serverHost='aol.com', serverPort=70, serverProtocol='gopher', serverAuthToken='token', clientId='test-fuzzer1', tool='test-tool') collector._session.get = myget # call refresh collector.refresh() # check that it worked assert {f.name for f in sigs_path.iterdir()} == {'test2.signature', 'other.txt'} assert (sigs_path / 'test2.signature').read_text() == 'test2' assert 'other.txt' in capsys.readouterr()[ 1] # should have had a warning about unrecognized file # check that 404 raises class response_t(object): # noqa status_code = requests.codes["not found"] text = "Not found" collector._session.get = lambda *_, **__: response_t() with pytest.raises(RuntimeError, match='Server unexpectedly responded'): collector.refresh() # check that bad zips raise errors with (sigs_path / 'other.txt').open('rb') as fp: class response_t(object): # noqa status_code = requests.codes["ok"] text = "OK" raw = fp collector._session.get = lambda *_, **__: response_t() with pytest.raises(zipfile.BadZipfile, match='not a zip file'): collector.refresh() with outzip_path.open('r+b') as fp: # corrupt the CRC field for the signature file in the zip fp.seek(0x42) fp.write(b'\xFF') with outzip_path.open('rb') as fp: class response_t(object): # noqa status_code = requests.codes["ok"] text = "OK" raw = fp collector._session.get = lambda *_, **__: response_t() with pytest.raises(RuntimeError, match='Bad CRC'): collector.refresh()