def test_compile(self): cbitstruct.compile(fmt=FMT, names=NAMES) cbitstruct.compile(FMT, names=NAMES) cbitstruct.compile(FMT, NAMES) cbitstruct.compile(fmt=FMT) cbitstruct.compile(FMT)
def test_bad_format(self): """Test of bad format. """ formats = [('g1', "bad char 'g' in format"), ('s1u1f32b1t8r8G13', "bad char 'G' in format"), ('s1u1f32b1t8r8G13S3', "bad char 'G' in format"), ('s', "bad format 's'"), ('1', "bad format '1'"), ('ss1', "bad format 'ss1'"), ('1s', "bad format '1s'"), ('foo', "bad format 'foo'"), ('s>1>', "bad format 's>1>'"), ('s0', "bad format 's0'")] for fmt, expected_error in formats: with self.assertRaises(Error) as cm: cbitstruct.compile(fmt)
def test_compile(self): cf = cbitstruct.compile('u1u1s6u7u9') packed = cf.pack(0, 0, -2, 65, 22) self.assertEqual(packed, b'\x3e\x82\x16') unpacked = cf.unpack(b'\x3e\x82\x16') self.assertEqual(unpacked, (0, 0, -2, 65, 22))
def test_compile_pack_unpack_formats(self): fmts = [('u1s2p3', None, (1, -1)), ('u1 s2 p3', None, (1, -1)), ('u1s2p3', ['a', 'b'], { 'a': 1, 'b': -1 })] for fmt, names, decoded in fmts: if names is None: cf = cbitstruct.compile(fmt) packed_1 = cf.pack(*decoded) packed_2 = pack(fmt, *decoded) else: cf = cbitstruct.compile(fmt, names) packed_1 = cf.pack(decoded) packed_2 = pack_dict(fmt, names, decoded) self.assertEqual(packed_1, b'\xe0') self.assertEqual(packed_2, b'\xe0')
def test_byte_order_format(self): """Test of a format with only byte order information. """ cf = cbitstruct.compile('>') self.assertEqual(cf.pack(), b'') self.assertEqual(cf.pack(1), b'') self.assertEqual(cf.unpack(b''), ()) self.assertEqual(cf.unpack(b'\x00'), ())
def test_empty_format(self): """Test of empty format type. """ cf = cbitstruct.compile('') self.assertEqual(cf.pack(), b'') self.assertEqual(cf.pack(1), b'') self.assertEqual(cf.unpack(b''), ()) self.assertEqual(cf.unpack(b'\x00'), ())
import asyncio import functools import operator import hexdump import cbitstruct as bitstruct from .db import db_lookup from .codes import * from dns.models import A_Record, Domain header_struct = bitstruct.compile(header_format) question_struct = bitstruct.compile(question_format) answer_struct = bitstruct.compile(answer_format) opt_struct = bitstruct.compile(opt_format) optanswer_struct = bitstruct.compile(optanswer_format) label_struct = bitstruct.compile(label_format) soa_struct = bitstruct.compile(soa_format) mx_struct = bitstruct.compile(mx_format) caa_flags_struct = bitstruct.compile(caa_flags_format) srv_struct = bitstruct.compile(srv_format) async def Query(pool, data): # message = data.decode() # MARK: Header # TODO: Handle OPCODE_operation # TODO: Handle RD_recursion_desired # TODO: Handle QR_response # TODO: Handle TC_truncation # TODO: Handle AD_authentic_data
def test_compile_formats(self): cbitstruct.compile('p1u1') cbitstruct.compile('p1u1', ['a'])
from threading import Thread from queue import Queue from django.conf import settings import hexdump import asyncpg #import struct #import bitstruct #import bitstruct.c as bitstruct import cbitstruct as bitstruct from .codes import * from dns.models import A_Record, Domain header = bitstruct.compile(header_format) question = bitstruct.compile(question_format) answer = bitstruct.compile(answer_format) opt = bitstruct.compile(opt_format) #header_format = '!H2B4H' class DNSServerProtocol: def __init__(self, q): self.q = q def connection_made(self, transport): self.transport = transport # print(f'Created UDP connection with {transport=}')
import random import cbitstruct import bitstruct fmt = "s12u45p127f32f64s32r13p2P8r32u16u2s12" nbytes = (bitstruct.calcsize(fmt) + 7) // 8 random.seed(0) data = bytes([random.randint(0, 255) for _ in range(nbytes)]) dst = bytearray([0] * nbytes) values = bitstruct.unpack(fmt, data) names = string.ascii_letters[:len(values)] values_dict = {n: v for n, v in zip(names, values)} bs = bitstruct.compile(fmt) cbs = cbitstruct.compile(fmt) dbs = bitstruct.compile(fmt, names) cdbs = cbitstruct.compile(fmt, names) NBS = 10000 NCBS = 100000 class PerfTest(unittest.TestCase): def generic(self, name, bitstruct, cbitstruct): bstime = timeit.timeit(bitstruct, number=NBS, globals=globals()) / NBS cbstime = timeit.timeit(cbitstruct, number=NCBS, globals=globals()) / NCBS improvement = bstime / cbstime