wa-lang.org/wazero@v1.0.2/imports/wasi_snapshot_preview1/random_test.go (about)

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