Skip to content

Commit

Permalink
fix: operation not permitted with python/amd64 (#8)
Browse files Browse the repository at this point in the history
* fix: amd64 sometimes operation not permitted

* fix: tests

* fix: tests

* fix: tests
  • Loading branch information
Yeuoly authored Jun 28, 2024
1 parent 05549d2 commit 0210cc2
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests-amd64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ jobs:
run: pip install httpx requests jinja2

- name: Run Intgeration tests
run: sudo go test -v github.com/langgenius/dify-sandbox/tests/integration_tests/...
run: sudo go test -timeout 120s -v ./tests/integration_tests/...
env:
PYTHON_PATH: /usr/bin/python3.10
2 changes: 1 addition & 1 deletion .github/workflows/tests-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ jobs:
run: sudo ln -s "$(which go)" /usr/local/bin/go

- name: Run Intgeration tests
run: sudo go test -v github.com/langgenius/dify-sandbox/tests/integration_tests/...
run: sudo go test -timeout 120s -v ./tests/integration_tests/...
env:
PYTHON_PATH: /usr/bin/python3.10
6 changes: 0 additions & 6 deletions cmd/test/fuzz_python_amd64/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ func main() {
if find_syscall(i, original) == -1 {
list[i] = append(list[i], i)
}

// for j := 217; j < 218; j++ {
// if find_syscall(j, list[i]) == -1 {
// list[i] = append(list[i], j)
// }
// }
}

lock := sync.Mutex{}
Expand Down
5 changes: 3 additions & 2 deletions cmd/test/fuzz_python_amd64/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def excepthook(type, value, tb):

# declare main function here
def main() -> dict:
import requests
print(requests.get("https://www.google.com").text)
return {
"message": [1, 2, 3]
}

from json import loads, dumps
from base64 import b64decode
Expand Down
1 change: 1 addition & 0 deletions internal/static/python_syscall/syscalls_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var ALLOW_SYSCALLS = []int{
syscall.SYS_EXIT, syscall.SYS_EXIT_GROUP,
syscall.SYS_TGKILL, syscall.SYS_RT_SIGACTION, syscall.SYS_IOCTL,
syscall.SYS_SCHED_YIELD,
syscall.SYS_SET_ROBUST_LIST, SYS_RSEQ,

// time
syscall.SYS_CLOCK_GETTIME, syscall.SYS_GETTIMEOFDAY, syscall.SYS_NANOSLEEP,
Expand Down
9 changes: 9 additions & 0 deletions tests/integration_tests/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package integrationtests_test

import "testing"

func runMultipleTestings(t *testing.T, iterations int, testFunc func(*testing.T)) {
for i := 0; i < iterations; i++ {
testFunc(t)
}
}
60 changes: 32 additions & 28 deletions tests/integration_tests/nodejs_feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,45 @@ import (

func TestNodejsBase64(t *testing.T) {
// Test case for base64
resp := service.RunNodeJsCode(`
runMultipleTestings(t, 30, func(t *testing.T) {
resp := service.RunNodeJsCode(`
const base64 = Buffer.from("hello world").toString("base64");
console.log(Buffer.from(base64, "base64").toString());
`, "", &types.RunnerOptions{
EnableNetwork: true,
`, "", &types.RunnerOptions{
EnableNetwork: true,
})
if resp.Code != 0 {
t.Fatal(resp)
}

if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}

if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
})
if resp.Code != 0 {
t.Error(resp)
}

if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}

if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
}

func TestNodejsJSON(t *testing.T) {
// Test case for json
resp := service.RunNodeJsCode(`
runMultipleTestings(t, 30, func(t *testing.T) {
resp := service.RunNodeJsCode(`
console.log(JSON.stringify({"hello": "world"}));
`, "", &types.RunnerOptions{
EnableNetwork: true,
`, "", &types.RunnerOptions{
EnableNetwork: true,
})
if resp.Code != 0 {
t.Error(resp)
}

if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello":"world"}`) {
t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}

if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
})
if resp.Code != 0 {
t.Error(resp)
}

if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello":"world"}`) {
t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}

if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
}
84 changes: 45 additions & 39 deletions tests/integration_tests/python_feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,69 @@ import (

func TestPythonBase64(t *testing.T) {
// Test case for base64
resp := service.RunPython3Code(`
runMultipleTestings(t, 50, func(t *testing.T) {
resp := service.RunPython3Code(`
import base64
print(base64.b64decode(base64.b64encode(b"hello world")).decode())
`, "", &types.RunnerOptions{
EnableNetwork: true,
})
if resp.Code != 0 {
t.Error(resp)
}
`, "", &types.RunnerOptions{
EnableNetwork: true,
})
if resp.Code != 0 {
t.Fatal(resp)
}

if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}
if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") {
t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}

if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
})
}

func TestPythonJSON(t *testing.T) {
// Test case for json
resp := service.RunPython3Code(`
runMultipleTestings(t, 50, func(t *testing.T) {
// Test case for json
resp := service.RunPython3Code(`
import json
print(json.dumps({"hello": "world"}))
`, "", &types.RunnerOptions{
EnableNetwork: true,
})
if resp.Code != 0 {
t.Error(resp)
}
`, "", &types.RunnerOptions{
EnableNetwork: true,
})
if resp.Code != 0 {
t.Fatal(resp)
}

if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello": "world"}`) {
t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}
if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello": "world"}`) {
t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}

if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
})
}

func TestPythonHttp(t *testing.T) {
// Test case for http
resp := service.RunPython3Code(`
runMultipleTestings(t, 10, func(t *testing.T) {
resp := service.RunPython3Code(`
import requests
print(requests.get("https://www.bilibili.com").content)
`, "", &types.RunnerOptions{
EnableNetwork: true,
})
if resp.Code != 0 {
t.Error(resp)
}
EnableNetwork: true,
})
if resp.Code != 0 {
t.Fatal(resp)
}

if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "bilibili") {
t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}
if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "bilibili") {
t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout)
}

if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
if resp.Data.(*service.RunCodeResponse).Stderr != "" {
t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr)
}
})
}

0 comments on commit 0210cc2

Please sign in to comment.