forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: avoids resolving target port if targetPortStr is empty. (envoy…
- Loading branch information
Showing
2 changed files
with
82 additions
and
9 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,73 @@ | ||
// Copyright The OWASP Coraza contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package wasmplugin | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/proxytest" | ||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
) | ||
|
||
func TestRetrieveAddressInfo(t *testing.T) { | ||
testCases := map[string]struct { | ||
address []byte | ||
port []byte | ||
expectedTargetIP string | ||
expectedPort int | ||
}{ | ||
"empty": { | ||
expectedTargetIP: "", | ||
expectedPort: 0, | ||
}, | ||
"127.0.0.1:8080": { | ||
address: []byte("127.0.0.10:8080"), | ||
expectedTargetIP: "127.0.0.10", | ||
expectedPort: 8080, | ||
}, | ||
"127.0.0.1:8080 with port": { | ||
address: []byte("127.0.0.11:8080"), | ||
port: []byte{5, 10, 0, 0, 0, 0, 0, 0}, // 256*10 + 5 | ||
expectedTargetIP: "127.0.0.11", | ||
expectedPort: 2565, | ||
}, | ||
} | ||
|
||
for _, target := range []string{"source", "destination"} { | ||
t.Run(target, func(t *testing.T) { | ||
for name, tCase := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
opt := proxytest. | ||
NewEmulatorOption(). | ||
WithVMContext(NewVMContext()) | ||
|
||
host, reset := proxytest.NewHostEmulator(opt) | ||
defer reset() | ||
|
||
require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin()) | ||
|
||
id := host.InitializeHttpContext() | ||
|
||
if len(tCase.address) > 0 { | ||
err := host.SetProperty([]string{target, "address"}, []byte(tCase.address)) | ||
require.NoError(t, err) | ||
} | ||
|
||
if len(tCase.port) > 0 { | ||
err := host.SetProperty([]string{target, "port"}, []byte(tCase.port)) | ||
require.NoError(t, err) | ||
} | ||
|
||
targetIP, port := retrieveAddressInfo(target) | ||
assert.Equal(t, tCase.expectedTargetIP, targetIP) | ||
assert.Equal(t, tCase.expectedPort, port) | ||
|
||
host.CompleteHttpContext(id) | ||
}) | ||
} | ||
}) | ||
} | ||
} |