subService_test.go 20 KB

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