github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/imports/wasi_snapshot_preview1/random_test.go (about) 1 package wasi_snapshot_preview1_test 2 3 import ( 4 "bytes" 5 "errors" 6 "io" 7 "testing" 8 "testing/iotest" 9 10 "github.com/tetratelabs/wazero" 11 "github.com/tetratelabs/wazero/internal/testing/require" 12 "github.com/tetratelabs/wazero/internal/wasip1" 13 ) 14 15 func Test_randomGet(t *testing.T) { 16 mod, r, log := requireProxyModule(t, wazero.NewModuleConfig()) 17 defer r.Close(testCtx) 18 19 expectedMemory := []byte{ 20 '?', // `offset` is after this 21 0x53, 0x8c, 0x7f, 0x96, 0xb1, // random data from seed value of 42 22 '?', // stopped after encoding 23 } 24 25 length := uint32(5) // arbitrary length, 26 offset := uint32(1) // offset, 27 28 maskMemory(t, mod, len(expectedMemory)) 29 30 // Invoke randomGet and check the memory side effects! 31 requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.RandomGetName, uint64(offset), uint64(length)) 32 require.Equal(t, ` 33 ==> wasi_snapshot_preview1.random_get(buf=1,buf_len=5) 34 <== errno=ESUCCESS 35 `, "\n"+log.String()) 36 37 actual, ok := mod.Memory().Read(0, offset+length+1) 38 require.True(t, ok) 39 require.Equal(t, expectedMemory, actual) 40 } 41 42 func Test_randomGet_Errors(t *testing.T) { 43 mod, r, log := requireProxyModule(t, wazero.NewModuleConfig()) 44 defer r.Close(testCtx) 45 46 memorySize := mod.Memory().Size() 47 48 tests := []struct { 49 name string 50 offset, length uint32 51 expectedLog string 52 }{ 53 { 54 name: "out-of-memory", 55 offset: memorySize, 56 length: 1, 57 expectedLog: ` 58 ==> wasi_snapshot_preview1.random_get(buf=65536,buf_len=1) 59 <== errno=EFAULT 60 `, 61 }, 62 { 63 name: "random length exceeds maximum valid address by 1", 64 offset: 0, // arbitrary valid offset 65 length: memorySize + 1, 66 expectedLog: ` 67 ==> wasi_snapshot_preview1.random_get(buf=0,buf_len=65537) 68 <== errno=EFAULT 69 `, 70 }, 71 } 72 73 for _, tt := range tests { 74 tc := tt 75 76 t.Run(tc.name, func(t *testing.T) { 77 defer log.Reset() 78 79 requireErrnoResult(t, wasip1.ErrnoFault, mod, wasip1.RandomGetName, uint64(tc.offset), uint64(tc.length)) 80 require.Equal(t, tc.expectedLog, "\n"+log.String()) 81 }) 82 } 83 } 84 85 func Test_randomGet_SourceError(t *testing.T) { 86 tests := []struct { 87 name string 88 randSource io.Reader 89 expectedLog string 90 }{ 91 { 92 name: "error", 93 randSource: iotest.ErrReader(errors.New("RandSource error")), 94 expectedLog: ` 95 ==> wasi_snapshot_preview1.random_get(buf=1,buf_len=5) 96 <== errno=EIO 97 `, 98 }, 99 { 100 name: "incomplete", 101 randSource: bytes.NewReader([]byte{1, 2}), 102 expectedLog: ` 103 ==> wasi_snapshot_preview1.random_get(buf=1,buf_len=5) 104 <== errno=EIO 105 `, 106 }, 107 } 108 109 for _, tt := range tests { 110 tc := tt 111 t.Run(tc.name, func(t *testing.T) { 112 mod, r, log := requireProxyModule(t, wazero.NewModuleConfig(). 113 WithRandSource(tc.randSource)) 114 defer r.Close(testCtx) 115 116 requireErrnoResult(t, wasip1.ErrnoIo, mod, wasip1.RandomGetName, uint64(1), uint64(5)) // arbitrary offset and length 117 require.Equal(t, tc.expectedLog, "\n"+log.String()) 118 }) 119 } 120 }