testutil

package
v0.0.0-...-234771b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package testutil provides general utilities for protocols UTs.

Package testutil provides general utilities for protocols UTs.

Index

Examples

Constants

View Source
const (
	TLSDisabled = false
	TLSEnabled  = true
)

Constants to represent whether the connection should be encrypted with TLSEnabled.

Variables

This section is empty.

Functions

func GetShortTestName

func GetShortTestName(proto, subtest string) string

GetShortTestName generates a suffix in the form "<protocol>-<subtest name>" to be passed to WithPCAP.

func WithPCAP

func WithPCAP(t *testing.T, port string, suffix string, alwaysSave bool) io.Writer

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL