| 1 |
import pytest |
| 2 |
|
| 3 |
import varlink |
| 4 |
|
| 5 |
|
| 6 |
def test_scanner_1() -> None: |
| 7 |
interface = varlink.Interface("""# Example Varlink service |
| 8 |
interface org.example.more |
| 9 |
|
| 10 |
# Enum, returning either start, progress or end |
| 11 |
# progress: [0-100] |
| 12 |
type State ( |
| 13 |
start: ?bool, |
| 14 |
progress: ?int, |
| 15 |
end: ?bool |
| 16 |
) |
| 17 |
|
| 18 |
method TestMap(map: [string]string) -> (map: [string](i: int, val: string)) |
| 19 |
|
| 20 |
# Returns the same string |
| 21 |
method Ping(ping: string) -> (pong: string) |
| 22 |
|
| 23 |
# Dummy progress method |
| 24 |
# n: number of progress steps |
| 25 |
method TestMore(n: int) -> (state: State) |
| 26 |
|
| 27 |
# Stop serving |
| 28 |
method StopServing() -> () |
| 29 |
|
| 30 |
type ErrorChain ( |
| 31 |
description: string, |
| 32 |
caused_by: ?ErrorChain |
| 33 |
) |
| 34 |
|
| 35 |
error ActionFailed (reason: ?ErrorChain) |
| 36 |
""") |
| 37 |
assert interface.name == "org.example.more" |
| 38 |
assert interface.get_method("Ping") is not None |
| 39 |
assert interface.get_method("TestMore") is not None |
| 40 |
assert interface.get_method("TestMap") is not None |
| 41 |
assert interface.get_method("StopServing") is not None |
| 42 |
assert isinstance(interface.members.get("ActionFailed"), varlink.scanner._Error) |
| 43 |
assert isinstance(interface.members.get("State"), varlink.scanner._Alias) |
| 44 |
|
| 45 |
|
| 46 |
def test_doubleoption() -> None: |
| 47 |
interface = None |
| 48 |
try: |
| 49 |
interface = varlink.Interface(""" |
| 50 |
interface org.example.doubleoption |
| 51 |
method Foo(a: ??string) -> () |
| 52 |
""") |
| 53 |
except SyntaxError: |
| 54 |
pass |
| 55 |
|
| 56 |
assert interface is None |
| 57 |
|
| 58 |
|
| 59 |
def test_complex() -> None: |
| 60 |
interface = varlink.Interface(""" |
| 61 |
interface org.example.complex |
| 62 |
|
| 63 |
type TypeEnum ( a, b, c ) |
| 64 |
|
| 65 |
type TypeFoo ( |
| 66 |
bool: bool, |
| 67 |
int: int, |
| 68 |
float: float, |
| 69 |
string: ?string, |
| 70 |
enum: ?[]( foo, bar, baz ), |
| 71 |
type: ?TypeEnum, |
| 72 |
anon: ( foo: bool, bar: int, baz: [](a: int, b: int) ), |
| 73 |
object: object |
| 74 |
) |
| 75 |
|
| 76 |
method Foo(a: (b: bool, c: int), foo: TypeFoo) -> (a: [](b: bool, c: int), foo: TypeFoo) |
| 77 |
|
| 78 |
error ErrorFoo (a: (b: bool, c: int), foo: TypeFoo) |
| 79 |
""") |
| 80 |
|
| 81 |
assert interface.name == "org.example.complex" |
| 82 |
assert interface.get_method("Foo") is not None |
| 83 |
assert isinstance(interface.members.get("ErrorFoo"), varlink.scanner._Error) |
| 84 |
assert isinstance(interface.members.get("TypeEnum"), varlink.scanner._Alias) |
| 85 |
|
| 86 |
|
| 87 |
invalid_interfacenames = [ |
| 88 |
"interface .a.b.c\nmethod F()->()", |
| 89 |
"interface com.-example.leadinghyphen\nmethod F()->()", |
| 90 |
"interface com.example-.danglinghyphen-\nmethod F()->()", |
| 91 |
"interface co9.example.number-toplevel\nmethod F()->()", |
| 92 |
"interface 1om.example.number-toplevel\nmethod F()->()", |
| 93 |
"interface ab\nmethod F()->()", |
| 94 |
"interface .a.b.c\nmethod F()->()", |
| 95 |
"interface a.b.c.\nmethod F()->()", |
| 96 |
"interface a..b.c\nmethod F()->()", |
| 97 |
"interface 1.b.c\nmethod F()->()", |
| 98 |
"interface 8a.0.0\nmethod F()->()", |
| 99 |
"interface -a.b.c\nmethod F()->()", |
| 100 |
"interface a.b.c-\nmethod F()->()", |
| 101 |
"interface a.b-.c-\nmethod F()->()", |
| 102 |
"interface a.-b.c-\nmethod F()->()", |
| 103 |
"interface a.-.c\nmethod F()->()", |
| 104 |
"interface a.*.c\nmethod F()->()", |
| 105 |
"interface a.?\nmethod F()->()", |
| 106 |
] |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.parametrize("description", invalid_interfacenames) |
| 110 |
def test_interfacename_invalid(description) -> None: |
| 111 |
with pytest.raises(SyntaxError): |
| 112 |
varlink.Interface(description) |
| 113 |
|
| 114 |
|
| 115 |
valid_interfacenames = [ |
| 116 |
"interface a.b\nmethod F()->()", |
| 117 |
"interface a.b.c\nmethod F()->()", |
| 118 |
"interface a.1\nmethod F()->()", |
| 119 |
"interface a.0.0\nmethod F()->()", |
| 120 |
"interface org.varlink.service\nmethod F()->()", |
| 121 |
"interface com.example.0example\nmethod F()->()", |
| 122 |
"interface com.example.example-dash\nmethod F()->()", |
| 123 |
"interface xn--lgbbat1ad8j.example.algeria\nmethod F()->()", |
| 124 |
"interface xn--c1yn36f.xn--c1yn36f.xn--c1yn36f\nmethod F()->()", |
| 125 |
] |
| 126 |
|
| 127 |
|
| 128 |
@pytest.mark.parametrize("description", valid_interfacenames) |
| 129 |
def test_interfacename_valid(description) -> None: |
| 130 |
assert varlink.Interface(description).name is not None |
| 131 |
|
| 132 |
|
| 133 |
def test_bad_types() -> None: |
| 134 |
interface = varlink.Interface(""" |
| 135 |
interface org.example.testerrors |
| 136 |
type TypeEnum ( a, b, c ) |
| 137 |
type TypeDict (dict: [string]string) |
| 138 |
|
| 139 |
method Foo(param: TypeEnum) -> () |
| 140 |
method Bar(param: TypeDict) -> () |
| 141 |
""") |
| 142 |
foo = interface.get_method("Foo") |
| 143 |
with pytest.raises(varlink.InvalidParameter): |
| 144 |
interface.filter_params("test.call", foo.in_type, False, (), {"param": "d"}) |
| 145 |
|
| 146 |
bar = interface.get_method("Bar") |
| 147 |
with pytest.raises(varlink.InvalidParameter): |
| 148 |
interface.filter_params("test.call", bar.in_type, False, (), {"param": {"dict": [1, 2, 3]}}) |
| 149 |
|
| 150 |
|
| 151 |
def test_method_not_found() -> None: |
| 152 |
interface = varlink.Interface(""" |
| 153 |
interface org.example.testerrors |
| 154 |
""") |
| 155 |
|
| 156 |
with pytest.raises(varlink.MethodNotFound): |
| 157 |
interface.get_method("Bar") |
| 158 |
|
| 159 |
|
| 160 |
def test_struct_errors() -> None: |
| 161 |
missing_colon_after_name = """ |
| 162 |
interface org.example.teststruct |
| 163 |
|
| 164 |
type TypeStruct ( struct ?[](first: int, second: string) ) |
| 165 |
""" |
| 166 |
|
| 167 |
with pytest.raises(SyntaxError, match="after 'struct'"): |
| 168 |
varlink.Interface(missing_colon_after_name) |
| 169 |
|
| 170 |
missing_colon_in_struct = """ |
| 171 |
interface org.example.teststruct |
| 172 |
|
| 173 |
type MyType ( |
| 174 |
nullable_array_struct: ?[](first: int, second string) |
| 175 |
) |
| 176 |
method Start() -> (client_id: string) |
| 177 |
""" |
| 178 |
with pytest.raises(SyntaxError, match="after 'second'"): |
| 179 |
varlink.Interface(missing_colon_in_struct) |
| 180 |
|
| 181 |
|
| 182 |
def test_unexpected() -> None: |
| 183 |
invalid = """ |
| 184 |
interface org.example.unexpected |
| 185 |
|
| 186 |
invalid |
| 187 |
""" |
| 188 |
|
| 189 |
with pytest.raises(SyntaxError, match="expected type, method, or error"): |
| 190 |
varlink.Interface(invalid) |