|
| 1 | +/* |
| 2 | + * |
| 3 | + * xk6-browser - a browser automation extension for k6 |
| 4 | + * Copyright (C) 2021 Load Impact |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +package common |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "encoding/json" |
| 26 | + "fmt" |
| 27 | + "math" |
| 28 | + "testing" |
| 29 | + |
| 30 | + "github.com/chromedp/cdproto/cdp" |
| 31 | + "github.com/chromedp/cdproto/runtime" |
| 32 | + "github.com/dop251/goja" |
| 33 | + "github.com/sirupsen/logrus" |
| 34 | + "github.com/stretchr/testify/require" |
| 35 | +) |
| 36 | + |
| 37 | +func newExecCtx() (*ExecutionContext, context.Context, *goja.Runtime) { |
| 38 | + ctx := context.Background() |
| 39 | + logger := NewLogger(ctx, logrus.New(), false, nil) |
| 40 | + execCtx := NewExecutionContext(ctx, nil, nil, runtime.ExecutionContextID(123456789), logger) |
| 41 | + |
| 42 | + return execCtx, ctx, goja.New() |
| 43 | +} |
| 44 | + |
| 45 | +func TestConvertArgument(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + t.Run("int64", func(t *testing.T) { |
| 49 | + execCtx, ctx, rt := newExecCtx() |
| 50 | + var value int64 = 777 |
| 51 | + arg, _ := convertArgument(ctx, execCtx, rt.ToValue(value)) |
| 52 | + |
| 53 | + require.NotNil(t, arg) |
| 54 | + result, _ := json.Marshal(value) |
| 55 | + require.Equal(t, result, []byte(arg.Value)) |
| 56 | + require.Empty(t, arg.UnserializableValue) |
| 57 | + require.Empty(t, arg.ObjectID) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("int64 maxint", func(t *testing.T) { |
| 61 | + execCtx, ctx, rt := newExecCtx() |
| 62 | + |
| 63 | + var value int64 = math.MaxInt32 + 1 |
| 64 | + arg, _ := convertArgument(ctx, execCtx, rt.ToValue(value)) |
| 65 | + |
| 66 | + require.NotNil(t, arg) |
| 67 | + require.Equal(t, fmt.Sprintf("%dn", value), string(arg.UnserializableValue)) |
| 68 | + require.Empty(t, arg.Value) |
| 69 | + require.Empty(t, arg.ObjectID) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("float64", func(t *testing.T) { |
| 73 | + execCtx, ctx, rt := newExecCtx() |
| 74 | + |
| 75 | + var value float64 = 777.0 |
| 76 | + arg, _ := convertArgument(ctx, execCtx, rt.ToValue(value)) |
| 77 | + |
| 78 | + require.NotNil(t, arg) |
| 79 | + result, _ := json.Marshal(value) |
| 80 | + require.Equal(t, result, []byte(arg.Value)) |
| 81 | + require.Empty(t, arg.UnserializableValue) |
| 82 | + require.Empty(t, arg.ObjectID) |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("float64 unserializable values", func(t *testing.T) { |
| 86 | + execCtx, ctx, rt := newExecCtx() |
| 87 | + |
| 88 | + unserializableValues := []struct { |
| 89 | + value float64 |
| 90 | + expected string |
| 91 | + }{ |
| 92 | + { |
| 93 | + value: math.Float64frombits(0 | (1 << 63)), |
| 94 | + expected: "-0", |
| 95 | + }, |
| 96 | + { |
| 97 | + value: math.Inf(0), |
| 98 | + expected: "Infinity", |
| 99 | + }, |
| 100 | + { |
| 101 | + value: math.Inf(-1), |
| 102 | + expected: "-Infinity", |
| 103 | + }, |
| 104 | + { |
| 105 | + value: math.NaN(), |
| 106 | + expected: "NaN", |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + for _, v := range unserializableValues { |
| 111 | + arg, _ := convertArgument(ctx, execCtx, rt.ToValue(v.value)) |
| 112 | + require.NotNil(t, arg) |
| 113 | + require.Equal(t, v.expected, string(arg.UnserializableValue)) |
| 114 | + require.Empty(t, arg.Value) |
| 115 | + require.Empty(t, arg.ObjectID) |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + t.Run("bool", func(t *testing.T) { |
| 120 | + execCtx, ctx, rt := newExecCtx() |
| 121 | + |
| 122 | + var value bool = true |
| 123 | + arg, _ := convertArgument(ctx, execCtx, rt.ToValue(value)) |
| 124 | + |
| 125 | + require.NotNil(t, arg) |
| 126 | + result, _ := json.Marshal(value) |
| 127 | + require.Equal(t, result, []byte(arg.Value)) |
| 128 | + require.Empty(t, arg.UnserializableValue) |
| 129 | + require.Empty(t, arg.ObjectID) |
| 130 | + }) |
| 131 | + |
| 132 | + t.Run("string", func(t *testing.T) { |
| 133 | + execCtx, ctx, rt := newExecCtx() |
| 134 | + var value string = "hello world" |
| 135 | + arg, _ := convertArgument(ctx, execCtx, rt.ToValue(value)) |
| 136 | + |
| 137 | + require.NotNil(t, arg) |
| 138 | + result, _ := json.Marshal(value) |
| 139 | + require.Equal(t, result, []byte(arg.Value)) |
| 140 | + require.Empty(t, arg.UnserializableValue) |
| 141 | + require.Empty(t, arg.ObjectID) |
| 142 | + }) |
| 143 | + |
| 144 | + t.Run("*BaseJSHandle", func(t *testing.T) { |
| 145 | + execCtx, ctx, rt := newExecCtx() |
| 146 | + timeoutSetings := NewTimeoutSettings(nil) |
| 147 | + frameManager := NewFrameManager(ctx, nil, nil, timeoutSetings, NewLogger(ctx, NullLogger(), false, nil)) |
| 148 | + frame := NewFrame(ctx, frameManager, nil, cdp.FrameID("frame_id_0123456789")) |
| 149 | + remoteObjValue := "hellow world" |
| 150 | + result, _ := json.Marshal(remoteObjValue) |
| 151 | + remoteObject := &runtime.RemoteObject{ |
| 152 | + Type: "string", |
| 153 | + Value: result, |
| 154 | + } |
| 155 | + |
| 156 | + value := NewJSHandle(ctx, nil, execCtx, frame, remoteObject, execCtx.logger) |
| 157 | + arg, _ := convertArgument(ctx, execCtx, rt.ToValue(value)) |
| 158 | + |
| 159 | + require.NotNil(t, arg) |
| 160 | + require.Equal(t, result, []byte(arg.Value)) |
| 161 | + require.Empty(t, arg.UnserializableValue) |
| 162 | + require.Empty(t, arg.ObjectID) |
| 163 | + }) |
| 164 | + |
| 165 | + t.Run("*BaseJSHandle wrong context", func(t *testing.T) { |
| 166 | + execCtx, ctx, rt := newExecCtx() |
| 167 | + timeoutSetings := NewTimeoutSettings(nil) |
| 168 | + frameManager := NewFrameManager(ctx, nil, nil, timeoutSetings, NewLogger(ctx, NullLogger(), false, nil)) |
| 169 | + frame := NewFrame(ctx, frameManager, nil, cdp.FrameID("frame_id_0123456789")) |
| 170 | + remoteObjectID := runtime.RemoteObjectID("object_id_0123456789") |
| 171 | + remoteObject := &runtime.RemoteObject{ |
| 172 | + Type: "object", |
| 173 | + Subtype: "node", |
| 174 | + ObjectID: remoteObjectID, |
| 175 | + } |
| 176 | + execCtx2 := NewExecutionContext(ctx, nil, nil, runtime.ExecutionContextID(123456789), execCtx.logger) |
| 177 | + |
| 178 | + value := NewJSHandle(ctx, nil, execCtx2, frame, remoteObject, execCtx.logger) |
| 179 | + arg, err := convertArgument(ctx, execCtx, rt.ToValue(value)) |
| 180 | + |
| 181 | + require.Nil(t, arg) |
| 182 | + require.ErrorIs(t, ErrWrongExecutionContext, err) |
| 183 | + }) |
| 184 | + |
| 185 | + t.Run("*BaseJSHandle is disposed", func(t *testing.T) { |
| 186 | + execCtx, ctx, rt := newExecCtx() |
| 187 | + timeoutSetings := NewTimeoutSettings(nil) |
| 188 | + frameManager := NewFrameManager(ctx, nil, nil, timeoutSetings, NewLogger(ctx, NullLogger(), false, nil)) |
| 189 | + frame := NewFrame(ctx, frameManager, nil, cdp.FrameID("frame_id_0123456789")) |
| 190 | + remoteObjectID := runtime.RemoteObjectID("object_id_0123456789") |
| 191 | + remoteObject := &runtime.RemoteObject{ |
| 192 | + Type: "object", |
| 193 | + Subtype: "node", |
| 194 | + ObjectID: remoteObjectID, |
| 195 | + } |
| 196 | + |
| 197 | + value := NewJSHandle(ctx, nil, execCtx, frame, remoteObject, execCtx.logger) |
| 198 | + value.(*BaseJSHandle).disposed = true |
| 199 | + arg, err := convertArgument(ctx, execCtx, rt.ToValue(value)) |
| 200 | + |
| 201 | + require.Nil(t, arg) |
| 202 | + require.ErrorIs(t, ErrJSHandleDisposed, err) |
| 203 | + }) |
| 204 | + |
| 205 | + t.Run("*BaseJSHandle as *ElementHandle", func(t *testing.T) { |
| 206 | + execCtx, ctx, rt := newExecCtx() |
| 207 | + timeoutSetings := NewTimeoutSettings(nil) |
| 208 | + frameManager := NewFrameManager(ctx, nil, nil, timeoutSetings, NewLogger(ctx, NullLogger(), false, nil)) |
| 209 | + frame := NewFrame(ctx, frameManager, nil, cdp.FrameID("frame_id_0123456789")) |
| 210 | + remoteObjectID := runtime.RemoteObjectID("object_id_0123456789") |
| 211 | + remoteObject := &runtime.RemoteObject{ |
| 212 | + Type: "object", |
| 213 | + Subtype: "node", |
| 214 | + ObjectID: remoteObjectID, |
| 215 | + } |
| 216 | + |
| 217 | + value := NewJSHandle(ctx, nil, execCtx, frame, remoteObject, execCtx.logger) |
| 218 | + arg, _ := convertArgument(ctx, execCtx, rt.ToValue(value)) |
| 219 | + |
| 220 | + require.NotNil(t, arg) |
| 221 | + require.Equal(t, remoteObjectID, arg.ObjectID) |
| 222 | + require.Empty(t, arg.Value) |
| 223 | + require.Empty(t, arg.UnserializableValue) |
| 224 | + }) |
| 225 | +} |
0 commit comments