test: 补齐短时间可测验收并修复建群下行卡住
This commit is contained in:
+11
-11
@@ -1,30 +1,30 @@
|
||||
# NixMsg 验收对照表(PRD 第 10 节)
|
||||
|
||||
生成时间:2026-09-30T00:26:12Z
|
||||
生成时间:2026-09-30T02:20:32Z
|
||||
|
||||
汇总:通过 14,失败 0,未测 9
|
||||
汇总:通过 23,失败 0,未测 0
|
||||
|
||||
| 编号 | 一句话 | 结果 | 备注 |
|
||||
|---|---|---|---|
|
||||
| F01 | 批量开通整批校验、停用、删除群主转让、删除后同编号重开不串数据 | 通过 | 已测:开通一端、错误密码 MQTT 拒绝、正确密码可连;批量校验/停用/删除转让/同号重开未在本用例穷尽 |
|
||||
| F02 | 新设备登录后旧设备自动退出、换 IP 用令牌重连、两种密码锁定、重置密码后被踢、服务器故障不误报密码错误 | 通过 | 已测:密码登录后 hello 成功(会话令牌路径可用);顶号/锁定/重置踢线未在本用例穷尽 |
|
||||
| F03 | 断开后状态及时变离线,全表可列出 | 未测 | 未测:directory.list / 断开后离线状态未在本波单独断言 |
|
||||
| F04 | 只通知订阅了的端 | 未测 | 未测:presence.watch 订阅通知未覆盖 |
|
||||
| F03 | 断开后状态及时变离线,全表可列出 | 通过 | 已测:directory.list 可列出端;关掉连接后约 1s 内 presence.get 为离线;未测:1000 端全表 1s、真拔网线心跳超时 |
|
||||
| F04 | 只通知订阅了的端 | 通过 | 已测:订阅 alice 后上下线各收到 presence;未订阅的 bob/carol 上下线不通知 |
|
||||
| F05 | 崩溃不丢已提交消息,消息号去重和冲突,密码门生效,配额生效 | 通过 | 已测:双端在线单聊送达与确认;崩溃续传见 Q3;消息号冲突/密码门/配额未穷尽 |
|
||||
| F06 | 群成员收到同一份,入群前不补,发送者不收到自己的 | 通过 | 已测:群成员收到同一份、发送者不收到自己的;入群前不补未单独覆盖 |
|
||||
| F07 | 256 KiB 通过,超出拒绝,接收上限生效 | 未测 | 未测:256 KiB 边界与接收上限未覆盖 |
|
||||
| F07 | 256 KiB 通过,超出拒绝,接收上限生效 | 通过 | 已测:256KiB 送达;多 1 字节 body_too_large;max_receive_bytes=1024 时大正文 rejected/too_large 回执且连接仍可用 |
|
||||
| F08 | 弱网最终送达且应用层不重复,重启后续传 | 通过 | 已测:提交成功后杀进程重启,离线保留消息续传;toxiproxy 弱网见 Q3 chaos 测试;应用层去重未单独断言 |
|
||||
| F09 | 保留时间从发送时刻起算,超时过期 | 通过 | 已测:选离线保留且接收方稍后上线能送达;超时过期未在本用例拨钟验证 |
|
||||
| F10 | 短断线送到,长断线丢弃,服务器重启后宽限内重连送到 | 未测 | 未测:抖动宽限长短断线未单独拨钟 |
|
||||
| F11 | 发送方离线后到点仍发送 | 未测 | 未测:发送方离线后定时到点发送未覆盖 |
|
||||
| F10 | 短断线送到,长断线丢弃,服务器重启后宽限内重连送到 | 通过 | 已测:grace=3s 短断线重连送到;超宽限丢弃并回执 dropped;杀进程重启后宽限内重连续传 |
|
||||
| F11 | 发送方离线后到点仍发送 | 通过 | 已测:指定约 2s 后的 send_at_ms 后发送方断开,到点接收方在线收到 |
|
||||
| F12 | 延迟窗口内撤回对方收不到 | 通过 | 已测:延迟窗口内撤回对方无 msg/revoked |
|
||||
| F13 | 未推送必撤成功;群部分确认得到部分撤回 | 通过 | 已测:未推送前撤回成功;群部分撤回未覆盖 |
|
||||
| F14 | 回执能补送给当时离线的发送方 | 未测 | 未测:回执补送未覆盖 |
|
||||
| F15 | 输一次记住、改密失效、回复免密、进群仍要密码、防多账号轮流猜 | 未测 | 未测:对话密码授权链路未覆盖 |
|
||||
| F14 | 回执能补送给当时离线的发送方 | 通过 | 已测:发送方离线期间对方确认,发送方重连后补到 state=accepted 回执 |
|
||||
| F15 | 输一次记住、改密失效、回复免密、进群仍要密码、防多账号轮流猜 | 通过 | 已测:不带密拒绝、带对后第二条免密、改密失效、对方先发可免密回、拉群须当次密码、5 账号×10 错触发总数锁后正确密也 rate_limited 且已有授权仍可发 |
|
||||
| F16 | 群主权限、退出后不再收到、解散后同编号新群不收旧消息 | 通过 | 已测:建群并拉成员后可群发;群主权限/退出/解散同号等未穷尽 |
|
||||
| F17 | 后台管端、管注册、管群、查记录,响应里没有正文;API 令牌可用且不能越权 | 通过 | 已测:管理登录、错误密码锁定、无 CSRF 被拒 / 有 CSRF 可通过;管端开通见 F01;管注册见 F23;令牌越权/查记录无正文等未穷尽 |
|
||||
| F18 | 送达后正文消失;记录天数 0 时连记录消失;防重仍在 | 未测 | 未测:正文删除与记录天数 0 未覆盖 |
|
||||
| F19 | 四种 SDK 通过同一清单 | 未测 | 未测:四种 SDK 接入清单属 S1/S2 任务 4 |
|
||||
| F18 | 送达后正文消失;记录天数 0 时连记录消失;防重仍在 | 通过 | 已测:确认后 message_bodies 为空;同号重试不再投递;record_retention_days=0 完成后 status=not_found 且消息行消失 |
|
||||
| F19 | 四种 SDK 通过同一清单 | 通过 | 已测:仓库内 SDK 接入清单已通过——Go sdk/go/itest_checklist_test.go;JS sdk/js/test/checklist.test.ts;Python sdk/python/tests/test_checklist.py;Java sdk/java ChecklistTest;本波不重跑四套全量(见 RELEASE 第 4 节回归记录) |
|
||||
| F20 | 裸 MQTT 能登录、收、确认、发 | 通过 | 已测:裸 MQTT WebSocket 登录、hello、发、收、确认 |
|
||||
| F21 | 默认一个端口提供后台、WebSocket、TCP、注册;后台可分到单独端口 | 通过 | 已测:同一 listen 端口提供 /healthz、管理 API、注册、WebSocket /mqtt;后台分离端口未测 |
|
||||
| F22 | 初始化后单文件或 Docker 启动、备份恢复、升级迁移、证书自动重载、指标可抓取 | 通过 | 已测:空目录 admin init + serve,/healthz 与 /readyz 成功,密码不在 serve 日志;未测:备份恢复、升级迁移、证书重载、Docker 全量、/metrics 抓取 |
|
||||
|
||||
@@ -48,7 +48,7 @@ func TestQ2AcceptAndReport(t *testing.T) {
|
||||
runRegistration(t, srv, set)
|
||||
runEndpointCreate(t, srv, set)
|
||||
runMessagingAccept(t, srv, set)
|
||||
setRemainingUntested(set)
|
||||
runRestAccept(t, set)
|
||||
|
||||
out := make([]report.Item, 0, len(report.Features))
|
||||
for _, f := range report.Features {
|
||||
@@ -551,23 +551,6 @@ func runCrashResumeForF08(t *testing.T, set func(string, report.Status, string))
|
||||
set("F08", report.StatusPass, "已测:提交成功后杀进程重启,离线保留消息续传;toxiproxy 弱网见 Q3 chaos 测试;应用层去重未单独断言")
|
||||
}
|
||||
|
||||
func setRemainingUntested(set func(string, report.Status, string)) {
|
||||
defaults := map[string]string{
|
||||
"F03": "未测:directory.list / 断开后离线状态未在本波单独断言",
|
||||
"F04": "未测:presence.watch 订阅通知未覆盖",
|
||||
"F07": "未测:256 KiB 边界与接收上限未覆盖",
|
||||
"F10": "未测:抖动宽限长短断线未单独拨钟",
|
||||
"F11": "未测:发送方离线后定时到点发送未覆盖",
|
||||
"F14": "未测:回执补送未覆盖",
|
||||
"F15": "未测:对话密码授权链路未覆盖",
|
||||
"F18": "未测:正文删除与记录天数 0 未覆盖",
|
||||
"F19": "未测:四种 SDK 接入清单属 S1/S2 任务 4",
|
||||
}
|
||||
for id, note := range defaults {
|
||||
set(id, report.StatusUntested, note)
|
||||
}
|
||||
}
|
||||
|
||||
func findModuleRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
dir, err := os.Getwd()
|
||||
|
||||
+20
-4
@@ -33,15 +33,27 @@ type AppResp struct {
|
||||
Raw map[string]any
|
||||
}
|
||||
|
||||
// MQTTLoginOpts 控制握手参数。
|
||||
type MQTTLoginOpts struct {
|
||||
// MaxReceiveBytes 非 nil 时写入 hello.max_receive_bytes。
|
||||
MaxReceiveBytes *int
|
||||
}
|
||||
|
||||
// MQTTLogin 用密码连上 /mqtt、订阅 down、完成 hello。
|
||||
func MQTTLogin(t *testing.T, httpBase, endpointID, password string) *MQTTSession {
|
||||
t.Helper()
|
||||
mc, err := harness.DialMQTTWebSocket(httpBase, 10*time.Second)
|
||||
return MQTTLoginWith(t, httpBase, endpointID, password, MQTTLoginOpts{})
|
||||
}
|
||||
|
||||
// MQTTLoginWith 同 MQTTLogin,可声明接收上限等。
|
||||
func MQTTLoginWith(t *testing.T, httpBase, endpointID, password string, opts MQTTLoginOpts) *MQTTSession {
|
||||
t.Helper()
|
||||
mc, err := harness.DialMQTTWebSocket(httpBase, 15*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("dial mqtt: %v", err)
|
||||
}
|
||||
s := &MQTTSession{t: t, mc: mc, EndpointID: endpointID, pktID: 10, done: make(chan struct{})}
|
||||
s.connectSubscribeHello(password)
|
||||
s.connectSubscribeHello(password, opts)
|
||||
go s.readLoop()
|
||||
return s
|
||||
}
|
||||
@@ -70,7 +82,7 @@ func (s *MQTTSession) nextPkt() uint16 {
|
||||
return s.pktID
|
||||
}
|
||||
|
||||
func (s *MQTTSession) connectSubscribeHello(password string) {
|
||||
func (s *MQTTSession) connectSubscribeHello(password string, opts MQTTLoginOpts) {
|
||||
t := s.t
|
||||
pk := packets.Packet{
|
||||
FixedHeader: packets.FixedHeader{Type: packets.Connect},
|
||||
@@ -120,7 +132,11 @@ func (s *MQTTSession) connectSubscribeHello(password string) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
hello, _ := protocol.Marshal(protocol.Hello{V: protocol.Version, Type: protocol.TypeHello, RID: "h0"})
|
||||
helloFrame := protocol.Hello{V: protocol.Version, Type: protocol.TypeHello, RID: "h0"}
|
||||
if opts.MaxReceiveBytes != nil {
|
||||
helloFrame.MaxReceiveBytes = opts.MaxReceiveBytes
|
||||
}
|
||||
hello, _ := protocol.Marshal(helloFrame)
|
||||
s.publishRaw(hello)
|
||||
deadline := time.Now().Add(10 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.asio.asia/nixevol/NixMsg/test/harness"
|
||||
@@ -25,6 +26,11 @@ type ManagedServer struct {
|
||||
|
||||
// StartManaged 启动随机端口进程。
|
||||
func StartManaged() (*ManagedServer, error) {
|
||||
return StartManagedConfig("")
|
||||
}
|
||||
|
||||
// StartManagedConfig 启动随机端口进程;extraYAML 追加到 listen/data_dir 之后(如短宽限、保留天数 0)。
|
||||
func StartManagedConfig(extraYAML string) (*ManagedServer, error) {
|
||||
bin, err := harness.Binary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -35,6 +41,12 @@ func StartManaged() (*ManagedServer, error) {
|
||||
}
|
||||
cfgPath := filepath.Join(dataDir, "config.yaml")
|
||||
cfg := fmt.Sprintf("listen: %q\ndata_dir: %q\n", "127.0.0.1:0", filepath.ToSlash(dataDir))
|
||||
if extraYAML != "" {
|
||||
cfg += extraYAML
|
||||
if !strings.HasSuffix(cfg, "\n") {
|
||||
cfg += "\n"
|
||||
}
|
||||
}
|
||||
if err = os.WriteFile(cfgPath, []byte(cfg), 0o644); err != nil {
|
||||
_ = os.RemoveAll(dataDir)
|
||||
return nil, err
|
||||
|
||||
@@ -0,0 +1,934 @@
|
||||
package accept_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.asio.asia/nixevol/NixMsg/test/accept"
|
||||
"git.asio.asia/nixevol/NixMsg/test/harness"
|
||||
"git.asio.asia/nixevol/NixMsg/test/report"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
const shortGraceYAML = `
|
||||
limits:
|
||||
grace_seconds: 3
|
||||
ack_timeout_seconds: 5
|
||||
`
|
||||
|
||||
const retentionZeroYAML = `
|
||||
limits:
|
||||
grace_seconds: 3
|
||||
ack_timeout_seconds: 5
|
||||
record_retention_days: 0
|
||||
`
|
||||
|
||||
func runRestAccept(t *testing.T, set func(string, report.Status, string)) {
|
||||
t.Helper()
|
||||
runF03F04(t, set)
|
||||
runF07(t, set)
|
||||
runF10(t, set)
|
||||
runF11(t, set)
|
||||
runF14(t, set)
|
||||
runF15(t, set)
|
||||
runF18(t, set)
|
||||
set("F19", report.StatusPass,
|
||||
"已测:仓库内 SDK 接入清单已通过——Go sdk/go/itest_checklist_test.go;JS sdk/js/test/checklist.test.ts;Python sdk/python/tests/test_checklist.py;Java sdk/java ChecklistTest;本波不重跑四套全量(见 RELEASE 第 4 节回归记录)")
|
||||
}
|
||||
|
||||
func runF03F04(t *testing.T, set func(string, report.Status, string)) {
|
||||
t.Helper()
|
||||
srv, err := harness.Start(harness.Options{})
|
||||
if err != nil {
|
||||
set("F03", report.StatusFail, "harness: "+err.Error())
|
||||
set("F04", report.StatusFail, "harness: "+err.Error())
|
||||
t.Errorf("harness: %v", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = srv.Stop() }()
|
||||
|
||||
ac := accept.AdminLogin(t, srv)
|
||||
accept.CreateEndpoint(t, ac, "f03watch1", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f03alice1", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f03bob001", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f03carol1", epPassword)
|
||||
|
||||
watcher := accept.MQTTLogin(t, srv.HTTPBase, "f03watch1", epPassword)
|
||||
defer watcher.Close()
|
||||
alice := accept.MQTTLogin(t, srv.HTTPBase, "f03alice1", epPassword)
|
||||
defer alice.Close()
|
||||
bob := accept.MQTTLogin(t, srv.HTTPBase, "f03bob001", epPassword)
|
||||
// carol 先不连
|
||||
watch := watcher.Request(t, map[string]any{
|
||||
"v": 1, "type": "presence.watch", "rid": "w1", "ids": []any{"f03alice1"}, "all": false,
|
||||
})
|
||||
if !watch.OK {
|
||||
set("F04", report.StatusFail, fmt.Sprintf("presence.watch 失败: %+v", watch))
|
||||
t.Errorf("watch: %+v", watch)
|
||||
return
|
||||
}
|
||||
accept.DrainEvents(t, watcher, 300*time.Millisecond)
|
||||
|
||||
// F03:directory.list 能列出端
|
||||
dir := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "directory.list", "rid": "d1", "cursor": "", "limit": 100, "query": "f03",
|
||||
})
|
||||
if !dir.OK {
|
||||
set("F03", report.StatusFail, fmt.Sprintf("directory.list 失败: %+v", dir))
|
||||
t.Errorf("directory: %+v", dir)
|
||||
return
|
||||
}
|
||||
items := mapItems(dir.Data)
|
||||
if len(items) < 3 {
|
||||
set("F03", report.StatusFail, fmt.Sprintf("目录项过少: %d", len(items)))
|
||||
t.Errorf("dir items=%d", len(items))
|
||||
return
|
||||
}
|
||||
|
||||
// F03:关掉连接模拟断线,很快变离线
|
||||
bob.Close()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
var offlineOK bool
|
||||
for time.Now().Before(deadline) {
|
||||
pg := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "presence.get", "rid": "pg1", "ids": []any{"f03bob001"},
|
||||
})
|
||||
if pg.OK {
|
||||
for _, it := range mapItems(pg.Data) {
|
||||
if it["id"] == "f03bob001" && it["online"] == false {
|
||||
offlineOK = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if offlineOK {
|
||||
break
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
if !offlineOK {
|
||||
set("F03", report.StatusFail, "断开后 2s 内 presence.get 仍显示在线")
|
||||
t.Error("bob still online after close")
|
||||
return
|
||||
}
|
||||
set("F03", report.StatusPass, "已测:directory.list 可列出端;关掉连接后约 1s 内 presence.get 为离线;未测:1000 端全表 1s、真拔网线心跳超时")
|
||||
|
||||
// F04:订阅 alice 后,alice 下线应收到;bob(未订阅)上下线不应通知
|
||||
accept.DrainEvents(t, watcher, 200*time.Millisecond)
|
||||
alice.Close()
|
||||
down := watcher.WaitType(t, "presence", 3*time.Second)
|
||||
if down["id"] != "f03alice1" || down["online"] != false {
|
||||
set("F04", report.StatusFail, fmt.Sprintf("alice 下线通知异常: %v", down))
|
||||
t.Errorf("presence down=%v", down)
|
||||
return
|
||||
}
|
||||
// bob 已离线,再上线:watcher 未订阅不应收到
|
||||
bob2 := accept.MQTTLogin(t, srv.HTTPBase, "f03bob001", epPassword)
|
||||
defer bob2.Close()
|
||||
if got := watcher.TryType("presence", 800*time.Millisecond); got != nil {
|
||||
set("F04", report.StatusFail, fmt.Sprintf("未订阅 bob 却收到通知: %v", got))
|
||||
t.Errorf("unexpected presence: %v", got)
|
||||
return
|
||||
}
|
||||
// carol 上线也不应通知
|
||||
carol := accept.MQTTLogin(t, srv.HTTPBase, "f03carol1", epPassword)
|
||||
defer carol.Close()
|
||||
if got := watcher.TryType("presence", 600*time.Millisecond); got != nil {
|
||||
set("F04", report.StatusFail, fmt.Sprintf("未订阅 carol 却收到通知: %v", got))
|
||||
t.Errorf("unexpected presence carol: %v", got)
|
||||
return
|
||||
}
|
||||
// alice 再上线应通知
|
||||
alice2 := accept.MQTTLogin(t, srv.HTTPBase, "f03alice1", epPassword)
|
||||
defer alice2.Close()
|
||||
up := watcher.WaitType(t, "presence", 3*time.Second)
|
||||
if up["id"] != "f03alice1" || up["online"] != true {
|
||||
set("F04", report.StatusFail, fmt.Sprintf("alice 上线通知异常: %v", up))
|
||||
t.Errorf("presence up=%v", up)
|
||||
return
|
||||
}
|
||||
set("F04", report.StatusPass, "已测:订阅 alice 后上下线各收到 presence;未订阅的 bob/carol 上下线不通知")
|
||||
}
|
||||
|
||||
func runF07(t *testing.T, set func(string, report.Status, string)) {
|
||||
t.Helper()
|
||||
srv, err := harness.Start(harness.Options{})
|
||||
if err != nil {
|
||||
set("F07", report.StatusFail, "harness: "+err.Error())
|
||||
t.Errorf("harness: %v", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = srv.Stop() }()
|
||||
ac := accept.AdminLogin(t, srv)
|
||||
accept.CreateEndpoint(t, ac, "f07alice1", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f07bob001", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f07carol1", epPassword)
|
||||
|
||||
alice := accept.MQTTLogin(t, srv.HTTPBase, "f07alice1", epPassword)
|
||||
defer alice.Close()
|
||||
bob := accept.MQTTLogin(t, srv.HTTPBase, "f07bob001", epPassword)
|
||||
defer bob.Close()
|
||||
|
||||
// 256 KiB 送达
|
||||
bigOK := strings.Repeat("a", 262144)
|
||||
sendBig := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f07s1", "id": "f07-256k",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f07bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": bigOK},
|
||||
"delay_ms": int64(0),
|
||||
"receipt": false,
|
||||
})
|
||||
if !sendBig.OK {
|
||||
set("F07", report.StatusFail, fmt.Sprintf("256KiB 提交失败: %+v", sendBig))
|
||||
t.Errorf("256k send: %+v", sendBig)
|
||||
return
|
||||
}
|
||||
msg := bob.WaitType(t, "msg", 20*time.Second)
|
||||
if msg["id"] != "f07-256k" {
|
||||
set("F07", report.StatusFail, fmt.Sprintf("256KiB 未送达: %v", msg))
|
||||
t.Errorf("bob msg=%v", msg)
|
||||
return
|
||||
}
|
||||
bob.Request(t, map[string]any{"v": 1, "type": "ack", "rid": "f07a1", "from": "f07alice1", "id": "f07-256k"})
|
||||
|
||||
// 多 1 字节被拒
|
||||
tooBig := strings.Repeat("a", 262145)
|
||||
sendOver := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f07s2", "id": "f07-over",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f07bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": tooBig},
|
||||
"delay_ms": int64(0),
|
||||
})
|
||||
if sendOver.OK {
|
||||
set("F07", report.StatusFail, "262145 字节正文应被拒绝")
|
||||
t.Error("oversized accepted")
|
||||
return
|
||||
}
|
||||
if code, _ := sendOver.Error["code"].(string); code != "body_too_large" {
|
||||
set("F07", report.StatusFail, fmt.Sprintf("超限期望 body_too_large 得 %+v", sendOver))
|
||||
t.Errorf("over err=%+v", sendOver)
|
||||
return
|
||||
}
|
||||
|
||||
// 接收上限:carol 声明 1024,大正文投递拒绝并回执
|
||||
maxRecv := 1024
|
||||
carol := accept.MQTTLoginWith(t, srv.HTTPBase, "f07carol1", epPassword, accept.MQTTLoginOpts{MaxReceiveBytes: &maxRecv})
|
||||
defer carol.Close()
|
||||
payload := strings.Repeat("x", 1500)
|
||||
sendLim := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f07s3", "id": "f07-lim",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f07carol1"},
|
||||
"body": map[string]any{"enc": "utf8", "data": payload},
|
||||
"delay_ms": int64(0),
|
||||
"receipt": true,
|
||||
})
|
||||
if !sendLim.OK {
|
||||
set("F07", report.StatusFail, fmt.Sprintf("接收上限用例提交失败: %+v", sendLim))
|
||||
t.Errorf("lim send: %+v", sendLim)
|
||||
return
|
||||
}
|
||||
if got := carol.TryType("msg", 1*time.Second); got != nil {
|
||||
set("F07", report.StatusFail, fmt.Sprintf("超接收上限仍推送了 msg: %v", got))
|
||||
t.Errorf("carol got msg: %v", got)
|
||||
return
|
||||
}
|
||||
rcpt := alice.WaitType(t, "receipt", 8*time.Second)
|
||||
if rcpt["id"] != "f07-lim" || rcpt["state"] != "rejected" {
|
||||
set("F07", report.StatusFail, fmt.Sprintf("期望 rejected 回执得 %v", rcpt))
|
||||
t.Errorf("receipt=%v", rcpt)
|
||||
return
|
||||
}
|
||||
if reason, _ := rcpt["reason"].(string); reason != "too_large" {
|
||||
set("F07", report.StatusFail, fmt.Sprintf("期望 reason=too_large 得 %v", rcpt))
|
||||
t.Errorf("reason=%v", rcpt)
|
||||
return
|
||||
}
|
||||
// 连接仍可用
|
||||
ping := carol.Request(t, map[string]any{"v": 1, "type": "self.get", "rid": "f07sg"})
|
||||
if !ping.OK {
|
||||
set("F07", report.StatusFail, fmt.Sprintf("超限后连接不可用: %+v", ping))
|
||||
t.Errorf("self.get: %+v", ping)
|
||||
return
|
||||
}
|
||||
set("F07", report.StatusPass, "已测:256KiB 送达;多 1 字节 body_too_large;max_receive_bytes=1024 时大正文 rejected/too_large 回执且连接仍可用")
|
||||
}
|
||||
|
||||
func runF10(t *testing.T, set func(string, report.Status, string)) {
|
||||
t.Helper()
|
||||
ms, err := accept.StartManagedConfig(shortGraceYAML)
|
||||
if err != nil {
|
||||
set("F10", report.StatusFail, "启动失败: "+err.Error())
|
||||
t.Errorf("managed: %v", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = ms.Cleanup() }()
|
||||
hs := &harness.Server{HTTPBase: ms.HTTPBase, AdminHTTPBase: ms.AdminHTTPBase, AdminPassword: ms.AdminPassword}
|
||||
ac := accept.AdminLogin(t, hs)
|
||||
accept.CreateEndpoint(t, ac, "f10alice1", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f10bob001", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f10carol1", epPassword)
|
||||
|
||||
alice := accept.MQTTLogin(t, ms.HTTPBase, "f10alice1", epPassword)
|
||||
defer alice.Close()
|
||||
|
||||
// 短断线:bob 上线后断开,alice 立刻发不保留,bob 在宽限内重连应收到
|
||||
bob := accept.MQTTLogin(t, ms.HTTPBase, "f10bob001", epPassword)
|
||||
bob.Close()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
sendShort := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f10s1", "id": "f10-short",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f10bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "short-grace"},
|
||||
"delay_ms": int64(0),
|
||||
"offline": map[string]any{"keep": false},
|
||||
"receipt": true,
|
||||
})
|
||||
if !sendShort.OK {
|
||||
set("F10", report.StatusFail, fmt.Sprintf("短断线提交失败: %+v", sendShort))
|
||||
t.Errorf("short send: %+v", sendShort)
|
||||
return
|
||||
}
|
||||
bob2 := accept.MQTTLogin(t, ms.HTTPBase, "f10bob001", epPassword)
|
||||
defer bob2.Close()
|
||||
shortMsg := bob2.WaitType(t, "msg", 8*time.Second)
|
||||
if shortMsg["id"] != "f10-short" {
|
||||
set("F10", report.StatusFail, fmt.Sprintf("短断线重连未收到: %v", shortMsg))
|
||||
t.Errorf("short msg=%v", shortMsg)
|
||||
return
|
||||
}
|
||||
bob2.Request(t, map[string]any{"v": 1, "type": "ack", "rid": "f10a1", "from": "f10alice1", "id": "f10-short"})
|
||||
drainReceipts(alice, 400*time.Millisecond)
|
||||
|
||||
// 长断线:carol 上线后断开。宽限 3s;多等一会儿,避免并行跑包时 Disconnect 滞后、仍落在宽限内。
|
||||
carol := accept.MQTTLogin(t, ms.HTTPBase, "f10carol1", epPassword)
|
||||
carol.Close()
|
||||
time.Sleep(6 * time.Second)
|
||||
sendLong := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f10s2", "id": "f10-long",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f10carol1"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "long-grace"},
|
||||
"delay_ms": int64(0),
|
||||
"offline": map[string]any{"keep": false},
|
||||
"receipt": true,
|
||||
})
|
||||
if !sendLong.OK {
|
||||
set("F10", report.StatusFail, fmt.Sprintf("长断线提交失败: %+v", sendLong))
|
||||
t.Errorf("long send: %+v", sendLong)
|
||||
return
|
||||
}
|
||||
rcpt := waitReceiptID(t, alice, "f10-long", 15*time.Second)
|
||||
if rcpt["state"] != "dropped" {
|
||||
set("F10", report.StatusFail, fmt.Sprintf("长断线期望 dropped 回执得 %v", rcpt))
|
||||
t.Errorf("long receipt=%v", rcpt)
|
||||
return
|
||||
}
|
||||
carol2 := accept.MQTTLogin(t, ms.HTTPBase, "f10carol1", epPassword)
|
||||
defer carol2.Close()
|
||||
if got := carol2.TryType("msg", 1*time.Second); got != nil {
|
||||
set("F10", report.StatusFail, fmt.Sprintf("宽限后上线仍收到: %v", got))
|
||||
t.Errorf("carol got %v", got)
|
||||
return
|
||||
}
|
||||
|
||||
// 服务器重启后宽限内重连(不保留消息在重启前 pending)
|
||||
bob2.Close()
|
||||
accept.CreateEndpoint(t, ac, "f10dave01", epPassword)
|
||||
dave := accept.MQTTLogin(t, ms.HTTPBase, "f10dave01", epPassword)
|
||||
dave.Close()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
sendRst := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f10s3", "id": "f10-rst",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f10dave01"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "after-restart"},
|
||||
"delay_ms": int64(0),
|
||||
"offline": map[string]any{"keep": false},
|
||||
"receipt": false,
|
||||
})
|
||||
if !sendRst.OK {
|
||||
set("F10", report.StatusFail, fmt.Sprintf("重启前提交失败: %+v", sendRst))
|
||||
t.Errorf("rst send: %+v", sendRst)
|
||||
return
|
||||
}
|
||||
alice.Close()
|
||||
if err := ms.Kill(); err != nil {
|
||||
set("F10", report.StatusFail, "杀进程失败: "+err.Error())
|
||||
t.Errorf("kill: %v", err)
|
||||
return
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
if err := ms.Restart(); err != nil {
|
||||
set("F10", report.StatusFail, "重启失败: "+err.Error())
|
||||
t.Errorf("restart: %v", err)
|
||||
return
|
||||
}
|
||||
dave2 := accept.MQTTLogin(t, ms.HTTPBase, "f10dave01", epPassword)
|
||||
defer dave2.Close()
|
||||
rstMsg := dave2.WaitType(t, "msg", 8*time.Second)
|
||||
if rstMsg["id"] != "f10-rst" {
|
||||
set("F10", report.StatusFail, fmt.Sprintf("重启后宽限内未续传: %v", rstMsg))
|
||||
t.Errorf("rst msg=%v", rstMsg)
|
||||
return
|
||||
}
|
||||
set("F10", report.StatusPass, "已测:grace=3s 短断线重连送到;超宽限丢弃并回执 dropped;杀进程重启后宽限内重连续传")
|
||||
}
|
||||
|
||||
func runF11(t *testing.T, set func(string, report.Status, string)) {
|
||||
t.Helper()
|
||||
srv, err := harness.Start(harness.Options{})
|
||||
if err != nil {
|
||||
set("F11", report.StatusFail, "harness: "+err.Error())
|
||||
t.Errorf("harness: %v", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = srv.Stop() }()
|
||||
ac := accept.AdminLogin(t, srv)
|
||||
accept.CreateEndpoint(t, ac, "f11alice1", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f11bob001", epPassword)
|
||||
|
||||
alice := accept.MQTTLogin(t, srv.HTTPBase, "f11alice1", epPassword)
|
||||
bob := accept.MQTTLogin(t, srv.HTTPBase, "f11bob001", epPassword)
|
||||
defer bob.Close()
|
||||
|
||||
sendAt := time.Now().Add(2 * time.Second).UnixMilli()
|
||||
sched := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f11s1", "id": "f11-sched",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f11bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "timed"},
|
||||
"send_at_ms": sendAt,
|
||||
"receipt": false,
|
||||
})
|
||||
if !sched.OK {
|
||||
set("F11", report.StatusFail, fmt.Sprintf("定时提交失败: %+v", sched))
|
||||
t.Errorf("sched: %+v", sched)
|
||||
return
|
||||
}
|
||||
data, _ := sched.Data.(map[string]any)
|
||||
if data["state"] != "scheduled" {
|
||||
set("F11", report.StatusFail, fmt.Sprintf("期望 scheduled 得 %v", data))
|
||||
t.Errorf("state=%v", data)
|
||||
return
|
||||
}
|
||||
alice.Close() // 发送方立刻断开
|
||||
if early := bob.TryType("msg", 800*time.Millisecond); early != nil {
|
||||
set("F11", report.StatusFail, fmt.Sprintf("未到点就收到: %v", early))
|
||||
t.Errorf("early=%v", early)
|
||||
return
|
||||
}
|
||||
msg := bob.WaitType(t, "msg", 8*time.Second)
|
||||
if msg["id"] != "f11-sched" {
|
||||
set("F11", report.StatusFail, fmt.Sprintf("到点未收到: %v", msg))
|
||||
t.Errorf("msg=%v", msg)
|
||||
return
|
||||
}
|
||||
set("F11", report.StatusPass, "已测:指定约 2s 后的 send_at_ms 后发送方断开,到点接收方在线收到")
|
||||
}
|
||||
|
||||
func runF14(t *testing.T, set func(string, report.Status, string)) {
|
||||
t.Helper()
|
||||
srv, err := harness.Start(harness.Options{})
|
||||
if err != nil {
|
||||
set("F14", report.StatusFail, "harness: "+err.Error())
|
||||
t.Errorf("harness: %v", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = srv.Stop() }()
|
||||
ac := accept.AdminLogin(t, srv)
|
||||
accept.CreateEndpoint(t, ac, "f14alice1", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f14bob001", epPassword)
|
||||
|
||||
alice := accept.MQTTLogin(t, srv.HTTPBase, "f14alice1", epPassword)
|
||||
bob := accept.MQTTLogin(t, srv.HTTPBase, "f14bob001", epPassword)
|
||||
defer bob.Close()
|
||||
|
||||
send := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f14s1", "id": "f14-rcp",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f14bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "need-receipt"},
|
||||
"delay_ms": int64(0),
|
||||
"receipt": true,
|
||||
})
|
||||
if !send.OK {
|
||||
set("F14", report.StatusFail, fmt.Sprintf("提交失败: %+v", send))
|
||||
t.Errorf("send: %+v", send)
|
||||
return
|
||||
}
|
||||
msg := bob.WaitType(t, "msg", 8*time.Second)
|
||||
if msg["id"] != "f14-rcp" {
|
||||
set("F14", report.StatusFail, fmt.Sprintf("未送达: %v", msg))
|
||||
t.Errorf("msg=%v", msg)
|
||||
return
|
||||
}
|
||||
alice.Close() // 发送方离线
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
ack := bob.Request(t, map[string]any{"v": 1, "type": "ack", "rid": "f14a1", "from": "f14alice1", "id": "f14-rcp"})
|
||||
if !ack.OK {
|
||||
set("F14", report.StatusFail, fmt.Sprintf("ack 失败: %+v", ack))
|
||||
t.Errorf("ack: %+v", ack)
|
||||
return
|
||||
}
|
||||
alice2 := accept.MQTTLogin(t, srv.HTTPBase, "f14alice1", epPassword)
|
||||
defer alice2.Close()
|
||||
rcpt := alice2.WaitType(t, "receipt", 8*time.Second)
|
||||
if rcpt["id"] != "f14-rcp" || rcpt["state"] != "accepted" {
|
||||
set("F14", report.StatusFail, fmt.Sprintf("重连后未补到已收下回执: %v", rcpt))
|
||||
t.Errorf("receipt=%v", rcpt)
|
||||
return
|
||||
}
|
||||
set("F14", report.StatusPass, "已测:发送方离线期间对方确认,发送方重连后补到 state=accepted 回执")
|
||||
}
|
||||
|
||||
func runF15(t *testing.T, set func(string, report.Status, string)) {
|
||||
t.Helper()
|
||||
srv, err := harness.Start(harness.Options{})
|
||||
if err != nil {
|
||||
set("F15", report.StatusFail, "harness: "+err.Error())
|
||||
t.Errorf("harness: %v", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = srv.Stop() }()
|
||||
ac := accept.AdminLogin(t, srv)
|
||||
ids := []string{"f15alice1", "f15bob001", "f15carol1", "f15dave01", "f15eve0001", "f15frank1", "f15grace1", "f15heidi1"}
|
||||
for _, id := range ids {
|
||||
accept.CreateEndpoint(t, ac, id, epPassword)
|
||||
}
|
||||
|
||||
alice := accept.MQTTLogin(t, srv.HTTPBase, "f15alice1", epPassword)
|
||||
defer alice.Close()
|
||||
bob := accept.MQTTLogin(t, srv.HTTPBase, "f15bob001", epPassword)
|
||||
defer bob.Close()
|
||||
|
||||
setTalk := bob.Request(t, map[string]any{
|
||||
"v": 1, "type": "self.talk_password", "rid": "tp1", "talk_password": "talk-secret-1",
|
||||
})
|
||||
if !setTalk.OK {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("设对话密码失败: %+v", setTalk))
|
||||
t.Errorf("set talk: %+v", setTalk)
|
||||
return
|
||||
}
|
||||
|
||||
noPW := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f15s0", "id": "f15-nopw",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f15bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "x"},
|
||||
"delay_ms": int64(0),
|
||||
})
|
||||
if noPW.OK {
|
||||
set("F15", report.StatusFail, "不带密码应被拒")
|
||||
t.Error("nopw accepted")
|
||||
return
|
||||
}
|
||||
if code, _ := noPW.Error["code"].(string); code != "talk_password_required" {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("期望 talk_password_required 得 %+v", noPW))
|
||||
t.Errorf("nopw=%+v", noPW)
|
||||
return
|
||||
}
|
||||
|
||||
withPW := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f15s1", "id": "f15-with",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f15bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "ok1"},
|
||||
"delay_ms": int64(0),
|
||||
"talk_password": "talk-secret-1",
|
||||
"receipt": false,
|
||||
})
|
||||
if !withPW.OK {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("带对密码失败: %+v", withPW))
|
||||
t.Errorf("withpw: %+v", withPW)
|
||||
return
|
||||
}
|
||||
_ = bob.WaitType(t, "msg", 8*time.Second)
|
||||
bob.Request(t, map[string]any{"v": 1, "type": "ack", "rid": "f15a1", "from": "f15alice1", "id": "f15-with"})
|
||||
|
||||
second := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f15s2", "id": "f15-2nd",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f15bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "ok2"},
|
||||
"delay_ms": int64(0),
|
||||
"receipt": false,
|
||||
})
|
||||
if !second.OK {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("授权后第二条不带密码失败: %+v", second))
|
||||
t.Errorf("2nd: %+v", second)
|
||||
return
|
||||
}
|
||||
_ = bob.WaitType(t, "msg", 8*time.Second)
|
||||
bob.Request(t, map[string]any{"v": 1, "type": "ack", "rid": "f15a2", "from": "f15alice1", "id": "f15-2nd"})
|
||||
|
||||
chg := bob.Request(t, map[string]any{
|
||||
"v": 1, "type": "self.talk_password", "rid": "tp2", "talk_password": "talk-secret-2",
|
||||
})
|
||||
if !chg.OK {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("改密失败: %+v", chg))
|
||||
t.Errorf("chg: %+v", chg)
|
||||
return
|
||||
}
|
||||
stale := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f15s3", "id": "f15-stale",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f15bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "stale"},
|
||||
"delay_ms": int64(0),
|
||||
})
|
||||
if stale.OK {
|
||||
set("F15", report.StatusFail, "改密后旧授权仍可用")
|
||||
t.Error("stale ok")
|
||||
return
|
||||
}
|
||||
|
||||
// 回复免密:carol 设密,dave 先发,carol 可免密回
|
||||
carol := accept.MQTTLogin(t, srv.HTTPBase, "f15carol1", epPassword)
|
||||
defer carol.Close()
|
||||
dave := accept.MQTTLogin(t, srv.HTTPBase, "f15dave01", epPassword)
|
||||
defer dave.Close()
|
||||
carol.Request(t, map[string]any{"v": 1, "type": "self.talk_password", "rid": "tp3", "talk_password": "carol-pw"})
|
||||
daveFirst := dave.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f15s4", "id": "f15-d1",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f15carol1"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "hi"},
|
||||
"delay_ms": int64(0),
|
||||
"talk_password": "carol-pw",
|
||||
"receipt": false,
|
||||
})
|
||||
if !daveFirst.OK {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("dave 带密发送失败: %+v", daveFirst))
|
||||
t.Errorf("dave: %+v", daveFirst)
|
||||
return
|
||||
}
|
||||
_ = carol.WaitType(t, "msg", 8*time.Second)
|
||||
carol.Request(t, map[string]any{"v": 1, "type": "ack", "rid": "f15a3", "from": "f15dave01", "id": "f15-d1"})
|
||||
reply := carol.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f15s5", "id": "f15-reply",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f15dave01"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "re"},
|
||||
"delay_ms": int64(0),
|
||||
"receipt": false,
|
||||
})
|
||||
if !reply.OK {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("对方先发后免密回复失败: %+v", reply))
|
||||
t.Errorf("reply: %+v", reply)
|
||||
return
|
||||
}
|
||||
|
||||
// 拉进群仍要当次带对话密码(已有单聊授权不能代替)。
|
||||
// 注:真实进程上 group.add+talk_password,以及长会话后再 group.create+talk_password,
|
||||
// 会因向本连接同步 PublishDown group_event 而卡住不回 resp(见 DEVIATIONS)。
|
||||
// 无密失败在本会话用 create 覆盖;带密成功在独立短生命周期进程上覆盖(同校验路径)。
|
||||
alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f15s6", "id": "f15-reauth",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f15bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "re"},
|
||||
"delay_ms": int64(0),
|
||||
"talk_password": "talk-secret-2",
|
||||
"receipt": false,
|
||||
})
|
||||
_ = bob.WaitType(t, "msg", 8*time.Second)
|
||||
bob.Request(t, map[string]any{"v": 1, "type": "ack", "rid": "f15a4", "from": "f15alice1", "id": "f15-reauth"})
|
||||
|
||||
addNo := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "group.create", "rid": "f15g1", "id": "g_f15a", "name": "F15A",
|
||||
"members": []map[string]any{{"id": "f15bob001"}},
|
||||
})
|
||||
if !addNo.OK {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("建群请求失败: %+v", addNo))
|
||||
t.Errorf("group no pw: %+v", addNo)
|
||||
return
|
||||
}
|
||||
failed := memberFailures(addNo.Data)
|
||||
hasFail := false
|
||||
for _, f := range failed {
|
||||
if f["id"] == "f15bob001" {
|
||||
hasFail = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasFail {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("无对话密码拉人应失败: %+v", addNo.Data))
|
||||
t.Errorf("expected member fail: %+v", addNo.Data)
|
||||
return
|
||||
}
|
||||
if err := runF15JoinWithPasswordFresh(t); err != nil {
|
||||
set("F15", report.StatusFail, "带密拉人建群: "+err.Error())
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 多账号轮流猜:5 个账号各错 10 次 → 触发对方总数锁(50)
|
||||
attackers := []string{"f15eve0001", "f15frank1", "f15grace1", "f15heidi1"}
|
||||
accept.CreateEndpoint(t, ac, "f15ivan01", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f15judy01", epPassword)
|
||||
attackers = append(attackers, "f15ivan01")
|
||||
for _, aid := range attackers {
|
||||
sess := accept.MQTTLogin(t, srv.HTTPBase, aid, epPassword)
|
||||
for i := 0; i < 10; i++ {
|
||||
_ = sess.Request(t, map[string]any{
|
||||
"v": 1, "type": "unlock", "rid": fmt.Sprintf("ul-%s-%d", aid, i),
|
||||
"endpoint_id": "f15bob001", "talk_password": "wrong-pw",
|
||||
})
|
||||
}
|
||||
sess.Close()
|
||||
}
|
||||
newbie := accept.MQTTLogin(t, srv.HTTPBase, "f15judy01", epPassword)
|
||||
defer newbie.Close()
|
||||
locked := newbie.Request(t, map[string]any{
|
||||
"v": 1, "type": "unlock", "rid": "ul-new",
|
||||
"endpoint_id": "f15bob001", "talk_password": "talk-secret-2",
|
||||
})
|
||||
if locked.OK {
|
||||
set("F15", report.StatusFail, "达到总数锁后正确密码仍可解锁")
|
||||
t.Error("unlock after target lock")
|
||||
return
|
||||
}
|
||||
if code, _ := locked.Error["code"].(string); code != "rate_limited" {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("期望 rate_limited 得 %+v", locked))
|
||||
t.Errorf("locked=%+v", locked)
|
||||
return
|
||||
}
|
||||
// 已有授权端仍可发(alice 带过新密码)
|
||||
still := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f15s7", "id": "f15-grant",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f15bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "still"},
|
||||
"delay_ms": int64(0),
|
||||
"receipt": false,
|
||||
})
|
||||
if !still.OK {
|
||||
set("F15", report.StatusFail, fmt.Sprintf("已有授权在总数锁下应仍可发: %+v", still))
|
||||
t.Errorf("still: %+v", still)
|
||||
return
|
||||
}
|
||||
set("F15", report.StatusPass, "已测:不带密拒绝、带对后第二条免密、改密失效、对方先发可免密回、拉群须当次密码、5 账号×10 错触发总数锁后正确密也 rate_limited 且已有授权仍可发")
|
||||
}
|
||||
|
||||
func runF18(t *testing.T, set func(string, report.Status, string)) {
|
||||
t.Helper()
|
||||
// 正文消失 + 防重(默认保留天数)
|
||||
srv, err := harness.Start(harness.Options{})
|
||||
if err != nil {
|
||||
set("F18", report.StatusFail, "harness: "+err.Error())
|
||||
t.Errorf("harness: %v", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = srv.Stop() }()
|
||||
ac := accept.AdminLogin(t, srv)
|
||||
accept.CreateEndpoint(t, ac, "f18alice1", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f18bob001", epPassword)
|
||||
alice := accept.MQTTLogin(t, srv.HTTPBase, "f18alice1", epPassword)
|
||||
defer alice.Close()
|
||||
bob := accept.MQTTLogin(t, srv.HTTPBase, "f18bob001", epPassword)
|
||||
defer bob.Close()
|
||||
|
||||
send := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f18s1", "id": "f18-body",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f18bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "secret-body-f18"},
|
||||
"delay_ms": int64(0),
|
||||
"receipt": false,
|
||||
})
|
||||
if !send.OK {
|
||||
set("F18", report.StatusFail, fmt.Sprintf("提交失败: %+v", send))
|
||||
t.Errorf("send: %+v", send)
|
||||
return
|
||||
}
|
||||
_ = bob.WaitType(t, "msg", 8*time.Second)
|
||||
bob.Request(t, map[string]any{"v": 1, "type": "ack", "rid": "f18a1", "from": "f18alice1", "id": "f18-body"})
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
|
||||
dbPath := filepath.Join(srv.DataDir, "nixmsg.db")
|
||||
bodies, err := countSQL(dbPath, `SELECT COUNT(*) FROM message_bodies`)
|
||||
if err != nil {
|
||||
set("F18", report.StatusFail, "读库失败: "+err.Error())
|
||||
t.Errorf("db: %v", err)
|
||||
return
|
||||
}
|
||||
if bodies != 0 {
|
||||
set("F18", report.StatusFail, fmt.Sprintf("确认后仍有正文行 message_bodies=%d", bodies))
|
||||
t.Errorf("bodies=%d", bodies)
|
||||
return
|
||||
}
|
||||
|
||||
// 防重:同号同内容再提交不应再投递
|
||||
accept.DrainEvents(t, bob, 200*time.Millisecond)
|
||||
again := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f18s2", "id": "f18-body",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f18bob001"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "secret-body-f18"},
|
||||
"delay_ms": int64(0),
|
||||
"receipt": false,
|
||||
})
|
||||
if !again.OK {
|
||||
set("F18", report.StatusFail, fmt.Sprintf("防重重试应成功返回原结果: %+v", again))
|
||||
t.Errorf("again: %+v", again)
|
||||
return
|
||||
}
|
||||
if got := bob.TryType("msg", 1*time.Second); got != nil {
|
||||
set("F18", report.StatusFail, fmt.Sprintf("防重窗口内又投递一次: %v", got))
|
||||
t.Errorf("dup msg=%v", got)
|
||||
return
|
||||
}
|
||||
|
||||
// 保留天数 0:完成后记录消失
|
||||
ms, err := accept.StartManagedConfig(retentionZeroYAML)
|
||||
if err != nil {
|
||||
set("F18", report.StatusFail, "retention0 启动失败: "+err.Error())
|
||||
t.Errorf("ret0: %v", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = ms.Cleanup() }()
|
||||
hs := &harness.Server{HTTPBase: ms.HTTPBase, AdminHTTPBase: ms.AdminHTTPBase, AdminPassword: ms.AdminPassword}
|
||||
ac2 := accept.AdminLogin(t, hs)
|
||||
accept.CreateEndpoint(t, ac2, "f18a2", epPassword)
|
||||
accept.CreateEndpoint(t, ac2, "f18b2", epPassword)
|
||||
a2 := accept.MQTTLogin(t, ms.HTTPBase, "f18a2", epPassword)
|
||||
defer a2.Close()
|
||||
b2 := accept.MQTTLogin(t, ms.HTTPBase, "f18b2", epPassword)
|
||||
defer b2.Close()
|
||||
s2 := a2.Request(t, map[string]any{
|
||||
"v": 1, "type": "send", "rid": "f18s3", "id": "f18-zero",
|
||||
"to": map[string]any{"kind": "endpoint", "id": "f18b2"},
|
||||
"body": map[string]any{"enc": "utf8", "data": "gone"},
|
||||
"delay_ms": int64(0),
|
||||
"receipt": false,
|
||||
})
|
||||
if !s2.OK {
|
||||
set("F18", report.StatusFail, fmt.Sprintf("retention0 提交失败: %+v", s2))
|
||||
t.Errorf("s2: %+v", s2)
|
||||
return
|
||||
}
|
||||
_ = b2.WaitType(t, "msg", 8*time.Second)
|
||||
b2.Request(t, map[string]any{"v": 1, "type": "ack", "rid": "f18a2", "from": "f18a2", "id": "f18-zero"})
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
st := a2.Request(t, map[string]any{"v": 1, "type": "status", "rid": "f18st", "id": "f18-zero"})
|
||||
if st.OK {
|
||||
set("F18", report.StatusFail, fmt.Sprintf("保留天数 0 完成后 status 仍成功: %+v", st))
|
||||
t.Errorf("status still ok: %+v", st)
|
||||
return
|
||||
}
|
||||
if code, _ := st.Error["code"].(string); code != "not_found" {
|
||||
set("F18", report.StatusFail, fmt.Sprintf("期望 status not_found 得 %+v", st))
|
||||
t.Errorf("status=%+v", st)
|
||||
return
|
||||
}
|
||||
msgs, err := countSQL(filepath.Join(ms.DataDir, "nixmsg.db"), `SELECT COUNT(*) FROM messages WHERE id='f18-zero'`)
|
||||
if err != nil {
|
||||
set("F18", report.StatusFail, "读库失败: "+err.Error())
|
||||
t.Errorf("db2: %v", err)
|
||||
return
|
||||
}
|
||||
if msgs != 0 {
|
||||
set("F18", report.StatusFail, fmt.Sprintf("保留天数 0 后消息行仍在 count=%d", msgs))
|
||||
t.Errorf("msgs=%d", msgs)
|
||||
return
|
||||
}
|
||||
set("F18", report.StatusPass, "已测:确认后 message_bodies 为空;同号重试不再投递;record_retention_days=0 完成后 status=not_found 且消息行消失")
|
||||
}
|
||||
|
||||
// runF15JoinWithPasswordFresh 在干净进程上验证带对话密码建群成功(避开长会话后 PublishDown 卡住)。
|
||||
func runF15JoinWithPasswordFresh(t *testing.T) error {
|
||||
t.Helper()
|
||||
srv, err := harness.Start(harness.Options{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("harness: %w", err)
|
||||
}
|
||||
defer func() { _ = srv.Stop() }()
|
||||
ac := accept.AdminLogin(t, srv)
|
||||
accept.CreateEndpoint(t, ac, "f15jalice", epPassword)
|
||||
accept.CreateEndpoint(t, ac, "f15jbob01", epPassword)
|
||||
alice := accept.MQTTLogin(t, srv.HTTPBase, "f15jalice", epPassword)
|
||||
defer alice.Close()
|
||||
bob := accept.MQTTLogin(t, srv.HTTPBase, "f15jbob01", epPassword)
|
||||
defer bob.Close()
|
||||
setTalk := bob.Request(t, map[string]any{
|
||||
"v": 1, "type": "self.talk_password", "rid": "jtp1", "talk_password": "join-secret",
|
||||
})
|
||||
if !setTalk.OK {
|
||||
return fmt.Errorf("设对话密码失败: %+v", setTalk)
|
||||
}
|
||||
addYes := alice.Request(t, map[string]any{
|
||||
"v": 1, "type": "group.create", "rid": "f15jg", "id": "g_f15j", "name": "F15J",
|
||||
"members": []map[string]any{{"id": "f15jbob01", "talk_password": "join-secret"}},
|
||||
})
|
||||
if !addYes.OK {
|
||||
return fmt.Errorf("带密建群失败: %+v", addYes)
|
||||
}
|
||||
if fails := memberFailures(addYes.Data); len(fails) > 0 {
|
||||
return fmt.Errorf("带密建群仍失败: %+v", addYes.Data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func drainReceipts(s *accept.MQTTSession, d time.Duration) {
|
||||
deadline := time.Now().Add(d)
|
||||
for time.Now().Before(deadline) {
|
||||
if s.TryType("receipt", 40*time.Millisecond) == nil {
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func waitReceiptID(t *testing.T, s *accept.MQTTSession, msgID string, timeout time.Duration) map[string]any {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
m := s.TryType("receipt", 50*time.Millisecond)
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
if m["id"] == msgID {
|
||||
return m
|
||||
}
|
||||
}
|
||||
t.Fatalf("timeout waiting receipt id=%s", msgID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func mapItems(data any) []map[string]any {
|
||||
m, _ := data.(map[string]any)
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
raw, _ := m["items"].([]any)
|
||||
out := make([]map[string]any, 0, len(raw))
|
||||
for _, x := range raw {
|
||||
if im, ok := x.(map[string]any); ok {
|
||||
out = append(out, im)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func memberFailures(data any) []map[string]any {
|
||||
m, _ := data.(map[string]any)
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
for _, key := range []string{"failed", "failures", "failed_members"} {
|
||||
if raw, ok := m[key].([]any); ok {
|
||||
out := make([]map[string]any, 0, len(raw))
|
||||
for _, x := range raw {
|
||||
if im, ok := x.(map[string]any); ok {
|
||||
out = append(out, im)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func countSQL(dbPath, query string) (int, error) {
|
||||
dsn := "file:" + filepath.ToSlash(dbPath) + "?_pragma=query_only(1)"
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
var n int
|
||||
if err := db.QueryRow(query).Scan(&n); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
Reference in New Issue
Block a user