Files
wazero/internal/platform/mmap_test.go
Crypt Keeper 507ce79080 Adds sys.Walltime and sys.Nanotime for security and determinism (#616)
This adds two clock interfaces: sys.Walltime and sys.Nanotime to
allow implementations to override readings for purposes of security or
determinism.

The default values of both are a fake timestamp, to avoid the sandbox
break we formerly had by returning the real time. This is similar to how
we don't inherit OS Env values.
2022-06-04 15:14:31 +08:00

54 lines
1.3 KiB
Go

package platform
import (
"crypto/rand"
"io"
"testing"
"github.com/tetratelabs/wazero/internal/testing/require"
)
var testCode, _ = io.ReadAll(io.LimitReader(rand.Reader, 8*1024))
func Test_MmapCodeSegment(t *testing.T) {
if !CompilerSupported() {
t.Skip()
}
newCode, err := MmapCodeSegment(testCode)
require.NoError(t, err)
// Verify that the mmap is the same as the original.
require.Equal(t, testCode, newCode)
// TODO: test newCode can executed.
t.Run("panic on zero length", func(t *testing.T) {
captured := require.CapturePanic(func() {
_, _ = MmapCodeSegment(make([]byte, 0))
})
require.EqualError(t, captured, "BUG: MmapCodeSegment with zero length")
})
}
func Test_MunmapCodeSegment(t *testing.T) {
if !CompilerSupported() {
t.Skip()
}
// Errors if never mapped
require.Error(t, MunmapCodeSegment(testCode))
newCode, err := MmapCodeSegment(testCode)
require.NoError(t, err)
// First munmap should succeed.
require.NoError(t, MunmapCodeSegment(newCode))
// Double munmap should fail.
require.Error(t, MunmapCodeSegment(newCode))
t.Run("panic on zero length", func(t *testing.T) {
captured := require.CapturePanic(func() {
_ = MunmapCodeSegment(make([]byte, 0))
})
require.EqualError(t, captured, "BUG: MunmapCodeSegment with zero length")
})
}