本文整理汇总了Python中sandbox.Sandbox.open方法的典型用法代码示例。如果您正苦于以下问题:Python Sandbox.open方法的具体用法?Python Sandbox.open怎么用?Python Sandbox.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.Sandbox
的用法示例。
在下文中一共展示了Sandbox.open方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_subprocess_series
# 需要导入模块: from sandbox import Sandbox [as 别名]
# 或者: from sandbox.Sandbox import open [as 别名]
def test_subprocess_series():
profile = Profile({'max_processes': 5})
s = Sandbox()
s.clone_bin("sh")
s.clone_bin("echo")
s.clone_bin("cat")
with s.open("/sandbox_content.txt", 'w') as f:
f.write('\n'.join([
'hello',
''
]))
with s.open("/sandbox_subprocess.sh", 'w') as f:
f.write('\n'.join([
'#!/bin/sh',
]))
for i in range(0, 10):
f.write('\n'.join([
'#!/bin/sh',
'cat /sandbox_content.txt &',
]))
feedback = s.process(["sh", "/sandbox_subprocess.sh"], profile=profile, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = feedback.stdout.read()
assert len(stdout.split('hello')) <= (5 + 1)
assert feedback.ended_correctly
assert feedback.return_code != 0
del s
return True
示例2: test_fork_bombe
# 需要导入模块: from sandbox import Sandbox [as 别名]
# 或者: from sandbox.Sandbox import open [as 别名]
def test_fork_bombe():
# we do not launch the fork bomb if the subprocess test is not working
assert test_subprocess()
assert test_subprocess_series()
assert Profile.default_values['max_processes'] == 0
assert Profile()['max_processes'] == 0
s = Sandbox()
s.clone_bin("sh")
s.clone_bin("echo")
with s.open("/sandbox_subprocess.sh", 'w') as f:
f.write('\n'.join([
'#!/bin/sh',
'echo "visible";',
'sh $1 &',
'echo "hidden";'
]))
feedback = s.process(["sh", "/sandbox_subprocess.sh"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = feedback.stdout.read()
assert 'visible' in stdout
assert 'hidden' not in stdout
assert 'fork' in feedback.stderr.read()
assert feedback.ended_correctly
assert feedback.return_code != 0
del s
示例3: test_subprocess
# 需要导入模块: from sandbox import Sandbox [as 别名]
# 或者: from sandbox.Sandbox import open [as 别名]
def test_subprocess():
profile = Profile({'max_processes': 32})
s = Sandbox()
s.clone_bin("sh")
s.clone_bin("echo")
s.clone_bin("cat")
with s.open("/sandbox_subprocess.sh", 'w') as f:
f.write('\n'.join([
'#!/bin/sh',
'echo "visible";',
'cat $0;',
'echo "hidden";'
]))
feedback = s.process(["sh", "/sandbox_subprocess.sh"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = feedback.stdout.read()
assert 'visible' in stdout
assert 'hidden' not in stdout
assert 'fork' in feedback.stderr.read()
assert feedback.ended_correctly
assert feedback.return_code != 0
feedback = s.process(["sh", "/sandbox_subprocess.sh"], profile=profile, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = feedback.stdout.read()
assert 'visible' in stdout
assert 'hidden' in stdout
assert 'fork' not in feedback.stderr.read()
assert feedback.ended_correctly
assert feedback.return_code == 0
del s
return True
示例4: test_clobbering
# 需要导入模块: from sandbox import Sandbox [as 别名]
# 或者: from sandbox.Sandbox import open [as 别名]
def test_clobbering():
s = Sandbox()
s.clone_bin("echo")
s.clone_bin("cat")
s.clone_bin("sh")
cat_size = os.stat(s.to_main_basis('/bin/cat'))
with s.open("/sandbox_clobber.sh", 'w') as f:
f.write('\n'.join([
'#!/bin/sh',
'echo "clobbering $1 ..."',
'echo "$0 $*" > $1',
'echo "$?"'
]))
feedback = s.process(["sh", "/sandbox_clobber.sh", "/bin/cat"], stdout=subprocess.PIPE)
assert feedback.ended_correctly
assert feedback.return_code == 0
i = 0
for l in feedback.stdout:
assert i != 0 or l == "clobbering /bin/cat ...\n"
assert i != 1 or int(l) != 0
i += 1
assert os.stat(s.to_main_basis('/bin/cat')) == cat_size
del s
示例5: test_environment
# 需要导入模块: from sandbox import Sandbox [as 别名]
# 或者: from sandbox.Sandbox import open [as 别名]
def test_environment():
s = Sandbox()
s.clone_bin("echo")
s.clone_bin("sh")
assert s.shell_environment['USER'] == s.user_name
assert s.user_name != 'root'
assert pwd.getpwnam(s.user_name).pw_uid != 0
with s.open("/sandbox_env.sh", 'w') as f:
f.write('\n'.join([
'#!/bin/sh',
'echo $HOME',
'echo $USER',
'echo $PATH',
'echo $SHELL'
]))
feedback = s.process(["sh", "/sandbox_env.sh"], stdout=subprocess.PIPE)
assert feedback.ended_correctly
assert feedback.return_code == 0
i = 0
for l in feedback.stdout:
assert i != 0 or l.startswith(s.shell_environment['HOME'])
assert i != 1 or l.startswith(s.shell_environment['USER'])
assert i != 2 or l.startswith(s.shell_environment['PATH'])
assert i != 3 or l.startswith(s.shell_environment['SHELL'])
i += 1
del s
示例6: test_sleep_abort
# 需要导入模块: from sandbox import Sandbox [as 别名]
# 或者: from sandbox.Sandbox import open [as 别名]
def test_sleep_abort():
profile = Profile({'max_cpu_time': 10, 'max_duration': 1, 'max_processes': 32})
s = Sandbox()
s.clone_bin("sh")
s.clone_bin("sleep")
with s.open("/sandbox_sleep.sh", 'w') as f:
f.write('\n'.join([
'#!/bin/sh',
'sleep 5'
]))
feedback = s.process(["sh", "/sandbox_sleep.sh"], profile=profile)
assert feedback.killing_signal == signal.SIGKILL
assert not feedback.ended_correctly
assert feedback.return_code == 0
assert 'max_cpu_time' not in feedback.report
assert 'max_duration' in feedback.report
del s
示例7: test_infinite_loop
# 需要导入模块: from sandbox import Sandbox [as 别名]
# 或者: from sandbox.Sandbox import open [as 别名]
def test_infinite_loop():
profile = Profile({'max_cpu_time': 1, 'max_duration': 10})
s = Sandbox()
s.clone_bin("sh")
with s.open("/sandbox_infinite.sh", 'w') as f:
f.write('\n'.join([
'#!/bin/sh',
'while :',
'do',
' echo "hello"',
'done'
]))
feedback = s.process(["sh", "/sandbox_infinite.sh"], profile=profile)
assert feedback.killing_signal != 0
assert not feedback.ended_correctly
assert feedback.return_code == 0
#assert 'max_cpu_time' in feedback.report # commented for non determinist behavior on linux
assert 'max_duration' not in feedback.report
del s