| 1 | use std::fs; |
| 2 | use std::path::{Path, PathBuf}; |
| 3 | |
| 4 | const NON_GRAPH_HOSTS: &[&str] = &[ |
| 5 | "cloud.microsoft", |
| 6 | "microsoft.com", |
| 7 | "office.com", |
| 8 | "office.net", |
| 9 | "skype.com", |
| 10 | "live.com", |
| 11 | "sharepoint.com", |
| 12 | "microsoftonline.com", |
| 13 | "microsoft365.com", |
| 14 | "giphy.com", |
| 15 | ]; |
| 16 | |
| 17 | const NOT_A_SERVICE: &[&str] = &["graph.microsoft.com", "schema.skype.com"]; |
| 18 | |
| 19 | const ALLOWED_PATHS: &[(&str, &str)] = &[ |
| 20 | ("chatsvc/", "Teams service clients, one module per service"), |
| 21 | ("calling/src/signaling.rs", "call signaling and flightproxy"), |
| 22 | ("calling/src/relay.rs", "TURN relay tokens"), |
| 23 | ("calling/src/meeting_options.rs", "meeting options MT"), |
| 24 | ("calling/src/whiteboard.rs", "meeting whiteboard MT"), |
| 25 | ("calling/src/breakout.rs", "breakout room join link parsing"), |
| 26 | ("calling/assets/", "in-page caption bot command"), |
| 27 | ( |
| 28 | "session/", |
| 29 | "sign-in, token refresh, page hosts, token audiences", |
| 30 | ), |
| 31 | ("browser/", "headless browser page targets"), |
| 32 | ("webview/", "WebView2 page hosts"), |
| 33 | ("app-base/src/task_dialog/sdk.rs", "task dialog host origin"), |
| 34 | ("core/src/links.rs", "link classification"), |
| 35 | ("kvt-sleutels/src/session.rs", "KVT: sign-in hosts of the sleutels.kvt.nl session tab"), |
| 36 | ("core/src/meeting_join.rs", "meeting join link parsing"), |
| 37 | ("core/src/message_link.rs", "message link parsing"), |
| 38 | ("core/src/call_event.rs", "recording link host check"), |
| 39 | ("core/src/external_image.rs", "GIF host check"), |
| 40 | ("app-call-model/src/agenda.rs", "demo join link"), |
| 41 | ("app-call-model/src/demo.rs", "demo data"), |
| 42 | ("app-state/src/demo.rs", "demo data"), |
| 43 | ("app-base/src/demo_", "demo data"), |
| 44 | ("app/src/layout_guard.rs", "layout test fixture"), |
| 45 | ("graph/src/tabs.rs", "channel tab web links"), |
| 46 | ("app/src/inline_video.rs", "SharePoint video player link"), |
| 47 | ("app/src/views/status_menu.rs", "View account web link"), |
| 48 | ("app-state/src/call/week.rs", "demo join link"), |
| 49 | ]; |
| 50 | |
| 51 | #[test] |
| 52 | fn non_graph_hosts_stay_in_their_service_modules() { |
| 53 | let crates_directory = Path::new(env!("CARGO_MANIFEST_DIR")).join(".."); |
| 54 | let mut files = Vec::new(); |
| 55 | source_files(&crates_directory, &mut files); |
| 56 | let mut offenders = Vec::new(); |
| 57 | for file in files { |
| 58 | let relative = file |
| 59 | .strip_prefix(&crates_directory) |
| 60 | .expect("file below crates") |
| 61 | .to_string_lossy() |
| 62 | .replace('\\', "/"); |
| 63 | if ALLOWED_PATHS |
| 64 | .iter() |
| 65 | .any(|(prefix, _)| relative.starts_with(prefix)) |
| 66 | { |
| 67 | continue; |
| 68 | } |
| 69 | let source = fs::read_to_string(&file).expect("read source file"); |
| 70 | for (index, line) in production_lines(&source) { |
| 71 | if names_non_graph_host(line) { |
| 72 | offenders.push(format!("{relative}:{}: {}", index + 1, line.trim())); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | assert!( |
| 77 | offenders.is_empty(), |
| 78 | "non-Graph hosts outside their service modules (move them or extend ALLOWED_PATHS and docs/research/internal-endpoints.md):\n{}", |
| 79 | offenders.join("\n") |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | fn source_files(directory: &Path, files: &mut Vec<PathBuf>) { |
| 84 | for entry in fs::read_dir(directory).expect("read source directory") { |
| 85 | let path = entry.expect("directory entry").path(); |
| 86 | let name = path |
| 87 | .file_name() |
| 88 | .and_then(|name| name.to_str()) |
| 89 | .unwrap_or(""); |
| 90 | if path.is_dir() { |
| 91 | if !matches!(name, "tests" | "examples" | "target" | "benches") { |
| 92 | source_files(&path, files); |
| 93 | } |
| 94 | } else if (name.ends_with(".rs") || name.ends_with(".js")) && name != "tests.rs" { |
| 95 | files.push(path); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | fn production_lines(source: &str) -> impl Iterator<Item = (usize, &str)> { |
| 101 | source |
| 102 | .lines() |
| 103 | .enumerate() |
| 104 | .take_while(|(_, line)| line.trim() != "#[cfg(test)]") |
| 105 | .filter(|(_, line)| !line.trim_start().starts_with("//")) |
| 106 | } |
| 107 | |
| 108 | fn names_non_graph_host(line: &str) -> bool { |
| 109 | let mut cleaned = line.to_owned(); |
| 110 | for host in NOT_A_SERVICE { |
| 111 | cleaned = cleaned.replace(host, ""); |
| 112 | } |
| 113 | NON_GRAPH_HOSTS.iter().any(|host| cleaned.contains(host)) |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn host_check_ignores_graph_and_markup() { |
| 118 | assert!(!names_non_graph_host( |
| 119 | r#"const GRAPH: &str = "https://graph.microsoft.com";"# |
| 120 | )); |
| 121 | assert!(!names_non_graph_host( |
| 122 | r#"itemtype="http://schema.skype.com/Mention""# |
| 123 | )); |
| 124 | assert!(names_non_graph_host( |
| 125 | r#"format!("https://teams.cloud.microsoft/api/mt/{region}")"# |
| 126 | )); |
| 127 | } |