Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
wip: network filter test framwork
Browse files Browse the repository at this point in the history
  • Loading branch information
mathetake committed Sep 15, 2020
1 parent 7014d19 commit 44efc6e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
3 changes: 0 additions & 3 deletions examples/http_auth_random/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,17 @@ func (ctx *httpHeaders) OnHttpCallResponse(_ uint32, _ int, bodySize int, _ int)
b, err := proxywasm.HostCallGetHttpCallResponseBody(0, bodySize)
if err != nil {
proxywasm.LogCritical("failed to get response body: ", err.Error())
proxywasm.HostCallResumeHttpRequest()
return
}

s := fnv.New32a()
if _, err := s.Write(b); err != nil {
proxywasm.LogCritical("failed to calculate hash: ", err.Error())
proxywasm.HostCallResumeHttpRequest()
return
}

if s.Sum32()%2 == 0 {
proxywasm.LogInfo("access granted")
proxywasm.HostCallResumeHttpRequest()
return
}

Expand Down
1 change: 1 addition & 0 deletions proxytest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## test framework for proxy-wasm-go-sdk
33 changes: 33 additions & 0 deletions proxytest/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package proxytest

import (
"log"
"reflect"
"unsafe"

"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/rawhostcall"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)

type baseHost struct {
rawhostcall.DefaultProxyWAMSHost
logs [types.LogLevelMax][]string
}

func (b *baseHost) ProxyLog(logLevel types.LogLevel, messageData *byte, messageSize int) types.Status {
str := *(*string)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(messageData)),
Len: messageSize,
Cap: messageSize,
}))

b.logs[logLevel] = append(b.logs[logLevel], str)
return types.StatusOK
}

func (b *baseHost) GetLogs(level types.LogLevel) []string {
if level >= types.LogLevelMax {
log.Fatal("invalid log level", level)
}
return b.logs[level]
}
77 changes: 77 additions & 0 deletions proxytest/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package proxytest

import (
"log"

"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)

type NetworkFilterHost struct {
baseHost
newContext func(contextID uint32) proxywasm.StreamContext
streams map[uint32]streamState
}

type streamState struct {
upstream, downstream []byte
ctx proxywasm.StreamContext
}

func NewNetworkFilterHost(f func(contextID uint32) proxywasm.StreamContext) *NetworkFilterHost {
return &NetworkFilterHost{
newContext: f,
}
}

func (n *NetworkFilterHost) PutUpstreamData(contextID uint32, data []byte) {
stream := n.streams[contextID]
if len(data) > 0 {
stream.upstream = append(stream.upstream, data...)
}

action := stream.ctx.OnUpstreamData(len(stream.upstream), false)
switch action {
case types.ActionPause:
return
case types.ActionContinue:
// TODO: verify the behavior is correct
stream.upstream = []byte{}
default:
log.Fatal("invalid action type: ", action)
}
}

func (n *NetworkFilterHost) PutDownstreamData(contextID uint32, data []byte) {
stream := n.streams[contextID]
if len(data) > 0 {
stream.downstream = append(stream.downstream, data...)
}

action := stream.ctx.OnDownstreamData(len(stream.downstream), false)
switch action {
case types.ActionPause:
return
case types.ActionContinue:
// TODO: verify the behavior is correct
stream.downstream = []byte{}
default:
log.Fatal("invalid action type: ", action)
}
}

func (n *NetworkFilterHost) InitConnection() (contextID uint32) {
contextID = uint32(len(n.streams) + 1)
ctx := n.newContext(contextID)
n.streams[contextID] = streamState{ctx: ctx}
ctx.OnNewConnection()
return
}

func (n *NetworkFilterHost) CloseUpstreamConnection(contextID uint32) {
n.streams[contextID].ctx.OnUpstreamStreamClose(types.PeerTypeLocal) // peerType will be removed in the next ABI
}

func (n *NetworkFilterHost) CloseDownstreamConnection(contextID uint32) {
n.streams[contextID].ctx.OnUpstreamStreamClose(types.PeerTypeLocal) // peerType will be removed in the next ABI
}
2 changes: 2 additions & 0 deletions proxywasm/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type RootContext interface {
OnVMStart(vmConfigurationSize int) bool
}

// TODO: pass context id as the first argument
type StreamContext interface {
Context
OnDownstreamData(dataSize int, endOfStream bool) types.Action
Expand All @@ -41,6 +42,7 @@ type StreamContext interface {
OnUpstreamStreamClose(peerType types.PeerType)
}

// TODO: pass context id as the first argument
type HttpContext interface {
Context
OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action
Expand Down
1 change: 1 addition & 0 deletions proxywasm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
LogLevelWarn LogLevel = 3
LogLevelError LogLevel = 4
LogLevelCritical LogLevel = 5
LogLevelMax LogLevel = 6
)

type Status uint32
Expand Down

0 comments on commit 44efc6e

Please sign in to comment.