mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
[root] - UPDATE documentation: enhanced README and TESTING guidelines - UPDATE dependencies: bump dependencies [config/components] - UPDATE mail component: apply update following changes in related package - UPDATE smtp component: apply update following changes in related package [mail] - MAJOR REFACTORING - REFACTOR package structure: reorganized into 4 specialized subpackages (queuer, render, sender, smtp) - ADD mail/queuer: mail queue management with counter, monitoring, and comprehensive tests - ADD mail/render: email template rendering with themes and direction handling (moved from mailer package) - ADD mail/sender: email composition and sending with attachments, priorities, and encoding - ADD mail/smtp: SMTP protocol handling with TLS modes and DSN support - ADD documentation: comprehensive README and TESTING for all subpackages - ADD tests: complete test suites with benchmarks, concurrency, and edge cases for all subpackages [mailer] - DEPRECATED - DELETE package: entire package merged into mail/render [mailPooler] - DEPRECATED - DELETE package: entire package merged into mail/queuer [smtp] - DEPRECATED - DELETE root package: entire package moved to mail/smtp - REFACTOR tlsmode: enhanced with encoding, formatting, and viper support (moved to mail/smtp/tlsmode) [size] - ADD documentation: comprehensive README - UPDATE interface: improved Size type methods - UPDATE encoding: enhanced marshaling support - UPDATE formatting: better unit handling and display - UPDATE parsing: improved error handling and validation [socket/server/unix] - ADD platform support: macOS-specific permission handling (perm_darwin.go) - ADD platform support: Linux-specific permission handling (perm_linux.go) - UPDATE listener: improved Unix socket and datagram listeners - UPDATE error handling: enhanced error messages for Unix sockets [socket/server/unixgram] - ADD platform support: macOS-specific permission handling (perm_darwin.go) - ADD platform support: Linux-specific permission handling (perm_linux.go) - UPDATE listener: improved Unix datagram listener - UPDATE error handling: enhanced error messages [socket/server/tcp] - UPDATE listener: improved TCP listener implementation
455 lines
12 KiB
Go
455 lines
12 KiB
Go
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2022 Nicolas JUHEL
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*
|
|
*/
|
|
|
|
package config_test
|
|
|
|
import (
|
|
smtpcfg "github.com/nabbar/golib/mail/smtp/config"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("SMTP Config Error Handling", func() {
|
|
|
|
Describe("Error Code Coverage", func() {
|
|
It("should return ErrorConfigInvalidDSN for empty DSN via Validate", func() {
|
|
model := newConfigModel("")
|
|
err := model.Validate()
|
|
// Validate() specifically checks for empty DSN
|
|
Expect(err).ToNot(BeNil())
|
|
// Error message is from parent validator error
|
|
Expect(err.Error()).To(ContainSubstring("validation error"))
|
|
})
|
|
|
|
It("should return error for DSN without slash", func() {
|
|
model := newConfigModel("tcp(localhost:25)")
|
|
_, err := model.Config()
|
|
// May auto-correct or error
|
|
_ = err
|
|
})
|
|
|
|
It("should return ErrorConfigInvalidNetwork for unclosed brace", func() {
|
|
model := newConfigModel("tcp(localhost:25")
|
|
_, err := model.Config()
|
|
Expect(err).ToNot(BeNil())
|
|
// Should mention network or brace issue
|
|
})
|
|
|
|
It("should handle extra closing brace", func() {
|
|
model := newConfigModel("tcp(localhost:25))/")
|
|
_, err := model.Config()
|
|
// May or may not error depending on parser
|
|
_ = err
|
|
})
|
|
|
|
It("should return error for malformed query parameters", func() {
|
|
// Using invalid URL encoding
|
|
model := newConfigModel("tcp(localhost:25)/?invalid=%%")
|
|
_, err := model.Config()
|
|
// Should error on invalid URL encoding
|
|
_ = err
|
|
})
|
|
|
|
It("should handle ErrorConfigInvalidHost", func() {
|
|
// DSN format that triggers host validation error
|
|
model := newConfigModel("invalid-format")
|
|
_, err := model.Config()
|
|
if err != nil {
|
|
// Should mention host or format issue
|
|
_ = err.Error()
|
|
}
|
|
})
|
|
})
|
|
|
|
Describe("Validation Error Scenarios", func() {
|
|
It("should fail validation with completely invalid DSN", func() {
|
|
model := newConfigModel("not-a-dsn-at-all")
|
|
err := model.Validate()
|
|
// Parser may handle any string and create config
|
|
// Validation depends on what Validate() checks
|
|
_ = err
|
|
})
|
|
|
|
It("should fail validation with missing critical components", func() {
|
|
model := newConfigModel("")
|
|
err := model.Validate()
|
|
// Empty DSN should fail validation
|
|
Expect(err).ToNot(BeNil())
|
|
})
|
|
|
|
It("should provide error details for validator failures via Validate", func() {
|
|
model := smtpcfg.ConfigModel{
|
|
DSN: "", // Invalid empty DSN
|
|
}
|
|
err := model.Validate()
|
|
// Validate() checks for empty DSN
|
|
Expect(err).ToNot(BeNil())
|
|
// Error message is from parent validator error
|
|
Expect(err.Error()).To(ContainSubstring("validation error"))
|
|
})
|
|
})
|
|
|
|
Describe("Config Creation Error Paths", func() {
|
|
It("should handle New with empty DSN", func() {
|
|
model := smtpcfg.ConfigModel{DSN: ""}
|
|
_, err := smtpcfg.New(model)
|
|
// Empty DSN may be auto-corrected
|
|
// Use Validate() to check properly
|
|
validationErr := model.Validate()
|
|
Expect(validationErr).ToNot(BeNil())
|
|
_ = err
|
|
})
|
|
|
|
It("should handle Config() with empty DSN", func() {
|
|
model := newConfigModel("")
|
|
_, err := model.Config()
|
|
// Parser may auto-correct, but Validate() should catch it
|
|
validationErr := model.Validate()
|
|
Expect(validationErr).ToNot(BeNil())
|
|
_ = err
|
|
})
|
|
|
|
It("should handle various malformed DSN formats", func() {
|
|
malformedDSNs := []string{
|
|
"",
|
|
"tcp(localhost:25", // Missing closing brace
|
|
"tcp(localhost:25))", // Extra closing brace
|
|
"tcp localhost:25)/", // Missing opening brace
|
|
"@tcp(localhost:25)/", // Empty username
|
|
"tcp(:)/", // Empty host and port
|
|
"tcp()/", // Empty address
|
|
}
|
|
|
|
for _, dsn := range malformedDSNs {
|
|
model := newConfigModel(dsn)
|
|
_, err := model.Config()
|
|
// Each should either error or handle gracefully
|
|
_ = err
|
|
}
|
|
})
|
|
})
|
|
|
|
Describe("Query Parameter Error Handling", func() {
|
|
It("should handle invalid URL encoding in query", func() {
|
|
model := newConfigModel("tcp(localhost:25)/?key=%ZZ")
|
|
_, err := model.Config()
|
|
// Should error on invalid hex encoding
|
|
_ = err
|
|
})
|
|
|
|
It("should handle malformed query string", func() {
|
|
model := newConfigModel("tcp(localhost:25)/?&&&")
|
|
cfg, err := model.Config()
|
|
// May ignore malformed params
|
|
if err == nil {
|
|
Expect(cfg).ToNot(BeNil())
|
|
}
|
|
})
|
|
|
|
It("should handle query with only keys no values", func() {
|
|
model := newConfigModel("tcp(localhost:25)/?key1&key2&key3")
|
|
cfg, err := model.Config()
|
|
// Should handle as empty values
|
|
if err == nil {
|
|
Expect(cfg).ToNot(BeNil())
|
|
}
|
|
})
|
|
})
|
|
|
|
Describe("Address Parsing Error Paths", func() {
|
|
It("should handle address without closing paren", func() {
|
|
model := newConfigModel("tcp(localhost:25/")
|
|
_, err := model.Config()
|
|
Expect(err).ToNot(BeNil())
|
|
})
|
|
|
|
It("should handle address with multiple colons", func() {
|
|
model := newConfigModel("tcp(host:25:extra)/")
|
|
cfg, err := model.Config()
|
|
// May parse as IPv6 or error
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
|
|
It("should handle port parsing errors", func() {
|
|
model := newConfigModel("tcp(localhost:notaport)/")
|
|
cfg, err := model.Config()
|
|
// Should handle non-numeric port
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
|
|
It("should handle IPv6 parsing edge cases", func() {
|
|
cases := []string{
|
|
"tcp6([:1]:25)/", // Malformed IPv6
|
|
"tcp6([::]:25)/", // Valid but minimal
|
|
"tcp6([::::]:25)/", // Too many colons
|
|
"tcp6(::1:25)/", // Missing brackets
|
|
}
|
|
|
|
for _, dsn := range cases {
|
|
model := newConfigModel(dsn)
|
|
_, err := model.Config()
|
|
// Each may parse differently
|
|
_ = err
|
|
}
|
|
})
|
|
})
|
|
|
|
Describe("User/Password Parsing Edge Cases", func() {
|
|
It("should handle multiple @ symbols", func() {
|
|
model := newConfigModel("user@domain:pass@tcp(localhost:25)/")
|
|
cfg, err := model.Config()
|
|
// Last @ is used for splitting
|
|
if err == nil {
|
|
_ = cfg.GetUser()
|
|
}
|
|
})
|
|
|
|
It("should handle multiple colons in credentials", func() {
|
|
model := newConfigModel("user:pass:word:extra@tcp(localhost:25)/")
|
|
cfg, err := model.Config()
|
|
// First colon splits user:pass
|
|
if err == nil {
|
|
Expect(cfg.GetUser()).To(Equal("user"))
|
|
}
|
|
})
|
|
|
|
It("should handle empty username with password", func() {
|
|
model := newConfigModel(":password@tcp(localhost:25)/")
|
|
cfg, err := model.Config()
|
|
if err == nil {
|
|
Expect(cfg.GetUser()).To(BeEmpty())
|
|
Expect(cfg.GetPass()).To(Equal("password"))
|
|
}
|
|
})
|
|
|
|
It("should handle @ without credentials", func() {
|
|
model := newConfigModel("@tcp(localhost:25)/")
|
|
cfg, err := model.Config()
|
|
if err == nil {
|
|
Expect(cfg.GetUser()).To(BeEmpty())
|
|
}
|
|
})
|
|
})
|
|
|
|
Describe("TLS Parameter Edge Cases", func() {
|
|
It("should handle invalid SkipVerify values", func() {
|
|
invalidValues := []string{
|
|
"notabool",
|
|
"yes",
|
|
"no",
|
|
"1",
|
|
"0",
|
|
"TRUE",
|
|
"FALSE",
|
|
}
|
|
|
|
for _, val := range invalidValues {
|
|
model := newConfigModel("tcp(localhost:25)/?SkipVerify=" + val)
|
|
cfg, err := model.Config()
|
|
// Should use default or parse some values
|
|
if err == nil {
|
|
_ = cfg.IsTLSSkipVerify()
|
|
}
|
|
}
|
|
})
|
|
|
|
It("should handle empty ServerName", func() {
|
|
model := newConfigModel("tcp(localhost:25)/?ServerName=")
|
|
cfg, err := model.Config()
|
|
if err == nil {
|
|
Expect(cfg.GetTlSServerName()).To(BeEmpty())
|
|
}
|
|
})
|
|
|
|
It("should handle very long ServerName", func() {
|
|
longName := ""
|
|
for i := 0; i < 100; i++ {
|
|
longName += "subdomain."
|
|
}
|
|
longName += "example.com"
|
|
|
|
model := newConfigModel("tcp(localhost:25)/?ServerName=" + longName)
|
|
cfg, err := model.Config()
|
|
if err == nil {
|
|
Expect(cfg.GetTlSServerName()).To(Equal(longName))
|
|
}
|
|
})
|
|
})
|
|
|
|
Describe("Protocol and TLS Mode Edge Cases", func() {
|
|
It("should handle unknown protocol", func() {
|
|
model := newConfigModel("udp(localhost:25)/")
|
|
cfg, err := model.Config()
|
|
// Should use default or unknown protocol
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
|
|
It("should handle empty protocol", func() {
|
|
model := newConfigModel("(localhost:25)/")
|
|
cfg, err := model.Config()
|
|
// May error or use default
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
|
|
It("should handle unknown TLS mode", func() {
|
|
model := newConfigModel("tcp(localhost:25)/unknowntlsmode")
|
|
cfg, err := model.Config()
|
|
if err == nil {
|
|
// Should default to TLSNone for unknown modes
|
|
_ = cfg.GetTlsMode()
|
|
}
|
|
})
|
|
|
|
It("should handle TLS mode with special characters", func() {
|
|
model := newConfigModel("tcp(localhost:25)/tls-strict")
|
|
cfg, err := model.Config()
|
|
// May parse or ignore
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
})
|
|
|
|
Describe("Boundary Value Testing", func() {
|
|
It("should handle minimum port (1)", func() {
|
|
model := newConfigModel("tcp(localhost:1)/")
|
|
cfg, err := model.Config()
|
|
if err == nil {
|
|
Expect(cfg.GetPort()).To(Equal(1))
|
|
}
|
|
})
|
|
|
|
It("should handle maximum port (65535)", func() {
|
|
model := newConfigModel("tcp(localhost:65535)/")
|
|
cfg, err := model.Config()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cfg.GetPort()).To(Equal(65535))
|
|
})
|
|
|
|
It("should handle port 65536 (out of range)", func() {
|
|
model := newConfigModel("tcp(localhost:65536)/")
|
|
cfg, err := model.Config()
|
|
// May error or truncate
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
|
|
It("should handle very long hostname", func() {
|
|
longHost := ""
|
|
for i := 0; i < 255; i++ {
|
|
longHost += "a"
|
|
}
|
|
|
|
model := newConfigModel("tcp(" + longHost + ":25)/")
|
|
cfg, err := model.Config()
|
|
if err == nil {
|
|
Expect(len(cfg.GetHost())).To(Equal(255))
|
|
}
|
|
})
|
|
|
|
It("should handle empty components", func() {
|
|
model := newConfigModel("tcp()/")
|
|
cfg, err := model.Config()
|
|
if err == nil {
|
|
Expect(cfg.GetHost()).To(BeEmpty())
|
|
Expect(cfg.GetPort()).To(Equal(0))
|
|
}
|
|
})
|
|
})
|
|
|
|
Describe("DSN Auto-Correction", func() {
|
|
It("should auto-add trailing slash when missing", func() {
|
|
model := newConfigModel("tcp(localhost:25)")
|
|
cfg, err := model.Config()
|
|
// Parser should auto-add slash
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
|
|
It("should handle DSN with query but no slash", func() {
|
|
model := newConfigModel("tcp(localhost:25)?param=value")
|
|
cfg, err := model.Config()
|
|
// Should insert slash before query
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
|
|
It("should handle multiple slashes", func() {
|
|
model := newConfigModel("tcp(localhost:25)///")
|
|
cfg, err := model.Config()
|
|
// Should handle extra slashes
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
})
|
|
|
|
Describe("Regression Tests", func() {
|
|
It("should maintain backward compatibility with old DSN formats", func() {
|
|
oldFormats := []string{
|
|
"tcp(localhost:25)/",
|
|
"user:pass@tcp(localhost:25)/",
|
|
"tcp(localhost:587)/starttls",
|
|
}
|
|
|
|
for _, dsn := range oldFormats {
|
|
model := newConfigModel(dsn)
|
|
cfg, err := model.Config()
|
|
Expect(err).ToNot(HaveOccurred(), "DSN: %s", dsn)
|
|
Expect(cfg).ToNot(BeNil(), "DSN: %s", dsn)
|
|
}
|
|
})
|
|
|
|
It("should handle whitespace in DSN", func() {
|
|
model := newConfigModel(" tcp(localhost:25)/ ")
|
|
cfg, err := model.Config()
|
|
// Should either trim or error
|
|
_ = cfg
|
|
_ = err
|
|
})
|
|
|
|
It("should handle case sensitivity in components", func() {
|
|
cases := []string{
|
|
"TCP(localhost:25)/",
|
|
"tcp(LOCALHOST:25)/",
|
|
"tcp(localhost:25)/TLS",
|
|
"tcp(localhost:25)/StartTLS",
|
|
}
|
|
|
|
for _, dsn := range cases {
|
|
model := newConfigModel(dsn)
|
|
cfg, err := model.Config()
|
|
// Should handle case variations
|
|
_ = cfg
|
|
_ = err
|
|
}
|
|
})
|
|
})
|
|
})
|