def test_packing(self): cert, content = Certificate.pop(b'\x02\x00\x04\x00\x00\x01\x01\x04\x04aq\x0f\x02\x00\x00\x00\x00') self.assertEqual(b'\x04\x04aq\x0f\x02\x00\x00\x00\x00', content) self.assertEqual(CertType.IDENTITY, cert.type) self.assertEqual(2, cert.type_int) self.assertEqual(b'\x00\x00\x01\x01', cert.value) self.assertEqual(b'\x02\x00\x04\x00\x00\x01\x01', cert.pack())
def _unpack(cls, content, circ_id, link_protocol): cert_count, content = Size.CHAR.pop(content) certs = [] for i in range(cert_count): if not content: raise ValueError('CERTS cell indicates it should have %i certificates, but only contained %i' % (cert_count, len(certs))) cert, content = Certificate.pop(content) certs.append(cert) return CertsCell(certs, unused = content)
def test_constructor(self): test_data = ( ((1, b'\x7f\x00\x00\x01'), (CertType.LINK, 1, b'\x7f\x00\x00\x01')), ((2, b'\x7f\x00\x00\x01'), (CertType.IDENTITY, 2, b'\x7f\x00\x00\x01')), ((3, b'\x7f\x00\x00\x01'), (CertType.AUTHENTICATE, 3, b'\x7f\x00\x00\x01')), ((4, b'\x7f\x00\x00\x01'), (CertType.ED25519_SIGNING, 4, b'\x7f\x00\x00\x01')), ((CertType.IDENTITY, b'\x7f\x00\x00\x01'), (CertType.IDENTITY, 2, b'\x7f\x00\x00\x01')), ) for (cert_type, cert_value), (expected_type, expected_type_int, expected_value) in test_data: cert = Certificate(cert_type, cert_value) self.assertEqual(expected_type, cert.type) self.assertEqual(expected_type_int, cert.type_int) self.assertEqual(expected_value, cert.value)
def _unpack( cls, content: bytes, circ_id: int, link_protocol: 'stem.client.datatype.LinkProtocol' ) -> 'stem.client.cell.CertsCell': cert_count, content = Size.CHAR.pop(content) certs = [] # type: List[stem.client.datatype.Certificate] for i in range(cert_count): if not content: raise ValueError( 'CERTS cell indicates it should have %i certificates, but only contained %i' % (cert_count, len(certs))) cert, content = Certificate.pop(content) certs.append(cert) return CertsCell(certs, unused=content)
def test_certs_cell(self): for cell_bytes, certs in CERTS_CELLS.items(): self.assertEqual(cell_bytes, CertsCell(certs).pack(2)) self.assertEqual(certs, Cell.pop(cell_bytes, 2)[0].certificates) # extra bytes after the last certificate should be ignored self.assertEqual( [Certificate(1, '\x08')], Cell.pop(b'\x00\x00\x81\x00\x07\x01\x01\x00\x01\x08\x06\x04', 2)[0].certificates) # ... but truncated or missing certificates should error self.assertRaisesRegexp( ValueError, 'CERTS cell should have a certificate with 3 bytes, but only had 1 remaining', Cell.pop, b'\x00\x00\x81\x00\x05\x01\x01\x00\x03\x08', 2) self.assertRaisesRegexp( ValueError, 'CERTS cell indicates it should have 2 certificates, but only contained 1', Cell.pop, b'\x00\x00\x81\x00\x05\x02\x01\x00\x01\x08', 2)
(datetime.datetime(2018, 1, 14, 1, 46, 56), Address('127.0.0.1'), [Address('97.113.15.2')], b'\x01' + ZERO * 491, 2), } VPADDING_CELL_EMPTY_PACKED = b'\x00\x00\x80\x00\x00' VPADDING_CELLS = { VPADDING_CELL_EMPTY_PACKED: (b'', 2), b'\x00\x00\x80\x00\x01\x08': (b'\x08', 2), b'\x00\x00\x80\x00\x02\x08\x11': (b'\x08\x11', 2), b'\x00\x00\x80\x01\xfd' + RANDOM_PAYLOAD: (RANDOM_PAYLOAD, 2), } CERTS_CELLS = { b'\x00\x00\x81\x00\x01\x00': ([], b'', 2), b'\x00\x00\x81\x00\x04\x01\x01\x00\x00': ([Certificate(1, b'')], b'', 2), b'\x00\x00\x81\x00\x05\x01\x01\x00\x01\x08': ([Certificate(1, b'\x08')], b'', 2), b'\x00\x00\x81\x00\x07\x01\x01\x00\x01\x08' + b'\x06\x04': ([Certificate(1, b'\x08')], b'\x06\x04', 2), } AUTH_CHALLENGE_CELLS = { b'\x00\x00\x82\x00\x26' + CHALLENGE + b'\x00\x02\x00\x01\x00\x03': (CHALLENGE, [1, 3], b'', 2), b'\x00\x00\x82\x00\x28' + CHALLENGE + b'\x00\x02\x00\x01\x00\x03' + b'\x01\x02': (CHALLENGE, [1, 3], b'\x01\x02', 2), } class TestCell(unittest.TestCase):
b'\x00\x00\x08ZZ\xb6\x90\x04\x04\x7f\x00\x00\x01\x01\x04\x04aq\x0f\x02' + ZERO * (FIXED_PAYLOAD_LEN - 17): (datetime.datetime(2018, 1, 14, 1, 46, 56), Address('127.0.0.1'), [Address('97.113.15.2')], ZERO * 492, 2), b'\x00\x00\x08ZZ\xb6\x90\x04\x04\x7f\x00\x00\x01\x01\x04\x04aq\x0f\x02' + b'\x01' + ZERO * (FIXED_PAYLOAD_LEN - 18): (datetime.datetime(2018, 1, 14, 1, 46, 56), Address('127.0.0.1'), [Address('97.113.15.2')], b'\x01' + ZERO * 491, 2), } VPADDING_CELL_EMPTY_PACKED = b'\x00\x00\x80\x00\x00' VPADDING_CELLS = { VPADDING_CELL_EMPTY_PACKED: (b'', 2), b'\x00\x00\x80\x00\x01\x08': (b'\x08', 2), b'\x00\x00\x80\x00\x02\x08\x11': (b'\x08\x11', 2), b'\x00\x00\x80\x01\xfd' + RANDOM_PAYLOAD: (RANDOM_PAYLOAD, 2), } CERTS_CELLS = { b'\x00\x00\x81\x00\x01\x00': ([], b'', 2), b'\x00\x00\x81\x00\x04\x01\x01\x00\x00': ([Certificate(1, b'')], b'', 2), b'\x00\x00\x81\x00\x05\x01\x01\x00\x01\x08': ([Certificate(1, b'\x08')], b'', 2), b'\x00\x00\x81\x00\x07\x01\x01\x00\x01\x08' + b'\x06\x04': ([Certificate(1, b'\x08')], b'\x06\x04', 2), } AUTH_CHALLENGE_CELLS = { b'\x00\x00\x82\x00\x26' + CHALLENGE + b'\x00\x02\x00\x01\x00\x03': (CHALLENGE, [1, 3], b'', 2), b'\x00\x00\x82\x00\x28' + CHALLENGE + b'\x00\x02\x00\x01\x00\x03' + b'\x01\x02': (CHALLENGE, [1, 3], b'\x01\x02', 2), } class TestCell(unittest.TestCase): def test_by_name(self): cls = Cell.by_name('NETINFO') self.assertEqual('NETINFO', cls.NAME) self.assertEqual(8, cls.VALUE)
(datetime.datetime(2018, 1, 14, 1, 46, 56), Address('127.0.0.1'), [Address('97.113.15.2')]), } VPADDING_CELL_EMPTY_PACKED = b'\x00\x00\x80\x00\x00' VPADDING_CELLS = { VPADDING_CELL_EMPTY_PACKED: b'', b'\x00\x00\x80\x00\x01\x08': b'\x08', b'\x00\x00\x80\x00\x02\x08\x11': b'\x08\x11', b'\x00\x00\x80\x01\xfd' + RANDOM_PAYLOAD: RANDOM_PAYLOAD, } CERTS_CELLS = { b'\x00\x00\x81\x00\x01\x00': [], b'\x00\x00\x81\x00\x04\x01\x01\x00\x00': [Certificate(1, b'')], b'\x00\x00\x81\x00\x05\x01\x01\x00\x01\x08': [Certificate(1, b'\x08')], } AUTH_CHALLENGE_CELLS = { b'\x00\x00\x82\x00&' + CHALLENGE + b'\x00\x02\x00\x01\x00\x03': (CHALLENGE, [1, 3]), } class TestCell(unittest.TestCase): def test_by_name(self): cls = Cell.by_name('NETINFO') self.assertEqual('NETINFO', cls.NAME) self.assertEqual(8, cls.VALUE) self.assertEqual(True, cls.IS_FIXED_SIZE)
def test_unknown_type(self): cert = Certificate(12, 'hello') self.assertEqual(CertType.UNKNOWN, cert.type) self.assertEqual(12, cert.type_int) self.assertEqual('hello', cert.value)