wa-lang.org/wazero@v1.0.2/imports/proxywasm/internal/_vmstate_test.go (about)

     1  // Copyright 2021 Tetrate
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package internal
    16  
    17  import (
    18  	"sync"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
    24  )
    25  
    26  var currentStateMux sync.Mutex
    27  
    28  type testSetVMContext struct {
    29  	cnt int
    30  }
    31  
    32  func (*testSetVMContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus {
    33  	return types.OnVMStartStatusOK
    34  }
    35  
    36  func (ctx *testSetVMContext) NewPluginContext(contextID uint32) types.PluginContext {
    37  	ctx.cnt++
    38  	return &types.DefaultPluginContext{}
    39  }
    40  
    41  func TestSetVMContext(t *testing.T) {
    42  	currentStateMux.Lock()
    43  	defer currentStateMux.Unlock()
    44  
    45  	vmContext := &testSetVMContext{}
    46  	SetVMContext(vmContext)
    47  	_ = currentState.vmContext.NewPluginContext(0)
    48  	require.Equal(t, 1, vmContext.cnt)
    49  }
    50  
    51  func TestState_createPluginContext(t *testing.T) {
    52  	s := &state{
    53  		pluginContexts:    map[uint32]*pluginContextState{},
    54  		contextIDToRootID: map[uint32]uint32{},
    55  		vmContext:         &testSetVMContext{},
    56  	}
    57  
    58  	var cid uint32 = 100
    59  	s.createPluginContext(cid)
    60  	require.NotNil(t, s.pluginContexts[cid])
    61  }
    62  
    63  type (
    64  	testStateVMContext     struct{}
    65  	testStatePluginContext struct{ types.DefaultPluginContext }
    66  	testStateTcpContext    struct {
    67  		contextID uint32
    68  		types.DefaultTcpContext
    69  	}
    70  	testStateHttpContext struct {
    71  		contextID uint32
    72  		types.DefaultHttpContext
    73  	}
    74  )
    75  
    76  func (*testStateVMContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus {
    77  	return types.OnVMStartStatusOK
    78  }
    79  
    80  func (ctx *testStateVMContext) NewPluginContext(contextID uint32) types.PluginContext {
    81  	return &testStatePluginContext{}
    82  }
    83  
    84  func (ctx *testStatePluginContext) NewTcpContext(contextID uint32) types.TcpContext {
    85  	return &testStateTcpContext{contextID: contextID}
    86  }
    87  
    88  func (ctx *testStatePluginContext) NewHttpContext(contextID uint32) types.HttpContext {
    89  	return &testStateHttpContext{contextID: contextID}
    90  }
    91  
    92  func TestState_createTcpContext(t *testing.T) {
    93  	s := &state{
    94  		pluginContexts:    map[uint32]*pluginContextState{},
    95  		tcpContexts:       map[uint32]types.TcpContext{},
    96  		vmContext:         &testStateVMContext{},
    97  		contextIDToRootID: map[uint32]uint32{},
    98  	}
    99  
   100  	var (
   101  		cid uint32 = 100
   102  		rid uint32 = 10
   103  	)
   104  	s.createPluginContext(rid)
   105  	s.createTcpContext(cid, rid)
   106  	c, ok := s.tcpContexts[cid]
   107  	require.True(t, ok)
   108  	ctx, ok := c.(*testStateTcpContext)
   109  	require.True(t, ok)
   110  	require.Equal(t, cid, ctx.contextID)
   111  }
   112  
   113  func TestState_createHttpContext(t *testing.T) {
   114  	s := &state{
   115  		pluginContexts:    map[uint32]*pluginContextState{},
   116  		httpContexts:      map[uint32]types.HttpContext{},
   117  		vmContext:         &testStateVMContext{},
   118  		contextIDToRootID: map[uint32]uint32{},
   119  	}
   120  
   121  	var (
   122  		cid uint32 = 100
   123  		rid uint32 = 10
   124  	)
   125  	s.createPluginContext(rid)
   126  	s.createHttpContext(cid, rid)
   127  	c, ok := s.httpContexts[cid]
   128  	require.True(t, ok)
   129  	ctx, ok := c.(*testStateHttpContext)
   130  	require.True(t, ok)
   131  	require.Equal(t, cid, ctx.contextID)
   132  }