Browse Source

feat: tests

Yeuoly 1 year ago
parent
commit
b3834c9465

+ 2 - 4
cmd/test/fuzz_python_amd64/test.py

@@ -31,10 +31,8 @@ lib.DifySeccomp(65537, 1001, 1)
 
 
 # declare main function here
 # declare main function here
 def main() -> dict:
 def main() -> dict:
-    import requests
-    return {
-        "result": "awdawd",
-    }
+    import json
+    print(json.dumps({"hello": "world"}))
 
 
 from json import loads, dumps
 from json import loads, dumps
 from base64 import b64decode
 from base64 import b64decode

+ 1 - 2
cmd/test/python/main.go

@@ -12,8 +12,7 @@ import (
 func main() {
 func main() {
 	static.InitConfig("conf/config.yaml")
 	static.InitConfig("conf/config.yaml")
 	python.PreparePythonDependenciesEnv()
 	python.PreparePythonDependenciesEnv()
-	resp := service.RunPython3Code(`import httpx
-print(httpx.get("https://www.bilibili.com").text)`,
+	resp := service.RunPython3Code(`import json;print(json.dumps({"hello": "world"}))`,
 		``,
 		``,
 		&types.RunnerOptions{
 		&types.RunnerOptions{
 			EnableNetwork: true,
 			EnableNetwork: true,

+ 30 - 0
tests/integration_tests/malicious_test.go

@@ -23,3 +23,33 @@ os.fork()
 		t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
 		t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
 	}
 	}
 }
 }
+
+func TestExec(t *testing.T) {
+	// Test case for exec
+	resp := service.RunPython3Code(`
+import os
+os.execl("/bin/ls", "ls")
+	`, "", &types.RunnerOptions{})
+	if resp.Code != 0 {
+		t.Error(resp)
+	}
+
+	if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
+		t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
+	}
+}
+
+func TestRunCommand(t *testing.T) {
+	// Test case for run_command
+	resp := service.RunPython3Code(`
+import subprocess
+subprocess.run(["ls", "-l"])
+	`, "", &types.RunnerOptions{})
+	if resp.Code != 0 {
+		t.Error(resp)
+	}
+
+	if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
+		t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
+	}
+}

+ 63 - 2
tests/integration_tests/ordinary_feature_test.go

@@ -1,11 +1,72 @@
 package integrationtests_test
 package integrationtests_test
 
 
 import (
 import (
+	"strings"
 	"testing"
 	"testing"
 
 
-	"github.com/langgenius/dify-sandbox/internal/static"
+	"github.com/langgenius/dify-sandbox/internal/core/runner/types"
+	"github.com/langgenius/dify-sandbox/internal/service"
 )
 )
 
 
 func TestBase64(t *testing.T) {
 func TestBase64(t *testing.T) {
-	static.InitConfig("conf/config.yaml")
+	// Test case for base64
+	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)
+	}
+
+	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 TestJSON(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)
+	}
+
+	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 TestHttp(t *testing.T) {
+	// Test case for http
+	resp := service.RunPython3Code(`
+import requests
+print(requests.get("https://www.bilibili.com").content)
+	`, "", &types.RunnerOptions{
+		EnableNetwork: true,
+	})
+	if resp.Code != 0 {
+		t.Error(resp)
+	}
+
+	if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "bilibili") {
+		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)
+	}
 }
 }