This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## test framework for proxy-wasm-go-sdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters