Files
wazero/internal/gojs/crypto_test.go
Edoardo Vacchi 599c097603 Add -hostlogging=crypto (#1064)
This allows you to specify multiple logging scopes, both in API and the CLI.

e.g for the CLI.
```bash
$ wazero run --hostlogging=crypto --hostlogging=filesystem --mount=.:/:ro cat.wasm
```

e.g. for Go
```go
loggingCtx := context.WithValue(testCtx, experimental.FunctionListenerFactoryKey{},
	logging.NewHostLoggingListenerFactory(&log, logging.LogScopeCrypto|logging.LogScopeFilesystem))
```

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Adrian Cole <adrian@tetrate.io>
2023-01-28 12:51:44 +02:00

46 lines
1.2 KiB
Go

package gojs_test
import (
"bytes"
"context"
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/internal/testing/require"
)
func Test_crypto(t *testing.T) {
t.Parallel()
var log bytes.Buffer
loggingCtx := context.WithValue(testCtx, experimental.FunctionListenerFactoryKey{},
logging.NewHostLoggingListenerFactory(&log, logging.LogScopeCrypto))
stdout, stderr, err := compileAndRun(loggingCtx, "crypto", wazero.NewModuleConfig())
require.Zero(t, stderr)
require.EqualError(t, err, `module "" closed with exit_code(0)`)
require.Equal(t, `7a0c9f9f0d
`, stdout)
// TODO: Go 1.17 initializes randoms in a different order than Go 1.18,19
// When we move to 1.20, remove the workaround.
if log.String() != `==> go.runtime.getRandomData(r_len=32)
<==
==> go.runtime.getRandomData(r_len=8)
<==
==> go.syscall/js.valueCall(crypto.getRandomValues(r_len=5))
<== (n=5)
` {
require.Equal(t, `==> go.runtime.getRandomData(r_len=8)
<==
==> go.runtime.getRandomData(r_len=32)
<==
==> go.syscall/js.valueCall(crypto.getRandomValues(r_len=5))
<== (n=5)
`, log.String())
}
}