//go:build browser && darwin package callstest import ( "context" "encoding/json" "os" "os/exec" "path/filepath" "strconv" "strings" "testing" "time" ) // TestNativeMacIceRecovery is the incident of 2026-10-21, about 14:41 UTC: // Paul on the Mac app, sharing his screen; Farshad, in the call from a // browser, left it and came back from the Mac app; the connection between // Paul's app and Farshad's never came up. Paul saw a frozen picture, // "Reconnecting to Farshad (Mac)…" and "A participant's media could not // connect", and it never recovered on its own. Farshad leaving and joining // again mended it. // // Here: app A shares; app B (the stand-in for Farshad's first client) // joins and leaves; app C, the same person, joins with its ICE blocked // (ICETestBlock: every candidate either way leads nowhere) for // ICE_BLOCK_SECONDS. The block lifts by itself, and nothing that was // exchanged while it lasted is any use afterwards — only fresh candidates // (an ICE restart) or a fresh connection get the pair talking. Up to // 0.4.10 nothing ever made either, and the pair stayed down until someone // left. The test then asks that A decodes C's camera and screen again with // nobody doing anything. // // Variants (ICE_RECOVERY_CASE): // - "both" (default): C is the same build as A. // - "old-peer": C does nothing to recover (CALLS_AUTO_RECOVERY=0) and // sends no client version (CALLS_TEST_NO_CLIENT=1), as an app from // before this release; A has to mend the pair on its own. // - ICE_RECOVERY_PEER=: C is that // binary, which has no block of its own, so the block is A's (every // candidate A sends or receives) — the old app really is old. // // The firewall flake TestNativeMacScreenShare describes (two unsigned // apps on one Mac connecting only some of the time) is handled the way // TestNativeMacScreenPairRecovery handles it: A and B get three tries to // connect before anything else happens. func TestNativeMacIceRecovery(t *testing.T) { root, err := filepath.Abs("../..") if err != nil { t.Fatal(err) } current := filepath.Join(root, "native/apple/.build/debug/CallsInterop") peerBin := current if v := os.Getenv("ICE_RECOVERY_PEER"); v != "" { peerBin = v } for _, b := range []string{current, peerBin} { if _, err := os.Stat(b); err != nil { t.Fatalf("no interop executable at %s: build with make native-mac first", b) } } block := 25 * time.Second if v, err := time.ParseDuration(os.Getenv("ICE_BLOCK_SECONDS") + "s"); err == nil && v > 0 { block = v } mode := envOr("ICE_RECOVERY_CASE", "both") oldBinary := peerBin != current rig := newBrowserRig(t) room, _ := rig.createMeeting(t) ctx, cancel := context.WithTimeout(t.Context(), block+6*time.Minute) defer cancel() var a, b *nativeApp for attempt := 1; ; attempt++ { a = startNative(ctx, t, current, rig, room, rig.cookieA, "a") waitNative(t, "the sharing app is in", func() bool { return a.sample().Phase == "admitted" }, a) b = startNative(ctx, t, current, rig, room, rig.cookieB, "b") if pollNative(35*time.Second, func() bool { return a.stat("framesDecoded") > 15 && b.stat("framesDecoded") > 15 }) { break } a.leave(t) b.leave(t) if attempt == 3 { t.Fatal("the two apps never connected") } t.Logf("RETRY the two apps did not connect (attempt %d)", attempt) } defer a.leave(t) a.command(t, "screen-on") waitNative(t, "the first client sees the screen", func() bool { return b.stat("remoteScreens") == 1 && b.stat("screenFramesDecoded") > 15 }, a, b) t.Log("A is sharing, and B sees it") // Farshad leaves, and comes back from the app. b.leave(t) waitNative(t, "A sees B go", func() bool { return a.stat("rosterPeers") == 1 }, a) var env []string switch { case oldBinary: // The old app has no block of its own; A's blocks both ways. a.command(t, "ice-block "+strconv.FormatInt(block.Milliseconds(), 10)) case mode == "old-peer": env = []string{"CALLS_TEST_ICE_BLOCK_MS=" + strconv.FormatInt(block.Milliseconds(), 10), "CALLS_AUTO_RECOVERY=0", "CALLS_TEST_NO_CLIENT=1"} default: env = []string{"CALLS_TEST_ICE_BLOCK_MS=" + strconv.FormatInt(block.Milliseconds(), 10)} } blockedAt := time.Now() c := startNativeEnv(ctx, t, peerBin, rig, room, rig.cookieB, "c", env) defer c.leave(t) waitNative(t, "C is in the call", func() bool { return c.sample().Phase == "admitted" && a.stat("rosterPeers") == 2 }, a, c) // The connection does not come up while the block lasts. For its // first fifty seconds nothing worse than "Reconnecting to …" is said; // past a minute (spec §15.28) the failure notice is, and for an app // that does not say its version, that it may be old. const failure = "A participant’s media could not connect." oldPeer := oldBinary || mode == "old-peer" sawFailed, sawReconnecting, sawNotice, sawMaybeOld := false, false, false, false for time.Since(blockedAt) < block { s := a.sample() if s.Stats["iceFailedPeers"] > 0 { sawFailed = true } if s.Stats["iceConnectedPeers"] > 0 && s.Stats["framesDecoded"] > 0 { t.Fatalf("A connected to C through the block: %+v", s) } for _, notes := range s.Notices { for _, n := range notes { sawReconnecting = sawReconnecting || strings.HasPrefix(n, "Reconnecting to ") } } // Someone in their first moments is "connecting and can't hear you // yet" (§15.24) before they are "Reconnecting to …" (§15.23). if s.Stats["connectingShown"] > 0 { sawReconnecting = true } for _, notes := range s.Notices { for _, n := range notes { sawMaybeOld = sawMaybeOld || strings.Contains(n, "may be on an old version of Calls") } } if s.Status == failure { if time.Since(blockedAt) < 50*time.Second { t.Fatalf("the failure notice came %s in, before its minute", time.Since(blockedAt).Round(time.Second)) } sawNotice = true } time.Sleep(250 * time.Millisecond) } if !sawReconnecting { t.Fatalf("A's tile for C never said it was connecting, or Reconnecting to …, while the block lasted") } if block >= 75*time.Second { if !sawNotice { t.Fatalf("a connection failing for %s never said %q", block, failure) } if oldPeer != sawMaybeOld { t.Fatalf("old peer %v, but the tile's \"may be on an old version\" was %v", oldPeer, sawMaybeOld) } } t.Logf("block over after %s: A has seen ICE failed: %v; A says %q, notices %v", time.Since(blockedAt).Round(time.Second), sawFailed, a.sample().Status, a.sample().Notices) // Nobody does anything. A must decode C's camera and screen again. frames := a.stat("framesDecoded") limit := 90 * time.Second if v, err := time.ParseDuration(os.Getenv("ICE_RECOVERY_WITHIN")); err == nil { limit = v } until := time.Now().Add(limit) recovered := false for time.Now().Before(until) { if a.stat("framesDecoded") > frames+15 && c.stat("screenFramesDecoded") > 15 { recovered = true break } time.Sleep(500 * time.Millisecond) } sa, sc := a.sample(), c.sample() t.Logf("A: status %q, notices %v, clients %v, iceConnected %d, iceFailed %d, autoRestarts %d, autoRebuilds %d, iceRestarts %d", sa.Status, sa.Notices, sa.Clients, sa.Stats["iceConnectedPeers"], sa.Stats["iceFailedPeers"], sa.Stats["autoRestarts"], sa.Stats["autoRebuilds"], sa.Stats["iceRestarts"]) t.Logf("C: status %q, iceConnected %d, iceFailed %d, autoRestarts %d, autoRebuilds %d, mangled %d", sc.Status, sc.Stats["iceConnectedPeers"], sc.Stats["iceFailedPeers"], sc.Stats["autoRestarts"], sc.Stats["autoRebuilds"], sc.Stats["iceBlockMangled"]) if !recovered { t.Fatalf("STUCK: %s after the block lifted, A still decodes nothing from C (A %+v)", limit, sa) } t.Logf("PASS recovered %s after the block lifted, with nobody acting", time.Since(blockedAt.Add(block)).Round(time.Second)) // The words go with the trouble (a second's hold, and a sample's lag). if !pollNative(5*time.Second, func() bool { s := a.sample() for _, notes := range s.Notices { if len(notes) > 0 { return false } } return s.Status != failure }) { t.Fatalf("the notices outlived the recovery: %q %v", a.sample().Status, a.sample().Notices) } } // TestNativeMacIceBrowser is the same incident between the Mac app and a // browser (test/browser/native-ice.mjs): the app's ICE is blocked from the // start, and once the block lifts the two must hear each other again with // nobody acting — when both are this release, when the app is a stand-in // for one from before it (the browser mends it alone), and when the // browser's recovery is off (the app mends it alone). ICE_BLOCK_MS=80000 // also checks the minute-later failure sentence and "may be old". func TestNativeMacIceBrowser(t *testing.T) { root, err := filepath.Abs("../..") if err != nil { t.Fatal(err) } binary := filepath.Join(root, "native/apple/.build/debug/CallsInterop") if _, err := os.Stat(binary); err != nil { t.Fatal("build native interop executable with make native-mac first") } cases := []string{"both", "app-old", "web-old"} if only := os.Getenv("ICE_BROWSER_CASES"); only != "" { cases = strings.Split(only, ",") } for _, kase := range cases { t.Run(kase, func(t *testing.T) { rig := newBrowserRig(t) room, _ := rig.createMeeting(t) ctx, cancel := context.WithTimeout(t.Context(), 5*time.Minute) defer cancel() cmd := exec.CommandContext(ctx, "node", filepath.Join(root, "test/browser/native-ice.mjs"), rig.origin, room, binary, kase) cookies, _ := json.Marshal([]map[string]string{{"name": rig.cookieName, "value": rig.cookieB}}) input, _ := json.Marshal(map[string]string{"origin": rig.origin, "room": room, "cookies": string(cookies)}) cmd.Env = append(os.Environ(), "CALLS_COOKIE_NAME="+rig.cookieName, "CALLS_COOKIE_A="+rig.cookieA, "CALLS_NATIVE_INPUT="+string(input)) out, err := cmd.CombinedOutput() for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") { t.Log(line) } if err != nil { t.Fatalf("native-ice %s: %v", kase, err) } }) } }