| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "reflect" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/managedproxy" |
| 14 | ) |
| 15 | |
| 16 | func TestProxyRouteAPIIsAtomicAndSurvivesRefresh(t *testing.T) { |
| 17 | s := testServer(t) |
| 18 | p, _ := managedproxy.Load(s.profile) |
| 19 | entry := p.Selected |
| 20 | if err := p.Import("home", "https://example.com/home?token=private-token", []byte(`proxies: [{name: home, type: http, server: 127.0.0.1, port: 1080, password: private-password}]`), "home", 0, ""); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | exit := p.Sources[1].Nodes[0].ID |
| 24 | if err := p.Save(s.profile); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | oldValidate, oldStart, oldFetch := validateProxyCore, startProxyCore, fetchProxySource |
| 28 | t.Cleanup(func() { validateProxyCore, startProxyCore, fetchProxySource = oldValidate, oldStart, oldFetch }) |
| 29 | validateProxyCore = func(_ context.Context, profile string, p managedproxy.Store) error { |
| 30 | _, err := p.Runtime(profile) |
| 31 | return err |
| 32 | } |
| 33 | failStart := false |
| 34 | startProxyCore = func(context.Context, string, managedproxy.Store) error { |
| 35 | if failStart { |
| 36 | return errors.New("test core startup failed") |
| 37 | } |
| 38 | return nil |
| 39 | } |
| 40 | body, _ := json.Marshal(proxyInput{NodeIDs: []string{entry, exit}}) |
| 41 | if w := request(s, "POST", "/api/proxy/route", s.origin, string(body), false); w.Code != http.StatusUnauthorized { |
| 42 | t.Fatal("unauthenticated chain accepted", w.Code) |
| 43 | } |
| 44 | if w := request(s, "POST", "/api/proxy/route", s.origin, string(body), true); w.Code != http.StatusOK { |
| 45 | t.Fatal(w.Code, w.Body.String()) |
| 46 | } |
| 47 | deadline := time.Now().Add(3 * time.Second) |
| 48 | for { |
| 49 | s.mu.Lock() |
| 50 | job := s.jobs.snapshot() |
| 51 | s.mu.Unlock() |
| 52 | if !job.Running { |
| 53 | if job.Error != "" { |
| 54 | t.Fatal(job.Error) |
| 55 | } |
| 56 | break |
| 57 | } |
| 58 | if time.Now().After(deadline) { |
| 59 | t.Fatal("chain apply did not finish") |
| 60 | } |
| 61 | time.Sleep(10 * time.Millisecond) |
| 62 | } |
| 63 | applied, _ := managedproxy.Load(s.profile) |
| 64 | if !reflect.DeepEqual(applied.Chain, []string{entry, exit}) || applied.Selected != exit || applied.Revision == p.Revision { |
| 65 | t.Fatal("chain not applied atomically", applied.Chain) |
| 66 | } |
| 67 | w := request(s, "GET", "/api/proxy", s.origin, "", true) |
| 68 | var view struct { |
| 69 | Route []string `json:"route"` |
| 70 | } |
| 71 | if err := json.Unmarshal(w.Body.Bytes(), &view); err != nil || !reflect.DeepEqual(view.Route, applied.Chain) { |
| 72 | t.Fatal("chain status missing", err, w.Body.String()) |
| 73 | } |
| 74 | for _, secret := range []string{"private-token", "private-password", p.Auth} { |
| 75 | if strings.Contains(w.Body.String(), secret) { |
| 76 | t.Fatal("chain status leaked credentials") |
| 77 | } |
| 78 | } |
| 79 | cfgBefore, _ := loadConfig(s.configPath) |
| 80 | failStart = true |
| 81 | if err := s.changeProxy("route", proxyInput{NodeIDs: []string{exit}}, nil); err == nil { |
| 82 | t.Fatal("startup failure accepted") |
| 83 | } |
| 84 | after, _ := managedproxy.Load(s.profile) |
| 85 | cfgAfter, _ := loadConfig(s.configPath) |
| 86 | if !reflect.DeepEqual(after, applied) || !reflect.DeepEqual(cfgAfter, cfgBefore) { |
| 87 | t.Fatal("failed chain application replaced last saved route") |
| 88 | } |
| 89 | failStart = false |
| 90 | if err := s.changeProxy("route", proxyInput{NodeIDs: []string{entry, entry}}, nil); err == nil { |
| 91 | t.Fatal("repeated node accepted") |
| 92 | } |
| 93 | if err := s.changeProxy("delete", proxyInput{ID: "source"}, nil); err == nil { |
| 94 | t.Fatal("active transit source removed") |
| 95 | } |
| 96 | fetchProxySource = func(context.Context, *http.Client, string) ([]byte, string, error) { |
| 97 | return []byte(`proxies: [{name: home, type: http, server: 127.0.0.1, port: 2080, password: updated}]`), "", nil |
| 98 | } |
| 99 | if err := s.changeProxy("refresh", proxyInput{ID: "home", HostDownload: true}, nil); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | refreshed, _ := managedproxy.Load(s.profile) |
| 103 | if !reflect.DeepEqual(refreshed.Chain, applied.Chain) || refreshed.Sources[1].Nodes[0].Config["password"] != "updated" { |
| 104 | t.Fatal("subscription refresh lost the chain") |
| 105 | } |
| 106 | fetchProxySource = func(context.Context, *http.Client, string) ([]byte, string, error) { |
| 107 | return []byte(`proxies: [{name: replacement, type: http, server: 127.0.0.1, port: 3080}]`), "", nil |
| 108 | } |
| 109 | if err := s.changeProxy("refresh", proxyInput{ID: "home", HostDownload: true}, nil); err == nil { |
| 110 | t.Fatal("missing exit silently replaced") |
| 111 | } |
| 112 | retained, _ := managedproxy.Load(s.profile) |
| 113 | if !reflect.DeepEqual(retained.Chain, applied.Chain) || retained.Sources[1].Nodes[0].ID != exit { |
| 114 | t.Fatal("missing node refresh discarded the working chain") |
| 115 | } |
| 116 | if err := s.changeProxy("route", proxyInput{NodeIDs: []string{exit}}, nil); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | if err := s.changeProxy("delete", proxyInput{ID: "home"}, nil); err != nil { |
| 120 | t.Fatal("single-node source could not be deleted", err) |
| 121 | } |
| 122 | deleted, _ := managedproxy.Load(s.profile) |
| 123 | if deleted.Selected != "" || len(deleted.Chain) != 0 { |
| 124 | t.Fatal("deleted single-node source retained an active route") |
| 125 | } |
| 126 | |
| 127 | } |