wa-lang.org/wazero@v1.0.2/internal/wasm/binary/data_test.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"wa-lang.org/wazero/api"
     9  	"wa-lang.org/wazero/internal/testing/require"
    10  	"wa-lang.org/wazero/internal/wasm"
    11  )
    12  
    13  func Test_decodeDataSegment(t *testing.T) {
    14  	tests := []struct {
    15  		in       []byte
    16  		exp      *wasm.DataSegment
    17  		features api.CoreFeatures
    18  		expErr   string
    19  	}{
    20  		{
    21  			in: []byte{
    22  				0xf,
    23  				// Const expression.
    24  				wasm.OpcodeI32Const, 0x1, wasm.OpcodeEnd,
    25  				// Two initial data.
    26  				0x2, 0xf, 0xf,
    27  			},
    28  			features: api.CoreFeatureBulkMemoryOperations,
    29  			expErr:   "invalid data segment prefix: 0xf",
    30  		},
    31  		{
    32  			in: []byte{
    33  				0x0,
    34  				// Const expression.
    35  				wasm.OpcodeI32Const, 0x1, wasm.OpcodeEnd,
    36  				// Two initial data.
    37  				0x2, 0xf, 0xf,
    38  			},
    39  			exp: &wasm.DataSegment{
    40  				OffsetExpression: &wasm.ConstantExpression{
    41  					Opcode: wasm.OpcodeI32Const,
    42  					Data:   []byte{0x1},
    43  				},
    44  				Init: []byte{0xf, 0xf},
    45  			},
    46  			features: api.CoreFeatureBulkMemoryOperations,
    47  		},
    48  		{
    49  			in: []byte{
    50  				0x0,
    51  				// Const expression.
    52  				wasm.OpcodeI32Const, 0x1,
    53  				0x2, 0xf, 0xf,
    54  			},
    55  			expErr:   "read offset expression: constant expression has been not terminated",
    56  			features: api.CoreFeatureBulkMemoryOperations,
    57  		},
    58  		{
    59  			in: []byte{
    60  				0x1, // Passive data segment without memory index and const expr.
    61  				// Two initial data.
    62  				0x2, 0xf, 0xf,
    63  			},
    64  			exp: &wasm.DataSegment{
    65  				OffsetExpression: nil,
    66  				Init:             []byte{0xf, 0xf},
    67  			},
    68  			features: api.CoreFeatureBulkMemoryOperations,
    69  		},
    70  		{
    71  			in: []byte{
    72  				0x2,
    73  				0x0, // Memory index.
    74  				// Const expression.
    75  				wasm.OpcodeI32Const, 0x1, wasm.OpcodeEnd,
    76  				// Two initial data.
    77  				0x2, 0xf, 0xf,
    78  			},
    79  			exp: &wasm.DataSegment{
    80  				OffsetExpression: &wasm.ConstantExpression{
    81  					Opcode: wasm.OpcodeI32Const,
    82  					Data:   []byte{0x1},
    83  				},
    84  				Init: []byte{0xf, 0xf},
    85  			},
    86  			features: api.CoreFeatureBulkMemoryOperations,
    87  		},
    88  		{
    89  			in: []byte{
    90  				0x2,
    91  				0x1, // Memory index.
    92  				// Const expression.
    93  				wasm.OpcodeI32Const, 0x1, wasm.OpcodeEnd,
    94  				// Two initial data.
    95  				0x2, 0xf, 0xf,
    96  			},
    97  			expErr:   "memory index must be zero but was 1",
    98  			features: api.CoreFeatureBulkMemoryOperations,
    99  		},
   100  		{
   101  			in: []byte{
   102  				0x2,
   103  				0x0, // Memory index.
   104  				// Const expression.
   105  				wasm.OpcodeI32Const, 0x1,
   106  				// Two initial data.
   107  				0x2, 0xf, 0xf,
   108  			},
   109  			expErr:   "read offset expression: constant expression has been not terminated",
   110  			features: api.CoreFeatureBulkMemoryOperations,
   111  		},
   112  		{
   113  			in: []byte{
   114  				0x2,
   115  				0x0, // Memory index.
   116  				// Const expression.
   117  				wasm.OpcodeI32Const, 0x1, wasm.OpcodeEnd,
   118  				// Two initial data.
   119  				0x2, 0xf, 0xf,
   120  			},
   121  			features: api.CoreFeatureMutableGlobal,
   122  			expErr:   "non-zero prefix for data segment is invalid as feature \"bulk-memory-operations\" is disabled",
   123  		},
   124  	}
   125  
   126  	for i, tt := range tests {
   127  		tc := tt
   128  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   129  			actual, err := decodeDataSegment(bytes.NewReader(tc.in), tc.features)
   130  			if tc.expErr == "" {
   131  				require.NoError(t, err)
   132  				require.Equal(t, tc.exp, actual)
   133  			} else {
   134  				require.EqualError(t, err, tc.expErr)
   135  			}
   136  		})
   137  	}
   138  }