def fetcher(self, file_cache: AgentFileCache) -> TCPFetcher: return TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 6556), host_name=HostName("irrelevant_for_this_test"), timeout=0.1, encryption_settings={"use_regular": "allow"}, )
def fetcher(self, file_cache): return TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 6556), timeout=0.1, encryption_settings={"encryption": "settings"}, use_only_cache=False, )
def fetcher(self, monkeypatch, file_cache): # We use the TCPFetcher to test a general feature of the fetchers. return TCPFetcher( StubFileCache.from_json(file_cache.to_json()), family=socket.AF_INET, address=("1.2.3.4", 0), timeout=0.0, encryption_settings={}, use_only_cache=False, )
def test_validate_protocol_tls_allways_ok(self, file_cache: DefaultAgentFileCache) -> None: for setting in ("tls", "enforce", "enable", "disable"): TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), host_name=HostName("irrelevant_for_this_test"), timeout=0.0, encryption_settings={"use_regular": setting}, use_only_cache=False, )._validate_protocol(TransportProtocol.TLS)
def fetcher(self, monkeypatch: MonkeyPatch, file_cache: DefaultAgentFileCache) -> TCPFetcher: # We use the TCPFetcher to test a general feature of the fetchers. return TCPFetcher( StubFileCache.from_json(file_cache.to_json()), family=socket.AF_INET, address=("1.2.3.4", 0), timeout=0.0, host_name=HostName("irrelevant_for_this_test"), encryption_settings={}, use_only_cache=False, )
def test_decrypt_plaintext_is_noop(self, file_cache): settings = {"use_regular": "allow"} output = AgentRawData(b"<<<section:sep(0)>>>\nbody\n") fetcher = TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), timeout=0.0, encryption_settings=settings, use_only_cache=False, ) assert fetcher._decrypt(output) == output
def test_decrypt_plaintext_is_noop(self, file_cache: DefaultAgentFileCache) -> None: settings = {"use_regular": "allow"} output = b"<<<section:sep(0)>>>\nbody\n" fetcher = TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), host_name=HostName("irrelevant_for_this_test"), timeout=0.0, encryption_settings=settings, use_only_cache=False, ) assert fetcher._decrypt(TransportProtocol(output[:2]), AgentRawData(output[2:])) == output
def test_validate_protocol_encryption_with_disabled_raises( self, file_cache: DefaultAgentFileCache) -> None: settings = {"use_regular": "disable"} fetcher = TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), host_name=HostName("irrelevant_for_this_test"), timeout=0.0, encryption_settings=settings, use_only_cache=False, ) with pytest.raises(MKFetcherError): fetcher._validate_protocol(TransportProtocol.PBKDF2)
def test_decrypt_payload_with_wrong_protocol_raises_MKFetcherError(self, file_cache): settings = {"use_regular": "enforce"} output = AgentRawData(b"the first two bytes are not a number") fetcher = TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), timeout=0.0, encryption_settings=settings, use_only_cache=False, ) with pytest.raises(MKFetcherError): fetcher._decrypt(output)
def test_open_exception_becomes_fetcher_rerror( self, file_cache: AgentFileCache, monkeypatch: MonkeyPatch) -> None: with TCPFetcher( file_cache, family=socket.AF_INET, address=("This is not an IP address. Connecting fails.", 6556), host_name=HostName("irrelevant_for_this_test"), timeout=0.1, encryption_settings={"use_regular": "allow"}, ) as fetcher: fetched = fetcher.fetch(Mode.CHECKING) assert isinstance(fetched.error, MKFetcherError)
def test_decrypt_plaintext_with_enforce_raises_MKFetcherError(self, file_cache): settings = {"use_regular": "enforce"} output = AgentRawData(b"<<<section:sep(0)>>>\nbody\n") fetcher = TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), timeout=0.0, encryption_settings=settings, use_only_cache=False, ) with pytest.raises(MKFetcherError): fetcher._decrypt(output)
def test_with_cached_does_not_open(self, file_cache: AgentFileCache, monkeypatch: MonkeyPatch) -> None: file_cache.write(AgentRawData(b"<<<whatever>>>"), Mode.CHECKING) with TCPFetcher( file_cache, family=socket.AF_INET, address=("This is not an IP address. Connecting would fail.", 6556), host_name=HostName("irrelevant_for_this_test"), timeout=0.1, encryption_settings={"use_regular": "allow"}, ) as fetcher: fetched = fetcher.fetch(Mode.CHECKING) assert fetched.is_ok()
def test_validate_protocol_plaintext_with_enforce_raises( self, file_cache: AgentFileCache) -> None: settings = {"use_regular": "enforce"} fetcher = TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), host_name=HostName("irrelevant_for_this_test"), timeout=0.0, encryption_settings=settings, ) with pytest.raises(MKFetcherError): fetcher._validate_protocol(TransportProtocol.PLAIN, is_registered=False)
def test_validate_protocol_no_tls_with_registered_host_raises( self, file_cache: AgentFileCache) -> None: fetcher = TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), host_name=HostName("irrelevant_for_this_test"), timeout=0.0, encryption_settings={}, ) for p in TransportProtocol: if p is TransportProtocol.TLS: continue with pytest.raises(MKFetcherError): fetcher._validate_protocol(p, is_registered=True)
def test_validate_protocol_tls_required(self, file_cache: DefaultAgentFileCache) -> None: settings = {"use_regular": "tls"} fetcher = TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), host_name=HostName("irrelevant_for_this_test"), timeout=0.0, encryption_settings=settings, use_only_cache=False, ) for p in TransportProtocol: if p is TransportProtocol.TLS: continue with pytest.raises(MKFetcherError, match="TLS"): fetcher._validate_protocol(p)
def test_validate_protocol_tls_always_ok( self, file_cache: AgentFileCache) -> None: for setting, is_registered in cartesian_product( ("tls", "enforce", "enable", "disable"), (True, False), ): TCPFetcher( file_cache, family=socket.AF_INET, address=("1.2.3.4", 0), host_name=HostName("irrelevant_for_this_test"), timeout=0.0, encryption_settings={ "use_regular": setting }, )._validate_protocol(TransportProtocol.TLS, is_registered=is_registered)
def test_fetching_without_cache_raises_in_non_checking_mode(self) -> None: with TCPFetcher( StubFileCache( HostName("hostname"), base_path=Path(os.devnull), max_age=MaxAge.none(), disabled=False, use_outdated=True, simulation=False, use_only_cache=False, ), family=socket.AF_INET, address=("127.0.0.1", 6556), host_name=HostName("irrelevant_for_this_test"), timeout=0.1, encryption_settings={"use_regular": "allow"}, ) as fetcher: for mode in Mode: if mode is Mode.CHECKING: continue fetched = fetcher.fetch(mode) assert isinstance(fetched.error, MKFetcherError)