-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from Snawoot/improve_coverage
Improve coverage
- Loading branch information
Showing
8 changed files
with
123 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import asyncio | ||
import tempfile | ||
import os | ||
import contextlib | ||
|
||
import pytest | ||
import pynetstring | ||
|
||
from postfix_mta_sts_resolver.responder import STSSocketmapResponder | ||
import postfix_mta_sts_resolver.utils as utils | ||
import postfix_mta_sts_resolver.base_cache as base_cache | ||
|
||
@contextlib.contextmanager | ||
def set_env(**environ): | ||
old_environ = dict(os.environ) | ||
os.environ.update(environ) | ||
try: | ||
yield | ||
finally: | ||
os.environ.clear() | ||
os.environ.update(old_environ) | ||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.timeout(10) | ||
async def test_responder_expiration(event_loop): | ||
async def query(host, port, domain): | ||
reader, writer = await asyncio.open_connection(host, port) | ||
decoder = pynetstring.Decoder() | ||
writer.write(pynetstring.encode(b'test ' + domain.encode('ascii'))) | ||
try: | ||
while True: | ||
data = await reader.read(4096) | ||
assert data | ||
res = decoder.feed(data) | ||
if res: | ||
return res[0] | ||
finally: | ||
writer.close() | ||
with tempfile.NamedTemporaryFile() as cachedb: | ||
cfg = {} | ||
cfg["port"] = 18461 | ||
cfg["cache_grace"] = 0 | ||
cfg["shutdown_timeout"] = 1 | ||
cfg["cache"] = { | ||
"type": "sqlite", | ||
"options": { | ||
"filename": cachedb.name, | ||
}, | ||
} | ||
cfg = utils.populate_cfg_defaults(cfg) | ||
cache = utils.create_cache(cfg['cache']['type'], | ||
cfg['cache']['options']) | ||
await cache.setup() | ||
pol_body = { | ||
"version": "STSv1", | ||
"mode": "enforce", | ||
"mx": [ "mail.loc" ], | ||
"max_age": 1, | ||
} | ||
await cache.set("no-record.loc", base_cache.CacheEntry(0, "0", pol_body)) | ||
await cache.teardown() | ||
|
||
resp = STSSocketmapResponder(cfg, event_loop) | ||
await resp.start() | ||
try: | ||
result = await query(cfg['host'], cfg['port'], 'no-record.loc') | ||
assert result == b'NOTFOUND ' | ||
finally: | ||
await resp.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters