subService_test.go 18 KB

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