github.com/daeuniverse/quic-go@v0.0.0-20240413031024-943f218e0810/integrationtests/self/qlog_dir_test.go (about)

     1  package self_test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path"
     7  	"regexp"
     8  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	"github.com/daeuniverse/quic-go"
    12  	"github.com/daeuniverse/quic-go/qlog"
    13  )
    14  
    15  var _ = Describe("qlog dir tests", Serial, func() {
    16  	var originalQlogDirValue string
    17  	var tempTestDirPath string
    18  
    19  	BeforeEach(func() {
    20  		originalQlogDirValue = os.Getenv("QLOGDIR")
    21  		var err error
    22  		tempTestDirPath, err = os.MkdirTemp("", "temp_test_dir")
    23  		Expect(err).ToNot(HaveOccurred())
    24  	})
    25  
    26  	AfterEach(func() {
    27  		err := os.Setenv("QLOGDIR", originalQlogDirValue)
    28  		Expect(err).ToNot(HaveOccurred())
    29  		err = os.RemoveAll(tempTestDirPath)
    30  		Expect(err).ToNot(HaveOccurred())
    31  	})
    32  
    33  	handshake := func() {
    34  		serverStopped := make(chan struct{})
    35  		server, err := quic.ListenAddr(
    36  			"localhost:0",
    37  			getTLSConfig(),
    38  			&quic.Config{
    39  				Tracer: qlog.DefaultTracer,
    40  			},
    41  		)
    42  		Expect(err).ToNot(HaveOccurred())
    43  
    44  		go func() {
    45  			defer GinkgoRecover()
    46  			defer close(serverStopped)
    47  			for {
    48  				if _, err := server.Accept(context.Background()); err != nil {
    49  					return
    50  				}
    51  			}
    52  		}()
    53  
    54  		conn, err := quic.DialAddr(
    55  			context.Background(),
    56  			server.Addr().String(),
    57  			getTLSClientConfig(),
    58  			&quic.Config{
    59  				Tracer: qlog.DefaultTracer,
    60  			},
    61  		)
    62  		Expect(err).ToNot(HaveOccurred())
    63  		conn.CloseWithError(0, "")
    64  		server.Close()
    65  		<-serverStopped
    66  	}
    67  
    68  	It("environment variable is set", func() {
    69  		qlogDir := path.Join(tempTestDirPath, "qlogs")
    70  		err := os.Setenv("QLOGDIR", qlogDir)
    71  		Expect(err).ToNot(HaveOccurred())
    72  		handshake()
    73  		_, err = os.Stat(tempTestDirPath)
    74  		qlogDirCreated := !os.IsNotExist(err)
    75  		Expect(qlogDirCreated).To(BeTrue())
    76  		childs, err := os.ReadDir(qlogDir)
    77  		Expect(err).ToNot(HaveOccurred())
    78  		Expect(len(childs)).To(Equal(2))
    79  		odcids := make([]string, 0)
    80  		vantagePoints := make([]string, 0)
    81  		qlogFileNameRegexp := regexp.MustCompile(`^([0-f]+)_(client|server).qlog$`)
    82  		for _, child := range childs {
    83  			matches := qlogFileNameRegexp.FindStringSubmatch(child.Name())
    84  			odcids = append(odcids, matches[1])
    85  			vantagePoints = append(vantagePoints, matches[2])
    86  		}
    87  		Expect(odcids[0]).To(Equal(odcids[1]))
    88  		Expect(vantagePoints).To(ContainElements("client", "server"))
    89  	})
    90  })