43 lines
999 B
Go
43 lines
999 B
Go
package main
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLocalCertificateIdentityAndReuse(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if e := initCertificate("192.168.5.222", dir); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
c := filepath.Join(dir, "server.crt")
|
|
k := filepath.Join(dir, "server.key")
|
|
before, _ := os.ReadFile(c)
|
|
pair, e := tls.LoadX509KeyPair(c, k)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
cert, _ := x509.ParseCertificate(pair.Certificate[0])
|
|
for _, host := range []string{"192.168.5.222", "localhost", "127.0.0.1"} {
|
|
if e = cert.VerifyHostname(host); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
if cert.VerifyHostname("192.168.5.223") == nil {
|
|
t.Fatal("wrong IP trusted")
|
|
}
|
|
if e = initCertificate("192.168.5.222", dir); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
after, _ := os.ReadFile(c)
|
|
if string(before) != string(after) {
|
|
t.Fatal("certificate unexpectedly rotated")
|
|
}
|
|
if initCertificate("192.168.5.223", dir) == nil {
|
|
t.Fatal("existing mismatched certificate accepted")
|
|
}
|
|
}
|