From 332b7063957fa486cdaffdadacdd6dae735121fd Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Mon, 22 Aug 2022 16:55:57 +0900 Subject: [PATCH] Run unit tests with wasm binary (#3) --- .DS_Store | Bin 0 -> 8196 bytes .github/workflows/ci.yaml | 6 +-- go.mod | 5 ++- go.sum | 10 +++-- main_test.go | 77 +++++++++++++++++++++++++------------- 5 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0be54499eccc3dfb50833dde10c172bbe86a4793 GIT binary patch literal 8196 zcmeHMU2GIp6h3EKV6ITPMfx*~&<(4AbqU)-D^QiC+w!B3Vz>XG%k0iTCroGR&g?FN z)kZ^%M$!25rY5}jKq6{l0*Oz?{}-c3h(;fb@?@gCXw)~)ojcpmLZ2{7!rbKEbI(2J z%$Sb=Z7q_lHjhz_8^QZl&8TIffV5W~D%7D$DVv zo2QcT<6GKO$@r#?ol{dxZAfhB>dzgqCmi=#xdsgP0jQg^rsrla$o9$^ zc_C5LSY6P!)dexOSQ{E18O?EC^2~nI)_tpN_UOLp<@i#M*0#IgI{S)dnbtI1aP88B zLz4P}ZWl_VY;L#htaZ%vMtt+AFM4C1Tl6!5;)q4IVl2m(3H!cD-*O#q$Sit989TG2 zVdd&Icc(hGZp-Z2v+qLPtxI|RGRDTpC5~?$vdrRO#WMWyQQa$;j$t{6hs%~{vEvDUG70oqKc|sj2<*i3cmj75V$Ep^M9WdzS_qv9d#o+I&ERZBx zG;P0-WQr!WhN=d`Yu9OPz@vq5JycAtXKdgIeM|1qD~K0wYiHV^t>;af8%&yY-ov!v zvRo~HFdXcqg zCHMt?hdNp;txQl^70yI}7dsaD(qW2XO*AT5JVdIbSRd65`FHs(>wDB(>vgwMvNR3nY6d zTdNY*`9jII#*!*U7gqw7ZHTooiXmPj+16MGQ#ndzGuz52ns}{b?_=AT2yhMoA8UBcYsy*0? zJ8>62fLVg-5Dw!g=CObV9zqu<39cTN2&_-yQ}{HVz?1kQ!S-bW?(298PfNhfz;JgJ zfRf?|vtd}y79HDl4%28AxU&r+?)p7YBOlQ$^8eoX@BgoVqUZ^c2Ot<8 literal 0 HcmV?d00001 diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a285948..812dbf7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -48,12 +48,12 @@ jobs: restore-keys: | ${{ runner.os }}-tinygo- - - name: Run unit tests - run: make test - - name: Build WASM filter run: make build + - name: Run unit tests + run: make test + - name: Run e2e tests working-directory: e2e run: docker-compose up --abort-on-container-exit diff --git a/go.mod b/go.mod index 32d39dc..e482b64 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/corazawaf/coraza/v3 v3.0.0-20220818013656-f749c07295aa github.com/stretchr/testify v1.7.1 - github.com/tetratelabs/proxy-wasm-go-sdk v0.18.1-0.20220510133519-6240ca761207 + github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c github.com/tidwall/gjson v1.14.2 ) @@ -16,9 +16,10 @@ require ( github.com/magefile/mage v1.13.0 // indirect github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/tetratelabs/wazero v0.0.0-20220819021810-7f8e629c653f // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 886624b..a3da00c 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/tetratelabs/proxy-wasm-go-sdk v0.18.1-0.20220510133519-6240ca761207 h1:2QpMF4ADtHe8QDgvDJVa04Bu3diiQ04iezw7AaSH9PU= -github.com/tetratelabs/proxy-wasm-go-sdk v0.18.1-0.20220510133519-6240ca761207/go.mod h1:Aq7ewLe7qv2pmzRgoYWk+5kYLj/oSYMJjvmPXpGSAbc= +github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c h1:OCUFXVGixHLfNjg6/QYEhv+jHJ5mRGhpEUVFv9eWPJE= +github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c/go.mod h1:5t/pWFNJ9eMyu/K/Z+OeGhDJ9sN9eCo8fc2pyM/Qjg4= +github.com/tetratelabs/wazero v0.0.0-20220819021810-7f8e629c653f h1:+InPNMTyR4bufxW+MzqigSOXpe9Ph++NOIz/N9wtEYs= +github.com/tetratelabs/wazero v0.0.0-20220819021810-7f8e629c653f/go.mod h1:CD5smBN5rGZo7UNe8aUiWyYE3bDWED/CQSonog9NSEg= github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo= github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= @@ -89,5 +91,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main_test.go b/main_test.go index 1bd3a0f..01d97fa 100644 --- a/main_test.go +++ b/main_test.go @@ -8,6 +8,8 @@ package main import ( + "os" + "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -16,52 +18,75 @@ import ( ) func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) { - type testCase struct { + tests := []struct { + name string path string expectedAction types.Action responded403 bool - } - - for name, tCase := range map[string]testCase{ - "not matching URL": { + }{ + { + name: "not matching URL", path: "/", expectedAction: types.ActionContinue, responded403: false, }, - "matching URL": { + { + name: "matching URL", path: "/admin", expectedAction: types.ActionContinue, responded403: true, }, - } { - t.Run(name, func(t *testing.T) { - opt := proxytest. - NewEmulatorOption(). - WithVMContext(&vmContext{}). - WithPluginConfiguration([]byte(` + } + + for _, runner := range []string{"go", "wasm"} { + t.Run(runner, func(t *testing.T) { + var vm types.VMContext + switch runner { + case "go": + vm = &vmContext{} + case "wasm": + wasm, err := os.ReadFile(filepath.Join("build", "main.wasm")) + if err != nil { + t.Skip("wasm not found") + } + v, err := proxytest.NewWasmVMContext(wasm) + require.NoError(t, err) + vm = v + } + + for _, tc := range tests { + tt := tc + + t.Run(tt.name, func(t *testing.T) { + opt := proxytest. + NewEmulatorOption(). + WithVMContext(vm). + WithPluginConfiguration([]byte(` { "rules" : "SecRuleEngine On\nSecRule REQUEST_URI \"@streq /admin\" \"id:101,phase:1,t:lowercase,deny\"" } `)) - host, reset := proxytest.NewHostEmulator(opt) - defer reset() + host, reset := proxytest.NewHostEmulator(opt) + defer reset() - require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin()) + require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin()) - // Initialize http context. - id := host.InitializeHttpContext() + // Initialize http context. + id := host.InitializeHttpContext() - // Call OnHttpRequestHeaders. - hs := [][2]string{{":path", tCase.path}, {":method", "GET"}} - action := host.CallOnRequestHeaders(id, hs, false) - require.Equal(t, tCase.expectedAction, action) + // Call OnHttpRequestHeaders. + hs := [][2]string{{":path", tt.path}, {":method", "GET"}} + action := host.CallOnRequestHeaders(id, hs, false) + require.Equal(t, tt.expectedAction, action) - // Call OnHttpStreamDone. - host.CompleteHttpContext(id) + // Call OnHttpStreamDone. + host.CompleteHttpContext(id) - if tCase.responded403 { - resp := host.GetSentLocalResponse(id) - require.EqualValues(t, 403, resp.StatusCode) + if tt.responded403 { + resp := host.GetSentLocalResponse(id) + require.EqualValues(t, 403, resp.StatusCode) + } + }) } }) }