Documentation ¶
Overview ¶
Package testutil provides general utilities for protocols UTs.
Package testutil provides general utilities for protocols UTs.
Index ¶
Examples ¶
Constants ¶
const ( TLSDisabled = false TLSEnabled = true )
Constants to represent whether the connection should be encrypted with TLSEnabled.
Variables ¶
This section is empty.
Functions ¶
func GetShortTestName ¶
GetShortTestName generates a suffix in the form "<protocol>-<subtest name>" to be passed to WithPCAP.
func WithPCAP ¶
WithPCAP runs tcpdump for the duration of the test.
It returns an `io.Writer` to be used as a KeyLogWriter in tls.Config to be able, to decrypt the TLS traffic in the resulting PCAP. The resulting PCAPs and keylog files will be saved in `/tmp/test_pcaps`
Unless alwaysSave is true, WithPCAP will only save the resulting PCAP if the test fails.
Example (Plaintext) ¶
package main import ( "net/http" "testing" "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil" ) func main() { t := &testing.T{} // Run tcpdump alongside the test and only save the resulting // PCAP if the test failed. We don't need the keylog writer // here so we discard it. _ = testutil.WithPCAP(t, "80", testutil.GetShortTestName("HTTP", "simple request"), false) client := &http.Client{} resp, err := client.Get("http://httpbin.org/status/200") if err != nil { t.Errorf("error while making request") } if err = resp.Body.Close(); err != nil { t.Errorf("error while closing request body") } }
Output:
Example (Tls) ¶
package main import ( "crypto/tls" "net/http" "testing" "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil" ) func main() { t := &testing.T{} // Run tcpdump alongside the test and always save the resulting PCAP. klw := testutil.WithPCAP(t, "443", testutil.GetShortTestName("HTTP", "simple request - TLS"), true) client := &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, KeyLogWriter: klw, }, }, } resp, err := client.Get("https://httpbin.org/status/200") if err != nil { t.Errorf("error while making request") } if err = resp.Body.Close(); err != nil { t.Errorf("error while closing request body") } }
Output:
Types ¶
This section is empty.