| 1 | package edge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | // The gate script, run against this package's own backhaul path: a far |
| 16 | // edge serving a remote-homed host passes; the same edge with no mesh key |
| 17 | // mirrored answers unknown-host (edge.go:2699, :1428) and the gate FAILS. |
| 18 | func TestServiceGateAgainstARealBackhaul(t *testing.T) { |
| 19 | if _, err := exec.LookPath("curl"); err != nil { |
| 20 | t.Skip("curl not installed") |
| 21 | } |
| 22 | const version = "abc1234" |
| 23 | e, m, _ := hopEdge(t, "annie", "local.test") |
| 24 | _, eip := fakeHomeEdge(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 25 | w.Header().Set("X-Carlos-Version", version) |
| 26 | fmt.Fprint(w, "ok") |
| 27 | })) |
| 28 | homedOn(t, m, "betty", eip, "example.com") |
| 29 | gate := func() (string, error) { |
| 30 | ts := httptest.NewTLSServer(e) |
| 31 | defer ts.Close() |
| 32 | h, p, _ := net.SplitHostPort(ts.Listener.Addr().String()) |
| 33 | cmd := exec.Command("../../scripts/region-service-gate.sh", "example.com", version, h) |
| 34 | cmd.Env = append(os.Environ(), "GATE_PORT="+p, "GATE_INSECURE=1", "GATE_SKIP_PUBLIC=1") |
| 35 | out, err := cmd.CombinedOutput() |
| 36 | return string(out), err |
| 37 | } |
| 38 | if out, err := gate(); err != nil { |
| 39 | t.Fatalf("a working backhaul failed the gate: %v %s", err, out) |
| 40 | } |
| 41 | if err := m.s.Delete(context.Background(), meshKeyKey); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | refreshHomeMap(t, m) |
| 45 | if out, err := gate(); err == nil || !strings.Contains(out, "FAIL") { |
| 46 | t.Fatalf("a backhaul with no mesh key passed the gate: %s", out) |
| 47 | } |
| 48 | } |
| 49 | |