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

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     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 TestTableType(t *testing.T) {
    14  	zero := uint32(0)
    15  	max := wasm.MaximumFunctionIndex
    16  
    17  	tests := []struct {
    18  		name     string
    19  		input    *wasm.Table
    20  		expected []byte
    21  	}{
    22  		{
    23  			name:     "min 0 - funcref",
    24  			input:    &wasm.Table{Type: wasm.RefTypeFuncref},
    25  			expected: []byte{wasm.RefTypeFuncref, 0x0, 0},
    26  		},
    27  		{
    28  			name:     "min 0 - externref",
    29  			input:    &wasm.Table{Type: wasm.RefTypeExternref},
    30  			expected: []byte{wasm.RefTypeExternref, 0x0, 0},
    31  		},
    32  		{
    33  			name:     "min 0, max 0",
    34  			input:    &wasm.Table{Max: &zero, Type: wasm.RefTypeFuncref},
    35  			expected: []byte{wasm.RefTypeFuncref, 0x1, 0, 0},
    36  		},
    37  		{
    38  			name:     "min largest",
    39  			input:    &wasm.Table{Min: max, Type: wasm.RefTypeFuncref},
    40  			expected: []byte{wasm.RefTypeFuncref, 0x0, 0x80, 0x80, 0x80, 0x40},
    41  		},
    42  		{
    43  			name:     "min 0, max largest",
    44  			input:    &wasm.Table{Max: &max, Type: wasm.RefTypeFuncref},
    45  			expected: []byte{wasm.RefTypeFuncref, 0x1, 0, 0x80, 0x80, 0x80, 0x40},
    46  		},
    47  		{
    48  			name:     "min largest max largest",
    49  			input:    &wasm.Table{Min: max, Max: &max, Type: wasm.RefTypeFuncref},
    50  			expected: []byte{wasm.RefTypeFuncref, 0x1, 0x80, 0x80, 0x80, 0x40, 0x80, 0x80, 0x80, 0x40},
    51  		},
    52  	}
    53  
    54  	for _, tt := range tests {
    55  		tc := tt
    56  
    57  		b := encodeTable(tc.input)
    58  		t.Run(fmt.Sprintf("encode - %s", tc.name), func(t *testing.T) {
    59  			require.Equal(t, tc.expected, b)
    60  		})
    61  
    62  		t.Run(fmt.Sprintf("decode - %s", tc.name), func(t *testing.T) {
    63  			decoded, err := decodeTable(bytes.NewReader(b), api.CoreFeatureReferenceTypes)
    64  			require.NoError(t, err)
    65  			require.Equal(t, decoded, tc.input)
    66  		})
    67  	}
    68  }
    69  
    70  func TestDecodeTableType_Errors(t *testing.T) {
    71  	tests := []struct {
    72  		name        string
    73  		input       []byte
    74  		expectedErr string
    75  		features    api.CoreFeatures
    76  	}{
    77  		{
    78  			name:        "not func ref",
    79  			input:       []byte{0x50, 0x1, 0x80, 0x80, 0x4, 0},
    80  			expectedErr: "table type funcref is invalid: feature \"reference-types\" is disabled",
    81  		},
    82  		{
    83  			name:        "max < min",
    84  			input:       []byte{wasm.RefTypeFuncref, 0x1, 0x80, 0x80, 0x4, 0},
    85  			expectedErr: "table size minimum must not be greater than maximum",
    86  			features:    api.CoreFeatureReferenceTypes,
    87  		},
    88  		{
    89  			name:        "min > limit",
    90  			input:       []byte{wasm.RefTypeFuncref, 0x0, 0xff, 0xff, 0xff, 0xff, 0xf},
    91  			expectedErr: "table min must be at most 134217728",
    92  			features:    api.CoreFeatureReferenceTypes,
    93  		},
    94  	}
    95  
    96  	for _, tt := range tests {
    97  		tc := tt
    98  		t.Run(tc.name, func(t *testing.T) {
    99  			_, err := decodeTable(bytes.NewReader(tc.input), tc.features)
   100  			require.EqualError(t, err, tc.expectedErr)
   101  		})
   102  	}
   103  }