wa-lang.org/wazero@v1.0.2/imports/proxywasm/internal/_abi_callback_configuration_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  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  
    22  	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
    23  )
    24  
    25  type testConfigurationVMContext struct {
    26  	types.DefaultPluginContext
    27  	onVMStartCalled bool
    28  }
    29  
    30  func (c *testConfigurationVMContext) OnVMStart(int) types.OnVMStartStatus {
    31  	c.onVMStartCalled = true
    32  	return true
    33  }
    34  
    35  func (c *testConfigurationVMContext) NewPluginContext(uint32) types.PluginContext {
    36  	return &testConfigurationPluginContext{}
    37  }
    38  
    39  type testConfigurationPluginContext struct {
    40  	types.DefaultPluginContext
    41  	onPluginStartCalled bool
    42  }
    43  
    44  func (c *testConfigurationPluginContext) OnPluginStart(int) types.OnPluginStartStatus {
    45  	c.onPluginStartCalled = true
    46  	return true
    47  }
    48  
    49  func Test_pluginInitialization(t *testing.T) {
    50  	currentStateMux.Lock()
    51  	defer currentStateMux.Unlock()
    52  
    53  	vmContext := &testConfigurationVMContext{}
    54  	currentState = &state{
    55  		vmContext:         vmContext,
    56  		pluginContexts:    map[uint32]*pluginContextState{},
    57  		contextIDToRootID: map[uint32]uint32{},
    58  	}
    59  
    60  	// Check ABI version. There is no return value so just make sure it doesn't panic.
    61  	proxyABIVersion()
    62  
    63  	// Call OnVMStart.
    64  	proxyOnVMStart(0, 0)
    65  	require.True(t, vmContext.onVMStartCalled)
    66  	require.Equal(t, uint32(0), currentState.activeContextID)
    67  
    68  	// Allocate memory
    69  	require.NotNil(t, proxyOnMemoryAllocate(100))
    70  
    71  	// Create plugin context.
    72  	pluginContextID := uint32(100)
    73  	proxyOnContextCreate(pluginContextID, 0)
    74  	require.Contains(t, currentState.pluginContexts, pluginContextID)
    75  	pluginContext := currentState.pluginContexts[pluginContextID].context.(*testConfigurationPluginContext)
    76  
    77  	// Call OnPluginStart.
    78  	proxyOnConfigure(pluginContextID, 0)
    79  	require.True(t, pluginContext.onPluginStartCalled)
    80  	require.Equal(t, pluginContextID, currentState.activeContextID)
    81  }