wa-lang.org/wazero@v1.0.2/imports/proxywasm/internal/_abi_callback_l4_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 l4Context struct {
    26  	types.DefaultTcpContext
    27  	onDownstreamData,
    28  	onDownstreamClose,
    29  	onNewConnection,
    30  	onUpstreamData,
    31  	onUpstreamStreamClose bool
    32  }
    33  
    34  func (ctx *l4Context) OnDownstreamData(int, bool) types.Action {
    35  	ctx.onDownstreamData = true
    36  	return types.ActionContinue
    37  }
    38  
    39  func (ctx *l4Context) OnDownstreamClose(types.PeerType) { ctx.onDownstreamClose = true }
    40  
    41  func (ctx *l4Context) OnNewConnection() types.Action {
    42  	ctx.onNewConnection = true
    43  	return types.ActionContinue
    44  }
    45  
    46  func (ctx *l4Context) OnUpstreamData(int, bool) types.Action {
    47  	ctx.onUpstreamData = true
    48  	return types.ActionContinue
    49  }
    50  
    51  func (ctx *l4Context) OnUpstreamClose(types.PeerType) {
    52  	ctx.onUpstreamStreamClose = true
    53  }
    54  
    55  func Test_l4(t *testing.T) {
    56  	var cID uint32 = 100
    57  	currentStateMux.Lock()
    58  	defer currentStateMux.Unlock()
    59  
    60  	currentState = &state{tcpContexts: map[uint32]types.TcpContext{cID: &l4Context{}}}
    61  	ctx, ok := currentState.tcpContexts[cID].(*l4Context)
    62  	require.True(t, ok)
    63  
    64  	proxyOnNewConnection(cID)
    65  	require.True(t, ctx.onNewConnection)
    66  	proxyOnDownstreamData(cID, 0, false)
    67  	require.True(t, ctx.onDownstreamData)
    68  	proxyOnDownstreamConnectionClose(cID, 0)
    69  	require.True(t, ctx.onDownstreamClose)
    70  	proxyOnUpstreamData(cID, 0, false)
    71  	require.True(t, ctx.onUpstreamData)
    72  	proxyOnUpstreamConnectionClose(cID, 0)
    73  	require.True(t, ctx.onUpstreamStreamClose)
    74  }