github.com/ethereum/go-ethereum@v1.16.1/internal/jsre/jsre_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package jsre 18 19 import ( 20 "os" 21 "path/filepath" 22 "reflect" 23 "testing" 24 "time" 25 26 "github.com/dop251/goja" 27 ) 28 29 type testNativeObjectBinding struct { 30 vm *goja.Runtime 31 } 32 33 type msg struct { 34 Msg string 35 } 36 37 func (no *testNativeObjectBinding) TestMethod(call goja.FunctionCall) goja.Value { 38 m := call.Argument(0).ToString().String() 39 return no.vm.ToValue(&msg{m}) 40 } 41 42 func newWithTestJS(t *testing.T, testjs string) *JSRE { 43 dir := t.TempDir() 44 if testjs != "" { 45 if err := os.WriteFile(filepath.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil { 46 t.Fatal("cannot create test.js:", err) 47 } 48 } 49 jsre := New(dir, os.Stdout) 50 return jsre 51 } 52 53 func TestExec(t *testing.T) { 54 t.Parallel() 55 56 jsre := newWithTestJS(t, `msg = "testMsg"`) 57 58 err := jsre.Exec("test.js") 59 if err != nil { 60 t.Errorf("expected no error, got %v", err) 61 } 62 val, err := jsre.Run("msg") 63 if err != nil { 64 t.Errorf("expected no error, got %v", err) 65 } 66 if val.ExportType().Kind() != reflect.String { 67 t.Errorf("expected string value, got %v", val) 68 } 69 exp := "testMsg" 70 got := val.ToString().String() 71 if exp != got { 72 t.Errorf("expected '%v', got '%v'", exp, got) 73 } 74 jsre.Stop(false) 75 } 76 77 func TestNatto(t *testing.T) { 78 t.Parallel() 79 80 jsre := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`) 81 82 err := jsre.Exec("test.js") 83 if err != nil { 84 t.Fatalf("expected no error, got %v", err) 85 } 86 time.Sleep(100 * time.Millisecond) 87 val, err := jsre.Run("msg") 88 if err != nil { 89 t.Fatalf("expected no error, got %v", err) 90 } 91 if val.ExportType().Kind() != reflect.String { 92 t.Fatalf("expected string value, got %v", val) 93 } 94 exp := "testMsg" 95 got := val.ToString().String() 96 if exp != got { 97 t.Fatalf("expected '%v', got '%v'", exp, got) 98 } 99 jsre.Stop(false) 100 } 101 102 func TestBind(t *testing.T) { 103 t.Parallel() 104 105 jsre := New("", os.Stdout) 106 defer jsre.Stop(false) 107 108 jsre.Set("no", &testNativeObjectBinding{vm: jsre.vm}) 109 110 _, err := jsre.Run(`no.TestMethod("testMsg")`) 111 if err != nil { 112 t.Errorf("expected no error, got %v", err) 113 } 114 } 115 116 func TestLoadScript(t *testing.T) { 117 t.Parallel() 118 119 jsre := newWithTestJS(t, `msg = "testMsg"`) 120 121 _, err := jsre.Run(`loadScript("test.js")`) 122 if err != nil { 123 t.Errorf("expected no error, got %v", err) 124 } 125 val, err := jsre.Run("msg") 126 if err != nil { 127 t.Errorf("expected no error, got %v", err) 128 } 129 if val.ExportType().Kind() != reflect.String { 130 t.Errorf("expected string value, got %v", val) 131 } 132 exp := "testMsg" 133 got := val.ToString().String() 134 if exp != got { 135 t.Errorf("expected '%v', got '%v'", exp, got) 136 } 137 jsre.Stop(false) 138 }