subService_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. package sub
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/database/model"
  8. )
  9. func TestFindClientIndex(t *testing.T) {
  10. clients := []model.Client{
  11. {Email: "[email protected]"},
  12. {Email: "[email protected]"},
  13. {Email: "[email protected]"},
  14. }
  15. if got := findClientIndex(clients, "[email protected]"); got != 1 {
  16. t.Fatalf("findClientIndex middle = %d, want 1", got)
  17. }
  18. if got := findClientIndex(clients, "[email protected]"); got != 0 {
  19. t.Fatalf("findClientIndex first = %d, want 0", got)
  20. }
  21. if got := findClientIndex(clients, "[email protected]"); got != -1 {
  22. t.Fatalf("findClientIndex missing = %d, want -1", got)
  23. }
  24. if got := findClientIndex(nil, "x"); got != -1 {
  25. t.Fatalf("findClientIndex on nil slice = %d, want -1", got)
  26. }
  27. }
  28. func TestUnmarshalStreamSettings(t *testing.T) {
  29. got := unmarshalStreamSettings(`{"network":"ws","wsSettings":{"path":"/api"}}`)
  30. if got["network"] != "ws" {
  31. t.Fatalf("network = %v, want ws", got["network"])
  32. }
  33. ws, ok := got["wsSettings"].(map[string]any)
  34. if !ok || ws["path"] != "/api" {
  35. t.Fatalf("wsSettings = %v, want map with path=/api", got["wsSettings"])
  36. }
  37. }
  38. func TestUnmarshalStreamSettings_InvalidJSON(t *testing.T) {
  39. if got := unmarshalStreamSettings("not json"); got != nil {
  40. t.Fatalf("invalid JSON should produce nil map, got %#v", got)
  41. }
  42. }
  43. func TestSearchHost_StringValue(t *testing.T) {
  44. headers := map[string]any{"Host": "example.com"}
  45. if got := searchHost(headers); got != "example.com" {
  46. t.Fatalf("searchHost = %q, want example.com", got)
  47. }
  48. }
  49. func TestSearchHost_CaseInsensitiveKey(t *testing.T) {
  50. headers := map[string]any{"host": "example.com"}
  51. if got := searchHost(headers); got != "example.com" {
  52. t.Fatalf("searchHost = %q, want example.com", got)
  53. }
  54. headers2 := map[string]any{"HOST": "example.com"}
  55. if got := searchHost(headers2); got != "example.com" {
  56. t.Fatalf("searchHost uppercase = %q, want example.com", got)
  57. }
  58. }
  59. func TestSearchHost_ArrayValue(t *testing.T) {
  60. headers := map[string]any{"Host": []any{"first.example.com", "second.example.com"}}
  61. if got := searchHost(headers); got != "first.example.com" {
  62. t.Fatalf("searchHost array = %q, want first.example.com", got)
  63. }
  64. }
  65. func TestSearchHost_EmptyArray(t *testing.T) {
  66. headers := map[string]any{"Host": []any{}}
  67. if got := searchHost(headers); got != "" {
  68. t.Fatalf("searchHost empty array = %q, want empty", got)
  69. }
  70. }
  71. func TestSearchHost_NoHostKey(t *testing.T) {
  72. headers := map[string]any{"X-Other": "value"}
  73. if got := searchHost(headers); got != "" {
  74. t.Fatalf("searchHost no host = %q, want empty", got)
  75. }
  76. }
  77. func TestSearchHost_NotAMap(t *testing.T) {
  78. if got := searchHost("not a map"); got != "" {
  79. t.Fatalf("searchHost non-map = %q, want empty", got)
  80. }
  81. if got := searchHost(nil); got != "" {
  82. t.Fatalf("searchHost nil = %q, want empty", got)
  83. }
  84. }
  85. func TestSearchKey_FoundAtTopLevel(t *testing.T) {
  86. data := map[string]any{"foo": 42, "bar": "x"}
  87. got, ok := searchKey(data, "foo")
  88. if !ok {
  89. t.Fatal("expected to find foo")
  90. }
  91. if got != 42 {
  92. t.Fatalf("got %v, want 42", got)
  93. }
  94. }
  95. func TestSearchKey_FoundInNested(t *testing.T) {
  96. data := map[string]any{
  97. "outer": map[string]any{
  98. "inner": map[string]any{
  99. "target": "hit",
  100. },
  101. },
  102. }
  103. got, ok := searchKey(data, "target")
  104. if !ok {
  105. t.Fatal("expected to find target in nested map")
  106. }
  107. if got != "hit" {
  108. t.Fatalf("got %v, want hit", got)
  109. }
  110. }
  111. func TestSearchKey_FoundInsideArray(t *testing.T) {
  112. data := map[string]any{
  113. "list": []any{
  114. map[string]any{"other": 1},
  115. map[string]any{"needle": "found"},
  116. },
  117. }
  118. got, ok := searchKey(data, "needle")
  119. if !ok {
  120. t.Fatal("expected to find needle in array element")
  121. }
  122. if got != "found" {
  123. t.Fatalf("got %v, want found", got)
  124. }
  125. }
  126. func TestSearchKey_NotFound(t *testing.T) {
  127. data := map[string]any{"foo": "bar"}
  128. if _, ok := searchKey(data, "missing"); ok {
  129. t.Fatal("expected ok=false for missing key")
  130. }
  131. }
  132. func TestSearchKey_OnScalar(t *testing.T) {
  133. if _, ok := searchKey(42, "anything"); ok {
  134. t.Fatal("expected ok=false searching on a scalar")
  135. }
  136. }
  137. func TestBuildXhttpExtra_IncludesClientSideFieldsWhenPresent(t *testing.T) {
  138. extra := buildXhttpExtra(map[string]any{
  139. "path": "/xhttp",
  140. "host": "example.com",
  141. "mode": "packet-up",
  142. "xPaddingBytes": "100-1000",
  143. "uplinkHTTPMethod": "GET",
  144. "uplinkChunkSize": float64(4096),
  145. "noGRPCHeader": true,
  146. "scMinPostsIntervalMs": "20-40",
  147. "xmux": map[string]any{
  148. "maxConcurrency": "16-32",
  149. "hMaxRequestTimes": "600-900",
  150. "hMaxReusableSecs": "1800-3000",
  151. "hKeepAlivePeriod": float64(15),
  152. },
  153. "downloadSettings": map[string]any{
  154. "network": "xhttp",
  155. },
  156. "headers": map[string]any{
  157. "Host": "ignored.example.com",
  158. "X-Forwarded": "1",
  159. "X-Test-Empty": "",
  160. },
  161. })
  162. if extra["path"] != nil || extra["host"] != nil {
  163. t.Fatalf("path/host should stay top-level, got extra %#v", extra)
  164. }
  165. for _, key := range []string{
  166. "xPaddingBytes",
  167. "uplinkHTTPMethod",
  168. "uplinkChunkSize",
  169. "noGRPCHeader",
  170. "scMinPostsIntervalMs",
  171. "xmux",
  172. "downloadSettings",
  173. } {
  174. if _, ok := extra[key]; !ok {
  175. t.Fatalf("extra missing %q: %#v", key, extra)
  176. }
  177. }
  178. if _, ok := extra["mode"]; ok {
  179. t.Fatalf("mode should stay as a top-level query parameter, got extra %#v", extra)
  180. }
  181. headers, ok := extra["headers"].(map[string]any)
  182. if !ok {
  183. t.Fatalf("headers = %#v, want map", extra["headers"])
  184. }
  185. if _, ok := headers["Host"]; ok {
  186. t.Fatalf("headers should not include Host: %#v", headers)
  187. }
  188. if headers["X-Forwarded"] != "1" {
  189. t.Fatalf("headers[X-Forwarded] = %#v, want 1", headers["X-Forwarded"])
  190. }
  191. }
  192. func TestBuildXhttpExtra_LeavesDefaultClientSideFieldsOut(t *testing.T) {
  193. extra := buildXhttpExtra(map[string]any{
  194. "uplinkHTTPMethod": "",
  195. "uplinkChunkSize": float64(0),
  196. "noGRPCHeader": false,
  197. "xmux": map[string]any{},
  198. "downloadSettings": map[string]any{},
  199. })
  200. if extra != nil {
  201. t.Fatalf("default-only xhttp extra = %#v, want nil", extra)
  202. }
  203. }
  204. func TestCloneStringMap(t *testing.T) {
  205. src := map[string]string{"a": "1", "b": "2"}
  206. dst := cloneStringMap(src)
  207. if len(dst) != len(src) {
  208. t.Fatalf("clone length = %d, want %d", len(dst), len(src))
  209. }
  210. for k, v := range src {
  211. if dst[k] != v {
  212. t.Fatalf("clone[%q] = %q, want %q", k, dst[k], v)
  213. }
  214. }
  215. dst["a"] = "changed"
  216. if src["a"] == "changed" {
  217. t.Fatal("modifying clone leaked into source")
  218. }
  219. }
  220. func TestCloneStringMap_Empty(t *testing.T) {
  221. dst := cloneStringMap(map[string]string{})
  222. if dst == nil {
  223. t.Fatal("clone of empty map should not be nil")
  224. }
  225. if len(dst) != 0 {
  226. t.Fatalf("clone of empty map should be empty, got %v", dst)
  227. }
  228. }
  229. func TestGetHostFromXFH_HostOnly(t *testing.T) {
  230. got, err := getHostFromXFH("example.com")
  231. if err != nil {
  232. t.Fatalf("unexpected error: %v", err)
  233. }
  234. if got != "example.com" {
  235. t.Fatalf("got %q, want example.com", got)
  236. }
  237. }
  238. func TestGetHostFromXFH_HostWithPort(t *testing.T) {
  239. got, err := getHostFromXFH("example.com:8443")
  240. if err != nil {
  241. t.Fatalf("unexpected error: %v", err)
  242. }
  243. if got != "example.com" {
  244. t.Fatalf("got %q, want example.com", got)
  245. }
  246. }
  247. func TestGetHostFromXFH_IPv6WithPort(t *testing.T) {
  248. got, err := getHostFromXFH("[2606:4700::1111]:443")
  249. if err != nil {
  250. t.Fatalf("unexpected error: %v", err)
  251. }
  252. if got != "2606:4700::1111" {
  253. t.Fatalf("got %q, want 2606:4700::1111", got)
  254. }
  255. }
  256. func TestGetHostFromXFH_BadHostPort(t *testing.T) {
  257. if _, err := getHostFromXFH("example.com:8443:9999"); err == nil {
  258. t.Fatal("expected error for malformed host:port")
  259. }
  260. }
  261. func TestReadPositiveInt(t *testing.T) {
  262. cases := []struct {
  263. name string
  264. in any
  265. wantVal int
  266. wantOk bool
  267. }{
  268. {"int_positive", int(5), 5, true},
  269. {"int_zero", int(0), 0, false},
  270. {"int_negative", int(-3), -3, false},
  271. {"int32_positive", int32(7), 7, true},
  272. {"int64_positive", int64(99), 99, true},
  273. {"float64_positive", float64(12), 12, true},
  274. {"float64_zero", float64(0.0), 0, false},
  275. {"float64_negative", float64(-1.5), -1, false},
  276. {"float32_positive", float32(3), 3, true},
  277. {"string", "not a number", 0, false},
  278. {"nil", nil, 0, false},
  279. }
  280. for _, c := range cases {
  281. t.Run(c.name, func(t *testing.T) {
  282. gotVal, gotOk := readPositiveInt(c.in)
  283. if gotVal != c.wantVal || gotOk != c.wantOk {
  284. t.Fatalf("readPositiveInt(%v) = (%d, %v), want (%d, %v)", c.in, gotVal, gotOk, c.wantVal, c.wantOk)
  285. }
  286. })
  287. }
  288. }
  289. func TestSetStringParam(t *testing.T) {
  290. p := map[string]string{"existing": "value"}
  291. setStringParam(p, "new", "hello")
  292. if p["new"] != "hello" {
  293. t.Fatalf("missing key after set: %v", p)
  294. }
  295. setStringParam(p, "existing", "")
  296. if _, ok := p["existing"]; ok {
  297. t.Fatalf("empty value should delete the key, got %v", p)
  298. }
  299. }
  300. func TestSetIntParam(t *testing.T) {
  301. p := map[string]string{"existing": "10"}
  302. setIntParam(p, "n", 42)
  303. if p["n"] != "42" {
  304. t.Fatalf("set positive int: got %v", p)
  305. }
  306. setIntParam(p, "existing", 0)
  307. if _, ok := p["existing"]; ok {
  308. t.Fatalf("zero value should delete the key, got %v", p)
  309. }
  310. p["other"] = "5"
  311. setIntParam(p, "other", -1)
  312. if _, ok := p["other"]; ok {
  313. t.Fatalf("negative value should delete the key, got %v", p)
  314. }
  315. }
  316. func TestSetStringField(t *testing.T) {
  317. f := map[string]any{"existing": "value"}
  318. setStringField(f, "new", "hello")
  319. if f["new"] != "hello" {
  320. t.Fatalf("missing key after set: %v", f)
  321. }
  322. setStringField(f, "existing", "")
  323. if _, ok := f["existing"]; ok {
  324. t.Fatalf("empty value should delete the key, got %v", f)
  325. }
  326. }
  327. func TestSetIntField(t *testing.T) {
  328. f := map[string]any{"existing": 10}
  329. setIntField(f, "n", 7)
  330. if f["n"] != 7 {
  331. t.Fatalf("set positive int: got %v", f)
  332. }
  333. setIntField(f, "existing", 0)
  334. if _, ok := f["existing"]; ok {
  335. t.Fatalf("zero value should delete the key, got %v", f)
  336. }
  337. }
  338. func TestBuildVmessLink(t *testing.T) {
  339. obj := map[string]any{
  340. "v": "2",
  341. "ps": "remark",
  342. "add": "example.com",
  343. "port": 443,
  344. "net": "tcp",
  345. }
  346. link := buildVmessLink(obj)
  347. if !strings.HasPrefix(link, "vmess://") {
  348. t.Fatalf("missing vmess:// prefix: %q", link)
  349. }
  350. payload := strings.TrimPrefix(link, "vmess://")
  351. decoded, err := base64.StdEncoding.DecodeString(payload)
  352. if err != nil {
  353. t.Fatalf("base64 decode failed: %v", err)
  354. }
  355. var roundTrip map[string]any
  356. if err := json.Unmarshal(decoded, &roundTrip); err != nil {
  357. t.Fatalf("decoded payload is not JSON: %v\n%s", err, decoded)
  358. }
  359. if roundTrip["add"] != "example.com" {
  360. t.Fatalf("round-trip add = %v, want example.com", roundTrip["add"])
  361. }
  362. if roundTrip["ps"] != "remark" {
  363. t.Fatalf("round-trip ps = %v, want remark", roundTrip["ps"])
  364. }
  365. }
  366. func TestCloneVmessShareObj_CopiesEverythingByDefault(t *testing.T) {
  367. base := map[string]any{
  368. "v": "2",
  369. "sni": "example.com",
  370. "alpn": "h2",
  371. "fp": "chrome",
  372. "net": "tcp",
  373. }
  374. out := cloneVmessShareObj(base, "tls")
  375. for _, key := range []string{"sni", "alpn", "fp", "net", "v"} {
  376. if _, ok := out[key]; !ok {
  377. t.Fatalf("expected key %q to be preserved when security=tls, got %v", key, out)
  378. }
  379. }
  380. }
  381. func TestCloneVmessShareObj_NoneStripsTLSOnlyKeys(t *testing.T) {
  382. base := map[string]any{
  383. "v": "2",
  384. "sni": "example.com",
  385. "alpn": "h2",
  386. "fp": "chrome",
  387. "net": "tcp",
  388. }
  389. out := cloneVmessShareObj(base, "none")
  390. for _, key := range []string{"sni", "alpn", "fp"} {
  391. if _, ok := out[key]; ok {
  392. t.Fatalf("security=none should strip %q, got %v", key, out)
  393. }
  394. }
  395. if out["v"] != "2" || out["net"] != "tcp" {
  396. t.Fatalf("non-TLS keys should remain, got %v", out)
  397. }
  398. }
  399. func TestApplyExternalProxyTLSParams_UsesProxyDomainAndOverrides(t *testing.T) {
  400. params := map[string]string{
  401. "security": "tls",
  402. "sni": "origin.example.com",
  403. "fp": "firefox",
  404. "alpn": "h2",
  405. }
  406. ep := map[string]any{
  407. "dest": "proxy.example.com",
  408. "sni": "tls.example.com",
  409. "fingerprint": "chrome",
  410. "alpn": []any{"h3", "h2"},
  411. }
  412. applyExternalProxyTLSParams(ep, params, "tls")
  413. if params["sni"] != "tls.example.com" {
  414. t.Fatalf("sni = %q, want tls.example.com", params["sni"])
  415. }
  416. if params["fp"] != "chrome" {
  417. t.Fatalf("fp = %q, want chrome", params["fp"])
  418. }
  419. if params["alpn"] != "h3,h2" {
  420. t.Fatalf("alpn = %q, want h3,h2", params["alpn"])
  421. }
  422. }
  423. func TestApplyExternalProxyTLSParams_FallsBackToDestSNI(t *testing.T) {
  424. params := map[string]string{"security": "tls"}
  425. ep := map[string]any{"dest": "proxy.example.com"}
  426. applyExternalProxyTLSParams(ep, params, "tls")
  427. if params["sni"] != "proxy.example.com" {
  428. t.Fatalf("sni = %q, want proxy.example.com", params["sni"])
  429. }
  430. }
  431. func TestApplyExternalProxyTLSToStream_DoesNotLeakAcrossProxies(t *testing.T) {
  432. stream := map[string]any{
  433. "security": "tls",
  434. "tlsSettings": map[string]any{},
  435. }
  436. proxies := []map[string]any{
  437. {"dest": "a.example.com", "fingerprint": "chrome", "alpn": []any{"h3"}},
  438. {"dest": "b.example.com"},
  439. }
  440. results := make([]map[string]any, 0, len(proxies))
  441. for _, ep := range proxies {
  442. working := cloneStreamForExternalProxy(stream)
  443. applyExternalProxyTLSToStream(ep, working, "tls")
  444. ts := working["tlsSettings"].(map[string]any)
  445. snapshot := map[string]any{
  446. "serverName": ts["serverName"],
  447. "fingerprint": ts["fingerprint"],
  448. "alpn": ts["alpn"],
  449. }
  450. results = append(results, snapshot)
  451. }
  452. if results[0]["serverName"] != "a.example.com" || results[0]["fingerprint"] != "chrome" {
  453. t.Fatalf("proxy A snapshot = %v", results[0])
  454. }
  455. if results[1]["serverName"] != "b.example.com" {
  456. t.Fatalf("proxy B serverName = %v, want b.example.com", results[1]["serverName"])
  457. }
  458. if results[1]["fingerprint"] != nil {
  459. t.Fatalf("proxy B should inherit no fingerprint, got %v (leaked from A)", results[1]["fingerprint"])
  460. }
  461. if results[1]["alpn"] != nil {
  462. t.Fatalf("proxy B should inherit no alpn, got %v (leaked from A)", results[1]["alpn"])
  463. }
  464. }
  465. func TestApplyExternalProxyTLSParams_DoesNotApplyForNone(t *testing.T) {
  466. params := map[string]string{
  467. "security": "none",
  468. "sni": "origin.example.com",
  469. }
  470. ep := map[string]any{
  471. "dest": "proxy.example.com",
  472. "fingerprint": "chrome",
  473. "alpn": []any{"h3"},
  474. }
  475. applyExternalProxyTLSParams(ep, params, "none")
  476. if params["sni"] != "origin.example.com" {
  477. t.Fatalf("sni should not change for security=none, got %q", params["sni"])
  478. }
  479. if _, ok := params["fp"]; ok {
  480. t.Fatalf("fp should not be set for security=none, got %v", params)
  481. }
  482. if _, ok := params["alpn"]; ok {
  483. t.Fatalf("alpn should not be set for security=none, got %v", params)
  484. }
  485. }
  486. func TestExtractKcpShareFields_Defaults(t *testing.T) {
  487. stream := map[string]any{}
  488. got := extractKcpShareFields(stream)
  489. if got.headerType != "none" {
  490. t.Fatalf("default headerType = %q, want none", got.headerType)
  491. }
  492. if got.seed != "" || got.mtu != 0 || got.tti != 0 {
  493. t.Fatalf("default kcpShareFields should be zero except headerType, got %+v", got)
  494. }
  495. }
  496. func TestExtractKcpShareFields_ReadsAllFields(t *testing.T) {
  497. stream := map[string]any{
  498. "kcpSettings": map[string]any{
  499. "header": map[string]any{"type": "wechat-video"},
  500. "seed": "secret-seed",
  501. "mtu": float64(1350),
  502. "tti": float64(50),
  503. },
  504. }
  505. got := extractKcpShareFields(stream)
  506. if got.headerType != "wechat-video" {
  507. t.Fatalf("headerType = %q, want wechat-video", got.headerType)
  508. }
  509. if got.seed != "secret-seed" {
  510. t.Fatalf("seed = %q, want secret-seed", got.seed)
  511. }
  512. if got.mtu != 1350 {
  513. t.Fatalf("mtu = %d, want 1350", got.mtu)
  514. }
  515. if got.tti != 50 {
  516. t.Fatalf("tti = %d, want 50", got.tti)
  517. }
  518. }
  519. func TestKcpShareFields_ApplyToParams(t *testing.T) {
  520. params := map[string]string{}
  521. kcpShareFields{headerType: "wechat-video", seed: "s", mtu: 1350, tti: 50}.applyToParams(params)
  522. if params["headerType"] != "wechat-video" {
  523. t.Fatalf("headerType param = %q", params["headerType"])
  524. }
  525. if params["seed"] != "s" {
  526. t.Fatalf("seed param = %q", params["seed"])
  527. }
  528. if params["mtu"] != "1350" {
  529. t.Fatalf("mtu param = %q", params["mtu"])
  530. }
  531. if params["tti"] != "50" {
  532. t.Fatalf("tti param = %q", params["tti"])
  533. }
  534. }
  535. func TestKcpShareFields_ApplyToParams_NoneHeaderNotAdded(t *testing.T) {
  536. params := map[string]string{}
  537. kcpShareFields{headerType: "none"}.applyToParams(params)
  538. if _, ok := params["headerType"]; ok {
  539. t.Fatalf("headerType=none should not be added, got %v", params)
  540. }
  541. }
  542. func TestMarshalFinalMask_EmptyReturnsFalse(t *testing.T) {
  543. if _, ok := marshalFinalMask(map[string]any{}); ok {
  544. t.Fatal("expected ok=false for empty finalmask")
  545. }
  546. if _, ok := marshalFinalMask(nil); ok {
  547. t.Fatal("expected ok=false for nil finalmask")
  548. }
  549. }
  550. func TestMarshalFinalMask_WithContent(t *testing.T) {
  551. fm := map[string]any{
  552. "tcp": []any{
  553. map[string]any{"type": "fragment"},
  554. },
  555. }
  556. out, ok := marshalFinalMask(fm)
  557. if !ok {
  558. t.Fatal("expected ok=true for finalmask with valid tcp mask")
  559. }
  560. if !strings.Contains(out, `"tcp"`) {
  561. t.Fatalf("marshaled finalmask missing tcp key: %s", out)
  562. }
  563. if !strings.Contains(out, "fragment") {
  564. t.Fatalf("marshaled finalmask missing mask type: %s", out)
  565. }
  566. }
  567. func TestMarshalFinalMask_UnknownTypeIsDropped(t *testing.T) {
  568. fm := map[string]any{
  569. "tcp": []any{
  570. map[string]any{"type": "not-a-real-mask"},
  571. },
  572. }
  573. if _, ok := marshalFinalMask(fm); ok {
  574. t.Fatal("unknown mask types should be dropped, leaving nothing to marshal")
  575. }
  576. }
  577. func TestHasFinalMaskContent(t *testing.T) {
  578. if hasFinalMaskContent(nil) {
  579. t.Fatal("nil should not count as content")
  580. }
  581. if hasFinalMaskContent(map[string]any{}) {
  582. t.Fatal("empty map should not count as content")
  583. }
  584. if !hasFinalMaskContent(map[string]any{"x": 1}) {
  585. t.Fatal("non-empty map should count as content")
  586. }
  587. }