test: full config surface coverage; remove dead PeerUsesTLS helper

This commit is contained in:
2026-08-14 09:57:39 -04:00
parent 32eff914d5
commit f2a3e34f94
2 changed files with 97 additions and 9 deletions
-9
View File
@@ -172,15 +172,6 @@ func (c *Config) ListenAddr() string {
return fmt.Sprintf(":%d", c.Daemon.Port) return fmt.Sprintf(":%d", c.Daemon.Port)
} }
// PeerUsesTLS reports whether the given peer should be contacted over TLS.
// A per-peer override wins over the global security.tls setting.
func (c *Config) PeerUsesTLS(p PeerConfig) bool {
if p.TLS {
return true
}
return c.Security.TLS
}
// MaxBodyBytes returns the effective request body size limit, clamped to a // MaxBodyBytes returns the effective request body size limit, clamped to a
// sane minimum so a zero/negative config value never disables the limit. // sane minimum so a zero/negative config value never disables the limit.
func (c *Config) MaxBodyBytes() int64 { func (c *Config) MaxBodyBytes() int64 {
+97
View File
@@ -125,3 +125,100 @@ func TestListenAddr(t *testing.T) {
t.Errorf("ListenAddr = %q, want \":9137\"", addr) t.Errorf("ListenAddr = %q, want \":9137\"", addr)
} }
} }
func TestLoadFullConfig(t *testing.T) {
tmp := t.TempDir()
cfgDir := filepath.Join(tmp, ConfigDir)
if err := os.MkdirAll(cfgDir, 0755); err != nil {
t.Fatalf("MkdirAll: %v", err)
}
content := `
[daemon]
port = 9999
poll_interval_ms = 250
max_body_bytes = 2097152
history_size = 100
discovery = true
notify = true
sync_images = true
receive_files = true
receive_dir = "/tmp/received"
[security]
shared_key = "secret-key"
allowed_origins = ["host-a", "host-b"]
tls = true
cert_file = "/tmp/cert.pem"
key_file = "/tmp/key.pem"
insecure_skip_verify = true
[[peers]]
name = "bureau"
addr = "192.168.1.10:9137"
tls = true
`
if err := os.WriteFile(filepath.Join(cfgDir, ConfigFile), []byte(content), 0644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
t.Setenv("HOME", tmp)
t.Setenv("USERPROFILE", tmp)
cfg, err := Load()
if err != nil {
t.Fatalf("Load() error: %v", err)
}
if cfg.Daemon.MaxBodyBytes != 2097152 {
t.Errorf("max_body_bytes = %d, want 2097152", cfg.Daemon.MaxBodyBytes)
}
if cfg.Daemon.HistorySize != 100 {
t.Errorf("history_size = %d, want 100", cfg.Daemon.HistorySize)
}
if !cfg.Daemon.Discovery || !cfg.Daemon.Notify || !cfg.Daemon.SyncImages || !cfg.Daemon.ReceiveFiles {
t.Errorf("boolean flags not parsed: %+v", cfg.Daemon)
}
if cfg.Daemon.ReceiveDir != "/tmp/received" {
t.Errorf("receive_dir = %q", cfg.Daemon.ReceiveDir)
}
if cfg.Security.SharedKey != "secret-key" {
t.Errorf("shared_key = %q", cfg.Security.SharedKey)
}
if len(cfg.Security.AllowedOrigins) != 2 {
t.Errorf("allowed_origins = %v, want 2", cfg.Security.AllowedOrigins)
}
if !cfg.Security.TLS || !cfg.Security.InsecureSkipVerify {
t.Errorf("security flags not parsed: %+v", cfg.Security)
}
if len(cfg.Peers) != 1 || !cfg.Peers[0].TLS {
t.Errorf("peer TLS override not parsed: %+v", cfg.Peers)
}
}
func TestMaxBodyBytesDefault(t *testing.T) {
cfg := &Config{}
if got := cfg.MaxBodyBytes(); got != DefaultMaxBodyBytes {
t.Errorf("MaxBodyBytes default = %d, want %d", got, DefaultMaxBodyBytes)
}
cfg.Daemon.MaxBodyBytes = 100
if got := cfg.MaxBodyBytes(); got != 100 {
t.Errorf("MaxBodyBytes = %d, want 100", got)
}
}
func TestReceiveDirDefault(t *testing.T) {
tmp := t.TempDir()
t.Setenv("HOME", tmp)
t.Setenv("USERPROFILE", tmp)
cfg := &Config{}
got := cfg.ReceiveDir()
if got != filepath.Join(tmp, "Downloads", "clip-sync") {
t.Errorf("ReceiveDir default = %q, want %q", got, filepath.Join(tmp, "Downloads", "clip-sync"))
}
cfg.Daemon.ReceiveDir = "/custom"
if cfg.ReceiveDir() != "/custom" {
t.Errorf("ReceiveDir override = %q, want \"/custom\"", cfg.ReceiveDir())
}
}