Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

test(relay): spaces e2e follows the stale re-register rule (#4572)

ddupontba546bfa6100
diff --git a/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock b/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock
index afabcadcf2..c572960f37 100644
--- a/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock
+++ b/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock
@@ -2511,6 +2511,7 @@ dependencies = [
"tempfile",
"tokio",
"tokio-tungstenite 0.29.0",
+ "tokio-util",
"tonic",
]
diff --git a/libs/cua-spacesd/tests/spaces-e2e/Cargo.toml b/libs/cua-spacesd/tests/spaces-e2e/Cargo.toml
index 79ea41226f..202ca4887b 100644
--- a/libs/cua-spacesd/tests/spaces-e2e/Cargo.toml
+++ b/libs/cua-spacesd/tests/spaces-e2e/Cargo.toml
@@ -53,6 +53,8 @@ cua-sdk = { path = "../../../cua/crates/cua-sdk" }
cua-host = { path = "../../../cua/crates/cua-host", features = ["testing"] }
# The real relay, for the device-enrollment conformance of cua-host's client.
cua-relay = { path = "../../crates/cua-relay" }
+# Stops the live host the relay re-register case joins.
+tokio-util = "0.7"
cua-proto = { path = "../../../cua/crates/cua-proto", features = ["client", "server", "serde"] }
futures-util = "0.3"
hex = "0.4"
diff --git a/libs/cua-spacesd/tests/spaces-e2e/tests/relay_devices.rs b/libs/cua-spacesd/tests/spaces-e2e/tests/relay_devices.rs
index 9233399e08..e129e1725e 100644
--- a/libs/cua-spacesd/tests/spaces-e2e/tests/relay_devices.rs
+++ b/libs/cua-spacesd/tests/spaces-e2e/tests/relay_devices.rs
@@ -5,7 +5,8 @@
//! mode, a fake OIDC issuer, grace period over): enrollment with a second
//! factor, approval by code, a same-machine re-key, device sessions on the
//! machine directory and Spaces, audit, revocation, and a host re-running
-//! setup with its machine token only.
+//! setup: with its machine token, or with the account alone while the host
+//! is offline (never while it is live).
use std::sync::Arc;
@@ -109,22 +110,71 @@ async fn devices_enroll_and_reach_machines_on_the_real_relay() {
.await
.unwrap();
assert_ne!(rotated.machine_token, reg.machine_token);
- // Without it, a session alone cannot rotate the token.
+ // The host is not connected (a setup that died, a reinstall that lost
+ // the machine token): the owner's session alone re-registers it and
+ // rotates the token.
+ let request = RegisterRequest {
+ id: "0123abcd4567ef89".into(),
+ name: "studio-mac".into(),
+ allow: vec![],
+ host: None,
+ meta: Default::default(),
+ };
+ let rerotated = client.register(&stale, &request).await.unwrap();
+ assert_ne!(rerotated.machine_token, rotated.machine_token);
+ // Once the host is live, a session alone cannot rotate its token (and
+ // so cannot knock it offline); neither can the stale token.
+ let stop = tokio_util::sync::CancellationToken::new();
+ let unused = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let local = unused.local_addr().unwrap();
+ drop(unused);
+ let mut join = cua_relay::client::JoinConfig::new(
+ url.replace("http://", "ws://"),
+ rerotated.machine_token.clone(),
+ request.id.clone(),
+ local,
+ );
+ join.heartbeat = std::time::Duration::from_secs(1);
+ tokio::spawn(cua_relay::client::run(join, stop.clone()));
+ let mut online = false;
+ for _ in 0..200 {
+ if client
+ .machines(&stale)
+ .await
+ .unwrap()
+ .iter()
+ .any(|m| m.id == request.id && m.online)
+ {
+ online = true;
+ break;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(25)).await;
+ }
+ assert!(online, "the host connected");
+ let refused = client.register(&stale, &request).await.unwrap_err();
+ assert!(
+ refused
+ .to_string()
+ .contains("needs its machine token or an enrolled device"),
+ "{refused}"
+ );
+ assert!(
+ client
+ .register_as_machine(&stale, Some(&rotated.machine_token), &request)
+ .await
+ .is_err(),
+ "a superseded machine token is no proof"
+ );
assert!(
client
- .register(
- &stale,
- &RegisterRequest {
- id: "0123abcd4567ef89".into(),
- name: "x".into(),
- allow: vec![],
- host: None,
- meta: Default::default(),
- },
- )
+ .machines(&stale)
.await
- .is_err()
+ .unwrap()
+ .iter()
+ .any(|m| m.id == request.id && m.online),
+ "still connected"
);
+ stop.cancel();
// The session alone lists the machines by name only (the apps show
// them with a greyed-out Connect) and reaches none of them.
let listed = client.machines(&stale).await.unwrap();