Skip to content

Phase 2c:SipEncoder + 全 fixture roundtrip 闭环 - #5

Draft
lixuanqun wants to merge 1 commit into
cursor/sip-parser-3c36from
cursor/sip-encoder-3c36
Draft

Phase 2c:SipEncoder + 全 fixture roundtrip 闭环#5
lixuanqun wants to merge 1 commit into
cursor/sip-parser-3c36from
cursor/sip-encoder-3c36

Conversation

@lixuanqun

Copy link
Copy Markdown
Owner

概述

补齐 codec 的"另一半":把 SipMessage 写回字节,并让所有 accept fixture 通过 parse → encode → parse roundtrip 闸门。

🔗 stacked on PR #4#3#2

实装

SipEncoder

  • Request-Line / Status-Line 直写 ASCII
  • Headers 按插入顺序保序(关键:Via 必须保栈式语义)
  • Content-Length 始终由实际 body 长度推导——即使输入消息携带不一致的值也会被纠正,保住"wire 输出自洽"的关键不变量
  • Status-Code 用单字符算术输出(绕过 String.format 分配)
  • header value / reason phrase 走 UTF-8

Roundtrip 闸门

新增 everyAcceptFixtureRoundtripsThroughEncoder 动态测试系列。每个 accept fixture:

  1. SipParser.parse(fx.bytes())SipMessage A
  2. SipEncoder.encode(A) → bytes B
  3. SipParser.parse(B)SipMessage C
  4. 断言:A.body == C.body、A.startLine == C.startLine、headerCount 不变

测试

$ mvn -B verify
SUCCESS — 33 tests, 0 failures
  sip-message:   10
  sip-codec:     13  (8 parser + 5 encoder)
  compliance:    27 + 6 roundtrip
Open in Web Open in Cursor 

SipEncoder(手写 ByteArrayOutputStream 拼装)
- Request-Line / Status-Line:直写 ASCII
- Headers:按插入顺序保序输出(保 Via 栈式语义)
- 关键不变量:Content-Length 始终由实际 body 长度推导,
  即使输入消息携带不一致的 Content-Length 也会被纠正
- Status-Code 用单字符算术输出(避免 String 分配)
- header value / reason phrase 走 UTF-8(RFC 3261 §7.3.1)

SipEncoderTest(5 个单测)
- OPTIONS 请求编码
- 200 OK 响应编码
- Content-Length 与实际 body 长度自动同步
- 缺 Content-Length 时自动注入
- parse→encode→parse 不动语义

ParserConformanceTest 扩展
- 新增 'roundtrip' dynamic-test 系列
- 每个 accept fixture 跑 parse→encode→parse,断言:
  · body 字节级相等
  · start-line(method/URI 或 status/reason)相等
  · header count 与 manifest 声明一致

全 6 个 accept fixture 通过 roundtrip:
  self-test/simple-options, simple-200-ok, invite-with-sdp
  rfc4475/3.1.1.6-lwsdisp, 3.1.1.9-semiuri, 3.1.1.10-transports

mvn verify:9 模块 SUCCESS,33 tests 全绿。

Co-authored-by: li xuanqun <793005378@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants