def test_python_success(self): code = '''print("hello world")''' expected_out = "hello world" inp = "" pgm = Program(code, 'python3', self.sandbox) pgm.compile() out = pgm.execute(inp) assert expected_out == out.strip()
def test_network_access(self): code = '''import http conn = http.client.HTTPConnection('www.python.org') conn.request("HEAD","/index.html") ''' pgm = Program(code, 'python3', self.sandbox) pgm.compile() pgm.execute('')
def test_c_compilation_error(self): code = ''' #include<stdio.h> int main(){ printf("hello world") return 0; } ''' pgm = Program(code, 'C', self.sandbox) pgm.compile()
def test_file_access(self): code = '''f = open('../a.txt','w') f.write("hello world") f.close() ''' pgm = Program(code, 'python3', self.sandbox) pgm.compile() pgm.execute('') file_path = path.join(path.dirname(pgm.file_location), '../a.txt') if path.exists(file_path): with open(file_path, 'r') as fileObj: assert "hello world" != fileObj.read().strip() else: assert True
def test_c_error(self): code = ''' #include<stdio.h> int main(){ int a,b; scanf("%d",&b); a = 10/b; return 0; } ''' inp = "0" pgm = Program(code, 'C', self.sandbox) pgm.compile() pgm.execute(inp)
def test_c_success(self): code = ''' #include<stdio.h> int main(){ printf("hello world"); return 0; } ''' expected_out = "hello world" inp = "" pgm = Program(code, 'C', self.sandbox) pgm.compile() out = pgm.execute(inp) assert expected_out == out
#A CLI script to run the library from src.sandbox.no_sandbox import NoSandBox from src.Program import Program pgm = Program(''' #include<stdio.h> #include<stdlib.h> int main(){ char a[10]; scanf("%s",a); printf("hello %s",a); } ''','C',NoSandBox()) pgm.compile() print(pgm.execute('johnabraqw'))
def test_runtime_limit(self): code = '''while(True): pass''' pgm = Program(code, 'python3', self.sandbox) pgm.compile() pgm.execute('')
def test_memory_limit(self): code = '''a = [i for i in range(1024*1024*1024)]''' pgm = Program(code, 'python3', self.sandbox) pgm.compile() pgm.execute('')
def test_python_error(self): code = '''a = 1/0''' inp = "" pgm = Program(code, 'python3', self.sandbox) pgm.compile() pgm.execute(inp)