diff --git a/.github/workflows/tests-amd64.yml b/.github/workflows/tests-amd64.yml index e535151..ff3aec7 100644 --- a/.github/workflows/tests-amd64.yml +++ b/.github/workflows/tests-amd64.yml @@ -35,6 +35,14 @@ jobs: - name: Compile library run: bash ./build/build_amd64.sh + - name: Setup Nodejs20.11.1 + uses: actions/setup-node@v3 + with: + node-version: '20.11.1' + + - name: Link Nodejs + run: sudo ln -sf "$(which node)" /usr/local/bin/node + - name: Setup Python3.10 uses: actions/setup-python@v2 with: diff --git a/.github/workflows/tests-arm64.yml b/.github/workflows/tests-arm64.yml index 806d6b4..c46e395 100644 --- a/.github/workflows/tests-arm64.yml +++ b/.github/workflows/tests-arm64.yml @@ -35,6 +35,11 @@ jobs: - name: Compile library run: bash ./build/build_arm64.sh + - name: Setup Nodejs20.11.1 + uses: actions/setup-node@v3 + with: + node-version: '20.11.1' + - name: Setup Python3.10 run: sudo apt-get install -y python3.10 python3-pip diff --git a/tests/integration_tests/nodejs_feature_test.go b/tests/integration_tests/nodejs_feature_test.go new file mode 100644 index 0000000..705acad --- /dev/null +++ b/tests/integration_tests/nodejs_feature_test.go @@ -0,0 +1,50 @@ +package integrationtests_test + +import ( + "strings" + "testing" + + "github.com/langgenius/dify-sandbox/internal/core/runner/types" + "github.com/langgenius/dify-sandbox/internal/service" +) + +func TestNodejsBase64(t *testing.T) { + // Test case for base64 + resp := service.RunNodeJsCode(` +const base64 = Buffer.from("hello world").toString("base64"); +console.log(Buffer.from(base64, "base64").toString()); + `, "", &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 TestNodejsJSON(t *testing.T) { + // Test case for json + resp := service.RunNodeJsCode(` +console.log(JSON.stringify({"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) + } +} diff --git a/tests/integration_tests/nodejs_malicious_test.go b/tests/integration_tests/nodejs_malicious_test.go new file mode 100644 index 0000000..e4c9c8a --- /dev/null +++ b/tests/integration_tests/nodejs_malicious_test.go @@ -0,0 +1,36 @@ +package integrationtests_test + +import ( + "strings" + "testing" + + "github.com/langgenius/dify-sandbox/internal/core/runner/types" + "github.com/langgenius/dify-sandbox/internal/service" +) + +func TestNodejsRunCommand(t *testing.T) { + // Test case for run_command + resp := service.RunNodeJsCode(` +const { spawn } = require( 'child_process' ); +const ls = spawn( 'ls', [ '-lh', '/usr' ] ); + +ls.stdout.on( 'data', ( data ) => { + console.log(data); +} ); + +ls.stderr.on( 'data', ( data ) => { + console.log(data); +} ); + +ls.on( 'close', ( code ) => { + console.log(code); +} ); + `, "", &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) + } +} diff --git a/tests/integration_tests/ordinary_feature_test.go b/tests/integration_tests/python_feature_test.go similarity index 94% rename from tests/integration_tests/ordinary_feature_test.go rename to tests/integration_tests/python_feature_test.go index 3934c4c..66b4881 100644 --- a/tests/integration_tests/ordinary_feature_test.go +++ b/tests/integration_tests/python_feature_test.go @@ -8,7 +8,7 @@ import ( "github.com/langgenius/dify-sandbox/internal/service" ) -func TestBase64(t *testing.T) { +func TestPythonBase64(t *testing.T) { // Test case for base64 resp := service.RunPython3Code(` import base64 @@ -29,7 +29,7 @@ print(base64.b64decode(base64.b64encode(b"hello world")).decode()) } } -func TestJSON(t *testing.T) { +func TestPythonJSON(t *testing.T) { // Test case for json resp := service.RunPython3Code(` import json @@ -50,7 +50,7 @@ print(json.dumps({"hello": "world"})) } } -func TestHttp(t *testing.T) { +func TestPythonHttp(t *testing.T) { // Test case for http resp := service.RunPython3Code(` import requests diff --git a/tests/integration_tests/malicious_test.go b/tests/integration_tests/python_malicious_test.go similarity index 100% rename from tests/integration_tests/malicious_test.go rename to tests/integration_tests/python_malicious_test.go