Skip to content

Commit

Permalink
more hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
emlowe committed Apr 26, 2024
1 parent d39733e commit 45779f1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
1 change: 0 additions & 1 deletion chia/_tests/conftest.py
Expand Up @@ -827,7 +827,6 @@ async def farmer_one_harvester_simulator_wallet(

@pytest.fixture(scope="function")
async def farmer_one_harvester(tmp_path: Path, get_b_tools: BlockTools) -> AsyncIterator[FarmerOneHarvester]:
asyncio.get_event_loop().set_debug(True)
async with setup_farmer_multi_harvester(get_b_tools, 1, tmp_path, get_b_tools.constants, start_services=True) as _:
yield _

Expand Down
60 changes: 32 additions & 28 deletions chia/_tests/core/ssl/test_ssl.py
Expand Up @@ -45,13 +45,12 @@ async def establish_connection(server: ChiaServer, self_hostname: str, ssl_conte


@contextlib.contextmanager
def suppress_ssl_exception_report():
loop = asyncio.get_event_loop()
# loop.set_debug(True)
old_handler = loop.get_exception_handler()
def set_ssl_cert_error():
current_loop = asyncio.get_event_loop()
old_handler = current_loop.get_exception_handler()
old_handler_fn = old_handler # or lambda _loop, ctx: loop.default_exception_handler(ctx)

def ignore_exc(_loop, ctx):
def find_and_set(loop, ctx):
exc = ctx.get("exception")
if isinstance(exc, ssl.SSLCertVerificationError):
global ssl_cert_error
Expand All @@ -60,12 +59,11 @@ def ignore_exc(_loop, ctx):

old_handler_fn(loop, ctx)

loop.set_exception_handler(ignore_exc)
current_loop.set_exception_handler(find_and_set)
try:
yield
finally:
loop.set_exception_handler(old_handler)
loop.set_debug(False)
current_loop.set_exception_handler(old_handler)


class TestSSL:
Expand All @@ -80,8 +78,9 @@ async def test_public_connections(self, simulator_and_wallet, self_hostname):
assert success is True

@pytest.mark.anyio
async def test_farmer(self, farmer_one_harvester, self_hostname):
_, farmer_service, bt = farmer_one_harvester
async def test_farmer(self, farmer_one_harvester_not_started, self_hostname):

_, farmer_service, bt = farmer_one_harvester_not_started
farmer_api = farmer_service._api

farmer_server = farmer_api.farmer.server
Expand All @@ -96,25 +95,30 @@ async def test_farmer(self, farmer_one_harvester, self_hostname):
priv_crt,
priv_key,
)
# ssl_context = ssl_context_for_client(ca_private_crt_path, ca_private_key_path, priv_crt, priv_key)
# await establish_connection(farmer_server, self_hostname, ssl_context)

# Create not authenticated cert
pub_crt = farmer_server.root_path / "non_valid.crt"
pub_key = farmer_server.root_path / "non_valid.key"
generate_ca_signed_cert(chia_ca_crt_path.read_bytes(), chia_ca_key_path.read_bytes(), pub_crt, pub_key)
# ssl_context = ssl_context_for_client(chia_ca_crt_path, chia_ca_key_path, pub_crt, pub_key)
# with pytest.raises(aiohttp.ClientConnectorCertificateError):
# await establish_connection(farmer_server, self_hostname, ssl_context)
ssl_context = ssl_context_for_client(ca_private_crt_path, ca_private_key_path, pub_crt, pub_key)
try:
with suppress_ssl_exception_report():
await establish_connection(farmer_server, self_hostname, ssl_context)
except Exception as e:
print(e)
assert ssl_cert_error is True
finally:
assert ssl_cert_error is True
asyncio.get_event_loop().set_debug(True)
async with farmer_service.manage():
# ssl_context = ssl_context_for_client(ca_private_crt_path, ca_private_key_path, priv_crt, priv_key)
# await establish_connection(farmer_server, self_hostname, ssl_context)

# Create not authenticated cert
pub_crt = farmer_server.root_path / "non_valid.crt"
pub_key = farmer_server.root_path / "non_valid.key"
generate_ca_signed_cert(chia_ca_crt_path.read_bytes(), chia_ca_key_path.read_bytes(), pub_crt, pub_key)
# ssl_context = ssl_context_for_client(chia_ca_crt_path, chia_ca_key_path, pub_crt, pub_key)
# with pytest.raises(aiohttp.ClientConnectorCertificateError):
# await establish_connection(farmer_server, self_hostname, ssl_context)
ssl_context = ssl_context_for_client(ca_private_crt_path, ca_private_key_path, pub_crt, pub_key)
try:
with set_ssl_cert_error():
await establish_connection(farmer_server, self_hostname, ssl_context)
except Exception as e:
print(e)
assert ssl_cert_error is True
finally:
assert ssl_cert_error is True

asyncio.get_event_loop().set_debug(False)

@pytest.mark.anyio
async def test_full_node(self, simulator_and_wallet, self_hostname):
Expand Down

0 comments on commit 45779f1

Please sign in to comment.