Skip to content

Commit 5b8f14b

Browse files
authored
add tests for tl rpc parsing (#1582)
1 parent ef8c2fd commit 5b8f14b

4 files changed

Lines changed: 137 additions & 0 deletions

File tree

tests/python/tests/tl_primitives/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
entry: script
2+
components:
3+
script:
4+
image: KPHP
5+
scope: Request
6+
args: {}
7+
links: {}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
$operations = json_decode(file_get_contents('php://input'));
4+
5+
foreach ($operations as $op) {
6+
try {
7+
switch ($op['kind']) {
8+
case 'rpc_parse':
9+
$res = rpc_parse($op['new_rpc_data']);
10+
if ($res != $op['expected']) {
11+
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
12+
}
13+
break;
14+
case 'rpc_clean':
15+
$res = rpc_clean();
16+
if ($res != $op['expected']) {
17+
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
18+
}
19+
break;
20+
case 'fetch_int':
21+
$res = fetch_int();
22+
if ($res != $op['expected']) {
23+
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
24+
}
25+
break;
26+
case 'fetch_long':
27+
$res = fetch_long();
28+
if ($res != $op['expected']) {
29+
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
30+
}
31+
break;
32+
case 'fetch_double':
33+
$res = fetch_double();
34+
if ($res != $op['expected']) {
35+
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
36+
}
37+
break;
38+
case 'fetch_string':
39+
$res = fetch_string();
40+
if ($res != $op['expected']) {
41+
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
42+
}
43+
break;
44+
default:
45+
critical_error('unknown op ' . $op['kind']);
46+
}
47+
} catch (Exception $e) {
48+
if (!$op['expected_exception']) {
49+
throw $e;
50+
}
51+
}
52+
}
53+
54+
echo 'ok';
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import typing
2+
3+
import pytest
4+
5+
from python.lib.testcase import WebServerAutoTestCase
6+
7+
8+
def _operation(
9+
kind: str, kwargs: typing.Dict[str, typing.Any], expected_result: typing.Any, expected_exception: bool = False
10+
):
11+
return {
12+
"kind": kind,
13+
**kwargs,
14+
"expected": expected_result,
15+
"expected_exception": expected_exception,
16+
}
17+
18+
19+
def _assert_rpc_parse(new_rpc_data: str, expected_result: bool, expected_exception: bool = False):
20+
return _operation(
21+
'rpc_parse', dict(new_rpc_data=new_rpc_data), expected_result, expected_exception=expected_exception
22+
)
23+
24+
25+
def _assert_rpc_clean(expected_result: bool):
26+
return _operation('rpc_clean', dict(), expected_result)
27+
28+
29+
def _assert_fetch_int(expected_result: int, expected_exception: bool = False):
30+
return _operation('fetch_int', dict(), expected_result, expected_exception=expected_exception)
31+
32+
33+
def _assert_fetch_long(expected_result: int, expected_exception: bool = False):
34+
return _operation('fetch_long', dict(), expected_result, expected_exception=expected_exception)
35+
36+
37+
def _assert_fetch_double(expected_result: float, expected_exception: bool = False):
38+
return _operation('fetch_double', dict(), expected_result, expected_exception=expected_exception)
39+
40+
41+
def _assert_fetch_string(expected_result: str, expected_exception: bool = False):
42+
return _operation('fetch_string', dict(), expected_result, expected_exception=expected_exception)
43+
44+
45+
class TestTlPrimitives(WebServerAutoTestCase):
46+
def call(self, *operations):
47+
response = self.web_server.http_post(json=operations)
48+
self.assertEqual(response.status_code, 200)
49+
self.assertEqual(response.text, "ok")
50+
51+
def test_fetch(self):
52+
self.call(
53+
_assert_rpc_parse(
54+
'\x12\x34\x56\x78\x42\x00\x00\x00\x00\x00\x00\x00\x68\x56\x5b\x56\x06\x22\x09\x40\x03abc', True
55+
),
56+
_assert_fetch_int(0x78563412),
57+
_assert_fetch_long(0x42),
58+
_assert_fetch_double(3.14161365),
59+
_assert_fetch_string('abc'),
60+
)
61+
62+
@pytest.mark.kphp_skip
63+
def test_rpc_clean_k2(self):
64+
self.call(
65+
_assert_rpc_parse('\x12\x34\x56\x78', True),
66+
_assert_rpc_clean(True),
67+
_assert_fetch_int(0x777, expected_exception=True),
68+
)
69+
70+
@pytest.mark.k2_skip
71+
def test_rpc_clean_kphp(self):
72+
self.call(
73+
_assert_rpc_parse('\x12\x34\x56\x78', True),
74+
_assert_rpc_clean(True),
75+
_assert_fetch_int(0x78563412, expected_exception=False),
76+
)

0 commit comments

Comments
 (0)