MCPcopy Create free account
hub / github.com/SamNet-dev/snix / TestPatchVNextRewritesAddressAndPort

Function TestPatchVNextRewritesAddressAndPort

integrate/integrate_test.go:12–73  ·  view source on GitHub ↗

TestPatchVNextRewritesAddressAndPort verifies the Xray/v2ray VLESS shape is patched in-place and unrelated fields are preserved.

(t *testing.T)

Source from the content-addressed store, hash-verified

10// TestPatchVNextRewritesAddressAndPort verifies the Xray/v2ray VLESS shape
11// is patched in-place and unrelated fields are preserved.
12func TestPatchVNextRewritesAddressAndPort(t *testing.T) {
13 src := []byte(`{
14 "log": {"loglevel": "info"},
15 "outbounds": [
16 {
17 "protocol": "vless",
18 "settings": {
19 "vnext": [
20 {
21 "address": "myproxy.example.com",
22 "port": 443,
23 "users": [{"id": "abc", "encryption": "none"}]
24 }
25 ]
26 },
27 "streamSettings": {"network": "tcp"}
28 }
29 ]
30}`)
31 dir := t.TempDir()
32 p := filepath.Join(dir, "config.json")
33 if err := os.WriteFile(p, src, 0o644); err != nil {
34 t.Fatal(err)
35 }
36
37 patch := jsonProxyPatcher(p)
38 if err := patch("myproxy.example.com", 443); err != nil {
39 t.Fatalf("patch: %v", err)
40 }
41
42 // Backup exists.
43 if _, err := os.Stat(p + ".bak"); err != nil {
44 t.Fatalf("backup missing: %v", err)
45 }
46
47 // Re-read and verify.
48 got, _ := os.ReadFile(p)
49 var doc map[string]any
50 if err := json.Unmarshal(got, &doc); err != nil {
51 t.Fatal(err)
52 }
53 ob := doc["outbounds"].([]any)[0].(map[string]any)
54 settings := ob["settings"].(map[string]any)
55 vn := settings["vnext"].([]any)[0].(map[string]any)
56 if vn["address"] != "127.0.0.1" {
57 t.Fatalf("address: got %v want 127.0.0.1", vn["address"])
58 }
59 if vn["port"].(float64) != 40443 {
60 t.Fatalf("port: got %v want 40443", vn["port"])
61 }
62 // Untouched fields survived.
63 users := vn["users"].([]any)[0].(map[string]any)
64 if users["id"] != "abc" {
65 t.Fatalf("users.id clobbered: %v", users["id"])
66 }
67 if ob["streamSettings"] == nil {
68 t.Fatal("streamSettings dropped")
69 }

Callers

nothing calls this directly

Calls 1

jsonProxyPatcherFunction · 0.85

Tested by

no test coverage detected