| 1 | //go:build browser && darwin |
| 2 | |
| 3 | package callstest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | // TestNativeMacIceRecovery is the incident of 2026-10-21, about 14:41 UTC: |
| 18 | // Paul on the Mac app, sharing his screen; Farshad, in the call from a |
| 19 | // browser, left it and came back from the Mac app; the connection between |
| 20 | // Paul's app and Farshad's never came up. Paul saw a frozen picture, |
| 21 | // "Reconnecting to Farshad (Mac)…" and "A participant's media could not |
| 22 | // connect", and it never recovered on its own. Farshad leaving and joining |
| 23 | // again mended it. |
| 24 | // |
| 25 | // Here: app A shares; app B (the stand-in for Farshad's first client) |
| 26 | // joins and leaves; app C, the same person, joins with its ICE blocked |
| 27 | // (ICETestBlock: every candidate either way leads nowhere) for |
| 28 | // ICE_BLOCK_SECONDS. The block lifts by itself, and nothing that was |
| 29 | // exchanged while it lasted is any use afterwards — only fresh candidates |
| 30 | // (an ICE restart) or a fresh connection get the pair talking. Up to |
| 31 | // 0.4.10 nothing ever made either, and the pair stayed down until someone |
| 32 | // left. The test then asks that A decodes C's camera and screen again with |
| 33 | // nobody doing anything. |
| 34 | // |
| 35 | // Variants (ICE_RECOVERY_CASE): |
| 36 | // - "both" (default): C is the same build as A. |
| 37 | // - "old-peer": C does nothing to recover (CALLS_AUTO_RECOVERY=0) and |
| 38 | // sends no client version (CALLS_TEST_NO_CLIENT=1), as an app from |
| 39 | // before this release; A has to mend the pair on its own. |
| 40 | // - ICE_RECOVERY_PEER=<path to an older CallsInterop>: C is that |
| 41 | // binary, which has no block of its own, so the block is A's (every |
| 42 | // candidate A sends or receives) — the old app really is old. |
| 43 | // |
| 44 | // The firewall flake TestNativeMacScreenShare describes (two unsigned |
| 45 | // apps on one Mac connecting only some of the time) is handled the way |
| 46 | // TestNativeMacScreenPairRecovery handles it: A and B get three tries to |
| 47 | // connect before anything else happens. |
| 48 | func TestNativeMacIceRecovery(t *testing.T) { |
| 49 | root, err := filepath.Abs("../..") |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | current := filepath.Join(root, "native/apple/.build/debug/CallsInterop") |
| 54 | peerBin := current |
| 55 | if v := os.Getenv("ICE_RECOVERY_PEER"); v != "" { |
| 56 | peerBin = v |
| 57 | } |
| 58 | for _, b := range []string{current, peerBin} { |
| 59 | if _, err := os.Stat(b); err != nil { |
| 60 | t.Fatalf("no interop executable at %s: build with make native-mac first", b) |
| 61 | } |
| 62 | } |
| 63 | block := 25 * time.Second |
| 64 | if v, err := time.ParseDuration(os.Getenv("ICE_BLOCK_SECONDS") + "s"); err == nil && v > 0 { |
| 65 | block = v |
| 66 | } |
| 67 | mode := envOr("ICE_RECOVERY_CASE", "both") |
| 68 | oldBinary := peerBin != current |
| 69 | |
| 70 | rig := newBrowserRig(t) |
| 71 | room, _ := rig.createMeeting(t) |
| 72 | ctx, cancel := context.WithTimeout(t.Context(), block+6*time.Minute) |
| 73 | defer cancel() |
| 74 | |
| 75 | var a, b *nativeApp |
| 76 | for attempt := 1; ; attempt++ { |
| 77 | a = startNative(ctx, t, current, rig, room, rig.cookieA, "a") |
| 78 | waitNative(t, "the sharing app is in", func() bool { return a.sample().Phase == "admitted" }, a) |
| 79 | b = startNative(ctx, t, current, rig, room, rig.cookieB, "b") |
| 80 | if pollNative(35*time.Second, func() bool { return a.stat("framesDecoded") > 15 && b.stat("framesDecoded") > 15 }) { |
| 81 | break |
| 82 | } |
| 83 | a.leave(t) |
| 84 | b.leave(t) |
| 85 | if attempt == 3 { |
| 86 | t.Fatal("the two apps never connected") |
| 87 | } |
| 88 | t.Logf("RETRY the two apps did not connect (attempt %d)", attempt) |
| 89 | } |
| 90 | defer a.leave(t) |
| 91 | a.command(t, "screen-on") |
| 92 | waitNative(t, "the first client sees the screen", func() bool { |
| 93 | return b.stat("remoteScreens") == 1 && b.stat("screenFramesDecoded") > 15 |
| 94 | }, a, b) |
| 95 | t.Log("A is sharing, and B sees it") |
| 96 | |
| 97 | // Farshad leaves, and comes back from the app. |
| 98 | b.leave(t) |
| 99 | waitNative(t, "A sees B go", func() bool { return a.stat("rosterPeers") == 1 }, a) |
| 100 | |
| 101 | var env []string |
| 102 | switch { |
| 103 | case oldBinary: |
| 104 | // The old app has no block of its own; A's blocks both ways. |
| 105 | a.command(t, "ice-block "+strconv.FormatInt(block.Milliseconds(), 10)) |
| 106 | case mode == "old-peer": |
| 107 | env = []string{"CALLS_TEST_ICE_BLOCK_MS=" + strconv.FormatInt(block.Milliseconds(), 10), "CALLS_AUTO_RECOVERY=0", "CALLS_TEST_NO_CLIENT=1"} |
| 108 | default: |
| 109 | env = []string{"CALLS_TEST_ICE_BLOCK_MS=" + strconv.FormatInt(block.Milliseconds(), 10)} |
| 110 | } |
| 111 | blockedAt := time.Now() |
| 112 | c := startNativeEnv(ctx, t, peerBin, rig, room, rig.cookieB, "c", env) |
| 113 | defer c.leave(t) |
| 114 | waitNative(t, "C is in the call", func() bool { return c.sample().Phase == "admitted" && a.stat("rosterPeers") == 2 }, a, c) |
| 115 | |
| 116 | // The connection does not come up while the block lasts. For its |
| 117 | // first fifty seconds nothing worse than "Reconnecting to …" is said; |
| 118 | // past a minute (spec §15.28) the failure notice is, and for an app |
| 119 | // that does not say its version, that it may be old. |
| 120 | const failure = "A participant’s media could not connect." |
| 121 | oldPeer := oldBinary || mode == "old-peer" |
| 122 | sawFailed, sawReconnecting, sawNotice, sawMaybeOld := false, false, false, false |
| 123 | for time.Since(blockedAt) < block { |
| 124 | s := a.sample() |
| 125 | if s.Stats["iceFailedPeers"] > 0 { |
| 126 | sawFailed = true |
| 127 | } |
| 128 | if s.Stats["iceConnectedPeers"] > 0 && s.Stats["framesDecoded"] > 0 { |
| 129 | t.Fatalf("A connected to C through the block: %+v", s) |
| 130 | } |
| 131 | for _, notes := range s.Notices { |
| 132 | for _, n := range notes { |
| 133 | sawReconnecting = sawReconnecting || strings.HasPrefix(n, "Reconnecting to ") |
| 134 | } |
| 135 | } |
| 136 | // Someone in their first moments is "connecting and can't hear you |
| 137 | // yet" (§15.24) before they are "Reconnecting to …" (§15.23). |
| 138 | if s.Stats["connectingShown"] > 0 { |
| 139 | sawReconnecting = true |
| 140 | } |
| 141 | for _, notes := range s.Notices { |
| 142 | for _, n := range notes { |
| 143 | sawMaybeOld = sawMaybeOld || strings.Contains(n, "may be on an old version of Calls") |
| 144 | } |
| 145 | } |
| 146 | if s.Status == failure { |
| 147 | if time.Since(blockedAt) < 50*time.Second { |
| 148 | t.Fatalf("the failure notice came %s in, before its minute", time.Since(blockedAt).Round(time.Second)) |
| 149 | } |
| 150 | sawNotice = true |
| 151 | } |
| 152 | time.Sleep(250 * time.Millisecond) |
| 153 | } |
| 154 | if !sawReconnecting { |
| 155 | t.Fatalf("A's tile for C never said it was connecting, or Reconnecting to …, while the block lasted") |
| 156 | } |
| 157 | if block >= 75*time.Second { |
| 158 | if !sawNotice { |
| 159 | t.Fatalf("a connection failing for %s never said %q", block, failure) |
| 160 | } |
| 161 | if oldPeer != sawMaybeOld { |
| 162 | t.Fatalf("old peer %v, but the tile's \"may be on an old version\" was %v", oldPeer, sawMaybeOld) |
| 163 | } |
| 164 | } |
| 165 | t.Logf("block over after %s: A has seen ICE failed: %v; A says %q, notices %v", time.Since(blockedAt).Round(time.Second), |
| 166 | sawFailed, a.sample().Status, a.sample().Notices) |
| 167 | |
| 168 | // Nobody does anything. A must decode C's camera and screen again. |
| 169 | frames := a.stat("framesDecoded") |
| 170 | limit := 90 * time.Second |
| 171 | if v, err := time.ParseDuration(os.Getenv("ICE_RECOVERY_WITHIN")); err == nil { |
| 172 | limit = v |
| 173 | } |
| 174 | until := time.Now().Add(limit) |
| 175 | recovered := false |
| 176 | for time.Now().Before(until) { |
| 177 | if a.stat("framesDecoded") > frames+15 && c.stat("screenFramesDecoded") > 15 { |
| 178 | recovered = true |
| 179 | break |
| 180 | } |
| 181 | time.Sleep(500 * time.Millisecond) |
| 182 | } |
| 183 | sa, sc := a.sample(), c.sample() |
| 184 | t.Logf("A: status %q, notices %v, clients %v, iceConnected %d, iceFailed %d, autoRestarts %d, autoRebuilds %d, iceRestarts %d", |
| 185 | sa.Status, sa.Notices, sa.Clients, sa.Stats["iceConnectedPeers"], sa.Stats["iceFailedPeers"], |
| 186 | sa.Stats["autoRestarts"], sa.Stats["autoRebuilds"], sa.Stats["iceRestarts"]) |
| 187 | t.Logf("C: status %q, iceConnected %d, iceFailed %d, autoRestarts %d, autoRebuilds %d, mangled %d", |
| 188 | sc.Status, sc.Stats["iceConnectedPeers"], sc.Stats["iceFailedPeers"], sc.Stats["autoRestarts"], |
| 189 | sc.Stats["autoRebuilds"], sc.Stats["iceBlockMangled"]) |
| 190 | if !recovered { |
| 191 | t.Fatalf("STUCK: %s after the block lifted, A still decodes nothing from C (A %+v)", limit, sa) |
| 192 | } |
| 193 | t.Logf("PASS recovered %s after the block lifted, with nobody acting", time.Since(blockedAt.Add(block)).Round(time.Second)) |
| 194 | // The words go with the trouble (a second's hold, and a sample's lag). |
| 195 | if !pollNative(5*time.Second, func() bool { |
| 196 | s := a.sample() |
| 197 | for _, notes := range s.Notices { |
| 198 | if len(notes) > 0 { |
| 199 | return false |
| 200 | } |
| 201 | } |
| 202 | return s.Status != failure |
| 203 | }) { |
| 204 | t.Fatalf("the notices outlived the recovery: %q %v", a.sample().Status, a.sample().Notices) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // TestNativeMacIceBrowser is the same incident between the Mac app and a |
| 209 | // browser (test/browser/native-ice.mjs): the app's ICE is blocked from the |
| 210 | // start, and once the block lifts the two must hear each other again with |
| 211 | // nobody acting — when both are this release, when the app is a stand-in |
| 212 | // for one from before it (the browser mends it alone), and when the |
| 213 | // browser's recovery is off (the app mends it alone). ICE_BLOCK_MS=80000 |
| 214 | // also checks the minute-later failure sentence and "may be old". |
| 215 | func TestNativeMacIceBrowser(t *testing.T) { |
| 216 | root, err := filepath.Abs("../..") |
| 217 | if err != nil { |
| 218 | t.Fatal(err) |
| 219 | } |
| 220 | binary := filepath.Join(root, "native/apple/.build/debug/CallsInterop") |
| 221 | if _, err := os.Stat(binary); err != nil { |
| 222 | t.Fatal("build native interop executable with make native-mac first") |
| 223 | } |
| 224 | cases := []string{"both", "app-old", "web-old"} |
| 225 | if only := os.Getenv("ICE_BROWSER_CASES"); only != "" { |
| 226 | cases = strings.Split(only, ",") |
| 227 | } |
| 228 | for _, kase := range cases { |
| 229 | t.Run(kase, func(t *testing.T) { |
| 230 | rig := newBrowserRig(t) |
| 231 | room, _ := rig.createMeeting(t) |
| 232 | ctx, cancel := context.WithTimeout(t.Context(), 5*time.Minute) |
| 233 | defer cancel() |
| 234 | cmd := exec.CommandContext(ctx, "node", filepath.Join(root, "test/browser/native-ice.mjs"), rig.origin, room, binary, kase) |
| 235 | cookies, _ := json.Marshal([]map[string]string{{"name": rig.cookieName, "value": rig.cookieB}}) |
| 236 | input, _ := json.Marshal(map[string]string{"origin": rig.origin, "room": room, "cookies": string(cookies)}) |
| 237 | cmd.Env = append(os.Environ(), "CALLS_COOKIE_NAME="+rig.cookieName, "CALLS_COOKIE_A="+rig.cookieA, "CALLS_NATIVE_INPUT="+string(input)) |
| 238 | out, err := cmd.CombinedOutput() |
| 239 | for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") { |
| 240 | t.Log(line) |
| 241 | } |
| 242 | if err != nil { |
| 243 | t.Fatalf("native-ice %s: %v", kase, err) |
| 244 | } |
| 245 | }) |
| 246 | } |
| 247 | } |
| 248 | |