mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-02-19 07:50:43 +01:00
83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package tlsutil
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDialContextWithCache(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("listen: %v", err)
|
|
}
|
|
defer listener.Close()
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
conn, err := listener.Accept()
|
|
if err == nil {
|
|
conn.Close()
|
|
}
|
|
close(done)
|
|
}()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
|
|
conn, err := DialContextWithCache(ctx, "tcp", listener.Addr().String())
|
|
if err != nil {
|
|
t.Fatalf("DialContextWithCache error: %v", err)
|
|
}
|
|
conn.Close()
|
|
|
|
select {
|
|
case <-done:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("expected server accept")
|
|
}
|
|
}
|
|
|
|
func TestFetchFingerprint(t *testing.T) {
|
|
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer server.Close()
|
|
|
|
cert := server.TLS.Certificates[0]
|
|
if len(cert.Certificate) == 0 {
|
|
t.Fatal("expected server certificate")
|
|
}
|
|
|
|
sum := sha256.Sum256(cert.Certificate[0])
|
|
expected := hex.EncodeToString(sum[:])
|
|
|
|
fingerprint, err := FetchFingerprint(server.URL)
|
|
if err != nil {
|
|
t.Fatalf("FetchFingerprint error: %v", err)
|
|
}
|
|
if fingerprint != expected {
|
|
t.Fatalf("unexpected fingerprint: %s", fingerprint)
|
|
}
|
|
}
|
|
|
|
func TestFetchFingerprintInvalidURL(t *testing.T) {
|
|
_, err := FetchFingerprint("http://[::1")
|
|
if err == nil || !strings.Contains(err.Error(), "failed to parse host URL") {
|
|
t.Fatalf("expected parse error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestFetchFingerprintConnectionError(t *testing.T) {
|
|
_, err := FetchFingerprint("https://127.0.0.1:1")
|
|
if err == nil || !strings.Contains(err.Error(), "failed to connect") {
|
|
t.Fatalf("expected connection error, got %v", err)
|
|
}
|
|
}
|