144 lines
5.4 KiB
Go
144 lines
5.4 KiB
Go
// codec_test.go Day 31:CMPP 头/体编解码与鉴权算法。
|
|
package cmpp
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestHeaderRoundTrip 头编解码往返。
|
|
func TestHeaderRoundTrip(t *testing.T) {
|
|
h := &Header{TotalLength: 25, CommandID: CmdConnect, SequenceID: 7}
|
|
got, err := ParseHeader(h.Marshal())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.TotalLength != 25 || got.CommandID != CmdConnect || got.SequenceID != 7 {
|
|
t.Fatalf("往返失真: %+v", got)
|
|
}
|
|
}
|
|
|
|
// TestParseHeaderGuards 非法头拒绝。
|
|
func TestParseHeaderGuards(t *testing.T) {
|
|
if _, err := ParseHeader([]byte{1, 2, 3}); err == nil {
|
|
t.Fatal("短头应拒绝")
|
|
}
|
|
h := &Header{TotalLength: 8, CommandID: CmdActiveTest}
|
|
if _, err := ParseHeader(h.Marshal()); err == nil {
|
|
t.Fatal("total_length < 12 应拒绝")
|
|
}
|
|
h.TotalLength = MaxPacketLength + 1
|
|
if _, err := ParseHeader(h.Marshal()); err == nil {
|
|
t.Fatal("超大包应拒绝")
|
|
}
|
|
}
|
|
|
|
// TestConnectAuth 鉴权算法:编码 → 解析 → VerifyAuth 通过;错密钥失败。
|
|
// 布局依据 CMPP 2.0 官方规范(经真实网关验证):27B 包体 + 4B 大端时间戳。
|
|
func TestConnectAuth(t *testing.T) {
|
|
now := time.Date(2026, 9, 5, 23, 3, 1, 0, time.Local)
|
|
ts := authTimestamp(now)
|
|
if ts != 905230301 { // 月×1e8+日×1e6+时×1e4+分×100+秒
|
|
t.Fatalf("时间戳应为 MMDDHHMMSS 十进制压缩(905230301): %d", ts)
|
|
}
|
|
c := &Connect{SourceAddr: "901234", Secret: "s3cret", Version: 0x20}
|
|
body := c.Marshal(ts)
|
|
if len(body) != 27 {
|
|
t.Fatalf("包体应为 27 字节: %d", len(body))
|
|
}
|
|
addr, auth, gotTS, err := ParseConnect(body)
|
|
if err != nil || addr != "901234" || gotTS != ts {
|
|
t.Fatalf("解析: addr=%s ts=%d err=%v", addr, gotTS, err)
|
|
}
|
|
if !VerifyAuth("901234", "s3cret", ts, auth) {
|
|
t.Fatal("正确密钥鉴权应通过")
|
|
}
|
|
if VerifyAuth("901234", "wrong", ts, auth) {
|
|
t.Fatal("错误密钥鉴权应失败")
|
|
}
|
|
}
|
|
|
|
// TestConnectRespAuth 服务端鉴权串往返。
|
|
func TestConnectRespAuth(t *testing.T) {
|
|
ts := authTimestamp(time.Now())
|
|
c := &Connect{SourceAddr: "900001", Secret: "pw"}
|
|
clientAuth := c.Authenticator(ts)
|
|
resp := &ConnectResp{Status: 0, Secret: "pw", AuthenticatorClient: clientAuth}
|
|
body := resp.Marshal()
|
|
if len(body) != 21 { // Issue#3:CMPP 2.0 布局 4B+16B+1B
|
|
t.Fatalf("connect_resp 包体应为 21 字节: %d", len(body))
|
|
}
|
|
status, ismg, err := ParseConnectResp(body)
|
|
if err != nil || status != 0 || len(ismg) != 16 {
|
|
t.Fatalf("解析: status=%d err=%v", status, err)
|
|
}
|
|
want := (&ConnectResp{Status: 0, Secret: "pw", AuthenticatorClient: clientAuth}).AuthenticatorISMG()
|
|
if !bytes.Equal(want, ismg) {
|
|
t.Fatal("服务端鉴权串不一致")
|
|
}
|
|
}
|
|
|
|
// TestConnectRespCmpp20StatusLayout Issue#3 回归:服务端 Status 必须按 CMPP 2.0
|
|
// 规范编为 4 字节大端——此前 1B 编码(3.0 布局)致标准 2.0 客户端按 4B 解析读到
|
|
// [status,MD5[0..2]] 恒非 0,客户连接被误判失败(客户端工具报 -1)。
|
|
func TestConnectRespCmpp20StatusLayout(t *testing.T) {
|
|
resp := &ConnectResp{Status: ConnectBadAuth, Secret: "pw",
|
|
AuthenticatorClient: bytes.Repeat([]byte{0xAB}, 16)}
|
|
body := resp.Marshal()
|
|
if len(body) != 21 {
|
|
t.Fatalf("CMPP 2.0 connect_resp 包体应为 21 字节(4+16+1): %d", len(body))
|
|
}
|
|
// 第三方标准 2.0 客户端视角:前 4 字节大端 uint32 即 Status
|
|
if got := binary.BigEndian.Uint32(body[0:4]); got != uint32(ConnectBadAuth) {
|
|
t.Fatalf("2.0 客户端按 4B 解析 Status=%d, 期望 %d", got, ConnectBadAuth)
|
|
}
|
|
// 成功路径:前 4 字节全零,任何布局客户端均读到 0
|
|
ok := (&ConnectResp{Status: ConnectOK, Secret: "pw",
|
|
AuthenticatorClient: bytes.Repeat([]byte{0x01}, 16)}).Marshal()
|
|
if got := binary.BigEndian.Uint32(ok[0:4]); got != 0 {
|
|
t.Fatalf("成功状态 4B 解析应全零: %d", got)
|
|
}
|
|
// 自家客户端自适应解析(21B → 2.0 布局)拒绝码同样正确
|
|
status, ismg, err := ParseConnectResp(body)
|
|
if err != nil || status != ConnectBadAuth || len(ismg) != 16 {
|
|
t.Fatalf("自适应解析: status=%d ismg=%dB err=%v", status, len(ismg), err)
|
|
}
|
|
}
|
|
|
|
// TestParseConnectRespAdaptive Issue#3:客户端自适应两种规范布局。
|
|
// 2.0(21B/4B Status)与 3.0(18B/1B Status)网关均可正确读出状态码。
|
|
func TestParseConnectRespAdaptive(t *testing.T) {
|
|
// 3.0/兼容布局(1B status=3 + 16B auth + 1B ver)
|
|
b18 := append([]byte{3}, bytes.Repeat([]byte{0x11}, 17)...)
|
|
if st, ismg, err := ParseConnectResp(b18); err != nil || st != 3 || len(ismg) != 16 {
|
|
t.Fatalf("18B 布局解析: st=%d ismg=%dB err=%v", st, len(ismg), err)
|
|
}
|
|
// 2.0 无 Version 变体(4B status + 16B = 20B)
|
|
b20 := append([]byte{0, 0, 0, 4}, bytes.Repeat([]byte{0x22}, 16)...)
|
|
if st, ismg, err := ParseConnectResp(b20); err != nil || st != 4 || len(ismg) != 16 {
|
|
t.Fatalf("20B 布局解析: st=%d ismg=%dB err=%v", st, len(ismg), err)
|
|
}
|
|
// 空包拒绝
|
|
if _, _, err := ParseConnectResp(nil); err == nil {
|
|
t.Fatal("空包应拒绝")
|
|
}
|
|
}
|
|
|
|
// TestPacketReadWrite 包读写往返(WritePacket 自动补 TotalLength)。
|
|
func TestPacketReadWrite(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
h := &Header{CommandID: CmdActiveTestResp, SequenceID: 42}
|
|
if err := WritePacket(&buf, h, ActiveTestResp(0)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rh, body, err := ReadPacket(&buf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rh.CommandID != CmdActiveTestResp || rh.SequenceID != 42 || rh.TotalLength != 13 || len(body) != 1 || body[0] != 0 {
|
|
t.Fatalf("包往返异常: %+v %v", rh, body)
|
|
}
|
|
}
|