xref: /freebsd/tests/atf_python/sys/netpfil/ipfw/utils.py (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1#!/usr/bin/env python3
2
3import os
4import socket
5import struct
6import subprocess
7import sys
8from enum import Enum
9from typing import Dict
10from typing import List
11from typing import Optional
12from typing import Union
13from typing import Any
14from typing import NamedTuple
15import pytest
16
17
18def roundup2(val: int, num: int) -> int:
19    if val % num:
20        return (val | (num - 1)) + 1
21    else:
22        return val
23
24
25def align8(val: int) -> int:
26    return roundup2(val, 8)
27
28
29def enum_or_int(val) -> int:
30    if isinstance(val, Enum):
31        return val.value
32    return val
33
34
35def enum_from_int(enum_class: Enum, val) -> Enum:
36    if isinstance(val, Enum):
37        return val
38    for item in enum_class:
39        if val == item.value:
40            return item
41    return None
42
43
44class AttrDescr(NamedTuple):
45    val: Enum
46    cls: Any
47    child_map: Any = None
48    is_array: bool = False
49
50
51def prepare_attrs_map(attrs: List[AttrDescr]) -> Dict[str, Dict]:
52    ret = {}
53    for ad in attrs:
54        ret[ad.val.value] = {"ad": ad}
55        if ad.child_map:
56            ret[ad.val.value]["child"] = prepare_attrs_map(ad.child_map)
57            ret[ad.val.value]["is_array"] = ad.is_array
58    return ret
59
60
61
62