<reponame>ftheoleyre/openvisualizer
#!/usr/bin/env python

import json
import logging.handlers

import pytest

# ============================ logging =========================================
from openvisualizer.utils import byteinverse, hex2buf, format_ipv6_addr, buf2int

LOGFILE_NAME = 'test_utils.log'

log = logging.getLogger('test_utils')
log.setLevel(logging.ERROR)
log.addHandler(logging.NullHandler())

log_handler = logging.handlers.RotatingFileHandler(LOGFILE_NAME, backupCount=5, mode='w')

log_handler.setFormatter(logging.Formatter("%(asctime)s [%(name)s:%(levelname)s] %(message)s"))
for logger_name in ['test_utils', 'OpenLbr']:
    temp = logging.getLogger(logger_name)
    temp.setLevel(logging.DEBUG)
    temp.addHandler(log_handler)

# ============================ defines =========================================

# ============================ fixtures ========================================

# ===== expected_buf2int

EXPECTED_BUF2INT = [
    #           buf          int
    json.dumps(([0x01, 0x02], 0x0102)),
    json.dumps(([0xaa, 0xbb], 0xaabb)),
]


@pytest.fixture(params=EXPECTED_BUF2INT)
def expected_buf2int(request):
    return request.param


# ===== expected_hex2buf

EXPECTED_HEX2BUF = [
    #           hex          buf
    json.dumps(('abcd', [0xab, 0xcd])),
    json.dumps(('', [])),
    json.dumps(('aa', [0xaa])),
]


@pytest.fixture(params=EXPECTED_HEX2BUF)
def expected_hex2buf(request):
    return request.param


# ===== expected_byteinverse

EXPECTED_BYTEINVERSE = [
    #           b    b_inverse
    json.dumps((0x01, 0x80)),
    json.dumps((0x02, 0x40)),
    json.dumps((0x04, 0x20)),
    json.dumps((0x81, 0x81)),
]


@pytest.fixture(params=EXPECTED_BYTEINVERSE)
def expected_byteinverse(request):
    return request.param


# ===== expected_format_ipv6

EXPECTED_FORMAT_IPv6 = [
    json.dumps(
        (
            [  # list
                0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
                0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
            ],
            'fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b',  # string
        ),
    ),
    json.dumps(
        (
            [  # list
                0x01, 0x23, 0x45, 0x67, 0x00, 0x00, 0xcd, 0xef,
                0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
            ],
            'fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b',  # string
        ),
    ),
    json.dumps(
        (
            [  # list
                0x01, 0x23, 0x45, 0x67, 0x00, 0x00, 0x00, 0x00,
                0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
            ],
            'fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b',  # string
        ),
    ),
]


@pytest.fixture(params=EXPECTED_FORMAT_IPv6)
def expected_format_ipv6(request):
    return request.param


# ============================ helpers =========================================

# ============================ tests ===========================================

def test_buf2int(expected_buf2int):
    (exp_buf, exp_int) = json.loads(expected_buf2int)

    assert buf2int(exp_buf) == exp_int


def test_hex2buf(expected_hex2buf):
    (exp_hex, exp_buf) = json.loads(expected_hex2buf)
    exp_hex = str(exp_hex)

    assert hex2buf(exp_hex) == exp_buf


def test_byteinverse(expected_byteinverse):
    (b, b_inverse) = json.loads(expected_byteinverse)

    assert byteinverse(b) == b_inverse
    assert byteinverse(b_inverse) == b


def test_format_ipv6_addr(expected_format_ipv6):
    (ipv6_list, ipv6_string) = json.loads(expected_format_ipv6)

    log.info(ipv6_string)

    assert format_ipv6_addr(ipv6_list) == ipv6_string

#!/usr/bin/env python

import json
import logging.handlers

import pytest

# ============================ logging =========================================
from openvisualizer.utils import byteinverse, hex2buf, format_ipv6_addr, buf2int

LOGFILE_NAME = 'test_utils.log'

log = logging.getLogger('test_utils')
log.setLevel(logging.ERROR)
log.addHandler(logging.NullHandler())

log_handler = logging.handlers.RotatingFileHandler(LOGFILE_NAME, backupCount=5, mode='w')

log_handler.setFormatter(logging.Formatter("%(asctime)s [%(name)s:%(levelname)s] %(message)s"))
for logger_name in ['test_utils', 'OpenLbr']:
    temp = logging.getLogger(logger_name)
    temp.setLevel(logging.DEBUG)
    temp.addHandler(log_handler)

# ============================ defines =========================================

# ============================ fixtures ========================================

# ===== expected_buf2int

EXPECTED_BUF2INT = [
    #           buf          int
    json.dumps(([0x01, 0x02], 0x0102)),
    json.dumps(([0xaa, 0xbb], 0xaabb)),
]


@pytest.fixture(params=EXPECTED_BUF2INT)
def expected_buf2int(request):
    return request.param


# ===== expected_hex2buf

EXPECTED_HEX2BUF = [
    #           hex          buf
    json.dumps(('abcd', [0xab, 0xcd])),
    json.dumps(('', [])),
    json.dumps(('aa', [0xaa])),
]


@pytest.fixture(params=EXPECTED_HEX2BUF)
def expected_hex2buf(request):
    return request.param


# ===== expected_byteinverse

EXPECTED_BYTEINVERSE = [
    #           b    b_inverse
    json.dumps((0x01, 0x80)),
    json.dumps((0x02, 0x40)),
    json.dumps((0x04, 0x20)),
    json.dumps((0x81, 0x81)),
]


@pytest.fixture(params=EXPECTED_BYTEINVERSE)
def expected_byteinverse(request):
    return request.param


# ===== expected_format_ipv6

EXPECTED_FORMAT_IPv6 = [
    json.dumps(
        (
            [  # list
                0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
                0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
            ],
            'fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b',  # string
        ),
    ),
    json.dumps(
        (
            [  # list
                0x01, 0x23, 0x45, 0x67, 0x00, 0x00, 0xcd, 0xef,
                0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
            ],
            'fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b',  # string
        ),
    ),
    json.dumps(
        (
            [  # list
                0x01, 0x23, 0x45, 0x67, 0x00, 0x00, 0x00, 0x00,
                0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
            ],
            'fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b',  # string
        ),
    ),
]


@pytest.fixture(params=EXPECTED_FORMAT_IPv6)
def expected_format_ipv6(request):
    return request.param


# ============================ helpers =========================================

# ============================ tests ===========================================

def test_buf2int(expected_buf2int):
    (exp_buf, exp_int) = json.loads(expected_buf2int)

    assert buf2int(exp_buf) == exp_int


def test_hex2buf(expected_hex2buf):
    (exp_hex, exp_buf) = json.loads(expected_hex2buf)
    exp_hex = str(exp_hex)

    assert hex2buf(exp_hex) == exp_buf


def test_byteinverse(expected_byteinverse):
    (b, b_inverse) = json.loads(expected_byteinverse)

    assert byteinverse(b) == b_inverse
    assert byteinverse(b_inverse) == b


def test_format_ipv6_addr(expected_format_ipv6):
    (ipv6_list, ipv6_string) = json.loads(expected_format_ipv6)

    log.info(ipv6_string)

    assert format_ipv6_addr(ipv6_list) == ipv6_string
#!/usr/bin/env python # ============================ logging ========================================= # ============================ defines ========================================= # ============================ fixtures ======================================== # ===== expected_buf2int #           buf          int # ===== expected_hex2buf #           hex          buf # ===== expected_byteinverse #           b    b_inverse # ===== expected_format_ipv6 # list # string # list # string # list # string # ============================ helpers ========================================= # ============================ tests ===========================================