Smail/backend/config/ssl_policy_test.go
smail e05d1ff571
Some checks are pending
Security Checks / security (push) Waiting to run
统一证书热加载策略并记录内置续期验收边界
2026-09-18 15:08:37 +08:00

62 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package config
import (
"testing"
"time"
)
func TestSSLPolicyFollowsManagement(t *testing.T) {
for _, test := range []struct {
name string
cfg *Config
want string
}{
{name: "nil", want: SSLTypeUser},
{name: "empty", cfg: &Config{}, want: SSLTypeUser},
{name: "legacy_http", cfg: &Config{SSLType: SSLTypeAutoHTTP}, want: SSLTypeAutoHTTP},
{name: "legacy_dns", cfg: &Config{SSLType: SSLTypeAutoDNS}, want: SSLTypeAutoDNS},
{name: "legacy_manual", cfg: &Config{SSLType: SSLTypeUser}, want: SSLTypeUser},
{name: "legacy_upload", cfg: &Config{SSLType: SSLTypeAutoHTTP, SSL: SSLConfig{Mode: ManagedSSLModeUpload}}, want: SSLTypeUser},
{name: "legacy_default", cfg: &Config{SSLType: SSLTypeUser, SSL: SSLConfig{Mode: ManagedSSLModeDefault}}, want: SSLTypeAutoHTTP},
{name: "project_http", cfg: &Config{SSLType: SSLTypeAutoDNS, SSL: SSLConfig{Management: SSLManagementProject, Challenge: SSLChallengeHTTP}}, want: SSLTypeAutoHTTP},
{name: "project_dns", cfg: &Config{SSLType: SSLTypeUser, SSL: SSLConfig{Management: SSLManagementProject, Challenge: SSLChallengeDNS}}, want: SSLTypeAutoDNS},
{name: "project_transition", cfg: &Config{SSLType: SSLTypeUser, SSL: SSLConfig{Management: SSLManagementProject, Mode: ManagedSSLModeUpload}}, want: SSLTypeAutoHTTP},
{name: "project_legacy_dns", cfg: &Config{SSLType: SSLTypeAutoDNS, SSL: SSLConfig{Management: SSLManagementProject}}, want: SSLTypeAutoDNS},
{name: "custom_stale_auto", cfg: &Config{SSLType: SSLTypeAutoHTTP, SSL: SSLConfig{Management: SSLManagementCustom, Mode: ManagedSSLModeDefault}}, want: SSLTypeUser},
{name: "web_server_stale_auto", cfg: &Config{SSLType: SSLTypeAutoDNS, SSL: SSLConfig{Management: SSLManagementWebServer}}, want: SSLTypeUser},
{name: "proxy_project", cfg: &Config{TLSMode: " PROXY ", SSL: SSLConfig{Management: SSLManagementProject}}, want: SSLTypeUser},
{name: "invalid_management", cfg: &Config{SSLType: SSLTypeAutoHTTP, SSL: SSLConfig{Management: "invalid"}}, want: SSLTypeUser},
} {
t.Run(test.name, func(t *testing.T) {
if got := test.cfg.EffectiveSSLType(); got != test.want {
t.Fatalf("证书策略错误:got=%s want=%s", got, test.want)
}
if test.cfg.AutomaticSSLEnabled() != (test.want != SSLTypeUser) {
t.Fatal("自动续期开关与托管策略不一致")
}
})
}
}
func TestExternalCertificateReloadIgnoresLegacyDNSMode(t *testing.T) {
previousRoot := ROOT_PATH
ROOT_PATH = t.TempDir()
t.Cleanup(func() { ROOT_PATH = previousRoot })
key, cert := testManagedCertificate(t, "mail.example.com", time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour), 2048)
reference, err := ImportManagedSSL(ManagedSSLModeUpload, key, cert, nil, []string{"mail.example.com"})
if err != nil {
t.Fatal(err)
}
defer ReleaseStagedSSL(reference)
for _, management := range []string{SSLManagementCustom, SSLManagementWebServer} {
cfg := &Config{
Domain: "mail.example.com", WebDomain: "mail.example.com", SSLType: SSLTypeAutoDNS,
SSL: SSLConfig{Management: management, Mode: reference.Mode,
CertificateRef: reference.CertificateRef, PrivateKeyRef: reference.PrivateKeyRef},
}
if _, err := loadValidatedRuntimeCertificate(cfg); err != nil {
t.Fatalf("外部证书被旧 DNS 类型错误拒绝:management=%s err=%v", management, err)
}
}
}