Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
codejail
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
codejail
Commits
92a2a180
Commit
92a2a180
authored
May 16, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change how we dedent code in the tests.
parent
45a329ca
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
28 deletions
+28
-28
codejail/tests/test_jailpy.py
+28
-28
No files found.
codejail/tests/test_jailpy.py
View file @
92a2a180
...
@@ -11,12 +11,12 @@ from nose.plugins.skip import SkipTest
...
@@ -11,12 +11,12 @@ from nose.plugins.skip import SkipTest
from
codejail.jail_code
import
jail_code
,
is_configured
,
set_limit
,
LIMITS
from
codejail.jail_code
import
jail_code
,
is_configured
,
set_limit
,
LIMITS
dedent
=
textwrap
.
dedent
def
jailpy
(
code
=
None
,
*
args
,
**
kwargs
):
def
jailpy
(
*
args
,
**
kwargs
):
"""Run `jail_code` on Python."""
"""Run `jail_code` on Python."""
return
jail_code
(
"python"
,
*
args
,
**
kwargs
)
if
code
:
code
=
textwrap
.
dedent
(
code
)
return
jail_code
(
"python"
,
code
,
*
args
,
**
kwargs
)
def
file_here
(
fname
):
def
file_here
(
fname
):
...
@@ -55,7 +55,7 @@ class TestFeatures(JailCodeHelpers, unittest.TestCase):
...
@@ -55,7 +55,7 @@ class TestFeatures(JailCodeHelpers, unittest.TestCase):
res
=
jailpy
(
code
=
"""raise Exception('FAIL')"""
)
res
=
jailpy
(
code
=
"""raise Exception('FAIL')"""
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertEqual
(
res
.
stderr
,
dedent
(
"""
\
self
.
assertEqual
(
res
.
stderr
,
textwrap
.
dedent
(
"""
\
Traceback (most recent call last):
Traceback (most recent call last):
File "jailed_code", line 1, in <module>
File "jailed_code", line 1, in <module>
raise Exception('FAIL')
raise Exception('FAIL')
...
@@ -80,15 +80,15 @@ class TestFeatures(JailCodeHelpers, unittest.TestCase):
...
@@ -80,15 +80,15 @@ class TestFeatures(JailCodeHelpers, unittest.TestCase):
def
test_directories_are_copied
(
self
):
def
test_directories_are_copied
(
self
):
res
=
jailpy
(
res
=
jailpy
(
code
=
dedent
(
"""
\
code
=
"""
\
import os
import os
for path, dirs, files in os.walk("."):
for path, dirs, files in os.walk("."):
print (path, sorted(dirs), sorted(files))
print (path, sorted(dirs), sorted(files))
"""
)
,
"""
,
files
=
[
file_here
(
"hello.txt"
),
file_here
(
"pylib"
)]
files
=
[
file_here
(
"hello.txt"
),
file_here
(
"pylib"
)]
)
)
self
.
assertResultOk
(
res
)
self
.
assertResultOk
(
res
)
self
.
assertEqual
(
res
.
stdout
,
dedent
(
"""
\
self
.
assertEqual
(
res
.
stdout
,
textwrap
.
dedent
(
"""
\
('.', ['pylib'], ['hello.txt', 'jailed_code'])
('.', ['pylib'], ['hello.txt', 'jailed_code'])
('./pylib', [], ['module.py', 'module.pyc'])
('./pylib', [], ['module.py', 'module.pyc'])
"""
))
"""
))
...
@@ -118,55 +118,55 @@ class TestLimits(JailCodeHelpers, unittest.TestCase):
...
@@ -118,55 +118,55 @@ class TestLimits(JailCodeHelpers, unittest.TestCase):
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertNotEqual
(
res
.
status
,
0
)
def
test_cant_use_too_much_time
(
self
):
def
test_cant_use_too_much_time
(
self
):
res
=
jailpy
(
code
=
dedent
(
"""
\
res
=
jailpy
(
code
=
"""
\
import time
import time
time.sleep(5)
time.sleep(5)
print 'Done!'
print 'Done!'
"""
)
)
"""
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertEqual
(
res
.
stdout
,
""
)
def
test_cant_write_files
(
self
):
def
test_cant_write_files
(
self
):
res
=
jailpy
(
code
=
dedent
(
"""
\
res
=
jailpy
(
code
=
"""
\
print "Trying"
print "Trying"
with open("mydata.txt", "w") as f:
with open("mydata.txt", "w") as f:
f.write("hello")
f.write("hello")
with open("mydata.txt") as f2:
with open("mydata.txt") as f2:
print "Got this:", f2.read()
print "Got this:", f2.read()
"""
)
)
"""
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertEqual
(
res
.
stdout
,
"Trying
\n
"
)
self
.
assertEqual
(
res
.
stdout
,
"Trying
\n
"
)
self
.
assertIn
(
"ermission denied"
,
res
.
stderr
)
self
.
assertIn
(
"ermission denied"
,
res
.
stderr
)
def
test_cant_use_network
(
self
):
def
test_cant_use_network
(
self
):
res
=
jailpy
(
code
=
dedent
(
"""
\
res
=
jailpy
(
code
=
"""
\
import urllib
import urllib
print "Reading google"
print "Reading google"
u = urllib.urlopen("http://google.com")
u = urllib.urlopen("http://google.com")
google = u.read()
google = u.read()
print len(google)
print len(google)
"""
)
)
"""
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertEqual
(
res
.
stdout
,
"Reading google
\n
"
)
self
.
assertEqual
(
res
.
stdout
,
"Reading google
\n
"
)
self
.
assertIn
(
"IOError"
,
res
.
stderr
)
self
.
assertIn
(
"IOError"
,
res
.
stderr
)
def
test_cant_fork
(
self
):
def
test_cant_fork
(
self
):
res
=
jailpy
(
code
=
dedent
(
"""
\
res
=
jailpy
(
code
=
"""
\
import os
import os
print "Forking"
print "Forking"
child_ppid = os.fork()
child_ppid = os.fork()
"""
)
)
"""
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertEqual
(
res
.
stdout
,
"Forking
\n
"
)
self
.
assertEqual
(
res
.
stdout
,
"Forking
\n
"
)
self
.
assertIn
(
"OSError"
,
res
.
stderr
)
self
.
assertIn
(
"OSError"
,
res
.
stderr
)
def
test_cant_see_environment_variables
(
self
):
def
test_cant_see_environment_variables
(
self
):
os
.
environ
[
'HONEY_BOO_BOO'
]
=
'Look!'
os
.
environ
[
'HONEY_BOO_BOO'
]
=
'Look!'
res
=
jailpy
(
code
=
dedent
(
"""
\
res
=
jailpy
(
code
=
"""
\
import os
import os
for name, value in os.environ.items():
for name, value in os.environ.items():
print "
%
s:
%
r"
%
(name, value)
print "
%
s:
%
r"
%
(name, value)
"""
)
)
"""
)
self
.
assertResultOk
(
res
)
self
.
assertResultOk
(
res
)
self
.
assertNotIn
(
"HONEY"
,
res
.
stdout
)
self
.
assertNotIn
(
"HONEY"
,
res
.
stdout
)
...
@@ -202,11 +202,11 @@ class TestSymlinks(JailCodeHelpers, unittest.TestCase):
...
@@ -202,11 +202,11 @@ class TestSymlinks(JailCodeHelpers, unittest.TestCase):
# Run some code in the sandbox, with a copied directory containing
# Run some code in the sandbox, with a copied directory containing
# the symlink.
# the symlink.
res
=
jailpy
(
res
=
jailpy
(
code
=
dedent
(
"""
\
code
=
"""
\
print open('copied/here.txt').read() # can read
print open('copied/here.txt').read() # can read
print open('copied/herelink.txt').read() # can read
print open('copied/herelink.txt').read() # can read
print open('copied/link.txt').read() # can't read
print open('copied/link.txt').read() # can't read
"""
)
,
"""
,
files
=
[
self
.
copied
],
files
=
[
self
.
copied
],
)
)
self
.
assertEqual
(
res
.
stdout
,
"012345
\n
012345
\n
"
)
self
.
assertEqual
(
res
.
stdout
,
"012345
\n
012345
\n
"
)
...
@@ -215,11 +215,11 @@ class TestSymlinks(JailCodeHelpers, unittest.TestCase):
...
@@ -215,11 +215,11 @@ class TestSymlinks(JailCodeHelpers, unittest.TestCase):
def
test_symlinks_wont_copy_data
(
self
):
def
test_symlinks_wont_copy_data
(
self
):
# Run some code in the sandbox, with a copied file which is a symlink.
# Run some code in the sandbox, with a copied file which is a symlink.
res
=
jailpy
(
res
=
jailpy
(
code
=
dedent
(
"""
\
code
=
"""
\
print open('here.txt').read() # can read
print open('here.txt').read() # can read
print open('herelink.txt').read() # can read
print open('herelink.txt').read() # can read
print open('link.txt').read() # can't read
print open('link.txt').read() # can't read
"""
)
,
"""
,
files
=
[
self
.
here_txt
,
self
.
herelink_txt
,
self
.
link_txt
],
files
=
[
self
.
here_txt
,
self
.
herelink_txt
,
self
.
link_txt
],
)
)
self
.
assertEqual
(
res
.
stdout
,
"012345
\n
012345
\n
"
)
self
.
assertEqual
(
res
.
stdout
,
"012345
\n
012345
\n
"
)
...
@@ -263,7 +263,7 @@ class TestChangingLimits(JailCodeHelpers, unittest.TestCase):
...
@@ -263,7 +263,7 @@ class TestChangingLimits(JailCodeHelpers, unittest.TestCase):
class
TestMalware
(
JailCodeHelpers
,
unittest
.
TestCase
):
class
TestMalware
(
JailCodeHelpers
,
unittest
.
TestCase
):
def
test_crash_cpython
(
self
):
def
test_crash_cpython
(
self
):
# http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
# http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
res
=
jailpy
(
code
=
dedent
(
"""
\
res
=
jailpy
(
code
=
"""
\
import new, sys
import new, sys
bad_code = new.code(0,0,0,0,"KABOOM",(),(),(),"","",0,"")
bad_code = new.code(0,0,0,0,"KABOOM",(),(),(),"","",0,"")
crash_me = new.function(bad_code, {})
crash_me = new.function(bad_code, {})
...
@@ -271,22 +271,22 @@ class TestMalware(JailCodeHelpers, unittest.TestCase):
...
@@ -271,22 +271,22 @@ class TestMalware(JailCodeHelpers, unittest.TestCase):
sys.stdout.flush()
sys.stdout.flush()
crash_me()
crash_me()
print "The afterlife!"
print "The afterlife!"
"""
)
)
"""
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertEqual
(
res
.
stdout
,
"Here we go...
\n
"
)
self
.
assertEqual
(
res
.
stdout
,
"Here we go...
\n
"
)
self
.
assertEqual
(
res
.
stderr
,
""
)
self
.
assertEqual
(
res
.
stderr
,
""
)
def
test_read_etc_passwd
(
self
):
def
test_read_etc_passwd
(
self
):
res
=
jailpy
(
code
=
dedent
(
"""
\
res
=
jailpy
(
code
=
"""
\
bytes = len(open('/etc/passwd').read())
bytes = len(open('/etc/passwd').read())
print 'Gotcha', bytes
print 'Gotcha', bytes
"""
)
)
"""
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertIn
(
"ermission denied"
,
res
.
stderr
)
self
.
assertIn
(
"ermission denied"
,
res
.
stderr
)
def
test_find_other_sandboxes
(
self
):
def
test_find_other_sandboxes
(
self
):
res
=
jailpy
(
code
=
dedent
(
"""
res
=
jailpy
(
code
=
"""
import os
import os
places = [
places = [
"..", "/tmp", "/", "/home", "/etc", "/var"
"..", "/tmp", "/", "/home", "/etc", "/var"
...
@@ -300,6 +300,6 @@ class TestMalware(JailCodeHelpers, unittest.TestCase):
...
@@ -300,6 +300,6 @@ class TestMalware(JailCodeHelpers, unittest.TestCase):
else:
else:
print "Files in
%
r:
%
r"
%
(place, files)
print "Files in
%
r:
%
r"
%
(place, files)
print "Done."
print "Done."
"""
)
)
"""
)
self
.
assertResultOk
(
res
)
self
.
assertResultOk
(
res
)
self
.
assertEqual
(
res
.
stdout
,
"Done.
\n
"
)
self
.
assertEqual
(
res
.
stdout
,
"Done.
\n
"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment