Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
entry: script
components:
script:
image: KPHP
scope: Request
args: {}
links: {}
54 changes: 54 additions & 0 deletions tests/python/tests/tl_primitives/php/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

$operations = json_decode(file_get_contents('php://input'));

foreach ($operations as $op) {
try {
switch ($op['kind']) {
case 'rpc_parse':
$res = rpc_parse($op['new_rpc_data']);
if ($res != $op['expected']) {
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
}
break;
case 'rpc_clean':
$res = rpc_clean();
if ($res != $op['expected']) {
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
}
break;
case 'fetch_int':
$res = fetch_int();
if ($res != $op['expected']) {
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
}
break;
case 'fetch_long':
$res = fetch_long();
if ($res != $op['expected']) {
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
}
break;
case 'fetch_double':
$res = fetch_double();
if ($res != $op['expected']) {
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
}
break;
case 'fetch_string':
$res = fetch_string();
if ($res != $op['expected']) {
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
}
break;
default:
critical_error('unknown op ' . $op['kind']);
}
} catch (Exception $e) {
if (!$op['expected_exception']) {
throw $e;
}
}
}

echo 'ok';
76 changes: 76 additions & 0 deletions tests/python/tests/tl_primitives/test_tl_primitives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import typing

import pytest

from python.lib.testcase import WebServerAutoTestCase


def _operation(
kind: str, kwargs: typing.Dict[str, typing.Any], expected_result: typing.Any, expected_exception: bool = False
):
return {
"kind": kind,
**kwargs,
"expected": expected_result,
"expected_exception": expected_exception,
}


def _assert_rpc_parse(new_rpc_data: str, expected_result: bool, expected_exception: bool = False):
return _operation(
'rpc_parse', dict(new_rpc_data=new_rpc_data), expected_result, expected_exception=expected_exception
)


def _assert_rpc_clean(expected_result: bool):
return _operation('rpc_clean', dict(), expected_result)


def _assert_fetch_int(expected_result: int, expected_exception: bool = False):
return _operation('fetch_int', dict(), expected_result, expected_exception=expected_exception)


def _assert_fetch_long(expected_result: int, expected_exception: bool = False):
return _operation('fetch_long', dict(), expected_result, expected_exception=expected_exception)


def _assert_fetch_double(expected_result: float, expected_exception: bool = False):
return _operation('fetch_double', dict(), expected_result, expected_exception=expected_exception)


def _assert_fetch_string(expected_result: str, expected_exception: bool = False):
return _operation('fetch_string', dict(), expected_result, expected_exception=expected_exception)


class TestTlPrimitives(WebServerAutoTestCase):
def call(self, *operations):
response = self.web_server.http_post(json=operations)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.text, "ok")

def test_fetch(self):
self.call(
_assert_rpc_parse(
'\x12\x34\x56\x78\x42\x00\x00\x00\x00\x00\x00\x00\x68\x56\x5b\x56\x06\x22\x09\x40\x03abc', True
),
_assert_fetch_int(0x78563412),
_assert_fetch_long(0x42),
_assert_fetch_double(3.14161365),
_assert_fetch_string('abc'),
)

@pytest.mark.kphp_skip
def test_rpc_clean_k2(self):
self.call(
_assert_rpc_parse('\x12\x34\x56\x78', True),
_assert_rpc_clean(True),
_assert_fetch_int(0x777, expected_exception=True),
)

@pytest.mark.k2_skip
def test_rpc_clean_kphp(self):
self.call(
_assert_rpc_parse('\x12\x34\x56\x78', True),
_assert_rpc_clean(True),
_assert_fetch_int(0x78563412, expected_exception=False),
)
Loading