@@ -2,8 +2,16 @@ package server
22
33import (
44 "context"
5+ "crypto/rand"
6+ "crypto/rsa"
7+ "crypto/x509"
8+ "crypto/x509/pkix"
9+ "encoding/pem"
510 "log/slog"
11+ "math/big"
612 "net"
13+ "os"
14+ "path/filepath"
715 "testing"
816 "time"
917
@@ -936,3 +944,64 @@ func (c *countingClusterHooks) OnAgentHeartbeat(ctx context.Context, agentID str
936944 c .heartbeats ++
937945}
938946func (c * countingClusterHooks ) OnAgentDisconnected (ctx context.Context , agentID string ) {}
947+
948+ // listenTCP TLS 分支:cert/key 正常加载、坏路径/坏 CA 报错(此前 63.2%)。
949+ func TestListenTCP_TLSBranches (t * testing.T ) {
950+ dir := t .TempDir ()
951+ certPath := filepath .Join (dir , "cert.pem" )
952+ keyPath := filepath .Join (dir , "key.pem" )
953+ caPath := filepath .Join (dir , "ca.pem" )
954+
955+ // 生成自签证书(crypto/x509 标准库,无外部依赖)
956+ priv , err := rsa .GenerateKey (rand .Reader , 2048 )
957+ require .NoError (t , err )
958+ tmpl := x509.Certificate {
959+ SerialNumber : big .NewInt (1 ),
960+ Subject : pkix.Name {CommonName : "localhost" },
961+ NotBefore : time .Now ().Add (- time .Hour ),
962+ NotAfter : time .Now ().Add (time .Hour ),
963+ IsCA : true ,
964+ KeyUsage : x509 .KeyUsageKeyEncipherment | x509 .KeyUsageDigitalSignature | x509 .KeyUsageCertSign ,
965+ ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageServerAuth },
966+ }
967+ der , err := x509 .CreateCertificate (rand .Reader , & tmpl , & tmpl , & priv .PublicKey , priv )
968+ require .NoError (t , err )
969+ certPEM := pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : der })
970+ keyPEM := pem .EncodeToMemory (& pem.Block {Type : "RSA PRIVATE KEY" , Bytes : x509 .MarshalPKCS1PrivateKey (priv )})
971+ require .NoError (t , os .WriteFile (certPath , certPEM , 0o600 ))
972+ require .NoError (t , os .WriteFile (keyPath , keyPEM , 0o600 ))
973+ require .NoError (t , os .WriteFile (caPath , certPEM , 0o600 ))
974+
975+ // cert+key+CA 齐备:TLS listener 建立成功
976+ ln , err := listenTCP (& TCPListenerConfig {
977+ Address : "127.0.0.1:0" ,
978+ Insecure : false ,
979+ CertFile : certPath , KeyFile : keyPath , CAFile : caPath ,
980+ })
981+ require .NoError (t , err )
982+ ln .Close ()
983+
984+ // 坏证书路径 → load server certificate 错误
985+ _ , err = listenTCP (& TCPListenerConfig {
986+ Address : "127.0.0.1:0" , Insecure : false ,
987+ CertFile : filepath .Join (dir , "missing.pem" ), KeyFile : keyPath ,
988+ })
989+ require .ErrorContains (t , err , "load server certificate" )
990+
991+ // 坏 CA 文件路径 → read CA file 错误
992+ _ , err = listenTCP (& TCPListenerConfig {
993+ Address : "127.0.0.1:0" , Insecure : false ,
994+ CertFile : certPath , KeyFile : keyPath ,
995+ CAFile : filepath .Join (dir , "missing-ca.pem" ),
996+ })
997+ require .ErrorContains (t , err , "read CA file" )
998+
999+ // 非 PEM 内容 → append CA certificate 错误
1000+ badCA := filepath .Join (dir , "bad-ca.pem" )
1001+ require .NoError (t , os .WriteFile (badCA , []byte ("not a pem" ), 0o600 ))
1002+ _ , err = listenTCP (& TCPListenerConfig {
1003+ Address : "127.0.0.1:0" , Insecure : false ,
1004+ CertFile : certPath , KeyFile : keyPath , CAFile : badCA ,
1005+ })
1006+ require .ErrorContains (t , err , "append CA certificate" )
1007+ }
0 commit comments