94 lines
3.0 KiB
Go
94 lines
3.0 KiB
Go
// codec_test.go Day 31:CMPP 头/体编解码与鉴权算法。
|
|
package cmpp
|
|
|
|
import (
|
|
"bytes"
|
|
"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()
|
|
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("服务端鉴权串不一致")
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|