|
| 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