github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/wasm/binary/table_test.go (about)

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