def test_export_trans_except(): py_trans = bond.make_bond('Python', timeout=TIMEOUT, trans_except=True) py_not_trans = bond.make_bond('Python', timeout=TIMEOUT, trans_except=False) def call_me(): raise RuntimeError("a runtime error") # by default exceptions are transparent, so the following should correctly # restore the RuntimeError in RemoteException.data py_trans.export(call_me) py_trans.eval_block(r'''if True: failed = False try: call_me() except RuntimeError: failed = True ''') assert (py_trans.eval('failed') == True) # this one though will just generate a general Exception py_not_trans.export(call_me) py_not_trans.eval_block(r'''if True: failed = False try: call_me() except Exception as e: failed = not isinstance(e, RuntimeError) ''') assert (py_not_trans.eval('failed') == True)
def test_trans_except(): py_trans = bond.make_bond('Python', timeout=TIMEOUT, trans_except=True) py_not_trans = bond.make_bond('Python', timeout=TIMEOUT, trans_except=False) code = r'''def func(): raise RuntimeError("a runtime error") ''' # by default exceptions are transparent, so the following should correctly # restore the RuntimeError in RemoteException.data py_trans.eval_block(code) failed = False try: ret = py_trans.call('func') except bond.RemoteException as e: failed = isinstance(e.data, RuntimeError) assert (failed) # ensure the env didn't just die assert (py_trans.eval('1') == 1) # this one though will just always forward the remote exception py_not_trans.eval_block(code) failed = False try: ret = py_not_trans.call('func') except bond.RemoteException as e: failed = isinstance(e.data, str) assert (failed) # ensure the env didn't just die assert (py_not_trans.eval('1') == 1)
def test_export_trans_except(): perl_trans = bond.make_bond('Perl', timeout=TIMEOUT, trans_except=True) perl_not_trans = bond.make_bond('Perl', timeout=TIMEOUT, trans_except=False) def call_me(): raise RuntimeError("a runtime error") # by default exceptions are transparent, so the following should try to # serialize RuntimeError in JSON (and fail) perl_trans.export(call_me) perl_trans.eval_block(r''' sub test_exception() { my $ret = 0; eval { call_me(); }; $ret = 1 if $@ =~ /SerializationException/; return $ret; } ''') assert(perl_trans.call('test_exception') == True) # this one though will just generate a string perl_not_trans.export(call_me) perl_not_trans.eval_block(r''' sub test_exception() { my $ret = 0; eval { call_me(); }; $ret = 1 if $@ !~ /SerializationException/ && $@ =~ /a runtime error/; return $ret; } ''') assert(perl_not_trans.call('test_exception') == True)
def test_output_redirect(): capture = OutputCapture() # stdout (console) with capture: js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.eval_block(r'console.log("Hello world!");') assert (js.eval('1') == 1) ret = capture.stdout assert (str(ret) == "Hello world!\n") # stdout (stdout) with capture: js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.eval_block(r'process.stdout.write("Hello world!\n");') assert (js.eval('1') == 1) ret = capture.stdout assert (str(ret) == "Hello world!\n") # stderr (console) with capture: js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.eval_block(r'console.error("Hello world!");') assert (js.eval('1') == 1) ret = capture.stderr assert (str(ret) == "Hello world!\n") # stderr (stderr) with capture: js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.eval_block(r'process.stderr.write("Hello world!\n");') assert (js.eval('1') == 1) ret = capture.stderr assert (str(ret) == "Hello world!\n")
def test_trans_except(): perl_trans = bond.make_bond('Perl', timeout=TIMEOUT, trans_except=True) perl_not_trans = bond.make_bond('Perl', timeout=TIMEOUT, trans_except=False) code = r'''sub func() { die \&func; }''' # by default exceptions are transparent, so the following should try to # serialize the code block (and fail) perl_trans.eval_block(code) failed = False try: ret = perl_trans.call('func') except bond.SerializationException as e: print(e) failed = (e.side == "remote") assert(failed) # ensure the env didn't just die assert(perl_trans.eval('1') == 1) # this one though will just always forward the remote exception perl_not_trans.eval_block(code) failed = False try: ret = perl_not_trans.call('func') except bond.RemoteException as e: failed = True assert(failed) # ensure the env didn't just die assert(perl_not_trans.eval('1') == 1)
def test_output_redirect(): capture = OutputCapture() # stdout with capture: perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.eval_block(r'print "Hello world!\n";') assert(perl.eval('1') == 1) ret = capture.stdout assert(str(ret) == "Hello world!\n") # stderr with capture: perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.eval_block(r'print STDERR "Hello world!\n"') assert(perl.eval('1') == 1) ret = capture.stderr assert(str(ret) == "Hello world!\n") # warnings with capture: perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.eval_block(r'use warnings; "$warning_expected_on_stderr";') assert(perl.eval('1') == 1) ret = capture.stderr assert(str(ret).find('$warning_expected_on_stderr') >= 0)
def test_output_redirect(): capture = OutputCapture() # stdout with capture: perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.eval_block(r'print "Hello world!\n";') assert (perl.eval('1') == 1) ret = capture.stdout assert (str(ret) == "Hello world!\n") # stderr with capture: perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.eval_block(r'print STDERR "Hello world!\n"') assert (perl.eval('1') == 1) ret = capture.stderr assert (str(ret) == "Hello world!\n") # warnings with capture: perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.eval_block(r'use warnings; "$warning_expected_on_stderr";') assert (perl.eval('1') == 1) ret = capture.stderr assert (str(ret).find('$warning_expected_on_stderr') >= 0)
def test_output_redirect(): capture = OutputCapture() # stdout (console) with capture: js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.eval_block(r'console.log("Hello world!");') assert(js.eval('1') == 1) ret = capture.stdout assert(str(ret) == "Hello world!\n") # stdout (stdout) with capture: js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.eval_block(r'process.stdout.write("Hello world!\n");') assert(js.eval('1') == 1) ret = capture.stdout assert(str(ret) == "Hello world!\n") # stderr (console) with capture: js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.eval_block(r'console.error("Hello world!");') assert(js.eval('1') == 1) ret = capture.stderr assert(str(ret) == "Hello world!\n") # stderr (stderr) with capture: js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.eval_block(r'process.stderr.write("Hello world!\n");') assert(js.eval('1') == 1) ret = capture.stderr assert(str(ret) == "Hello world!\n")
def test_trans_except(): perl_trans = bond.make_bond('Perl', timeout=TIMEOUT, trans_except=True) perl_not_trans = bond.make_bond('Perl', timeout=TIMEOUT, trans_except=False) code = r'''sub func() { die \&func; }''' # by default exceptions are transparent, so the following should try to # serialize the code block (and fail) perl_trans.eval_block(code) failed = False try: ret = perl_trans.call('func') except bond.SerializationException as e: print(e) failed = (e.side == "remote") assert (failed) # ensure the env didn't just die assert (perl_trans.eval('1') == 1) # this one though will just always forward the remote exception perl_not_trans.eval_block(code) failed = False try: ret = perl_not_trans.call('func') except bond.RemoteException as e: failed = True assert (failed) # ensure the env didn't just die assert (perl_not_trans.eval('1') == 1)
def test_proxy(): py1 = bond.make_bond('Python', timeout=TIMEOUT) py1.eval_block(r'''def func_py1(arg): return arg + 1 ''') py2 = bond.make_bond('Python', timeout=TIMEOUT) py1.proxy('func_py1', py2) assert(py2.call('func_py1', 0) == 1)
def test_proxy(): py1 = bond.make_bond('Python', timeout=TIMEOUT) py1.eval_block(r'''def func_py1(arg): return arg + 1 ''') py2 = bond.make_bond('Python', timeout=TIMEOUT) py1.proxy('func_py1', py2) assert (py2.call('func_py1', 0) == 1)
def test_trans_except(): php_trans = bond.make_bond('PHP', timeout=TIMEOUT, trans_except=True) php_not_trans = bond.make_bond('PHP', timeout=TIMEOUT, trans_except=False) code = r''' if(PHP_VERSION_ID >= 50400) { class MyException extends Exception implements jsonSerializable { public function jsonSerialize() { return "MyException"; } } function func() { throw new MyException("an exception"); } } else { function func() { throw new Exception("an exception"); } } ''' # with transparent exceptions the following should try to serialize the # code block and call our jsonSerialize method php_trans.eval_block(code) failed = False try: ret = php_trans.call('func') except bond.RemoteException as e: print(e) failed = (str(e.data) == "MyException" or str(e.data) == "{}") assert (failed) # ensure the env didn't just die assert (php_trans.eval('1') == 1) # this one though will just forward the text php_not_trans.eval_block(code) failed = False try: ret = php_not_trans.call('func') except bond.RemoteException as e: print(e) failed = (str(e.data) == "an exception") assert (failed) # ensure the env didn't just die assert (php_not_trans.eval('1') == 1)
def test_trans_except(): php_trans = bond.make_bond('PHP', timeout=TIMEOUT, trans_except=True) php_not_trans = bond.make_bond('PHP', timeout=TIMEOUT, trans_except=False) code = r''' if(PHP_VERSION_ID >= 50400) { class MyException extends Exception implements jsonSerializable { public function jsonSerialize() { return "MyException"; } } function func() { throw new MyException("an exception"); } } else { function func() { throw new Exception("an exception"); } } ''' # with transparent exceptions the following should try to serialize the # code block and call our jsonSerialize method php_trans.eval_block(code) failed = False try: ret = php_trans.call('func') except bond.RemoteException as e: print(e) failed = (str(e.data) == "MyException" or str(e.data) == "{}") assert(failed) # ensure the env didn't just die assert(php_trans.eval('1') == 1) # this one though will just forward the text php_not_trans.eval_block(code) failed = False try: ret = php_not_trans.call('func') except bond.RemoteException as e: print(e) failed = (str(e.data) == "an exception") assert(failed) # ensure the env didn't just die assert(php_not_trans.eval('1') == 1)
def test_call_Perl_PHP(): php = bond.make_bond('PHP', timeout=TIMEOUT) perl = bond.make_bond('Perl', timeout=TIMEOUT) assert(php and perl) perl.eval_block(r'sub func_perl { shift() + 1; }') perl.proxy('func_perl', php) func_php = php.callable('func_perl') ret = func_php(0) assert(ret == 1)
def test_ref_xref(): py1 = bond.make_bond('Python', timeout=TIMEOUT) py2 = bond.make_bond('Python', timeout=TIMEOUT) failed = False try: py2.eval(py1.ref('1')) except bond.BondException as e: print(e) failed = True assert(failed)
def test_ref_xref(): py1 = bond.make_bond('Python', timeout=TIMEOUT) py2 = bond.make_bond('Python', timeout=TIMEOUT) failed = False try: py2.eval(py1.ref('1')) except bond.BondException as e: print(e) failed = True assert (failed)
def test_call_PHP_Perl(): php = bond.make_bond('PHP', timeout=TIMEOUT) perl = bond.make_bond('Perl', timeout=TIMEOUT) assert(php and perl) php.eval_block(r'function func_php($arg) { return $arg + 1; }') php.proxy('func_php', perl) func_perl = perl.callable('func_php') ret = func_perl(0) assert(ret == 1)
def test_export_recursive(): php = bond.make_bond('PHP', timeout=TIMEOUT) # define a remote function php.eval_block(r'function func_php($arg) { return $arg + 1; }') func_php = php.callable('func_php') assert(func_php(0) == 1) # define a local function that calls the remote def func_python(arg): return func_php(arg + 1) assert(func_python(0) == 2) # export the function remotely and call it php.export(func_python, 'remote_func_python') remote_func_python = php.callable('remote_func_python') assert(remote_func_python(0) == 2) # define a remote function that calls us recursively php.eval_block(r'function func_php_rec($arg) { return remote_func_python($arg) + 1; }') func_php_rec = php.callable('func_php_rec') assert(func_php_rec(0) == 3) # inception def func_python_rec(arg): return func_php_rec(arg) + 1 php.export(func_python_rec, 'remote_func_python_rec') php.eval_block(r'function func_php_rec_2($arg) { return remote_func_python_rec($arg) + 1; }') func_php_rec_2 = php.callable('func_php_rec_2') assert(func_php_rec_2(0) == 5)
def test_export(): def call_me(): return 42 php = bond.make_bond('PHP', timeout=TIMEOUT) php.export(call_me, 'call_me') assert(php.call('call_me') == 42)
def test_export(): def call_me(): return 42 js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.export(call_me, 'call_me') assert(js.call('call_me') == 42)
def test_export(): def call_me(): return 42 perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.export(call_me, 'call_me') assert (perl.call('call_me') == 42)
def test_eval(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # literal values assert (perl.eval('1') == 1) assert (perl.eval('undef') is None) assert (perl.eval_block('1;') is None) # expression assert (perl.eval('1 + 1') == 2) # check eval with unscoped variables perl.eval_block('$x = 1;') assert (perl.eval('$x // -1') == 1) # eval in Perl is scoped perl.eval_block('my $y = 1;') assert (perl.eval('$y // -1') == -1) # 'our' scope perl.eval_block('our $z = 1;') assert (perl.eval('$z // -1') == 1) # function definition perl.eval('sub test_perl { shift() + 1; }') assert (perl.eval('test_perl(0);') == 1)
def test_exception(): js = bond.make_bond('JavaScript', timeout=TIMEOUT) # remote exception js.eval_block('function exceptional() { throw new Error("an error") }') # ... in eval failed = False try: js.eval('exceptional()') except bond.RemoteException as e: print(e) failed = True assert(failed) # ... in eval_block failed = False try: js.eval_block('exceptional()') except bond.RemoteException as e: print(e) failed = True assert(failed) # ... in call failed = False try: js.call('exceptional') except bond.RemoteException as e: print(e) failed = True assert(failed)
def test_exception(): py = bond.make_bond('Python', timeout=TIMEOUT) # remote exception py.eval_block(r'''def exceptional(): raise Exception('an exception') ''') # ... in eval failed = False try: py.eval('exceptional()') except bond.RemoteException as e: print(e) failed = True assert (failed) # ... in eval_block failed = False try: py.eval_block('exceptional()') except bond.RemoteException as e: print(e) failed = True assert (failed) # ... in call failed = False try: py.call('exceptional') except bond.RemoteException as e: print(e) failed = True assert (failed)
def test_call_proto(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # without prototypes perl.eval('sub test_simple { "Hello world!"; }') perl_simple = perl.callable('test_simple') ret = perl_simple() assert(str(ret) == "Hello world!") ret = perl_simple(1) assert(str(ret) == "Hello world!") ret = perl_simple(1, 2) assert(str(ret) == "Hello world!") # with prototypes perl.eval('sub test_proto() { "Hello world!"; }') perl_proto = perl.callable('test_proto') ret = perl_proto() assert(str(ret) == "Hello world!") # broken statement failed = False try: perl_proto(1) except bond.RemoteException as e: print(e) failed = True assert(failed)
def test_export_recursive(): js = bond.make_bond('JavaScript', timeout=TIMEOUT) # define a remote function js.eval_block(r'function func_js(arg) { return arg + 1; }') func_js = js.callable('func_js') assert (func_js(0) == 1) # define a local function that calls the remote def func_python(arg): return func_js(arg + 1) assert (func_python(0) == 2) # export the function remotely and call it js.export(func_python, 'remote_func_python') remote_func_python = js.callable('remote_func_python') assert (remote_func_python(0) == 2) # define a remote function that calls us recursively js.eval_block( r'function func_js_rec(arg) { return remote_func_python(arg) + 1; }') func_js_rec = js.callable('func_js_rec') assert (func_js_rec(0) == 3) # inception def func_python_rec(arg): return func_js_rec(arg) + 1 js.export(func_python_rec, 'remote_func_python_rec') js.eval_block( r'function func_js_rec_2(arg) { return remote_func_python_rec(arg) + 1; }' ) func_js_rec_2 = js.callable('func_js_rec_2') assert (func_js_rec_2(0) == 5)
def test_export(): def call_me(): return 42 php = bond.make_bond('PHP', timeout=TIMEOUT) php.export(call_me, 'call_me') assert (php.call('call_me') == 42)
def test_eval_error(): php = bond.make_bond('PHP', timeout=TIMEOUT) # try a correct statement before assert (php.eval('1') == 1) # broken statement failed = False try: php.eval('"') except bond.RemoteException as e: print(e) failed = True assert (failed) # check that the environment is still alive assert (php.eval('1') == 1) # broken eval_block failed = False try: php.eval_block('"') except bond.RemoteException as e: print(e) failed = True assert (failed) # check that the environment is still alive assert (php.eval('1') == 1)
def test_buf_size_rmt(): js = bond.make_bond( 'JavaScript', "ssh localhost 'nodejs -i || node -e require\(\\\"repl\\\"\).start\(\)'", timeout=TIMEOUT, def_args=False) _test_buf_size(js)
def test_call_proto(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # without prototypes perl.eval('sub test_simple { "Hello world!"; }') perl_simple = perl.callable('test_simple') ret = perl_simple() assert (str(ret) == "Hello world!") ret = perl_simple(1) assert (str(ret) == "Hello world!") ret = perl_simple(1, 2) assert (str(ret) == "Hello world!") # with prototypes perl.eval('sub test_proto() { "Hello world!"; }') perl_proto = perl.callable('test_proto') ret = perl_proto() assert (str(ret) == "Hello world!") # broken statement failed = False try: perl_proto(1) except bond.RemoteException as e: print(e) failed = True assert (failed)
def test_ser_err(): php = bond.make_bond('PHP', timeout=TIMEOUT) # construct an unserializable type php.eval_block(r''' $fd = fopen("php://stdout", "w"); function func() { return fopen("php://stdout", "w"); } ''') # try to send it across failed = False try: php.eval('$fd') except bond.SerializationException as e: print(e) failed = (e.side == "remote") assert (failed) # ensure the env didn't just die assert (php.eval('1') == 1) # ... with call (return) failed = False try: php.call('func') except bond.SerializationException as e: print(e) failed = (e.side == "remote") assert (failed) # ensure the env didn't just die assert (php.eval('1') == 1)
def test_export(): def call_me(): return 42 py = bond.make_bond('Python', timeout=TIMEOUT) py.export(call_me, 'call_me') assert (py.call('call_me') == 42)
def test_ser_err(): php = bond.make_bond('PHP', timeout=TIMEOUT) # construct an unserializable type php.eval_block(r''' $fd = fopen("php://stdout", "w"); function func() { return fopen("php://stdout", "w"); } ''') # try to send it across failed = False try: php.eval('$fd') except bond.SerializationException as e: print(e) failed = (e.side == "remote") assert(failed) # ensure the env didn't just die assert(php.eval('1') == 1) # ... with call (return) failed = False try: php.call('func') except bond.SerializationException as e: print(e) failed = (e.side == "remote") assert(failed) # ensure the env didn't just die assert(php.eval('1') == 1)
def test_export_noret(): def call_me(): pass py = bond.make_bond('Python', timeout=TIMEOUT) py.export(call_me) assert(py.call('call_me') is None)
def test_call_marshalling(): perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.eval(r'sub test_str { "Hello world!"; }') assert (str(perl.call('test_str')) == "Hello world!") perl.eval(r'sub test_array { [42]; }') assert (perl.call('test_array') == [42]) perl.eval(r'sub test_number { 42; }') assert (perl.call('test_number') == 42) perl.eval(r'sub test_nothing { undef; }') assert (perl.call('test_nothing') is None) perl.eval(r'sub test_identity { shift(); }') perl_identity = perl.callable('test_identity') for value in [True, False, 0, 1, "String", [], [u"String"]]: ret = perl_identity(value) print("{} => {}".format(value, ret)) assert (str(ret) == str(value)) perl.eval(r'sub test_multi_arg { sprintf("%s %s", @_); }') assert (str(perl.call('test_multi_arg', "Hello", "world!")) == "Hello world!") perl.eval(r'sub test_nested { test_identity(shift()); }') perl_nested = perl.callable('test_nested') for value in [True, False, 0, 1, "String", [], [u"String"]]: ret = perl_nested(value) print("{} => {}".format(value, ret)) assert (str(ret) == str(value))
def test_export(): def call_me(): return 42 js = bond.make_bond('JavaScript', timeout=TIMEOUT) js.export(call_me, 'call_me') assert (js.call('call_me') == 42)
def test_exception(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # remote exception perl.eval_block('sub exceptional() { die $@; }') # ... in eval failed = False try: perl.eval('exceptional()') except bond.RemoteException as e: print(e) failed = True assert (failed) # ... in eval_block failed = False try: perl.eval_block('exceptional()') except bond.RemoteException as e: print(e) failed = True assert (failed) # ... in call failed = False try: perl.call('exceptional') except bond.RemoteException as e: print(e) failed = True assert (failed)
def test_call_marshalling(): php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'function test_str() { return "Hello world!"; }') assert(str(php.call('test_str')) == "Hello world!") php.eval_block(r'function test_array() { return array(42); }') assert(php.call('test_array') == [42]) php.eval_block(r'function test_number() { return 42; }') assert(php.call('test_number') == 42) php.eval_block(r'function test_bool() { return false; }') assert(php.call('test_bool') is False) php.eval_block(r'function test_nothing() { return null; }') assert(php.call('test_nothing') is None) php.eval_block(r'function test_identity($arg) { return $arg; }') php_identity = php.callable('test_identity') for value in [True, False, 0, 1, "String", [], [u"String"]]: ret = php_identity(value) print("{} => {}".format(value, ret)) assert(str(ret) == str(value)) php.eval_block(r'function test_multi_arg($arg1, $arg2) { return sprintf("%s %s", $arg1, $arg2); }') assert(str(php.call('test_multi_arg', "Hello", "world!")) == "Hello world!") php.eval_block(r'function test_nested($arg) { return test_identity($arg); }') php_nested = php.callable('test_nested') for value in [True, False, 0, 1, "String", [], [u"String"]]: ret = php_nested(value) print("{} => {}".format(value, ret)) assert(str(ret) == str(value))
def test_export_recursive(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # define a remote function perl.eval(r'sub func_perl { shift() + 1; }') func_perl = perl.callable('func_perl') assert (func_perl(0) == 1) # define a local function that calls the remote def func_python(arg): return func_perl(arg + 1) assert (func_python(0) == 2) # export the function remotely and call it perl.export(func_python, 'remote_func_python') remote_func_python = perl.callable('remote_func_python') assert (remote_func_python(0) == 2) # define a remote function that calls us recursively perl.eval(r'sub func_perl_rec { remote_func_python(shift()) + 1; }') func_perl_rec = perl.callable('func_perl_rec') assert (func_perl_rec(0) == 3) # inception def func_python_rec(arg): return func_perl_rec(arg) + 1 perl.export(func_python_rec, 'remote_func_python_rec') perl.eval(r'sub func_perl_rec_2 { remote_func_python_rec(shift()) + 1; }') func_perl_rec_2 = perl.callable('func_perl_rec_2') assert (func_perl_rec_2(0) == 5)
def test_exception(): js = bond.make_bond('JavaScript', timeout=TIMEOUT) # remote exception js.eval_block('function exceptional() { throw new Error("an error") }') # ... in eval failed = False try: js.eval('exceptional()') except bond.RemoteException as e: print(e) failed = True assert (failed) # ... in eval_block failed = False try: js.eval_block('exceptional()') except bond.RemoteException as e: print(e) failed = True assert (failed) # ... in call failed = False try: js.call('exceptional') except bond.RemoteException as e: print(e) failed = True assert (failed)
def test_eval_error(): php = bond.make_bond('PHP', timeout=TIMEOUT) # try a correct statement before assert(php.eval('1') == 1) # broken statement failed = False try: php.eval('"') except bond.RemoteException as e: print(e) failed = True assert(failed) # check that the environment is still alive assert(php.eval('1') == 1) # broken eval_block failed = False try: php.eval_block('"') except bond.RemoteException as e: print(e) failed = True assert(failed) # check that the environment is still alive assert(php.eval('1') == 1)
def test_export_recursive(): js = bond.make_bond('JavaScript', timeout=TIMEOUT) # define a remote function js.eval_block(r'function func_js(arg) { return arg + 1; }') func_js = js.callable('func_js') assert(func_js(0) == 1) # define a local function that calls the remote def func_python(arg): return func_js(arg + 1) assert(func_python(0) == 2) # export the function remotely and call it js.export(func_python, 'remote_func_python') remote_func_python = js.callable('remote_func_python') assert(remote_func_python(0) == 2) # define a remote function that calls us recursively js.eval_block(r'function func_js_rec(arg) { return remote_func_python(arg) + 1; }') func_js_rec = js.callable('func_js_rec') assert(func_js_rec(0) == 3) # inception def func_python_rec(arg): return func_js_rec(arg) + 1 js.export(func_python_rec, 'remote_func_python_rec') js.eval_block(r'function func_js_rec_2(arg) { return remote_func_python_rec(arg) + 1; }') func_js_rec_2 = js.callable('func_js_rec_2') assert(func_js_rec_2(0) == 5)
def test_call_stm(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # test the call interface with a normal function perl.eval_block('sub copy { shift() }') ret = perl.call('copy', "Hello world!") assert(str(ret) == "Hello world!") # test the call interface with some random syntax ret = perl.call('&{ \© }', "Hello world!") assert(str(ret) == "Hello world!") # test calling a function bypassing the prototype ret = perl.call('©', "Hello world!") assert(str(ret) == "Hello world!") # check return values depending on the context ret = perl.call('scalar split', ' ', "Hello world!") assert(ret == 2) ret = perl.call('split', ' ', "Hello world!") assert(ret == ["Hello", "world!"]) # try with some perl prototyped functions ret = perl.call('map { $_ }', "Hello world!") assert(str(ret) == "Hello world!")
def test_export_noret(): def call_me(): pass py = bond.make_bond('Python', timeout=TIMEOUT) py.export(call_me) assert (py.call('call_me') is None)
def test_export_recursive(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # define a remote function perl.eval(r'sub func_perl { shift() + 1; }') func_perl = perl.callable('func_perl') assert(func_perl(0) == 1) # define a local function that calls the remote def func_python(arg): return func_perl(arg + 1) assert(func_python(0) == 2) # export the function remotely and call it perl.export(func_python, 'remote_func_python') remote_func_python = perl.callable('remote_func_python') assert(remote_func_python(0) == 2) # define a remote function that calls us recursively perl.eval(r'sub func_perl_rec { remote_func_python(shift()) + 1; }') func_perl_rec = perl.callable('func_perl_rec') assert(func_perl_rec(0) == 3) # inception def func_python_rec(arg): return func_perl_rec(arg) + 1 perl.export(func_python_rec, 'remote_func_python_rec') perl.eval(r'sub func_perl_rec_2 { remote_func_python_rec(shift()) + 1; }') func_perl_rec_2 = perl.callable('func_perl_rec_2') assert(func_perl_rec_2(0) == 5)
def test_export(): def call_me(): return 42 perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.export(call_me, 'call_me') assert(perl.call('call_me') == 42)
def test_exception(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # remote exception perl.eval_block('sub exceptional() { die $@; }') # ... in eval failed = False try: perl.eval('exceptional()') except bond.RemoteException as e: print(e) failed = True assert(failed) # ... in eval_block failed = False try: perl.eval_block('exceptional()'); except bond.RemoteException as e: print(e) failed = True assert(failed) # ... in call failed = False try: perl.call('exceptional') except bond.RemoteException as e: print(e) failed = True assert(failed)
def test_eval(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # literal values assert(perl.eval('1') == 1) assert(perl.eval('undef') is None) assert(perl.eval_block('1;') is None) # expression assert(perl.eval('1 + 1') == 2) # check eval with unscoped variables perl.eval_block('$x = 1;') assert(perl.eval('$x // -1') == 1) # eval in Perl is scoped perl.eval_block('my $y = 1;') assert(perl.eval('$y // -1') == -1) # 'our' scope perl.eval_block('our $z = 1;') assert(perl.eval('$z // -1') == 1) # function definition perl.eval('sub test_perl { shift() + 1; }') assert(perl.eval('test_perl(0);') == 1)
def test_call_marshalling(): perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.eval(r'sub test_str { "Hello world!"; }') assert(str(perl.call('test_str')) == "Hello world!") perl.eval(r'sub test_array { [42]; }') assert(perl.call('test_array') == [42]) perl.eval(r'sub test_number { 42; }') assert(perl.call('test_number') == 42) perl.eval(r'sub test_nothing { undef; }') assert(perl.call('test_nothing') is None) perl.eval(r'sub test_identity { shift(); }') perl_identity = perl.callable('test_identity') for value in [True, False, 0, 1, "String", [], [u"String"]]: ret = perl_identity(value) print("{} => {}".format(value, ret)) assert(str(ret) == str(value)) perl.eval(r'sub test_multi_arg { sprintf("%s %s", @_); }') assert(str(perl.call('test_multi_arg', "Hello", "world!")) == "Hello world!") perl.eval(r'sub test_nested { test_identity(shift()); }') perl_nested = perl.callable('test_nested') for value in [True, False, 0, 1, "String", [], [u"String"]]: ret = perl_nested(value) print("{} => {}".format(value, ret)) assert(str(ret) == str(value))
def test_output_redirect(): capture = OutputCapture() # standard output (echo) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'echo "Hello world!\n";') assert (php.eval('1') == 1) ret = capture.stdout assert (str(ret) == "Hello world!\n") # standard output (STDOUT) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'fwrite(STDOUT, "Hello world!\n");') assert (php.eval('1') == 1) ret = capture.stdout assert (str(ret) == "Hello world!\n") # standard output (php://stdout) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'file_put_contents("php://stdout", "Hello world!\n");') assert (php.eval('1') == 1) ret = capture.stdout assert (str(ret) == "Hello world!\n") # standard error (STDERR) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'fwrite(STDERR, "Hello world!\n");') assert (php.eval('1') == 1) ret = capture.stderr assert (str(ret) == "Hello world!\n") # standard error (php://stderr) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'file_put_contents("php://stderr", "Hello world!\n");') assert (php.eval('1') == 1) ret = capture.stderr assert (str(ret) == "Hello world!\n") # error reporting (on) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'ini_set("display_errors", "1");') php.eval_block(r'trigger_error("Hello world!");') assert (php.eval('1') == 1) ret = capture.stderr assert (str(ret).find("Hello world!") >= 0) # error reporting (off) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'ini_set("display_errors", "0");') php.eval_block(r'trigger_error("Hello world!");') assert (php.eval('1') == 1) ret = capture.stderr assert (str(ret).find("Hello world!") < 0)
def test_output_redirect(): capture = OutputCapture() # standard output (echo) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'echo "Hello world!\n";') assert(php.eval('1') == 1) ret = capture.stdout assert(str(ret) == "Hello world!\n") # standard output (STDOUT) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'fwrite(STDOUT, "Hello world!\n");') assert(php.eval('1') == 1) ret = capture.stdout assert(str(ret) == "Hello world!\n") # standard output (php://stdout) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'file_put_contents("php://stdout", "Hello world!\n");') assert(php.eval('1') == 1) ret = capture.stdout assert(str(ret) == "Hello world!\n") # standard error (STDERR) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'fwrite(STDERR, "Hello world!\n");') assert(php.eval('1') == 1) ret = capture.stderr assert(str(ret) == "Hello world!\n") # standard error (php://stderr) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'file_put_contents("php://stderr", "Hello world!\n");') assert(php.eval('1') == 1) ret = capture.stderr assert(str(ret) == "Hello world!\n") # error reporting (on) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'ini_set("display_errors", "1");') php.eval_block(r'trigger_error("Hello world!");') assert(php.eval('1') == 1) ret = capture.stderr assert(str(ret).find("Hello world!") >= 0) # error reporting (off) with capture: php = bond.make_bond('PHP', timeout=TIMEOUT) php.eval_block(r'ini_set("display_errors", "0");') php.eval_block(r'trigger_error("Hello world!");') assert(php.eval('1') == 1) ret = capture.stderr assert(str(ret).find("Hello world!") < 0)
def test_stack_depth(): def no_exception(): pass def gen_exception(): raise Exception("test") def gen_ser_err(): return lambda x: x # check normal stack depth perl = bond.make_bond('Perl', timeout=TIMEOUT) assert(bond_repl_depth(perl) == 1) # check stack depth after calling a normal function perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.export(no_exception) perl.call('no_exception') assert(bond_repl_depth(perl) == 1) # check stack depth after returning a serializable exception perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.export(gen_exception) got_except = False try: perl.call('gen_exception') except bond.RemoteException as e: print(e) got_except = True assert(got_except) assert(bond_repl_depth(perl) == 1) # check stack depth after a remote serialization error perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.export(gen_ser_err) got_except = False try: perl.call('gen_ser_err') except bond.SerializationException as e: print(e) assert(e.side == "remote") got_except = True assert(got_except) assert(bond_repl_depth(perl) == 1)
def test_error_reporting_noise(): php = bond.make_bond('PHP', timeout=TIMEOUT) # ensure no noise is generated in the I/O stream, ever try: php.eval_block('error_reporting(E_ALL); echo $undefined;') except bond.RemoteException as e: print(e) pass assert(php.eval('1') == 1)
def test_call_simple(): perl = bond.make_bond('Perl', timeout=TIMEOUT) perl.eval('sub test_simple { "Hello world!"; }') perl_simple = perl.callable('test_simple') ret = perl_simple() assert(str(ret) == "Hello world!") perl.eval('sub test_proto() { "Hello world!"; }') perl_proto = perl.callable('test_proto') ret = perl_proto() assert(str(ret) == "Hello world!")
def test_call_builtin(): perl = bond.make_bond('Perl', timeout=TIMEOUT) # NOTE: sprintf is a built-in, and thus more compliated to call with # the same semantics perl_sprintf = perl.callable('sprintf') ret = perl_sprintf("Hello world!") assert(str(ret) == "Hello world!") # no exception should be thrown here perl.call('print', 'Hellow world!')