| 1 | import json |
| 2 | import os |
| 3 | import tempfile |
| 4 | from unittest import TestCase |
| 5 | |
| 6 | from aioquic.quic.logger import QuicFileLogger, QuicLogger |
| 7 | |
| 8 | SINGLE_TRACE = { |
| 9 | "qlog_format": "JSON", |
| 10 | "qlog_version": "0.3", |
| 11 | "traces": [ |
| 12 | { |
| 13 | "common_fields": { |
| 14 | "ODCID": "0000000000000000", |
| 15 | }, |
| 16 | "events": [], |
| 17 | "vantage_point": {"name": "aioquic", "type": "client"}, |
| 18 | } |
| 19 | ], |
| 20 | } |
| 21 | |
| 22 | |
| 23 | class QuicLoggerTest(TestCase): |
| 24 | def test_empty(self): |
| 25 | logger = QuicLogger() |
| 26 | self.assertEqual( |
| 27 | logger.to_dict(), |
| 28 | {"qlog_format": "JSON", "qlog_version": "0.3", "traces": []}, |
| 29 | ) |
| 30 | |
| 31 | def test_single_trace(self): |
| 32 | logger = QuicLogger() |
| 33 | trace = logger.start_trace(is_client=True, odcid=bytes(8)) |
| 34 | logger.end_trace(trace) |
| 35 | self.assertEqual(logger.to_dict(), SINGLE_TRACE) |
| 36 | |
| 37 | |
| 38 | class QuicFileLoggerTest(TestCase): |
| 39 | def test_invalid_path(self): |
| 40 | with self.assertRaises(ValueError) as cm: |
| 41 | QuicFileLogger("this_path_should_not_exist") |
| 42 | self.assertEqual( |
| 43 | str(cm.exception), |
| 44 | "QUIC log output directory 'this_path_should_not_exist' does not exist", |
| 45 | ) |
| 46 | |
| 47 | def test_single_trace(self): |
| 48 | with tempfile.TemporaryDirectory() as dirpath: |
| 49 | logger = QuicFileLogger(dirpath) |
| 50 | trace = logger.start_trace(is_client=True, odcid=bytes(8)) |
| 51 | logger.end_trace(trace) |
| 52 | |
| 53 | filepath = os.path.join(dirpath, "0000000000000000.qlog") |
| 54 | self.assertTrue(os.path.exists(filepath)) |
| 55 | |
| 56 | with open(filepath, "r") as fp: |
| 57 | data = json.load(fp) |
| 58 | self.assertEqual(data, SINGLE_TRACE) |