forked from quic-go/quic-go
GOPATH is a list of paths, similar to PATH. If someone does have a list set, the tests will try to use the full list as a path prefix to load the certificates, which won't work. But even handling GOPATH as a list does not solve the issue of what certificate or error_codes.go file to load, since the code might not be under the first one it finds. Instead, use the runtime functionality to get the filename of the path of the project at compilation time and perform the lookups relative to that, which guarantees that we're loading the files from the path of the code that is running.
45 lines
956 B
Go
45 lines
956 B
Go
package testdata
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"path"
|
|
"runtime"
|
|
)
|
|
|
|
var certPath string
|
|
|
|
func init() {
|
|
_, filename, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
panic("Failed to get current frame")
|
|
}
|
|
|
|
certPath = path.Join(path.Dir(path.Dir(filename)), "example")
|
|
}
|
|
|
|
// GetCertificatePaths returns the paths to 'fullchain.pem' and 'privkey.pem' for the
|
|
// quic.clemente.io cert.
|
|
func GetCertificatePaths() (string, string) {
|
|
return path.Join(certPath, "fullchain.pem"), path.Join(certPath, "privkey.pem")
|
|
}
|
|
|
|
// GetTLSConfig returns a tls config for quic.clemente.io
|
|
func GetTLSConfig() *tls.Config {
|
|
cert, err := tls.LoadX509KeyPair(GetCertificatePaths())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &tls.Config{
|
|
Certificates: []tls.Certificate{cert},
|
|
}
|
|
}
|
|
|
|
// GetCertificate returns a certificate for quic.clemente.io
|
|
func GetCertificate() tls.Certificate {
|
|
cert, err := tls.LoadX509KeyPair(GetCertificatePaths())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return cert
|
|
}
|