59eb34976a
iotlcm-admin-svr:管理后台服务。 - Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件) - 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、 MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位; 私网 IP → 文档地址段;库名 xiujie_admin → iotlcm - 真实手机号/邮箱 → 合法 11 位编造号 / example.com - 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、 私有依赖 go get 行、明文 registry 登录口令 - 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名 - Dockerfile 改用公共基础镜像 - 拼错的 RADEME.md → README.md,README 重写 - 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用 - 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
145 lines
4.1 KiB
Go
145 lines
4.1 KiB
Go
package utility
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/textproto"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestExternalRequestTimeoutDefaults(t *testing.T) {
|
|
for _, value := range []string{"", "invalid", "0s", "-1s"} {
|
|
if got := ExternalRequestTimeout(value); got != DefaultExternalRequestTimeout {
|
|
t.Errorf("invalid timeout %q resolved to %v", value, got)
|
|
}
|
|
}
|
|
if got := ExternalRequestTimeout(" 30s "); got != 30*time.Second {
|
|
t.Fatalf("configured timeout ignored: %v", got)
|
|
}
|
|
}
|
|
|
|
func startSMTPTestServer(t *testing.T, handle func(net.Conn)) string {
|
|
t.Helper()
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
_ = conn.SetDeadline(time.Now().Add(2 * time.Second))
|
|
handle(conn)
|
|
}()
|
|
t.Cleanup(func() {
|
|
_ = listener.Close()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(3 * time.Second):
|
|
t.Error("SMTP server connection was not released")
|
|
}
|
|
})
|
|
return listener.Addr().String()
|
|
}
|
|
|
|
func TestSMTPSuccessAndRejectedRecipient(t *testing.T) {
|
|
for _, reject := range []bool{false, true} {
|
|
t.Run(fmt.Sprintf("reject=%t", reject), func(t *testing.T) {
|
|
address := startSMTPTestServer(t, func(conn net.Conn) {
|
|
peer := textproto.NewConn(conn)
|
|
_ = peer.PrintfLine("220 localhost ready")
|
|
for {
|
|
line, err := peer.ReadLine()
|
|
if err != nil {
|
|
return
|
|
}
|
|
switch {
|
|
case strings.HasPrefix(line, "EHLO"), strings.HasPrefix(line, "HELO"), strings.HasPrefix(line, "MAIL"):
|
|
_ = peer.PrintfLine("250 OK")
|
|
case strings.HasPrefix(line, "RCPT"):
|
|
if reject {
|
|
_ = peer.PrintfLine("550 recipient rejected")
|
|
return
|
|
}
|
|
_ = peer.PrintfLine("250 OK")
|
|
case line == "DATA":
|
|
_ = peer.PrintfLine("354 send message")
|
|
if _, err := peer.ReadDotBytes(); err != nil {
|
|
return
|
|
}
|
|
_ = peer.PrintfLine("250 accepted")
|
|
case line == "QUIT":
|
|
_ = peer.PrintfLine("221 bye")
|
|
return
|
|
}
|
|
}
|
|
})
|
|
err := SendSMTP(context.Background(), SMTPOptions{
|
|
Address: address, Host: "localhost", From: "sender@example.com",
|
|
To: []string{"recipient@example.com"}, Data: []byte("Subject: test\r\n\r\nmessage\r\n"), Timeout: time.Second,
|
|
})
|
|
if (err != nil) != reject {
|
|
t.Fatalf("unexpected SMTP result: rejected=%t error=%v", reject, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSMTPTimeoutInterruptsGreetingTLSAndProtocol(t *testing.T) {
|
|
for _, scenario := range []string{"greeting", "tls", "mail", "starttls"} {
|
|
t.Run(scenario, func(t *testing.T) {
|
|
address := startSMTPTestServer(t, func(conn net.Conn) {
|
|
if scenario == "greeting" || scenario == "tls" {
|
|
_, _ = io.Copy(io.Discard, conn)
|
|
return
|
|
}
|
|
peer := textproto.NewConn(conn)
|
|
_ = peer.PrintfLine("220 localhost ready")
|
|
if _, err := peer.ReadLine(); err != nil {
|
|
return
|
|
}
|
|
if scenario == "starttls" {
|
|
_ = peer.PrintfLine("250-localhost")
|
|
_ = peer.PrintfLine("250 STARTTLS")
|
|
if _, err := peer.ReadLine(); err != nil {
|
|
return
|
|
}
|
|
_ = peer.PrintfLine("220 start TLS")
|
|
} else {
|
|
_ = peer.PrintfLine("250 OK")
|
|
}
|
|
_, _ = io.Copy(io.Discard, conn)
|
|
})
|
|
started := time.Now()
|
|
err := SendSMTP(context.Background(), SMTPOptions{
|
|
Address: address, Host: "localhost", SSL: scenario == "tls", From: "sender@example.com",
|
|
To: []string{"recipient@example.com"}, Data: []byte("message"), Timeout: 70 * time.Millisecond,
|
|
})
|
|
if err == nil || time.Since(started) > time.Second {
|
|
t.Fatalf("SMTP stall was not interrupted: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSMTPHonorsEarlierParentDeadline(t *testing.T) {
|
|
address := startSMTPTestServer(t, func(conn net.Conn) {
|
|
_, _ = io.Copy(io.Discard, conn)
|
|
})
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
defer cancel()
|
|
started := time.Now()
|
|
err := SendSMTP(ctx, SMTPOptions{Address: address, Host: "localhost", Timeout: 10 * time.Second})
|
|
if err == nil || time.Since(started) > time.Second {
|
|
t.Fatalf("parent deadline was not honored: %v", err)
|
|
}
|
|
}
|